Fixed a bug in setting the fragment in the OSS driver.
[ahxm.git] / ss_input.c
blob0075e1e52c5b58c43238c1e587d3a207f0a4de42
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>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
35 #include "annhell.h"
36 #include "sha1.h"
38 /*******************
39 Data
40 ********************/
42 /*******************
43 Code
44 ********************/
46 static int fget16(FILE * f)
47 /* Reads a 16 bit integer from a file in big endian byte ordering */
49 int c;
51 c=fgetc(f);
52 c += (fgetc(f) * 256);
54 return(c);
58 static int fget32(FILE * f)
59 /* Reads a 32 bit integer from a file in big endian byte ordering */
61 int c;
63 c=fgetc(f);
64 c += (fgetc(f) * 256);
65 c += (fgetc(f) * 65536);
66 c += (fgetc(f) * 16777216);
68 return(c);
72 static sample_t load_sample(FILE * f, int bits, int sign)
73 /* loads one sample from a file */
75 int s;
77 /* Caution: no error condition is checked */
79 if(bits == 8)
81 s=fgetc(f) - 128;
82 s <<= 8;
84 else
86 if(!sign)
87 s=fget16(f) - 32768;
88 else
89 s=(short int)fget16(f);
92 return((sample_t) s);
96 static void load_pcm_wave(FILE * f, int bits, int sign, struct ss_wave * w)
97 /* loads an interleaved stream from a file */
99 int n, i;
100 double m;
102 /* fills the channels */
103 for(m=0;m < w->size;m++)
105 i=(int) m;
107 for(n=0;n < w->n_channels;n++)
108 w->wave[n][i]=load_sample(f, bits, sign);
114 * ss_load_wav_file - Loads a file in .WAV format.
115 * @file: name of the file
116 * @base_freq: base frequency
117 * @min_freq: minimum frequency
118 * @max_freq: maximum frequency
119 * @loop_start: frame number of loop start (-1, no loop)
120 * @loop_end: frame number of loop end (-1, end of wave)
122 * Loads a file in .WAV format.
124 struct ss_wave * ss_load_wav_file(char * file,
125 double base_freq, double min_freq, double max_freq,
126 double loop_start, double loop_end)
128 FILE * f;
129 char dummydata[256];
130 int rlen, flen;
131 short int b_per_sec, n_channels;
132 char riffid[5], waveid[5], fmtid[5], dataid[5];
133 double size;
134 int s_rate, bits;
135 struct ss_wave * w;
137 if((f=path_fopen(file, "r")) == NULL)
138 return(NULL);
140 fread(riffid, 1, 4, f);
141 riffid[4] = 0;
142 fread(&rlen, 1, 4, f);
143 fread(waveid, 1, 4, f);
144 waveid[4] = 0;
146 if(strcmp(waveid,"WAVE"))
148 fclose(f);
149 return(NULL);
152 fread(fmtid, 1, 4, f);
153 fmtid[4] = 0;
154 flen=fget32(f);
156 if(flen > 240)
157 flen=240;
159 if(fget16(f) != 1)
161 /* wicked compressed format? fail */
162 fclose(f);
163 return(NULL);
166 n_channels=fget16(f);
167 s_rate=fget32(f);
168 b_per_sec=fget32(f);
170 bits=fget16(f) / n_channels;
171 bits *= 8;
173 fread(dummydata, 1, (size_t)flen - 14, f);
174 fread(dataid, 1, 4, f);
175 dataid[4] = 0;
177 size=(double) fget32(f);
178 if(bits == 16) size /= 2;
179 size /= (double) n_channels;
181 if((w=ss_alloc_wave(size, n_channels, s_rate)) != NULL)
183 w->base_freq=base_freq;
184 w->min_freq=min_freq;
185 w->max_freq=max_freq;
187 w->loop_start=loop_start;
189 if(loop_end < 0)
190 w->loop_end=size;
191 else
192 w->loop_end=loop_end;
194 load_pcm_wave(f, bits, 1, w);
197 fclose(f);
199 return(w);
203 static char * cached_base_name(char * file)
204 /* builds a unique cache file name using a SHA1 hash */
206 static char c_file[64];
207 unsigned char sha1[20];
208 SHA_CTX c;
209 int n;
211 SHA1_Init(&c);
212 SHA1_Update(&c, file, strlen(file));
213 SHA1_Final(sha1, &c);
215 for(n=0;n < sizeof(sha1);n++)
217 char tmp[3];
219 snprintf(tmp, sizeof(tmp), "%02x", sha1[n]);
220 c_file[n * 2]=tmp[0];
221 c_file[(n * 2) + 1]=tmp[1];
224 c_file[n * 2]='\0';
225 return(c_file);
229 static char * wave_file_preprocess(char * file)
230 /* preprocess the file, converting with external tools if necessary */
232 char * ext;
233 char * c_path;
234 char * c_basename;
235 char tmp[2048];
236 static char c_file[1024];
238 /* gets the file extension */
239 if((ext=strrchr(file, '.')) == NULL)
240 return(NULL);
242 /* if it's a .wav file, return as is; no
243 further processing is needed */
244 if(strcmp(ext, ".wav") == 0 || strcmp(ext, ".WAV") == 0)
245 return(file);
247 /* resolve the file name */
248 if((file=locate_file(file)) == NULL)
249 return(NULL);
251 /* build the directory cache name */
252 if((c_path=getenv("HOME")) == NULL)
253 c_path="/tmp";
255 snprintf(tmp, sizeof(tmp), "%s/.annhellcache", c_path);
256 tmp[sizeof(tmp) - 1]='\0';
258 /* create a suitable cached file name */
259 c_basename=cached_base_name(file);
261 snprintf(c_file, sizeof(c_file), "%s/%s.wav", tmp, c_basename);
262 c_file[sizeof(c_file) - 1]='\0';
264 /* does the file already exist? */
265 if(access(c_file, R_OK) == 0)
266 return(c_file);
268 /* create the cache directory */
269 mkdir(tmp, 0755);
271 /* try known conversions; this is an ugly hack */
273 if(strcmp(ext, ".flac") == 0 || strcmp(ext, ".FLAC") == 0)
275 /* flac file */
276 snprintf(tmp, sizeof(tmp), "flac -d -s -o '%s' '%s'",
277 c_file, file);
278 tmp[sizeof(tmp) - 1]='\0';
280 system(tmp);
282 else
283 if(strcmp(ext, ".mp3") == 0 || strcmp(ext, ".MP3") == 0)
285 /* mp3 file */
286 snprintf(tmp, sizeof(tmp), "mpg321 -q -w '%s' '%s'",
287 c_file, file);
288 tmp[sizeof(tmp) - 1]='\0';
290 system(tmp);
293 return(c_file);
298 * ss_load_wave_file - Loads a wave file.
299 * @file: name of the file
300 * @base_freq: base frequency
301 * @min_freq: minimum frequency
302 * @max_freq: maximum frequency
303 * @loop_start: frame number of loop start (-1, no loop)
304 * @loop_end: frame number of loop end (-1, end of wave)
306 * Loads a wave file.
308 struct ss_wave * ss_load_wave_file(char * file,
309 double base_freq, double min_freq, double max_freq,
310 double loop_start, double loop_end)
312 /* preprocess the file, given its extension */
313 if((file=wave_file_preprocess(file)) == NULL)
314 return(NULL);
316 return(ss_load_wav_file(file, base_freq, min_freq,
317 max_freq, loop_start, loop_end));
322 * ss_load_pat_file - Loads an instrument in .PAT format.
323 * @i: The instrument
324 * @filename: filename holding the instrument
326 * Loads data from a Gravis Ultrasound patch (.PAT) format and
327 * stores it as layers for an instrument.
329 * Returns -100 if the file could not be read, -101 or -102
330 * if the file is not recognized as a .PAT file, or 0 if
331 * everything went OK.
333 int ss_load_pat_file(struct ss_ins * i, char * file)
335 FILE * f;
336 char buffer[512];
337 int m, n, o;
338 int n_layers;
339 int flags, bits, sign, loop, pingpong;
340 struct ss_wave * w;
342 if((f=path_fopen(file, "r")) == NULL)
343 return(-100);
345 /* read signatures */
346 fread(buffer, 12, 1, f);
347 if(strcmp(buffer, "GF1PATCH110") != 0)
349 fclose(f);
350 return(-101);
353 fread(buffer, 10, 1, f);
354 if(strcmp(buffer, "ID#000002") != 0)
356 fclose(f);
357 return(-102);
360 /* skip description */
361 fread(buffer, 65, 1, f);
363 /* ignore volume */
364 fget16(f);
366 /* skip */
367 fread(buffer, 109, 1, f);
369 /* # of layers */
370 n_layers=fgetc(f);
372 /* skip */
373 fread(buffer, 40, 1, f);
375 for(n=0;n < n_layers;n++)
377 int size, s_rate;
378 double loop_start, loop_end;
379 double min_freq, max_freq, base_freq;
381 /* layer name */
382 fread(buffer, 8, 1, f);
384 size=(double)fget32(f);
385 loop_start=(double)fget32(f);
386 loop_end=(double)fget32(f);
387 s_rate=fget16(f);
389 min_freq=((double)fget32(f)) / 1000.0;
390 max_freq=((double)fget32(f)) / 1000.0;
391 base_freq=((double)fget32(f)) / 1000.0;
393 if(base_freq < 0)
394 break;
396 /* ignore fine-tune */
397 fget16(f);
399 /* ignore pan position */
400 fgetc(f);
402 /* skip envelope rate, value, tremolo and vibrato */
403 fread(buffer, 18, 1, f);
405 flags=fgetc(f);
407 bits=flags & 0x01 ? 16 : 8;
408 sign=flags & 0x02 ? 0 : 1;
409 loop=flags & 0x04 ? 1 : 0;
410 pingpong=flags & 0x08 ? 1 : 0;
412 if(bits == 16)
414 size /= 2;
415 loop_start /= 2;
416 loop_end /= 2;
419 /* skip frequency scale data */
420 fget16(f); fget16(f);
422 /* skip reserved */
423 fread(buffer, 36, 1, f);
425 if((w=ss_alloc_wave(size, 1, s_rate)) == NULL)
426 break;
428 /* set the rest of values */
429 w->loop_start=loop_start;
430 w->loop_end=loop_end;
431 w->base_freq=base_freq;
432 w->min_freq=min_freq;
433 w->max_freq=max_freq;
435 /* load the wave */
436 load_pcm_wave(f, bits, sign, w);
438 if(pingpong && loop)
440 int loop_size;
441 sample_t * ptr;
443 /* if ping-pong, realloc space for a reverse
444 version of the loop */
445 loop_size=(int)(loop_end - loop_start);
447 ptr=(sample_t *) malloc((size + loop_size + 1)
448 * sizeof(sample_t));
450 /* transfer start and loop */
451 for(m=0;m <= (int)loop_end;m++)
452 ptr[m]=w->wave[0][m];
454 /* transfer a reversed version of the loop */
455 for(o=m - 1;o >= loop_start;o--,m++)
456 ptr[m]=w->wave[0][o];
458 /* transfer the end */
459 for(o=loop_end + 1;o < size;o++,m++)
460 ptr[m]=w->wave[0][o];
462 w->loop_end += (double) loop_size;
463 w->size += (double) loop_size;
465 /* free and swap */
466 free(w->wave[0]);
467 w->wave[0]=ptr;
470 if(loop == 0) w->loop_start=-1;
472 /* finally add layer to instrument */
473 ss_ins_add_layer(i, w);
476 fclose(f);
477 return(0);