Finally got rid of effect.c, effect.h, event.c and event.h
[ahxm.git] / input.c
blob7fa7db7e21ea7eab731a59a8b127564509e675f1
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, int bits, int sign, struct ss_wave * w)
130 int n, i;
131 double m;
133 /* alloc buffers for channels */
134 for(n=0;n < w->n_channels;n++)
135 w->wave[n]=(float *)malloc(w->size * sizeof(float));
137 /* clean the rest of channels */
138 for(;n < CHANNELS;n++)
139 w->wave[n]=NULL;
141 for(m=0;m < w->size;m++)
143 i=(int) m;
145 for(n=0;n < w->n_channels;n++)
146 w->wave[n][i]=load_sample(f, bits, sign);
152 * load_wav_file - Loads a file in .WAV format.
153 * @filename: name of the file
154 * @size: pointer where the size of the samples is to be stoed
155 * @wave: pointers to hold the channel data
157 * Loads a file in .WAV format. @size will contain on output the
158 * size in samples of each channel stored in @wave.
160 * Returns -100 if the file could not be opened, -101 if the file
161 * is not recognized as .WAV, -102 if the file contains anything
162 * more than uncompressed PCM data, or 0 if everything went OK.
164 int load_wav_file(struct ss_wave * w, char * file)
166 FILE * f;
167 char dummydata[256];
168 int rlen, flen;
169 short int b_per_sec, n_channels;
170 char riffid[5], waveid[5], fmtid[5], dataid[5];
171 double size;
172 int s_rate, bits;
174 if((f=fopen(file, "r")) == NULL)
175 return(-100);
177 fread(riffid, 1, 4, f);
178 riffid[4] = 0;
179 fread(&rlen, 1, 4, f);
180 fread(waveid, 1, 4, f);
181 waveid[4] = 0;
183 if(strcmp(waveid,"WAVE"))
185 fclose(f);
186 return(-101);
189 fread(fmtid, 1, 4, f);
190 fmtid[4] = 0;
191 flen=fget32(f);
193 if(flen > 240)
194 flen=240;
196 if(fget16(f) != 1)
198 /* wicked compressed format? fail */
199 fclose(f);
200 return(-102);
203 n_channels=fget16(f);
204 s_rate=fget32(f);
205 b_per_sec=fget32(f);
207 bits=fget16(f) / n_channels;
208 bits *= 8;
210 fread(dummydata, 1, (size_t)flen - 14, f);
211 fread(dataid, 1, 4, f);
212 dataid[4] = 0;
214 size=(double) fget32(f);
215 if(bits == 16) size /= 2;
216 size /= (double) n_channels;
218 /* fill the ss_wave */
219 w->size=size;
220 w->n_channels=n_channels;
221 w->s_rate=s_rate;
222 w->loop_start=0;
223 w->loop_end=size;
224 w->base_freq=note_frequency(60);
225 w->min_freq=note_frequency(0);
226 w->max_freq=note_frequency(127);
228 load_pcm_wave(f, bits, 1, w);
230 fclose(f);
232 return(0);
237 * load_pat_file - Loads an instrument in .PAT format.
238 * @i: The instrument
239 * @filename: filename holding the instrument
241 * Loads data from a Gravis Ultrasound patch (.PAT) format and
242 * stores it as layers for an instrument.
244 * Returns -100 if the file could not be read, -101 or -102
245 * if the file is not recognized as a .PAT file, or 0 if
246 * everything went OK.
248 int load_pat_file(struct ss_ins * i, char * file)
250 FILE * f;
251 char buffer[512];
252 int m, n, o;
253 int n_layers;
254 int flags, bits, sign, loop, pingpong;
255 struct ss_wave w;
257 if((f=fopen(file, "r")) == NULL)
258 return(-100);
260 /* read signatures */
261 fread(buffer, 12, 1, f);
262 if(strcmp(buffer, "GF1PATCH110") != 0)
264 fclose(f);
265 return(-101);
268 fread(buffer, 10, 1, f);
269 if(strcmp(buffer, "ID#000002") != 0)
271 fclose(f);
272 return(-102);
275 /* skip description */
276 fread(buffer, 65, 1, f);
278 /* ignore volume */
279 fget16(f);
281 /* skip */
282 fread(buffer, 109, 1, f);
284 /* # of layers */
285 n_layers=fgetc(f);
287 /* skip */
288 fread(buffer, 40, 1, f);
290 for(n=0;n < n_layers;n++)
292 /* layer name */
293 fread(buffer, 8, 1, f);
294 printf("name: '%s'\n", buffer);
296 w.size=(double)fget32(f);
297 w.loop_start=(double)fget32(f);
298 w.loop_end=(double)fget32(f);
299 w.s_rate=fget16(f);
301 printf("size: %d [%d-%d], s_rate: %d\n",
302 (int)w.size, (int)w.loop_start, (int)w.loop_end, w.s_rate);
304 w.min_freq=((double)fget32(f)) / 1000.0;
305 w.max_freq=((double)fget32(f)) / 1000.0;
306 w.base_freq=((double)fget32(f)) / 1000.0;
308 printf("note_freqs: %lf - %lf - %lf\n",
309 w.min_freq, w.base_freq, w.max_freq);
311 if(w.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 w.size /= 2;
337 w.loop_start /= 2;
338 w.loop_end /= 2;
341 /* skip frequency scale data */
342 fget16(f); fget16(f);
344 /* skip reserved */
345 fread(buffer, 36, 1, f);
347 /* set to 1 channel (probably wrong) */
348 w.n_channels=1;
350 /* load the wave */
351 load_pcm_wave(f, bits, sign, &w);
353 if(pingpong && loop)
355 int loop_size;
356 float * ptr;
358 /* if ping-pong, realloc space for a reverse
359 version of the loop */
360 loop_size=(int)(w.loop_end - w.loop_start);
362 ptr=(float *) malloc(((int) w.size + loop_size + 1)
363 * sizeof(float));
365 /* transfer start and loop */
366 for(m=0;m <= (int)w.loop_end;m++)
367 ptr[m]=w.wave[0][m];
369 /* transfer a reversed version of the loop */
370 for(o=m-1;o >= (int)w.loop_start;o--,m++)
371 ptr[m]=w.wave[0][o];
373 /* transfer the end */
374 for(o=(int)w.loop_end + 1;o < (int)w.size;o++,m++)
375 ptr[m]=w.wave[0][o];
377 w.loop_end += (double) loop_size;
378 w.size += (double) loop_size;
380 /* free and swap */
381 free(w.wave[0]);
382 w.wave[0]=ptr;
385 if(loop == 0) w.loop_start=-1;
387 /* finally add layer to instrument */
388 ss_ins_add_layer(i, &w);
391 fclose(f);
392 return(0);