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 Perl Artistic License, available in COPYING.
8
9 instrum.c
10
11 Code to load and unload GUS-compatible instrument patches.
12
13 */
14
15 #if HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "SDL.h"
24
25 #include "timidity.h"
26 #include "options.h"
27 #include "common.h"
28 #include "instrum.h"
29 #include "resample.h"
30 #include "tables.h"
31
free_instrument(Instrument * ip)32 static void free_instrument(Instrument *ip)
33 {
34 Sample *sp;
35 int i;
36 if (!ip) return;
37 for (i=0; i<ip->samples; i++)
38 {
39 sp=&(ip->sample[i]);
40 free(sp->data);
41 }
42 free(ip->sample);
43 free(ip);
44 }
45
free_bank(MidiSong * song,int dr,int b)46 static void free_bank(MidiSong *song, int dr, int b)
47 {
48 int i;
49 ToneBank *bank=((dr) ? song->drumset[b] : song->tonebank[b]);
50 for (i=0; i<MAXBANK; i++)
51 if (bank->instrument[i])
52 {
53 /* Not that this could ever happen, of course */
54 if (bank->instrument[i] != MAGIC_LOAD_INSTRUMENT)
55 free_instrument(bank->instrument[i]);
56 bank->instrument[i]=0;
57 }
58 }
59
convert_envelope_rate(MidiSong * song,Uint8 rate)60 static Sint32 convert_envelope_rate(MidiSong *song, Uint8 rate)
61 {
62 Sint32 r;
63
64 r = 3 - ((rate >> 6) & 0x3);
65 r *= 3;
66 r = (Sint32) (rate & 0x3f) << r; /* 6.9 fixed point */
67
68 /* 15.15 fixed point. */
69 r = ((r * 44100) / song->rate) * song->control_ratio;
70
71 #ifdef FAST_DECAY
72 return r << 10;
73 #else
74 return r << 9;
75 #endif
76 }
77
convert_envelope_offset(Uint8 offset)78 static Sint32 convert_envelope_offset(Uint8 offset)
79 {
80 /* This is not too good... Can anyone tell me what these values mean?
81 Are they GUS-style "exponential" volumes? And what does that mean? */
82
83 /* 15.15 fixed point */
84 return offset << (7+15);
85 }
86
convert_tremolo_sweep(MidiSong * song,Uint8 sweep)87 static Sint32 convert_tremolo_sweep(MidiSong *song, Uint8 sweep)
88 {
89 if (!sweep)
90 return 0;
91
92 return
93 ((song->control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
94 (song->rate * sweep);
95 }
96
convert_vibrato_sweep(MidiSong * song,Uint8 sweep,Sint32 vib_control_ratio)97 static Sint32 convert_vibrato_sweep(MidiSong *song, Uint8 sweep,
98 Sint32 vib_control_ratio)
99 {
100 if (!sweep)
101 return 0;
102
103 return
104 (Sint32) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
105 / (double)(song->rate * sweep));
106
107 /* this was overflowing with seashore.pat
108
109 ((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
110 (song->rate * sweep); */
111 }
112
convert_tremolo_rate(MidiSong * song,Uint8 rate)113 static Sint32 convert_tremolo_rate(MidiSong *song, Uint8 rate)
114 {
115 return
116 ((SINE_CYCLE_LENGTH * song->control_ratio * rate) << RATE_SHIFT) /
117 (TREMOLO_RATE_TUNING * song->rate);
118 }
119
convert_vibrato_rate(MidiSong * song,Uint8 rate)120 static Sint32 convert_vibrato_rate(MidiSong *song, Uint8 rate)
121 {
122 /* Return a suitable vibrato_control_ratio value */
123 return
124 (VIBRATO_RATE_TUNING * song->rate) /
125 (rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
126 }
127
reverse_data(Sint16 * sp,Sint32 ls,Sint32 le)128 static void reverse_data(Sint16 *sp, Sint32 ls, Sint32 le)
129 {
130 Sint16 s, *ep=sp+le;
131 sp+=ls;
132 le-=ls;
133 le/=2;
134 while (le--)
135 {
136 s=*sp;
137 *sp++=*ep;
138 *ep--=s;
139 }
140 }
141
142 /*
143 If panning or note_to_use != -1, it will be used for all samples,
144 instead of the sample-specific values in the instrument file.
145
146 For note_to_use, any value <0 or >127 will be forced to 0.
147
148 For other parameters, 1 means yes, 0 means no, other values are
149 undefined.
150
151 TODO: do reverse loops right */
load_instrument(MidiSong * song,char * name,int percussion,int panning,int amp,int note_to_use,int strip_loop,int strip_envelope,int strip_tail)152 static Instrument *load_instrument(MidiSong *song, char *name, int percussion,
153 int panning, int amp, int note_to_use,
154 int strip_loop, int strip_envelope,
155 int strip_tail)
156 {
157 Instrument *ip;
158 Sample *sp;
159 SDL_RWops *rw;
160 char tmp[1024];
161 int i,j,noluck=0;
162 static char *patch_ext[] = PATCH_EXT_LIST;
163
164 if (!name) return 0;
165
166 /* Open patch file */
167 if ((rw=open_file(name)) == NULL)
168 {
169 noluck=1;
170 /* Try with various extensions */
171 for (i=0; patch_ext[i]; i++)
172 {
173 if (strlen(name)+strlen(patch_ext[i])<1024)
174 {
175 strcpy(tmp, name);
176 strcat(tmp, patch_ext[i]);
177 if ((rw=open_file(tmp)) != NULL)
178 {
179 noluck=0;
180 break;
181 }
182 }
183 }
184 }
185
186 if (noluck)
187 {
188 SNDDBG(("Instrument `%s' can't be found.\n", name));
189 return 0;
190 }
191
192 SNDDBG(("Loading instrument %s\n", tmp));
193
194 /* Read some headers and do cursory sanity checks. There are loads
195 of magic offsets. This could be rewritten... */
196
197 if ((239 != SDL_RWread(rw, tmp, 1, 239)) ||
198 (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
199 memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
200 differences are */
201 {
202 SNDDBG(("%s: not an instrument\n", name));
203 SDL_RWclose(rw);
204 return 0;
205 }
206
207 if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers,
208 0 means 1 */
209 {
210 SNDDBG(("Can't handle patches with %d instruments\n", tmp[82]));
211 SDL_RWclose(rw);
212 return 0;
213 }
214
215 if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
216 {
217 SNDDBG(("Can't handle instruments with %d layers\n", tmp[151]));
218 SDL_RWclose(rw);
219 return 0;
220 }
221
222 ip=safe_malloc(sizeof(Instrument));
223 ip->samples = tmp[198];
224 ip->sample = safe_malloc(sizeof(Sample) * ip->samples);
225 for (i=0; i<ip->samples; i++)
226 {
227
228 Uint8 fractions;
229 Sint32 tmplong;
230 Uint16 tmpshort;
231 Uint8 tmpchar;
232
233 #define READ_CHAR(thing) \
234 if (1 != SDL_RWread(rw, &tmpchar, 1, 1)) goto fail; \
235 thing = tmpchar;
236 #define READ_SHORT(thing) \
237 if (1 != SDL_RWread(rw, &tmpshort, 2, 1)) goto fail; \
238 thing = SDL_SwapLE16(tmpshort);
239 #define READ_LONG(thing) \
240 if (1 != SDL_RWread(rw, &tmplong, 4, 1)) goto fail; \
241 thing = SDL_SwapLE32(tmplong);
242
243 SDL_RWseek(rw, 7, RW_SEEK_CUR); /* Skip the wave name */
244
245 if (1 != SDL_RWread(rw, &fractions, 1, 1))
246 {
247 fail:
248 SNDDBG(("Error reading sample %d\n", i));
249 for (j=0; j<i; j++)
250 free(ip->sample[j].data);
251 free(ip->sample);
252 free(ip);
253 SDL_RWclose(rw);
254 return 0;
255 }
256
257 sp=&(ip->sample[i]);
258
259 READ_LONG(sp->data_length);
260 READ_LONG(sp->loop_start);
261 READ_LONG(sp->loop_end);
262 READ_SHORT(sp->sample_rate);
263 READ_LONG(sp->low_freq);
264 READ_LONG(sp->high_freq);
265 READ_LONG(sp->root_freq);
266 SDL_RWseek(rw, 2, RW_SEEK_CUR); /* Why have a "root frequency" and then
267 * "tuning"?? */
268
269 READ_CHAR(tmp[0]);
270
271 if (panning==-1)
272 sp->panning = (tmp[0] * 8 + 4) & 0x7f;
273 else
274 sp->panning=(Uint8)(panning & 0x7F);
275
276 /* envelope, tremolo, and vibrato */
277 if (18 != SDL_RWread(rw, tmp, 1, 18)) goto fail;
278
279 if (!tmp[13] || !tmp[14])
280 {
281 sp->tremolo_sweep_increment=
282 sp->tremolo_phase_increment=sp->tremolo_depth=0;
283 SNDDBG((" * no tremolo\n"));
284 }
285 else
286 {
287 sp->tremolo_sweep_increment=convert_tremolo_sweep(song, tmp[12]);
288 sp->tremolo_phase_increment=convert_tremolo_rate(song, tmp[13]);
289 sp->tremolo_depth=tmp[14];
290 SNDDBG((" * tremolo: sweep %d, phase %d, depth %d\n",
291 sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
292 sp->tremolo_depth));
293 }
294
295 if (!tmp[16] || !tmp[17])
296 {
297 sp->vibrato_sweep_increment=
298 sp->vibrato_control_ratio=sp->vibrato_depth=0;
299 SNDDBG((" * no vibrato\n"));
300 }
301 else
302 {
303 sp->vibrato_control_ratio=convert_vibrato_rate(song, tmp[16]);
304 sp->vibrato_sweep_increment=
305 convert_vibrato_sweep(song, tmp[15], sp->vibrato_control_ratio);
306 sp->vibrato_depth=tmp[17];
307 SNDDBG((" * vibrato: sweep %d, ctl %d, depth %d\n",
308 sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
309 sp->vibrato_depth));
310
311 }
312
313 READ_CHAR(sp->modes);
314
315 SDL_RWseek(rw, 40, RW_SEEK_CUR); /* skip the useless scale frequency, scale
316 factor (what's it mean?), and reserved
317 space */
318
319 /* Mark this as a fixed-pitch instrument if such a deed is desired. */
320 if (note_to_use!=-1)
321 sp->note_to_use=(Uint8)(note_to_use);
322 else
323 sp->note_to_use=0;
324
325 /* seashore.pat in the Midia patch set has no Sustain. I don't
326 understand why, and fixing it by adding the Sustain flag to
327 all looped patches probably breaks something else. We do it
328 anyway. */
329
330 if (sp->modes & MODES_LOOPING)
331 sp->modes |= MODES_SUSTAIN;
332
333 /* Strip any loops and envelopes we're permitted to */
334 if ((strip_loop==1) &&
335 (sp->modes & (MODES_SUSTAIN | MODES_LOOPING |
336 MODES_PINGPONG | MODES_REVERSE)))
337 {
338 SNDDBG((" - Removing loop and/or sustain\n"));
339 sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING |
340 MODES_PINGPONG | MODES_REVERSE);
341 }
342
343 if (strip_envelope==1)
344 {
345 if (sp->modes & MODES_ENVELOPE) {
346 SNDDBG((" - Removing envelope\n"));
347 }
348 sp->modes &= ~MODES_ENVELOPE;
349 }
350 else if (strip_envelope != 0)
351 {
352 /* Have to make a guess. */
353 if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
354 {
355 /* No loop? Then what's there to sustain? No envelope needed
356 either... */
357 sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
358 SNDDBG((" - No loop, removing sustain and envelope\n"));
359 }
360 else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100)
361 {
362 /* Envelope rates all maxed out? Envelope end at a high "offset"?
363 That's a weird envelope. Take it out. */
364 sp->modes &= ~MODES_ENVELOPE;
365 SNDDBG((" - Weirdness, removing envelope\n"));
366 }
367 else if (!(sp->modes & MODES_SUSTAIN))
368 {
369 /* No sustain? Then no envelope. I don't know if this is
370 justified, but patches without sustain usually don't need the
371 envelope either... at least the Gravis ones. They're mostly
372 drums. I think. */
373 sp->modes &= ~MODES_ENVELOPE;
374 SNDDBG((" - No sustain, removing envelope\n"));
375 }
376 }
377
378 for (j=0; j<6; j++)
379 {
380 sp->envelope_rate[j]=
381 convert_envelope_rate(song, tmp[j]);
382 sp->envelope_offset[j]=
383 convert_envelope_offset(tmp[6+j]);
384 }
385
386 /* Then read the sample data */
387 sp->data = (sample_t *) safe_malloc(sp->data_length+4);
388 if (1 != SDL_RWread(rw, sp->data, sp->data_length, 1))
389 goto fail;
390
391 if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
392 {
393 Sint32 k=sp->data_length;
394 Uint8 *cp=(Uint8 *)(sp->data);
395 Uint16 *tmp16,*new16;
396 sp->data_length *= 2;
397 sp->loop_start *= 2;
398 sp->loop_end *= 2;
399 tmp16 = new16 = (Uint16 *) safe_malloc(sp->data_length+4);
400 while (k--)
401 *tmp16++ = (Uint16)(*cp++) << 8;
402 free(sp->data);
403 sp->data = (sample_t *)new16;
404 }
405 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
406 else
407 /* convert to machine byte order */
408 {
409 Sint32 k=sp->data_length/2;
410 Sint16 *tmp16=(Sint16 *)sp->data,s;
411 while (k--)
412 {
413 s=SDL_SwapLE16(*tmp16);
414 *tmp16++=s;
415 }
416 }
417 #endif
418
419 if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
420 {
421 Sint32 k=sp->data_length/2;
422 Sint16 *tmp16=(Sint16 *)sp->data;
423 while (k--)
424 *tmp16++ ^= 0x8000;
425 }
426
427 /* Reverse reverse loops and pass them off as normal loops */
428 if (sp->modes & MODES_REVERSE)
429 {
430 Sint32 t;
431 /* The GUS apparently plays reverse loops by reversing the
432 whole sample. We do the same because the GUS does not SUCK. */
433
434 SNDDBG(("Reverse loop in %s\n", name));
435 reverse_data((Sint16 *)sp->data, 0, sp->data_length/2);
436
437 t=sp->loop_start;
438 sp->loop_start=sp->data_length - sp->loop_end;
439 sp->loop_end=sp->data_length - t;
440
441 sp->modes &= ~MODES_REVERSE;
442 sp->modes |= MODES_LOOPING; /* just in case */
443 }
444
445 #ifdef ADJUST_SAMPLE_VOLUMES
446 if (amp!=-1)
447 sp->volume=(float)((amp) / 100.0);
448 else
449 {
450 /* Try to determine a volume scaling factor for the sample.
451 This is a very crude adjustment, but things sound more
452 balanced with it. Still, this should be a runtime option. */
453 Sint32 k=sp->data_length/2;
454 Sint16 maxamp=0,a;
455 Sint16 *tmp16=(Sint16 *)sp->data;
456 while (k--)
457 {
458 a=*tmp16++;
459 if (a<0) a=-a;
460 if (a>maxamp)
461 maxamp=a;
462 }
463 sp->volume=(float)(32768.0 / maxamp);
464 SNDDBG((" * volume comp: %f\n", sp->volume));
465 }
466 #else
467 if (amp!=-1)
468 sp->volume=(double)(amp) / 100.0;
469 else
470 sp->volume=1.0;
471 #endif
472
473 sp->data_length /= 2; /* These are in bytes. Convert into samples. */
474 sp->loop_start /= 2;
475 sp->loop_end /= 2;
476
477 /* initialize the added extra sample space (see the +4 bytes in
478 allocation) using the last actual sample: */
479 sp->data[sp->data_length] = sp->data[sp->data_length+1] = 0;
480
481 /* Then fractional samples */
482 sp->data_length <<= FRACTION_BITS;
483 sp->loop_start <<= FRACTION_BITS;
484 sp->loop_end <<= FRACTION_BITS;
485
486 /* Adjust for fractional loop points. This is a guess. Does anyone
487 know what "fractions" really stands for? */
488 sp->loop_start |=
489 (fractions & 0x0F) << (FRACTION_BITS-4);
490 sp->loop_end |=
491 ((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
492
493 /* If this instrument will always be played on the same note,
494 and it's not looped, we can resample it now. */
495 if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
496 pre_resample(song, sp);
497
498 if (strip_tail==1)
499 {
500 /* Let's not really, just say we did. */
501 SNDDBG((" - Stripping tail\n"));
502 sp->data_length = sp->loop_end;
503 }
504 }
505
506 SDL_RWclose(rw);
507 return ip;
508 }
509
fill_bank(MidiSong * song,int dr,int b)510 static int fill_bank(MidiSong *song, int dr, int b)
511 {
512 int i, errors=0;
513 ToneBank *bank=((dr) ? song->drumset[b] : song->tonebank[b]);
514 if (!bank)
515 {
516 SNDDBG(("Huh. Tried to load instruments in non-existent %s %d\n",
517 (dr) ? "drumset" : "tone bank", b));
518 return 0;
519 }
520 for (i=0; i<MAXBANK; i++)
521 {
522 if (bank->instrument[i]==MAGIC_LOAD_INSTRUMENT)
523 {
524 if (!(bank->tone[i].name))
525 {
526 SNDDBG(("No instrument mapped to %s %d, program %d%s\n",
527 (dr)? "drum set" : "tone bank", b, i,
528 (b!=0) ? "" : " - this instrument will not be heard"));
529 if (b!=0)
530 {
531 /* Mark the corresponding instrument in the default
532 bank / drumset for loading (if it isn't already) */
533 if (!dr)
534 {
535 if (!(song->tonebank[0]->instrument[i]))
536 song->tonebank[0]->instrument[i] =
537 MAGIC_LOAD_INSTRUMENT;
538 }
539 else
540 {
541 if (!(song->drumset[0]->instrument[i]))
542 song->drumset[0]->instrument[i] =
543 MAGIC_LOAD_INSTRUMENT;
544 }
545 }
546 bank->instrument[i] = 0;
547 errors++;
548 }
549 else if (!(bank->instrument[i] =
550 load_instrument(song,
551 bank->tone[i].name,
552 (dr) ? 1 : 0,
553 bank->tone[i].pan,
554 bank->tone[i].amp,
555 (bank->tone[i].note!=-1) ?
556 bank->tone[i].note :
557 ((dr) ? i : -1),
558 (bank->tone[i].strip_loop!=-1) ?
559 bank->tone[i].strip_loop :
560 ((dr) ? 1 : -1),
561 (bank->tone[i].strip_envelope != -1) ?
562 bank->tone[i].strip_envelope :
563 ((dr) ? 1 : -1),
564 bank->tone[i].strip_tail )))
565 {
566 SNDDBG(("Couldn't load instrument %s (%s %d, program %d)\n",
567 bank->tone[i].name,
568 (dr)? "drum set" : "tone bank", b, i));
569 errors++;
570 }
571 }
572 }
573 return errors;
574 }
575
load_missing_instruments(MidiSong * song)576 int load_missing_instruments(MidiSong *song)
577 {
578 int i=MAXBANK,errors=0;
579 while (i--)
580 {
581 if (song->tonebank[i])
582 errors+=fill_bank(song,0,i);
583 if (song->drumset[i])
584 errors+=fill_bank(song,1,i);
585 }
586 return errors;
587 }
588
free_instruments(MidiSong * song)589 void free_instruments(MidiSong *song)
590 {
591 int i=MAXBANK;
592 while(i--)
593 {
594 if (song->tonebank[i])
595 free_bank(song, 0, i);
596 if (song->drumset[i])
597 free_bank(song, 1, i);
598 }
599 }
600
set_default_instrument(MidiSong * song,char * name)601 int set_default_instrument(MidiSong *song, char *name)
602 {
603 Instrument *ip;
604 if (!(ip=load_instrument(song, name, 0, -1, -1, -1, 0, 0, 0)))
605 return -1;
606 song->default_instrument = ip;
607 song->default_program = SPECIAL_PROGRAM;
608 return 0;
609 }
610