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.
56 c
+= (fgetc(f
) * 256);
63 * fget32 - Gets a 32 bit integer from a file.
66 * Reads a 32 bit integer from a file in big endian byte ordering.
73 c
+= (fgetc(f
) * 256);
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
, int bits
, int sign
, struct ss_wave
* w
)
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
++)
141 for(m
=0;m
< w
->size
;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
)
169 short int b_per_sec
, n_channels
;
170 char riffid
[5], waveid
[5], fmtid
[5], dataid
[5];
174 if((f
=fopen(file
, "r")) == NULL
)
177 fread(riffid
, 1, 4, f
);
179 fread(&rlen
, 1, 4, f
);
180 fread(waveid
, 1, 4, f
);
183 if(strcmp(waveid
,"WAVE"))
189 fread(fmtid
, 1, 4, f
);
198 /* wicked compressed format? fail */
203 n_channels
=fget16(f
);
207 bits
=fget16(f
) / n_channels
;
210 fread(dummydata
, 1, (size_t)flen
- 14, f
);
211 fread(dataid
, 1, 4, f
);
214 size
=(double) fget32(f
);
215 if(bits
== 16) size
/= 2;
216 size
/= (double) n_channels
;
218 /* fill the ss_wave */
220 w
->n_channels
=n_channels
;
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
);
237 * load_pat_file - Loads an instrument in .PAT format.
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
)
254 int flags
, bits
, sign
, loop
, pingpong
;
257 if((f
=fopen(file
, "r")) == NULL
)
260 /* read signatures */
261 fread(buffer
, 12, 1, f
);
262 if(strcmp(buffer
, "GF1PATCH110") != 0)
268 fread(buffer
, 10, 1, f
);
269 if(strcmp(buffer
, "ID#000002") != 0)
275 /* skip description */
276 fread(buffer
, 65, 1, f
);
282 fread(buffer
, 109, 1, f
);
288 fread(buffer
, 40, 1, f
);
290 for(n
=0;n
< n_layers
;n
++)
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
);
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
);
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
);
347 /* set to 1 channel (probably wrong) */
351 load_pcm_wave(f
, bits
, sign
, &w
);
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)
365 /* transfer start and loop */
366 for(m
=0;m
<= (int)w
.loop_end
;m
++)
369 /* transfer a reversed version of the loop */
370 for(o
=m
-1;o
>= (int)w
.loop_start
;o
--,m
++)
373 /* transfer the end */
374 for(o
=(int)w
.loop_end
+ 1;o
< (int)w
.size
;o
++,m
++)
377 w
.loop_end
+= (double) loop_size
;
378 w
.size
+= (double) loop_size
;
385 if(loop
== 0) w
.loop_start
=-1;
387 /* finally add layer to instrument */
388 ss_ins_add_layer(i
, &w
);