199
|
1 /*
|
|
2
|
|
3 TiMidity -- Experimental MIDI to WAVE converter
|
|
4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
|
|
5
|
|
6 This program is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with this program; if not, write to the Free Software
|
|
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 */
|
|
21
|
|
22 #if HAVE_CONFIG_H
|
|
23 # include <config.h>
|
|
24 #endif
|
|
25
|
|
26 #include <stdio.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29
|
|
30 #include "SDL_sound.h"
|
|
31
|
|
32 #define __SDL_SOUND_INTERNAL__
|
|
33 #include "SDL_sound_internal.h"
|
|
34
|
|
35 #include "timidity.h"
|
|
36
|
|
37 #include "options.h"
|
|
38 #include "common.h"
|
|
39 #include "instrum.h"
|
|
40 #include "playmidi.h"
|
|
41 #include "readmidi.h"
|
|
42 #include "output.h"
|
|
43
|
|
44 #include "tables.h"
|
|
45
|
|
46 ToneBank *master_tonebank[128], *master_drumset[128];
|
|
47
|
|
48 static char def_instr_name[256] = "";
|
|
49
|
|
50 #define MAXWORDS 10
|
|
51
|
|
52 /* Quick-and-dirty fgets() replacement. */
|
|
53
|
|
54 static char *RWgets(SDL_RWops *rw, char *s, int size)
|
|
55 {
|
|
56 int num_read = 0;
|
|
57 int newline = 0;
|
|
58
|
|
59 while (num_read < size && !newline)
|
|
60 {
|
|
61 if (SDL_RWread(rw, &s[num_read], 1, 1) != 1)
|
|
62 break;
|
|
63
|
|
64 /* Unlike fgets(), don't store newline. Under Windows/DOS we'll
|
|
65 * probably get an extra blank line for every line that's being
|
|
66 * read, but that should be ok.
|
|
67 */
|
|
68 if (s[num_read] == '\n' || s[num_read] == '\r')
|
|
69 {
|
|
70 s[num_read] = '\0';
|
|
71 newline = 1;
|
|
72 }
|
|
73
|
|
74 num_read++;
|
|
75 }
|
|
76
|
|
77 s[num_read] = '\0';
|
|
78
|
|
79 return (num_read != 0) ? s : NULL;
|
|
80 }
|
|
81
|
|
82 static int read_config_file(char *name)
|
|
83 {
|
|
84 SDL_RWops *rw;
|
|
85 char tmp[1024], *w[MAXWORDS], *cp;
|
|
86 ToneBank *bank=0;
|
|
87 int i, j, k, line=0, words;
|
|
88 static int rcf_count=0;
|
|
89
|
|
90 if (rcf_count>50)
|
|
91 {
|
|
92 SNDDBG(("Probable source loop in configuration files\n"));
|
|
93 return (-1);
|
|
94 }
|
|
95
|
|
96 if (!(rw=open_file(name)))
|
|
97 return -1;
|
|
98
|
|
99 while (RWgets(rw, tmp, sizeof(tmp)))
|
|
100 {
|
|
101 line++;
|
|
102 w[words=0]=strtok(tmp, " \t\240");
|
|
103 if (!w[0] || (*w[0]=='#')) continue;
|
|
104 while (w[words] && *w[words] != '#' && (words < MAXWORDS))
|
|
105 w[++words]=strtok(0," \t\240");
|
|
106 if (!strcmp(w[0], "dir"))
|
|
107 {
|
|
108 if (words < 2)
|
|
109 {
|
|
110 SNDDBG(("%s: line %d: No directory given\n", name, line));
|
|
111 return -2;
|
|
112 }
|
|
113 for (i=1; i<words; i++)
|
|
114 add_to_pathlist(w[i]);
|
|
115 }
|
|
116 else if (!strcmp(w[0], "source"))
|
|
117 {
|
|
118 if (words < 2)
|
|
119 {
|
|
120 SNDDBG(("%s: line %d: No file name given\n", name, line));
|
|
121 return -2;
|
|
122 }
|
|
123 for (i=1; i<words; i++)
|
|
124 {
|
|
125 rcf_count++;
|
|
126 read_config_file(w[i]);
|
|
127 rcf_count--;
|
|
128 }
|
|
129 }
|
|
130 else if (!strcmp(w[0], "default"))
|
|
131 {
|
|
132 if (words != 2)
|
|
133 {
|
|
134 SNDDBG(("%s: line %d: Must specify exactly one patch name\n",
|
|
135 name, line));
|
|
136 return -2;
|
|
137 }
|
|
138 strncpy(def_instr_name, w[1], 255);
|
|
139 def_instr_name[255]='\0';
|
|
140 }
|
|
141 else if (!strcmp(w[0], "drumset"))
|
|
142 {
|
|
143 if (words < 2)
|
|
144 {
|
|
145 SNDDBG(("%s: line %d: No drum set number given\n", name, line));
|
|
146 return -2;
|
|
147 }
|
|
148 i=atoi(w[1]);
|
|
149 if (i<0 || i>127)
|
|
150 {
|
|
151 SNDDBG(("%s: line %d: Drum set must be between 0 and 127\n",
|
|
152 name, line));
|
|
153 return -2;
|
|
154 }
|
|
155 if (!master_drumset[i])
|
|
156 {
|
|
157 master_drumset[i] = safe_malloc(sizeof(ToneBank));
|
|
158 memset(master_drumset[i], 0, sizeof(ToneBank));
|
|
159 master_drumset[i]->tone = safe_malloc(128 * sizeof(ToneBankElement));
|
|
160 memset(master_drumset[i]->tone, 0, 128 * sizeof(ToneBankElement));
|
|
161 }
|
|
162 bank=master_drumset[i];
|
|
163 }
|
|
164 else if (!strcmp(w[0], "bank"))
|
|
165 {
|
|
166 if (words < 2)
|
|
167 {
|
|
168 SNDDBG(("%s: line %d: No bank number given\n", name, line));
|
|
169 return -2;
|
|
170 }
|
|
171 i=atoi(w[1]);
|
|
172 if (i<0 || i>127)
|
|
173 {
|
|
174 SNDDBG(("%s: line %d: Tone bank must be between 0 and 127\n",
|
|
175 name, line));
|
|
176 return -2;
|
|
177 }
|
|
178 if (!master_tonebank[i])
|
|
179 {
|
|
180 master_tonebank[i] = safe_malloc(sizeof(ToneBank));
|
|
181 memset(master_tonebank[i], 0, sizeof(ToneBank));
|
|
182 master_tonebank[i]->tone = safe_malloc(128 * sizeof(ToneBankElement));
|
|
183 memset(master_tonebank[i]->tone, 0, 128 * sizeof(ToneBankElement));
|
|
184 }
|
|
185 bank=master_tonebank[i];
|
|
186 }
|
|
187 else
|
|
188 {
|
|
189 if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
|
|
190 {
|
|
191 SNDDBG(("%s: line %d: syntax error\n", name, line));
|
|
192 return -2;
|
|
193 }
|
|
194 i=atoi(w[0]);
|
|
195 if (i<0 || i>127)
|
|
196 {
|
|
197 SNDDBG(("%s: line %d: Program must be between 0 and 127\n",
|
|
198 name, line));
|
|
199 return -2;
|
|
200 }
|
|
201 if (!bank)
|
|
202 {
|
|
203 SNDDBG(("%s: line %d: Must specify tone bank or drum set before assignment\n",
|
|
204 name, line));
|
|
205 return -2;
|
|
206 }
|
|
207 if (bank->tone[i].name)
|
|
208 free(bank->tone[i].name);
|
|
209 strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
|
|
210 bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
|
|
211 bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
|
|
212 bank->tone[i].strip_tail=-1;
|
|
213
|
|
214 for (j=2; j<words; j++)
|
|
215 {
|
|
216 if (!(cp=strchr(w[j], '=')))
|
|
217 {
|
|
218 SNDDBG(("%s: line %d: bad patch option %s\n", name, line, w[j]));
|
|
219 return -2;
|
|
220 }
|
|
221 *cp++=0;
|
|
222 if (!strcmp(w[j], "amp"))
|
|
223 {
|
|
224 k=atoi(cp);
|
|
225 if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
|
|
226 {
|
|
227 SNDDBG(("%s: line %d: amplification must be between 0 and %d\n",
|
|
228 name, line, MAX_AMPLIFICATION));
|
|
229 return -2;
|
|
230 }
|
|
231 bank->tone[i].amp=k;
|
|
232 }
|
|
233 else if (!strcmp(w[j], "note"))
|
|
234 {
|
|
235 k=atoi(cp);
|
|
236 if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
|
|
237 {
|
|
238 SNDDBG(("%s: line %d: note must be between 0 and 127\n",
|
|
239 name, line));
|
|
240 return -2;
|
|
241 }
|
|
242 bank->tone[i].note=k;
|
|
243 }
|
|
244 else if (!strcmp(w[j], "pan"))
|
|
245 {
|
|
246 if (!strcmp(cp, "center"))
|
|
247 k=64;
|
|
248 else if (!strcmp(cp, "left"))
|
|
249 k=0;
|
|
250 else if (!strcmp(cp, "right"))
|
|
251 k=127;
|
|
252 else
|
|
253 k=((atoi(cp)+100) * 100) / 157;
|
|
254 if ((k<0 || k>127) || (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
|
|
255 {
|
|
256 SNDDBG(("%s: line %d: panning must be left, right, center, or between -100 and 100\n",
|
|
257 name, line));
|
|
258 return -2;
|
|
259 }
|
|
260 bank->tone[i].pan=k;
|
|
261 }
|
|
262 else if (!strcmp(w[j], "keep"))
|
|
263 {
|
|
264 if (!strcmp(cp, "env"))
|
|
265 bank->tone[i].strip_envelope=0;
|
|
266 else if (!strcmp(cp, "loop"))
|
|
267 bank->tone[i].strip_loop=0;
|
|
268 else
|
|
269 {
|
|
270 SNDDBG(("%s: line %d: keep must be env or loop\n", name, line));
|
|
271 return -2;
|
|
272 }
|
|
273 }
|
|
274 else if (!strcmp(w[j], "strip"))
|
|
275 {
|
|
276 if (!strcmp(cp, "env"))
|
|
277 bank->tone[i].strip_envelope=1;
|
|
278 else if (!strcmp(cp, "loop"))
|
|
279 bank->tone[i].strip_loop=1;
|
|
280 else if (!strcmp(cp, "tail"))
|
|
281 bank->tone[i].strip_tail=1;
|
|
282 else
|
|
283 {
|
|
284 SNDDBG(("%s: line %d: strip must be env, loop, or tail\n",
|
|
285 name, line));
|
|
286 return -2;
|
|
287 }
|
|
288 }
|
|
289 else
|
|
290 {
|
|
291 SNDDBG(("%s: line %d: bad patch option %s\n", name, line, w[j]));
|
|
292 return -2;
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296 }
|
|
297 SDL_RWclose(rw);
|
|
298 return 0;
|
|
299 }
|
|
300
|
|
301 int Timidity_Init()
|
|
302 {
|
|
303 /* !!! FIXME: This may be ugly, but slightly less so than requiring the
|
|
304 * default search path to have only one element. I think.
|
|
305 *
|
|
306 * We only need to include the likely locations for the config
|
|
307 * file itself since that file should contain any other directory
|
|
308 * that needs to be added to the search path.
|
|
309 */
|
|
310 #ifdef WIN32
|
|
311 add_to_pathlist("\\TIMIDITY");
|
|
312 #else
|
|
313 add_to_pathlist("/usr/local/lib/timidity");
|
|
314 add_to_pathlist("/etc");
|
|
315 #endif
|
|
316
|
|
317 /* Allocate memory for the standard tonebank and drumset */
|
|
318 master_tonebank[0] = safe_malloc(sizeof(ToneBank));
|
|
319 memset(master_tonebank[0], 0, sizeof(ToneBank));
|
|
320 master_tonebank[0]->tone = safe_malloc(128 * sizeof(ToneBankElement));
|
|
321 memset(master_tonebank[0]->tone, 0, 128 * sizeof(ToneBankElement));
|
|
322
|
|
323 master_drumset[0] = safe_malloc(sizeof(ToneBank));
|
|
324 memset(master_drumset[0], 0, sizeof(ToneBank));
|
|
325 master_drumset[0]->tone = safe_malloc(128 * sizeof(ToneBankElement));
|
|
326 memset(master_drumset[0]->tone, 0, 128 * sizeof(ToneBankElement));
|
|
327
|
|
328 return read_config_file(CONFIG_FILE);
|
|
329 }
|
|
330
|
|
331 MidiSong *Timidity_LoadSong(SDL_RWops *rw, SDL_AudioSpec *audio)
|
|
332 {
|
|
333 MidiSong *song;
|
|
334 Sint32 events;
|
|
335 int i;
|
|
336
|
|
337 if (rw == NULL)
|
|
338 return NULL;
|
|
339
|
|
340 /* Allocate memory for the song */
|
|
341 song = (MidiSong *)safe_malloc(sizeof(*song));
|
|
342 memset(song, 0, sizeof(*song));
|
|
343
|
|
344 for (i = 0; i < 128; i++)
|
|
345 {
|
|
346 if (master_tonebank[i])
|
|
347 {
|
|
348 song->tonebank[i] = safe_malloc(sizeof(ToneBank));
|
|
349 song->tonebank[i]->tone = master_tonebank[i]->tone;
|
|
350 }
|
|
351 if (master_drumset[i])
|
|
352 {
|
|
353 song->drumset[i] = safe_malloc(sizeof(ToneBank));
|
|
354 song->drumset[i]->tone = master_drumset[i]->tone;
|
|
355 }
|
|
356 }
|
|
357
|
|
358 song->amplification = DEFAULT_AMPLIFICATION;
|
|
359 song->voices = DEFAULT_VOICES;
|
|
360 song->drumchannels = DEFAULT_DRUMCHANNELS;
|
|
361
|
|
362 song->rw = rw;
|
|
363
|
|
364 song->rate = audio->freq;
|
|
365 song->encoding = 0;
|
|
366 if ((audio->format & 0xFF) == 16)
|
|
367 song->encoding |= PE_16BIT;
|
|
368 if (audio->format & 0x8000)
|
|
369 song->encoding |= PE_SIGNED;
|
|
370 if (audio->channels == 1)
|
|
371 song->encoding |= PE_MONO;
|
|
372 switch (audio->format) {
|
|
373 case AUDIO_S8:
|
|
374 song->write = s32tos8;
|
|
375 break;
|
|
376 case AUDIO_U8:
|
|
377 song->write = s32tou8;
|
|
378 break;
|
|
379 case AUDIO_S16LSB:
|
|
380 song->write = s32tos16l;
|
|
381 break;
|
|
382 case AUDIO_S16MSB:
|
|
383 song->write = s32tos16b;
|
|
384 break;
|
|
385 case AUDIO_U16LSB:
|
|
386 song->write = s32tou16l;
|
|
387 break;
|
|
388 default:
|
|
389 SNDDBG(("Unsupported audio format"));
|
|
390 song->write = s32tou16l;
|
|
391 break;
|
|
392 }
|
|
393
|
|
394 song->buffer_size = audio->samples;
|
|
395 song->resample_buffer = safe_malloc(audio->samples * sizeof(sample_t));
|
|
396 song->common_buffer = safe_malloc(audio->samples * 2 * sizeof(Sint32));
|
|
397
|
|
398 song->control_ratio = audio->freq / CONTROLS_PER_SECOND;
|
|
399 if (song->control_ratio < 1)
|
|
400 song->control_ratio = 1;
|
|
401 else if (song->control_ratio > MAX_CONTROL_RATIO)
|
|
402 song->control_ratio = MAX_CONTROL_RATIO;
|
|
403
|
|
404 song->lost_notes = 0;
|
|
405 song->cut_notes = 0;
|
|
406
|
|
407 song->events = read_midi_file(song, &events, &song->samples);
|
|
408
|
|
409 /* The RWops can safely be closed at this point, but let's make that the
|
|
410 * responsibility of the caller.
|
|
411 */
|
|
412
|
|
413 /* Make sure everything is okay */
|
|
414 if (!song->events) {
|
|
415 free(song);
|
|
416 return(NULL);
|
|
417 }
|
|
418
|
|
419 song->default_instrument = 0;
|
|
420 song->default_program = DEFAULT_PROGRAM;
|
|
421
|
|
422 if (*def_instr_name)
|
|
423 set_default_instrument(song, def_instr_name);
|
|
424
|
|
425 load_missing_instruments(song);
|
|
426
|
|
427 return(song);
|
|
428 }
|
|
429
|
|
430 void Timidity_FreeSong(MidiSong *song)
|
|
431 {
|
|
432 int i;
|
|
433
|
|
434 free_instruments(song);
|
|
435
|
|
436 for (i = 0; i < 128; i++)
|
|
437 {
|
|
438 if (song->tonebank[i])
|
|
439 free(song->tonebank[i]);
|
|
440 if (song->drumset[i])
|
|
441 free(song->drumset[i]);
|
|
442 }
|
|
443
|
|
444 free(song->common_buffer);
|
|
445 free(song->resample_buffer);
|
|
446 free(song->events);
|
|
447 free(song);
|
|
448 }
|
|
449
|
|
450 void Timidity_Exit(void)
|
|
451 {
|
|
452 int i;
|
|
453
|
|
454 for (i = 0; i < 128; i++)
|
|
455 {
|
|
456 if (master_tonebank[i])
|
|
457 {
|
|
458 free(master_tonebank[i]->tone);
|
|
459 free(master_tonebank[i]);
|
|
460 }
|
|
461 if (master_drumset[i])
|
|
462 {
|
|
463 free(master_drumset[i]->tone);
|
|
464 free(master_drumset[i]);
|
|
465 }
|
|
466 }
|
|
467
|
|
468 free_pathlist();
|
|
469 }
|