Branch annhell-branch-paged MERGED.
[ahxm.git] / ss_input.c
blob6ed117b1efc1534f040061d142abfca535b86fc1
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 /* maximum page size */
43 int ss_page_size = 441000;
45 /*******************
46 Code
47 ********************/
49 static int fget16(FILE * f)
50 /* Reads a 16 bit integer from a file in big endian byte ordering */
52 int c;
54 c = fgetc(f);
55 c += (fgetc(f) * 256);
57 return(c);
61 static int fget32(FILE * f)
62 /* Reads a 32 bit integer from a file in big endian byte ordering */
64 int c;
66 c = fgetc(f);
67 c += (fgetc(f) * 256);
68 c += (fgetc(f) * 65536);
69 c += (fgetc(f) * 16777216);
71 return(c);
75 static sample_t load_sample(FILE * f, int bits, int sign)
76 /* loads one sample from a file */
78 int s;
80 /* if on eof, return silence */
81 if(feof(f)) return(0);
83 if(bits == 8)
85 s = fgetc(f) - 128;
86 s <<= 8;
88 else
90 if(!sign)
91 s = fget16(f) - 32768;
92 else
93 s = (short int)fget16(f);
96 return((sample_t) s);
100 void load_pcm_wave(FILE * f, struct ss_wave * w)
101 /* loads an interleaved stream from a file */
103 int n, m;
105 /* fills the channels */
106 for(m = 0;m < w->p_size;m++)
108 for(n = 0;n < w->n_channels;n++)
109 w->wave[n][m] = load_sample(f, w->bits, w->sign);
115 * ss_load_wav_file - Loads a file in .WAV format.
116 * @file: name of the file
117 * @base_freq: base frequency
118 * @min_freq: minimum frequency
119 * @max_freq: maximum frequency
120 * @loop_start: frame number of loop start (-1, no loop)
121 * @loop_end: frame number of loop end (-1, end of wave)
123 * Loads a file in .WAV format.
125 struct ss_wave * ss_load_wav_file(char * file,
126 double base_freq, double min_freq, double max_freq,
127 double loop_start, double loop_end)
129 FILE * f;
130 char dummydata[256];
131 int rlen, flen;
132 short int b_per_sec, n_channels;
133 char riffid[5], waveid[5], fmtid[5], dataid[5];
134 double size;
135 int s_rate, bits;
136 struct ss_wave * w;
137 int p;
139 if((f = path_fopen(file, "r")) == NULL)
140 return(NULL);
142 fread(riffid, 1, 4, f);
143 riffid[4] = 0;
144 fread(&rlen, 1, 4, f);
145 fread(waveid, 1, 4, f);
146 waveid[4] = 0;
148 if(strcmp(waveid,"WAVE"))
150 fclose(f);
151 return(NULL);
154 fread(fmtid, 1, 4, f);
155 fmtid[4] = 0;
156 flen = fget32(f);
158 if(flen > 240)
159 flen = 240;
161 if(fget16(f) != 1)
163 /* wicked compressed format? fail */
164 fclose(f);
165 return(NULL);
168 n_channels = fget16(f);
169 s_rate = fget32(f);
170 b_per_sec = fget32(f);
172 bits = fget16(f) / n_channels;
173 bits *= 8;
175 fread(dummydata, 1, (size_t)flen - 14, f);
176 fread(dataid, 1, 4, f);
177 dataid[4] = 0;
179 size = (double) fget32(f);
180 if(bits == 16) size /= 2;
181 size /= (double) n_channels;
183 p = size > ss_page_size ? ss_page_size : size;
185 if((w = ss_alloc_wave(size, n_channels, s_rate, p)) != NULL)
187 w->base_freq = base_freq;
188 w->min_freq = min_freq;
189 w->max_freq = max_freq;
191 w->loop_start = loop_start;
193 if(loop_end < 0)
194 w->loop_end = size;
195 else
196 w->loop_end = loop_end;
198 /* fill the info needed for paging */
199 w->filename = strdup(file);
200 w->f_pos = ftell(f);
201 w->bits = bits;
202 w->sign = 1;
204 /* set the page offset further the end, to
205 force a page reading the first time it's used */
206 w->p_offset = (int) size;
209 fclose(f);
211 return(w);
215 static char * cached_base_name(char * file)
216 /* builds a unique cache file name using a SHA1 hash */
218 static char c_file[64];
219 unsigned char sha1[20];
220 SHA_CTX c;
221 int n;
223 SHA1_Init(&c);
224 SHA1_Update(&c, file, strlen(file));
225 SHA1_Final(sha1, &c);
227 for(n = 0;n < sizeof(sha1);n++)
229 char tmp[3];
231 snprintf(tmp, sizeof(tmp), "%02x", sha1[n]);
232 c_file[n * 2] = tmp[0];
233 c_file[(n * 2) + 1] = tmp[1];
236 c_file[n * 2] = '\0';
237 return(c_file);
241 static char * wave_file_preprocess(char * file)
242 /* preprocess the file, converting with external tools if necessary */
244 char * ext;
245 char * c_path;
246 char * c_basename;
247 char tmp[2048];
248 static char c_file[1024];
250 /* gets the file extension */
251 if((ext = strrchr(file, '.')) == NULL)
252 return(NULL);
254 /* if it's a .wav file, return as is; no
255 further processing is needed */
256 if(strcmp(ext, ".wav") == 0 || strcmp(ext, ".WAV") == 0)
257 return(file);
259 /* resolve the file name */
260 if((file = locate_file(file)) == NULL)
261 return(NULL);
263 /* build the directory cache name */
264 if((c_path = getenv("HOME")) == NULL)
265 c_path = "/tmp";
267 snprintf(tmp, sizeof(tmp), "%s/.annhellcache", c_path);
268 tmp[sizeof(tmp) - 1] = '\0';
270 /* create a suitable cached file name */
271 c_basename = cached_base_name(file);
273 snprintf(c_file, sizeof(c_file), "%s/%s.wav", tmp, c_basename);
274 c_file[sizeof(c_file) - 1] = '\0';
276 /* does the file already exist? */
277 if(access(c_file, R_OK) == 0)
278 return(c_file);
280 /* create the cache directory */
281 mkdir(tmp, 0755);
283 if(debug)
284 printf("Caching '%s' to '%s'\n", file, c_file);
286 /* try known conversions; this is an ugly hack */
288 if(strcmp(ext, ".flac") == 0 || strcmp(ext, ".FLAC") == 0)
290 /* flac file */
291 snprintf(tmp, sizeof(tmp), "flac -d -s -o '%s' '%s'",
292 c_file, file);
293 tmp[sizeof(tmp) - 1] = '\0';
295 system(tmp);
297 else
298 if(strcmp(ext, ".mp3") == 0 || strcmp(ext, ".MP3") == 0)
300 /* mp3 file */
301 snprintf(tmp, sizeof(tmp), "mpg321 -q -w '%s' '%s'",
302 c_file, file);
303 tmp[sizeof(tmp) - 1] = '\0';
305 system(tmp);
308 return(c_file);
313 * ss_load_wave_file - Loads a wave file.
314 * @file: name of the file
315 * @base_freq: base frequency
316 * @min_freq: minimum frequency
317 * @max_freq: maximum frequency
318 * @loop_start: frame number of loop start (-1, no loop)
319 * @loop_end: frame number of loop end (-1, end of wave)
321 * Loads a wave file.
323 struct ss_wave * ss_load_wave_file(char * file,
324 double base_freq, double min_freq, double max_freq,
325 double loop_start, double loop_end)
327 /* preprocess the file, given its extension */
328 if((file = wave_file_preprocess(file)) == NULL)
329 return(NULL);
331 return(ss_load_wav_file(file, base_freq, min_freq,
332 max_freq, loop_start, loop_end));
337 * ss_load_pat_file - Loads an instrument in .PAT format.
338 * @i: The instrument
339 * @filename: filename holding the instrument
341 * Loads data from a Gravis Ultrasound patch (.PAT) format and
342 * stores it as layers for an instrument.
344 * Returns -100 if the file could not be read, -101 or -102
345 * if the file is not recognized as a .PAT file, or 0 if
346 * everything went OK.
348 int ss_load_pat_file(struct ss_ins * i, char * file)
350 FILE * f;
351 char buffer[512];
352 int m, n, o;
353 int n_layers;
354 int flags, bits, sign, loop, pingpong;
355 struct ss_wave * w;
357 if((f = path_fopen(file, "r")) == NULL)
358 return(-100);
360 /* read signatures */
361 fread(buffer, 12, 1, f);
362 if(strcmp(buffer, "GF1PATCH110") != 0)
364 fclose(f);
365 return(-101);
368 fread(buffer, 10, 1, f);
369 if(strcmp(buffer, "ID#000002") != 0)
371 fclose(f);
372 return(-102);
375 /* skip description */
376 fread(buffer, 65, 1, f);
378 /* ignore volume */
379 fget16(f);
381 /* skip */
382 fread(buffer, 109, 1, f);
384 /* # of layers */
385 n_layers = fgetc(f);
387 /* skip */
388 fread(buffer, 40, 1, f);
390 for(n = 0;n < n_layers;n++)
392 int size, s_rate;
393 double loop_start, loop_end;
394 double min_freq, max_freq, base_freq;
396 /* layer name */
397 fread(buffer, 8, 1, f);
399 size = (double)fget32(f);
400 loop_start = (double)fget32(f);
401 loop_end = (double)fget32(f);
402 s_rate = fget16(f);
404 min_freq = ((double)fget32(f)) / 1000.0;
405 max_freq = ((double)fget32(f)) / 1000.0;
406 base_freq = ((double)fget32(f)) / 1000.0;
408 if(base_freq < 0)
409 break;
411 /* ignore fine-tune */
412 fget16(f);
414 /* ignore pan position */
415 fgetc(f);
417 /* skip envelope rate, value, tremolo and vibrato */
418 fread(buffer, 18, 1, f);
420 flags = fgetc(f);
422 bits = flags & 0x01 ? 16 : 8;
423 sign = flags & 0x02 ? 0 : 1;
424 loop = flags & 0x04 ? 1 : 0;
425 pingpong = flags & 0x08 ? 1 : 0;
427 if(bits == 16)
429 size /= 2;
430 loop_start /= 2;
431 loop_end /= 2;
434 /* skip frequency scale data */
435 fget16(f); fget16(f);
437 /* skip reserved */
438 fread(buffer, 36, 1, f);
440 if((w = ss_alloc_wave(size, 1, s_rate, -1)) == NULL)
441 break;
443 /* set the rest of values */
444 w->loop_start = loop_start;
445 w->loop_end = loop_end;
446 w->base_freq = base_freq;
447 w->min_freq = min_freq;
448 w->max_freq = max_freq;
449 w->bits = bits;
450 w->sign = sign;
452 /* load the wave */
453 ss_prepare_wave(w);
454 load_pcm_wave(f, w);
456 if(pingpong && loop)
458 int loop_size;
459 sample_t * ptr;
461 /* if ping-pong, realloc space for a reverse
462 version of the loop */
463 loop_size = (int)(loop_end - loop_start);
465 ptr = (sample_t *) malloc((size + loop_size + 1)
466 * sizeof(sample_t));
468 /* transfer start and loop */
469 for(m = 0;m <= (int)loop_end;m++)
470 ptr[m] = w->wave[0][m];
472 /* transfer a reversed version of the loop */
473 for(o = m - 1;o >= loop_start;o--, m++)
474 ptr[m] = w->wave[0][o];
476 /* transfer the end */
477 for(o = loop_end + 1;o < size;o++, m++)
478 ptr[m] = w->wave[0][o];
480 w->loop_end += (double) loop_size;
481 w->size += (double) loop_size;
482 w->p_size += loop_size;
484 /* free and swap */
485 free(w->wave[0]);
486 w->wave[0] = ptr;
489 if(loop == 0) w->loop_start = -1;
491 /* finally add layer to instrument */
492 ss_ins_add_layer(i, w);
495 fclose(f);
496 return(0);