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
46 * fget16 - Gets a 16 bit integer from a file.
49 * Reads a 16 bit integer from a file in big endian byte ordering.
63 * fget32 - Gets a 32 bit integer from a file.
66 * Reads a 32 bit integer from a file in big endian byte ordering.
74 c
+=(fgetc(f
) * 65536);
75 c
+=(fgetc(f
) * 16777216);
82 * load_sample - Loads one sample from a 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
)
104 s
=(short int)fget16(f
);
112 * load_pcm_wave - Loads a stream of PCM wave data from a 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
[])
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
++)
142 for(m
=0;m
< size
;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
[])
171 short int b_per_sec
,n_channels
;
172 char riffid
[5],waveid
[5],fmtid
[5],dataid
[5];
175 if((f
=fopen(filename
,"r"))==NULL
)
184 if(strcmp(waveid
,"WAVE"))
199 /* wicked compressed format? fail */
204 n_channels
=fget16(f
);
208 bits
=fget16(f
) / n_channels
;
211 fread(dummydata
,1,(size_t)flen
-14,f
);
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
);
228 * load_pat_file - Loads an instrument in .PAT format.
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
)
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
)
253 /* read signatures */
254 fread(buffer
, 12, 1, f
);
255 if(strcmp(buffer
, "GF1PATCH110")!=0)
261 fread(buffer
, 10, 1, f
);
262 if(strcmp(buffer
, "ID#000002")!=0)
268 /* skip description */
269 fread(buffer
, 65, 1, f
);
275 fread(buffer
, 109, 1, f
);
281 fread(buffer
, 40, 1, f
);
283 for(n
=0;n
< n_layers
;n
++)
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
);
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));
314 /* ignore fine-tune */
317 /* ignore pan position */
320 /* skip envelope rate, value, tremolo and vibrato */
321 fread(buffer
, 18, 1, 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
);
341 /* skip frequency scale data */
342 fget16(f
); fget16(f
);
345 fread(buffer
, 36, 1, f
);
348 load_pcm_wave(f
, size
, bits
, sign
, 1, wave
);
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)
362 /* transfer start and loop */
363 for(m
=0;m
<= (int)loop_end
;m
++)
366 /* transfer a reversed version of the loop */
367 for(o
=m
-1;o
>= (int)loop_start
;o
--,m
++)
370 /* transfer the end */
371 for(o
=(int)loop_end
+1;o
< (int)size
;o
++,m
++)
374 loop_end
+= (double) loop_size
;
375 size
+= (double) loop_size
;
382 if(loop
==0) loop_start
=-1;
384 /* finally add layer to instrument */
386 base_freq
, min_freq
, max_freq
,
387 1, wave
, size
, s_rate
,
388 loop_start
, loop_end
);