Each volume was being applied to all the accumulated frame samples
[ahxm.git] / ss_input.c
blob941122e3faa6901d672fe355300a58a522c9c8ff
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 ss_input.c - Code to load softsynth sounds in different formats
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "ss_core.h"
33 #include "ss_gen.h"
34 #include "ss_ins.h"
35 #include "ss_input.h"
37 /*******************
38 Data
39 ********************/
41 /*******************
42 Code
43 ********************/
45 static int fget16(FILE * f)
46 /* Reads a 16 bit integer from a file in big endian byte ordering */
48 int c;
50 c=fgetc(f);
51 c += (fgetc(f) * 256);
53 return(c);
57 static int fget32(FILE * f)
58 /* Reads a 32 bit integer from a file in big endian byte ordering */
60 int c;
62 c=fgetc(f);
63 c += (fgetc(f) * 256);
64 c += (fgetc(f) * 65536);
65 c += (fgetc(f) * 16777216);
67 return(c);
71 static float load_sample(FILE * f, int bits, int sign)
72 /* loads one sample from a file */
74 int s;
76 /* Caution: no error condition is checked */
78 if(bits == 8)
80 s=fgetc(f) - 128;
81 s <<= 8;
83 else
85 if(!sign)
86 s=fget16(f) - 32768;
87 else
88 s=(short int)fget16(f);
91 return((float) s);
95 static void load_pcm_wave(FILE * f, int bits, int sign, struct ss_wave * w)
96 /* loads an interleaved stream from a file */
98 int n, i;
99 double m;
101 /* fills the channels */
102 for(m=0;m < w->size;m++)
104 i=(int) m;
106 for(n=0;n < w->n_channels;n++)
107 w->wave[n][i]=load_sample(f, bits, sign);
113 * ss_load_wav_file - Loads a file in .WAV format.
114 * @file: name of the file
115 * @base_freq: base frequency
116 * @min_freq: minimum frequency
117 * @max_freq: maximum frequency
119 * Loads a file in .WAV format.
121 struct ss_wave * ss_load_wav_file(char * file, double base_freq,
122 double min_freq, double max_freq)
124 FILE * f;
125 char dummydata[256];
126 int rlen, flen;
127 short int b_per_sec, n_channels;
128 char riffid[5], waveid[5], fmtid[5], dataid[5];
129 double size;
130 int s_rate, bits;
131 struct ss_wave * w;
133 if((f=fopen(file, "r")) == NULL)
134 return(NULL);
136 fread(riffid, 1, 4, f);
137 riffid[4] = 0;
138 fread(&rlen, 1, 4, f);
139 fread(waveid, 1, 4, f);
140 waveid[4] = 0;
142 if(strcmp(waveid,"WAVE"))
144 fclose(f);
145 return(NULL);
148 fread(fmtid, 1, 4, f);
149 fmtid[4] = 0;
150 flen=fget32(f);
152 if(flen > 240)
153 flen=240;
155 if(fget16(f) != 1)
157 /* wicked compressed format? fail */
158 fclose(f);
159 return(NULL);
162 n_channels=fget16(f);
163 s_rate=fget32(f);
164 b_per_sec=fget32(f);
166 bits=fget16(f) / n_channels;
167 bits *= 8;
169 fread(dummydata, 1, (size_t)flen - 14, f);
170 fread(dataid, 1, 4, f);
171 dataid[4] = 0;
173 size=(double) fget32(f);
174 if(bits == 16) size /= 2;
175 size /= (double) n_channels;
177 if((w=ss_alloc_wave(size, n_channels, s_rate)) != NULL)
179 w->loop_start=-1;
180 w->loop_end=size;
181 w->base_freq=base_freq;
182 w->min_freq=min_freq;
183 w->max_freq=max_freq;
185 load_pcm_wave(f, bits, 1, w);
188 fclose(f);
190 return(w);
195 * ss_load_pat_file - Loads an instrument in .PAT format.
196 * @i: The instrument
197 * @filename: filename holding the instrument
199 * Loads data from a Gravis Ultrasound patch (.PAT) format and
200 * stores it as layers for an instrument.
202 * Returns -100 if the file could not be read, -101 or -102
203 * if the file is not recognized as a .PAT file, or 0 if
204 * everything went OK.
206 int ss_load_pat_file(struct ss_ins * i, char * file)
208 FILE * f;
209 char buffer[512];
210 int m, n, o;
211 int n_layers;
212 int flags, bits, sign, loop, pingpong;
213 struct ss_wave * w;
215 if((f=fopen(file, "r")) == NULL)
216 return(-100);
218 /* read signatures */
219 fread(buffer, 12, 1, f);
220 if(strcmp(buffer, "GF1PATCH110") != 0)
222 fclose(f);
223 return(-101);
226 fread(buffer, 10, 1, f);
227 if(strcmp(buffer, "ID#000002") != 0)
229 fclose(f);
230 return(-102);
233 /* skip description */
234 fread(buffer, 65, 1, f);
236 /* ignore volume */
237 fget16(f);
239 /* skip */
240 fread(buffer, 109, 1, f);
242 /* # of layers */
243 n_layers=fgetc(f);
245 /* skip */
246 fread(buffer, 40, 1, f);
248 for(n=0;n < n_layers;n++)
250 int size, s_rate;
251 double loop_start, loop_end;
252 double min_freq, max_freq, base_freq;
254 /* layer name */
255 fread(buffer, 8, 1, f);
256 printf("name: '%s'\n", buffer);
258 size=(double)fget32(f);
259 loop_start=(double)fget32(f);
260 loop_end=(double)fget32(f);
261 s_rate=fget16(f);
263 printf("size: %d [%d-%d], s_rate: %d\n",
264 size, (int)loop_start, (int)loop_end, s_rate);
266 min_freq=((double)fget32(f)) / 1000.0;
267 max_freq=((double)fget32(f)) / 1000.0;
268 base_freq=((double)fget32(f)) / 1000.0;
270 printf("note_freqs: %lf - %lf - %lf\n",
271 min_freq, base_freq, max_freq);
273 if(base_freq < 0)
274 break;
276 /* ignore fine-tune */
277 fget16(f);
279 /* ignore pan position */
280 fgetc(f);
282 /* skip envelope rate, value, tremolo and vibrato */
283 fread(buffer, 18, 1, f);
285 flags=fgetc(f);
286 printf("flags: 0x%02X\n", flags);
288 bits=flags & 0x01 ? 16 : 8;
289 sign=flags & 0x02 ? 0 : 1;
290 loop=flags & 0x04 ? 1 : 0;
291 pingpong=flags & 0x08 ? 1 : 0;
293 printf("bits: %d, sign: %d, loop: %d pingpong: %d\n",
294 bits, sign, loop, pingpong);
296 if(bits == 16)
298 size /= 2;
299 loop_start /= 2;
300 loop_end /= 2;
303 /* skip frequency scale data */
304 fget16(f); fget16(f);
306 /* skip reserved */
307 fread(buffer, 36, 1, f);
309 if((w=ss_alloc_wave(size, 1, s_rate)) == NULL)
310 break;
312 /* set the rest of values */
313 w->loop_start=loop_start;
314 w->loop_end=loop_end;
315 w->base_freq=base_freq;
316 w->min_freq=min_freq;
317 w->max_freq=max_freq;
319 /* load the wave */
320 load_pcm_wave(f, bits, sign, w);
322 if(pingpong && loop)
324 int loop_size;
325 float * ptr;
327 /* if ping-pong, realloc space for a reverse
328 version of the loop */
329 loop_size=(int)(loop_end - loop_start);
331 ptr=(float *) malloc((size + loop_size + 1)
332 * sizeof(float));
334 /* transfer start and loop */
335 for(m=0;m <= (int)loop_end;m++)
336 ptr[m]=w->wave[0][m];
338 /* transfer a reversed version of the loop */
339 for(o=m - 1;o >= loop_start;o--,m++)
340 ptr[m]=w->wave[0][o];
342 /* transfer the end */
343 for(o=loop_end + 1;o < size;o++,m++)
344 ptr[m]=w->wave[0][o];
346 w->loop_end += (double) loop_size;
347 w->size += (double) loop_size;
349 /* free and swap */
350 free(w->wave[0]);
351 w->wave[0]=ptr;
354 if(loop == 0) w->loop_start=-1;
356 /* finally add layer to instrument */
357 ss_ins_add_layer(i, w);
360 fclose(f);
361 return(0);