New function ss_gen_vibrato() (API still not clear).
[ahxm.git] / input.c
blobda61aabddb491e9379a76f616cb9bf987032ce1f
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
6 input.c - Code to load 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 "core.h"
33 #include "ss_gen.h"
34 #include "ss_ins.h"
35 #include "input.h"
37 /*******************
38 Data
39 ********************/
41 /*******************
42 Code
43 ********************/
45 /**
46 * fget16 - Gets a 16 bit integer from a file.
47 * @f: the file
49 * Reads a 16 bit integer from a file in big endian byte ordering.
51 int fget16(FILE * f)
53 int c;
55 c=fgetc(f);
56 c+=(fgetc(f) * 256);
58 return(c);
62 /**
63 * fget32 - Gets a 32 bit integer from a file.
64 * @f: the file
66 * Reads a 32 bit integer from a file in big endian byte ordering.
68 int fget32(FILE * f)
70 int c;
72 c=fgetc(f);
73 c+=(fgetc(f) * 256);
74 c+=(fgetc(f) * 65536);
75 c+=(fgetc(f) * 16777216);
77 return(c);
81 /**
82 * load_sample - Loads one sample from a file.
83 * @f: the file
84 * @bits: number of bits of the sample
85 * @sign: nonzero if the sample is signed
87 * Loads one sample from a file. @bits should be 8 or 16, and sign
88 * should be nonzero for signed samples.
90 float load_sample(FILE * f, int bits, int sign)
92 int s;
94 if(bits == 8)
96 s=fgetc(f) - 128;
97 s<<=8;
99 else
101 if(!sign)
102 s=fget16(f) - 32768;
103 else
104 s=(short int)fget16(f);
107 return((float) s);
112 * load_pcm_wave - Loads a stream of PCM wave data from a file.
113 * @f: the file
114 * @size: number of samples to be read
115 * @bits: number of bits of the sample
116 * @sign: nonzero if the samples are signed
117 * @n_channels: number of channels to be read
118 * @wave: array where the wave are to be stored
120 * Loads an interleaved stream of @n_channels of PCM data from
121 * a file. @size is the number of samples to be read, and @bits
122 * and @sign about the kind of sample. A buffer is allocated
123 * for each PCM channel and stored in @wave.
125 * No error condition is tested, so the file should better contain
126 * that number of samples.
128 void load_pcm_wave(FILE * f, double size, int bits, int sign,
129 int n_channels, float * wave[])
131 int n, i;
132 double m;
134 /* alloc buffers for channels */
135 for(n=0;n < n_channels;n++)
136 wave[n]=(float *)malloc(size * sizeof(float));
138 /* clean the rest of channels */
139 for(;n < CHANNELS;n++)
140 wave[n]=NULL;
142 for(m=0;m < size;m++)
144 i=(int) m;
146 for(n=0;n < n_channels;n++)
147 wave[n][i]=load_sample(f, bits, sign);
153 * load_wav_file - Loads a file in .WAV format.
154 * @filename: name of the file
155 * @size: pointer where the size of the samples is to be stoed
156 * @wave: pointers to hold the channel data
158 * Loads a file in .WAV format. @size will contain on output the
159 * size in samples of each channel stored in @wave.
161 * Returns -100 if the file could not be opened, -101 if the
162 * file is not recognized as .WAV, -102 if the file contains
163 * anything more than uncompressed PCM data, or the number of
164 * stored channels if everything went OK.
166 int load_wav_file(char * filename, double * size, int * s_rate, float * wave[])
168 FILE * f;
169 char dummydata[256];
170 int rlen,flen;
171 short int b_per_sec,n_channels;
172 char riffid[5],waveid[5],fmtid[5],dataid[5];
173 int bits;
175 if((f=fopen(filename,"r"))==NULL)
176 return(-100);
178 fread(riffid,1,4,f);
179 riffid[4] = 0;
180 fread(&rlen,1,4,f);
181 fread(waveid,1,4,f);
182 waveid[4] = 0;
184 if(strcmp(waveid,"WAVE"))
186 fclose(f);
187 return(-101);
190 fread(fmtid,1,4,f);
191 fmtid[4] = 0;
192 flen=fget32(f);
194 if(flen>240)
195 flen=240;
197 if(fget16(f) != 1)
199 /* wicked compressed format? fail */
200 fclose(f);
201 return(-102);
204 n_channels=fget16(f);
205 *s_rate=fget32(f);
206 b_per_sec=fget32(f);
208 bits=fget16(f) / n_channels;
209 bits*=8;
211 fread(dummydata,1,(size_t)flen-14,f);
212 fread(dataid,1,4,f);
213 dataid[4] = 0;
215 *size=(double) fget32(f);
216 if(bits==16) *size /= 2;
217 *size /= (double) n_channels;
219 load_pcm_wave(f, *size, bits, 1, n_channels, wave);
221 fclose(f);
223 return(n_channels);
228 * load_pat_file - Loads an instrument in .PAT format.
229 * @i: The instrument
230 * @filename: filename holding the instrument
232 * Loads data from a Gravis Ultrasound patch (.PAT) format and
233 * stores it as layers for an instrument.
235 * Returns -100 if the file could not be read, -101 or -102
236 * if the file is not recognized as a .PAT file, or 0 if
237 * everything went OK.
239 int load_pat_file(struct ss_ins * i, char * filename)
241 FILE * f;
242 char buffer[512];
243 int m, n, o;
244 int n_layers;
245 double size, loop_start, loop_end;
246 double base_freq, min_freq, max_freq;
247 int s_rate, flags, bits, sign, loop, pingpong;
248 float * wave[CHANNELS];
250 if((f=fopen(filename,"r"))==NULL)
251 return(-100);
253 /* read signatures */
254 fread(buffer, 12, 1, f);
255 if(strcmp(buffer, "GF1PATCH110")!=0)
257 fclose(f);
258 return(-101);
261 fread(buffer, 10, 1, f);
262 if(strcmp(buffer, "ID#000002")!=0)
264 fclose(f);
265 return(-102);
268 /* skip description */
269 fread(buffer, 65, 1, f);
271 /* ignore volume */
272 fget16(f);
274 /* skip */
275 fread(buffer, 109, 1, f);
277 /* # of layers */
278 n_layers=fgetc(f);
280 /* skip */
281 fread(buffer, 40, 1, f);
283 for(n=0;n < n_layers;n++)
285 /* layer name */
286 fread(buffer, 8, 1, f);
287 printf("name: '%s'\n", buffer);
289 size=(double)fget32(f);
290 loop_start=(double)fget32(f);
291 loop_end=(double)fget32(f);
292 s_rate=fget16(f);
294 printf("size: %d [%d-%d], s_rate: %d\n",
295 (int)size, (int)loop_start, (int)loop_end, s_rate);
297 /* min_note=_pat_find_note(fget32(f));
298 max_note=_pat_find_note(fget32(f));
299 base_note=_pat_find_note(fget32(f)); */
300 min_freq=((double)fget32(f)) / 1000;
301 max_freq=((double)fget32(f)) / 1000;
302 base_freq=((double)fget32(f)) / 1000;
304 printf("note_freqs: %lf - %lf - %lf\n",
305 min_freq, base_freq, max_freq);
306 /* printf("notes: %d - %d - %d\n",
307 _pat_find_note(min_note),
308 _pat_find_note(base_note),
309 _pat_find_note(max_note));
311 if(base_freq < 0)
312 break;
314 /* ignore fine-tune */
315 fget16(f);
317 /* ignore pan position */
318 fgetc(f);
320 /* skip envelope rate, value, tremolo and vibrato */
321 fread(buffer, 18, 1, f);
323 flags=fgetc(f);
324 printf("flags: 0x%02X\n", flags);
326 bits=flags & 0x01 ? 16 : 8;
327 sign=flags & 0x02 ? 0 : 1;
328 loop=flags & 0x04 ? 1 : 0;
329 pingpong=flags & 0x08 ? 1 : 0;
331 printf("bits: %d, sign: %d, loop: %d pingpong: %d\n",
332 bits, sign, loop, pingpong);
334 if(bits == 16)
336 size /= 2;
337 loop_start /= 2;
338 loop_end /= 2;
341 /* skip frequency scale data */
342 fget16(f); fget16(f);
344 /* skip reserved */
345 fread(buffer, 36, 1, f);
347 /* load the wave */
348 load_pcm_wave(f, size, bits, sign, 1, wave);
350 if(pingpong && loop)
352 int loop_size;
353 float * ptr;
355 /* if ping-pong, realloc space for a reverse
356 version of the loop */
357 loop_size=(int)(loop_end - loop_start);
359 ptr=(float *) malloc(((int) size + loop_size + 1)
360 * sizeof(float));
362 /* transfer start and loop */
363 for(m=0;m <= (int)loop_end;m++)
364 ptr[m]=wave[0][m];
366 /* transfer a reversed version of the loop */
367 for(o=m-1;o >= (int)loop_start;o--,m++)
368 ptr[m]=wave[0][o];
370 /* transfer the end */
371 for(o=(int)loop_end+1;o < (int)size;o++,m++)
372 ptr[m]=wave[0][o];
374 loop_end += (double) loop_size;
375 size += (double) loop_size;
377 /* free and swap */
378 free(wave[0]);
379 wave[0]=ptr;
382 if(loop==0) loop_start=-1;
384 /* finally add layer to instrument */
385 ss_ins_add_layer(i,
386 base_freq, min_freq, max_freq,
387 1, wave, size, s_rate,
388 loop_start, loop_end);
391 fclose(f);
392 return(0);