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 /* locate the file in the current library path */
140 if((file
= locate_file(file
)) == NULL
)
143 if((f
= fopen(file
, "r")) == NULL
)
146 fread(riffid
, 1, 4, f
);
148 fread(&rlen
, 1, 4, f
);
149 fread(waveid
, 1, 4, f
);
152 if(strcmp(waveid
,"WAVE"))
158 fread(fmtid
, 1, 4, f
);
167 /* wicked compressed format? fail */
172 n_channels
= fget16(f
);
174 b_per_sec
= fget32(f
);
176 bits
= fget16(f
) / n_channels
;
179 fread(dummydata
, 1, (size_t)flen
- 14, f
);
180 fread(dataid
, 1, 4, f
);
183 size
= (double) fget32(f
);
184 if(bits
== 16) size
/= 2;
185 size
/= (double) n_channels
;
187 p
= size
> ss_page_size
? ss_page_size
: size
;
189 if((w
= ss_alloc_wave(size
, n_channels
, s_rate
, p
)) != NULL
)
191 w
->base_freq
= base_freq
;
192 w
->min_freq
= min_freq
;
193 w
->max_freq
= max_freq
;
195 w
->loop_start
= loop_start
;
200 w
->loop_end
= loop_end
;
202 /* fill the info needed for paging */
203 w
->filename
= strdup(file
);
208 /* set the page offset further the end, to
209 force a page reading the first time it's used */
210 w
->p_offset
= (int) size
;
219 static char * cached_base_name(char * file
)
220 /* builds a unique cache file name using a SHA1 hash */
222 static char c_file
[64];
223 unsigned char sha1
[20];
228 SHA1_Update(&c
, file
, strlen(file
));
229 SHA1_Final(sha1
, &c
);
231 for(n
= 0;n
< sizeof(sha1
);n
++)
235 snprintf(tmp
, sizeof(tmp
), "%02x", sha1
[n
]);
236 c_file
[n
* 2] = tmp
[0];
237 c_file
[(n
* 2) + 1] = tmp
[1];
240 c_file
[n
* 2] = '\0';
245 static char * wave_file_preprocess(char * file
)
246 /* preprocess the file, converting with external tools if necessary */
252 static char c_file
[1024];
254 /* gets the file extension */
255 if((ext
= strrchr(file
, '.')) == NULL
)
258 /* if it's a .wav file, return as is; no
259 further processing is needed */
260 if(strcmp(ext
, ".wav") == 0 || strcmp(ext
, ".WAV") == 0)
263 /* resolve the file name */
264 if((file
= locate_file(file
)) == NULL
)
267 /* build the directory cache name */
268 if((c_path
= getenv("HOME")) == NULL
)
271 snprintf(tmp
, sizeof(tmp
), "%s/.annhellcache", c_path
);
272 tmp
[sizeof(tmp
) - 1] = '\0';
274 /* create a suitable cached file name */
275 c_basename
= cached_base_name(file
);
277 snprintf(c_file
, sizeof(c_file
), "%s/%s.wav", tmp
, c_basename
);
278 c_file
[sizeof(c_file
) - 1] = '\0';
280 /* does the file already exist? */
281 if(access(c_file
, R_OK
) == 0)
284 /* create the cache directory */
288 printf("Caching '%s' to '%s'\n", file
, c_file
);
290 /* try known conversions; this is an ugly hack */
292 if(strcmp(ext
, ".flac") == 0 || strcmp(ext
, ".FLAC") == 0)
295 snprintf(tmp
, sizeof(tmp
), "flac -d -s -o '%s' '%s'",
297 tmp
[sizeof(tmp
) - 1] = '\0';
302 if(strcmp(ext
, ".mp3") == 0 || strcmp(ext
, ".MP3") == 0)
305 snprintf(tmp
, sizeof(tmp
), "mpg321 -q -w '%s' '%s'",
307 tmp
[sizeof(tmp
) - 1] = '\0';
317 * ss_load_wave_file - Loads a wave file.
318 * @file: name of the file
319 * @base_freq: base frequency
320 * @min_freq: minimum frequency
321 * @max_freq: maximum frequency
322 * @loop_start: frame number of loop start (-1, no loop)
323 * @loop_end: frame number of loop end (-1, end of wave)
327 struct ss_wave
* ss_load_wave_file(char * file
,
328 double base_freq
, double min_freq
, double max_freq
,
329 double loop_start
, double loop_end
)
331 /* preprocess the file, given its extension */
332 if((file
= wave_file_preprocess(file
)) == NULL
)
335 return(ss_load_wav_file(file
, base_freq
, min_freq
,
336 max_freq
, loop_start
, loop_end
));
341 * ss_load_pat_file - Loads an instrument in .PAT format.
343 * @filename: filename holding the instrument
345 * Loads data from a Gravis Ultrasound patch (.PAT) format and
346 * stores it as layers for an instrument.
348 * Returns -100 if the file could not be read, -101 or -102
349 * if the file is not recognized as a .PAT file, or 0 if
350 * everything went OK.
352 int ss_load_pat_file(struct ss_ins
* i
, char * file
)
358 int flags
, bits
, sign
, loop
, pingpong
;
361 if((f
= path_fopen(file
, "r")) == NULL
)
364 /* read signatures */
365 fread(buffer
, 12, 1, f
);
366 if(strcmp(buffer
, "GF1PATCH110") != 0)
372 fread(buffer
, 10, 1, f
);
373 if(strcmp(buffer
, "ID#000002") != 0)
379 /* skip description */
380 fread(buffer
, 65, 1, f
);
386 fread(buffer
, 109, 1, f
);
392 fread(buffer
, 40, 1, f
);
394 for(n
= 0;n
< n_layers
;n
++)
397 double loop_start
, loop_end
;
398 double min_freq
, max_freq
, base_freq
;
401 fread(buffer
, 8, 1, f
);
403 size
= (double)fget32(f
);
404 loop_start
= (double)fget32(f
);
405 loop_end
= (double)fget32(f
);
408 min_freq
= ((double)fget32(f
)) / 1000.0;
409 max_freq
= ((double)fget32(f
)) / 1000.0;
410 base_freq
= ((double)fget32(f
)) / 1000.0;
415 /* ignore fine-tune */
418 /* ignore pan position */
421 /* skip envelope rate, value, tremolo and vibrato */
422 fread(buffer
, 18, 1, f
);
426 bits
= flags
& 0x01 ? 16 : 8;
427 sign
= flags
& 0x02 ? 0 : 1;
428 loop
= flags
& 0x04 ? 1 : 0;
429 pingpong
= flags
& 0x08 ? 1 : 0;
438 /* skip frequency scale data */
439 fget16(f
); fget16(f
);
442 fread(buffer
, 36, 1, f
);
444 if((w
= ss_alloc_wave(size
, 1, s_rate
, -1)) == NULL
)
447 /* set the rest of values */
448 w
->loop_start
= loop_start
;
449 w
->loop_end
= loop_end
;
450 w
->base_freq
= base_freq
;
451 w
->min_freq
= min_freq
;
452 w
->max_freq
= max_freq
;
465 /* if ping-pong, realloc space for a reverse
466 version of the loop */
467 loop_size
= (int)(loop_end
- loop_start
);
469 ptr
= (sample_t
*) malloc((size
+ loop_size
+ 1)
472 /* transfer start and loop */
473 for(m
= 0;m
<= (int)loop_end
;m
++)
474 ptr
[m
] = w
->wave
[0][m
];
476 /* transfer a reversed version of the loop */
477 for(o
= m
- 1;o
>= loop_start
;o
--, m
++)
478 ptr
[m
] = w
->wave
[0][o
];
480 /* transfer the end */
481 for(o
= loop_end
+ 1;o
< size
;o
++, m
++)
482 ptr
[m
] = w
->wave
[0][o
];
484 w
->loop_end
+= (double) loop_size
;
485 w
->size
+= (double) loop_size
;
486 w
->p_size
+= loop_size
;
493 if(loop
== 0) w
->loop_start
= -1;
495 /* finally add layer to instrument */
496 ss_ins_add_layer(i
, w
);