3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2006 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
33 #include <sys/types.h>
42 /* maximum page size */
43 int ss_page_size
= 441000;
49 static int fget16(FILE * f
)
50 /* Reads a 16 bit integer from a file in big endian byte ordering */
55 c
+= (fgetc(f
) * 256);
61 static int fget32(FILE * f
)
62 /* Reads a 32 bit integer from a file in big endian byte ordering */
67 c
+= (fgetc(f
) * 256);
68 c
+= (fgetc(f
) * 65536);
69 c
+= (fgetc(f
) * 16777216);
75 static sample_t
load_sample(FILE * f
, int bits
, int sign
)
76 /* loads one sample from a file */
80 /* if on eof, return silence */
81 if(feof(f
)) return(0);
91 s
= fget16(f
) - 32768;
93 s
= (short int)fget16(f
);
100 void load_pcm_wave(FILE * f
, struct ss_wave
* w
)
101 /* loads an interleaved stream from a file */
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
)
132 short int b_per_sec
, n_channels
;
133 char riffid
[5], waveid
[5], fmtid
[5], dataid
[5];
139 if((f
= path_fopen(file
, "r")) == NULL
)
142 fread(riffid
, 1, 4, f
);
144 fread(&rlen
, 1, 4, f
);
145 fread(waveid
, 1, 4, f
);
148 if(strcmp(waveid
,"WAVE"))
154 fread(fmtid
, 1, 4, f
);
163 /* wicked compressed format? fail */
168 n_channels
= fget16(f
);
170 b_per_sec
= fget32(f
);
172 bits
= fget16(f
) / n_channels
;
175 fread(dummydata
, 1, (size_t)flen
- 14, f
);
176 fread(dataid
, 1, 4, f
);
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
;
196 w
->loop_end
= loop_end
;
198 /* fill the info needed for paging */
199 w
->filename
= strdup(file
);
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
;
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];
224 SHA1_Update(&c
, file
, strlen(file
));
225 SHA1_Final(sha1
, &c
);
227 for(n
= 0;n
< sizeof(sha1
);n
++)
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';
241 static char * wave_file_preprocess(char * file
)
242 /* preprocess the file, converting with external tools if necessary */
248 static char c_file
[1024];
250 /* gets the file extension */
251 if((ext
= strrchr(file
, '.')) == 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)
259 /* resolve the file name */
260 if((file
= locate_file(file
)) == NULL
)
263 /* build the directory cache name */
264 if((c_path
= getenv("HOME")) == NULL
)
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)
280 /* create the cache directory */
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)
291 snprintf(tmp
, sizeof(tmp
), "flac -d -s -o '%s' '%s'",
293 tmp
[sizeof(tmp
) - 1] = '\0';
298 if(strcmp(ext
, ".mp3") == 0 || strcmp(ext
, ".MP3") == 0)
301 snprintf(tmp
, sizeof(tmp
), "mpg321 -q -w '%s' '%s'",
303 tmp
[sizeof(tmp
) - 1] = '\0';
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)
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
)
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.
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
)
354 int flags
, bits
, sign
, loop
, pingpong
;
357 if((f
= path_fopen(file
, "r")) == NULL
)
360 /* read signatures */
361 fread(buffer
, 12, 1, f
);
362 if(strcmp(buffer
, "GF1PATCH110") != 0)
368 fread(buffer
, 10, 1, f
);
369 if(strcmp(buffer
, "ID#000002") != 0)
375 /* skip description */
376 fread(buffer
, 65, 1, f
);
382 fread(buffer
, 109, 1, f
);
388 fread(buffer
, 40, 1, f
);
390 for(n
= 0;n
< n_layers
;n
++)
393 double loop_start
, loop_end
;
394 double min_freq
, max_freq
, base_freq
;
397 fread(buffer
, 8, 1, f
);
399 size
= (double)fget32(f
);
400 loop_start
= (double)fget32(f
);
401 loop_end
= (double)fget32(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;
411 /* ignore fine-tune */
414 /* ignore pan position */
417 /* skip envelope rate, value, tremolo and vibrato */
418 fread(buffer
, 18, 1, 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;
434 /* skip frequency scale data */
435 fget16(f
); fget16(f
);
438 fread(buffer
, 36, 1, f
);
440 if((w
= ss_alloc_wave(size
, 1, s_rate
, -1)) == NULL
)
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
;
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)
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
;
489 if(loop
== 0) w
->loop_start
= -1;
491 /* finally add layer to instrument */
492 ss_ins_add_layer(i
, w
);