1 /*
2 TiMidity -- Experimental MIDI to WAVE converter
3 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 instrum.c
20
21 Code to load and unload GUS-compatible instrument patches.
22 */
23
24 #include "config.h"
25
26 #include "common.h"
27 #include "instrum.h"
28 #include "playmidi.h"
29 #include "mix.h"
30 #include "ctrlmode.h"
31 #include "resample.h"
32 #include "tables.h"
33
34 /* Some functions get aggravated if not even the standard banks are
35 available. */
36 static ToneBank standard_tonebank, standard_drumset;
37
38 ToneBank *tonebank[MAXBANK]={&standard_tonebank};
39 ToneBank *drumset[MAXBANK]={&standard_drumset};
40
41 /* This is a special instrument, used for all melodic programs */
42 InstrumentLayer *default_instrument=0;
43
44 /* This is only used for tracks that don't specify a program */
45 int default_program=DEFAULT_PROGRAM;
46
47 #ifdef FAST_DECAY
48 int fast_decay=1;
49 #else
50 int fast_decay=0;
51 #endif
52
53
54 int current_tune_number = 0;
55 int last_tune_purged = 0;
56 int current_patch_memory = 0;
57
free_instrument(Instrument * ip)58 static void free_instrument(Instrument *ip)
59 {
60 Sample *sp;
61 int i;
62 if (!ip) return;
63
64 if (!ip->contents)
65 for (i=0; i<ip->samples; i++)
66 {
67 sp=&(ip->sample[i]);
68 if (sp->data) free(sp->data);
69 }
70 free(ip->sample);
71
72 if (!ip->contents)
73 for (i=0; i<ip->right_samples; i++)
74 {
75 sp=&(ip->right_sample[i]);
76 if (sp->data) free(sp->data);
77 }
78 if (ip->right_sample)
79 free(ip->right_sample);
80 free(ip);
81 }
82
83
free_layer(InstrumentLayer * lp)84 static void free_layer(InstrumentLayer *lp)
85 {
86 InstrumentLayer *next;
87
88 current_patch_memory -= lp->size;
89
90 for (; lp; lp = next)
91 {
92 next = lp->next;
93 free_instrument(lp->instrument);
94 free(lp);
95 }
96 }
97
free_bank(int dr,int b)98 static void free_bank(int dr, int b)
99 {
100 ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
101
102 for (int i=0; i<MAXPROG; i++)
103 {
104 if (bank->tone[i].layer)
105 {
106 /* Not that this could ever happen, of course */
107 if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
108 {
109 if (! bank->tone[i].layer_copy)
110 free_layer(bank->tone[i].layer);
111 bank->tone[i].layer = NULL;
112 bank->tone[i].last_used=-1;
113 }
114 }
115 if (bank->tone[i].name)
116 {
117 free(bank->tone[i].name);
118 bank->tone[i].name = NULL;
119 }
120 }
121 }
122
123
convert_envelope_rate_attack(byte rate,byte fastness)124 int convert_envelope_rate_attack(byte rate, byte fastness)
125 {
126 int r = 3 - ((rate>>6) & 0x3);
127
128 r = (int)(rate & 0x3f) << (r * 3); /* 6.9 fixed point */
129
130 /* 15.15 fixed point. */
131 return (((r * 44100) / play_mode_rate) * control_ratio) << 10;
132 }
133
convert_envelope_rate(byte rate)134 int convert_envelope_rate(byte rate)
135 {
136 int r = 3 - ((rate>>6) & 0x3);
137
138 r = (int)(rate & 0x3f) << (r * 3); /* 6.9 fixed point */
139
140 /* 15.15 fixed point. */
141 return (((r * 44100) / play_mode_rate) * control_ratio) << ((fast_decay) ? 10 : 9);
142 }
143
convert_envelope_offset(byte offset)144 int convert_envelope_offset(byte offset)
145 {
146 /* This is not too good... Can anyone tell me what these values mean?
147 Are they GUS-style "exponential" volumes? And what does that mean? */
148
149 /* 15.15 fixed point */
150 return (int)offset << (7+15);
151 }
152
convert_tremolo_sweep(byte sweep)153 int convert_tremolo_sweep(byte sweep)
154 {
155 if (!sweep)
156 return 0;
157
158 return
159 ((control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
160 (play_mode_rate * sweep);
161 }
162
convert_vibrato_sweep(byte sweep,int vib_control_ratio)163 int convert_vibrato_sweep(byte sweep, int vib_control_ratio)
164 {
165 if (!sweep)
166 return 0;
167
168 return
169 (int) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
170 / (double)(play_mode_rate * sweep));
171
172 /* this was overflowing with seashore.pat
173
174 ((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
175 (play_mode_rate * sweep); */
176 }
177
convert_tremolo_rate(byte rate)178 int convert_tremolo_rate(byte rate)
179 {
180 return
181 ((SINE_CYCLE_LENGTH * control_ratio * rate) << RATE_SHIFT) /
182 (TREMOLO_RATE_TUNING * play_mode_rate);
183 }
184
convert_vibrato_rate(byte rate)185 int convert_vibrato_rate(byte rate)
186 {
187 /* Return a suitable vibrato_control_ratio value */
188 return
189 (VIBRATO_RATE_TUNING * play_mode_rate) /
190 (rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
191 }
192
reverse_data(s16_t * sp,int ls,int le)193 static void reverse_data(s16_t *sp, int ls, int le)
194 {
195 s16_t s, *ep=sp+le;
196 sp+=ls;
197 le-=ls;
198 le/=2;
199 while (le--)
200 {
201 s=*sp;
202 *sp++=*ep;
203 *ep--=s;
204 }
205 }
206
207 /*
208 If panning or note_to_use != -1, it will be used for all samples,
209 instead of the sample-specific values in the instrument file.
210
211 For note_to_use, any value <0 or >127 will be forced to 0.
212
213 For other parameters, 1 means yes, 0 means no, other values are
214 undefined.
215
216 TODO: do reverse loops right */
load_instrument(char * name,int font_type,int percussion,int panning,int amp,int cfg_tuning,int note_to_use,int strip_loop,int strip_envelope,int strip_tail,int bank,int gm_num,int sf_ix)217 static InstrumentLayer *load_instrument(char *name, int font_type, int percussion,
218 int panning, int amp, int cfg_tuning, int note_to_use,
219 int strip_loop, int strip_envelope,
220 int strip_tail, int bank, int gm_num, int sf_ix)
221 {
222 InstrumentLayer *lp, *lastlp;
223 InstrumentLayer *headlp = NULL;
224 Instrument *ip;
225
226 byte tmp[1024];
227 int i,j,noluck=0;
228
229 #ifdef PATCH_EXT_LIST
230 static const char *patch_ext[] = PATCH_EXT_LIST;
231 #endif
232 int sf2flag = 0;
233 int right_samples = 0;
234 int stereo_channels = 1, stereo_layer;
235 int vlayer_list[19][4], vlayer;
236 int vlayer_count = 0;
237
238 if (!name) return 0;
239
240 /* Open patch file */
241 FILE *fp = open_via_paths_NOCASE(name, OF_NORMAL);
242
243 if (fp == NULL)
244 {
245 noluck=1;
246
247 #ifdef PATCH_EXT_LIST
248 /* Try with various extensions */
249 for (i=0; patch_ext[i]; i++)
250 {
251 if (strlen(name)+strlen(patch_ext[i])<1024)
252 {
253 char path[1024];
254 strcpy(path, name);
255 strcat(path, patch_ext[i]);
256
257 fp = open_via_paths_NOCASE(path, OF_NORMAL);
258
259 if (fp)
260 {
261 noluck=0;
262 break;
263 }
264 }
265 }
266 #endif
267 }
268
269 if (noluck)
270 {
271 ctl_msg(CMSG_ERROR, VERB_NORMAL,
272 "Instrument `%s' can't be found.", name);
273 return 0;
274 }
275
276 ctl_msg(CMSG_INFO, VERB_DEBUG, "Loading instrument %s", name);
277
278 /* Read some headers and do cursory sanity checks. There are loads
279 of magic offsets. This could be rewritten... */
280
281 if ((239 != fread(tmp, 1, 239, fp)) ||
282 (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
283 memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
284 differences are */
285 {
286 ctl_msg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
287 return 0;
288 }
289
290 /* patch layout:
291 * bytes: info: starts at offset:
292 * 22 id (see above) 0
293 * 60 copyright 22
294 * 1 instruments 82
295 * 1 voices 83
296 * 1 channels 84
297 * 2 number of waveforms 85
298 * 2 master volume 87
299 * 4 datasize 89
300 * 36 reserved, but now: 93
301 * 7 "SF2EXT\0" id 93
302 * 1 right samples 100
303 * 28 reserved 101
304 * 2 instrument number 129
305 * 16 instrument name 131
306 * 4 instrument size 147
307 * 1 number of layers 151
308 * 40 reserved 152
309 * 1 layer duplicate 192
310 * 1 layer number 193
311 * 4 layer size 194
312 * 1 number of samples 198
313 * 40 reserved 199
314 * 239
315 * THEN, for each sample, see below
316 */
317
318 if (0 == memcmp(tmp + 93, "SF2EXT", 6))
319 {
320 sf2flag = 1;
321 vlayer_count = tmp[152];
322 }
323
324 if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers,
325 0 means 1 */
326 {
327 ctl_msg(CMSG_ERROR, VERB_NORMAL,
328 "Can't handle patches with %d instruments", tmp[82]);
329 return 0;
330 }
331
332 if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
333 {
334 ctl_msg(CMSG_ERROR, VERB_NORMAL,
335 "Can't handle instruments with %d layers", tmp[151]);
336 return 0;
337 }
338
339
340 if (sf2flag && vlayer_count > 0)
341 {
342 for (i = 0; i < 9; i++)
343 for (j = 0; j < 4; j++)
344 vlayer_list[i][j] = tmp[153+i*4+j];
345 for (i = 9; i < 19; i++)
346 for (j = 0; j < 4; j++)
347 vlayer_list[i][j] = tmp[199+(i-9)*4+j];
348 }
349 else
350 {
351 for (i = 0; i < 19; i++)
352 for (j = 0; j < 4; j++)
353 vlayer_list[i][j] = 0;
354 vlayer_list[0][0] = 0;
355 vlayer_list[0][1] = 127;
356 vlayer_list[0][2] = tmp[198];
357 vlayer_list[0][3] = 0;
358 vlayer_count = 1;
359 }
360
361 lastlp = 0;
362
363 for (vlayer = 0; vlayer < vlayer_count; vlayer++)
364 {
365
366 lp=(InstrumentLayer *)safe_malloc(sizeof(InstrumentLayer));
367 lp->size = sizeof(InstrumentLayer);
368 lp->lo = vlayer_list[vlayer][0];
369 lp->hi = vlayer_list[vlayer][1];
370 ip=(Instrument *)safe_malloc(sizeof(Instrument));
371 lp->size += sizeof(Instrument);
372 lp->instrument = ip;
373 lp->next = 0;
374
375 if (lastlp) lastlp->next = lp;
376 else headlp = lp;
377
378 lastlp = lp;
379
380 if (sf2flag) ip->type = INST_SF2;
381 else ip->type = INST_GUS;
382 ip->samples = vlayer_list[vlayer][2];
383 ip->sample = (Sample *)safe_malloc(sizeof(Sample) * ip->samples);
384 lp->size += sizeof(Sample) * ip->samples;
385 ip->left_samples = ip->samples;
386 ip->left_sample = ip->sample;
387 right_samples = vlayer_list[vlayer][3];
388 ip->right_samples = right_samples;
389 if (right_samples)
390 {
391 ip->right_sample = (Sample *)safe_malloc(sizeof(Sample) * right_samples);
392 lp->size += sizeof(Sample) * right_samples;
393 stereo_channels = 2;
394 }
395 else ip->right_sample = 0;
396 ip->contents = 0;
397
398 ctl_msg(CMSG_INFO, VERB_DEBUG_SILLY,
399 "%s%s[%d,%d] %s(%d-%d layer %d of %d)",
400 (percussion)? " ":"", name,
401 (percussion)? note_to_use : gm_num, bank,
402 (right_samples)? "(2) " : "",
403 lp->lo, lp->hi, vlayer+1, vlayer_count);
404
405 for (stereo_layer = 0; stereo_layer < stereo_channels; stereo_layer++)
406 {
407 int sample_count = ip->left_samples;
408
409 if (stereo_layer == 1)
410 sample_count = ip->right_samples;
411
412 for (i=0; i < sample_count; i++)
413 {
414 s32_t tmplong;
415 u16_t tmpshort;
416 u16_t sample_volume = 0;
417 byte tmpchar;
418
419 byte fractions;
420 byte sf2delay = 1;
421
422 #define READ_CHAR(thing) \
423 if (1 != fread(&tmpchar, 1, 1, fp)) goto fail; \
424 thing = tmpchar;
425 #define READ_SHORT(thing) \
426 if (1 != fread(&tmpshort, 2, 1, fp)) goto fail; \
427 thing = EPI_LE_U16(tmpshort);
428 #define READ_LONG(thing) \
429 if (1 != fread(&tmplong, 4, 1, fp)) goto fail; \
430 thing = EPI_LE_U32(tmplong);
431
432 /*
433 * 7 sample name
434 * 1 fractions
435 * 4 length
436 * 4 loop start
437 * 4 loop end
438 * 2 sample rate
439 * 4 low frequency
440 * 4 high frequency
441 * 2 finetune
442 * 1 panning
443 * 6 envelope rates |
444 * 6 envelope offsets | 18 bytes
445 * 3 tremolo sweep, rate, depth |
446 * 3 vibrato sweep, rate, depth |
447 * 1 sample mode
448 * 2 scale frequency
449 * 2 scale factor
450 * 2 sample volume (??)
451 * 34 reserved
452 * Now: 1 delay
453 * 33 reserved
454 */
455 skip(fp, 7); /* Skip the wave name */
456
457 if (1 != fread(&fractions, 1, 1, fp))
458 {
459 fail:
460 ctl_msg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
461
462 if (stereo_layer == 1)
463 {
464 for (j=0; j<i; j++)
465 free(ip->right_sample[j].data);
466 free(ip->right_sample);
467 i = ip->left_samples;
468 }
469 for (j=0; j<i; j++)
470 free(ip->left_sample[j].data);
471 free(ip->left_sample);
472 free(ip);
473 free(lp);
474 return 0;
475 }
476
477 Sample *sp;
478
479 if (stereo_layer == 0)
480 sp=&(ip->left_sample[i]);
481 else if (stereo_layer == 1)
482 sp=&(ip->right_sample[i]);
483 else
484 continue; // -AJA- added
485
486 READ_LONG(sp->data_length);
487 READ_LONG(sp->loop_start);
488 READ_LONG(sp->loop_end);
489 READ_SHORT(sp->sample_rate);
490 READ_LONG(sp->low_freq);
491 READ_LONG(sp->high_freq);
492 READ_LONG(sp->root_freq);
493 skip(fp, 2); /* Why have a "root frequency" and then "tuning"?? */
494
495 READ_CHAR(tmp[0]);
496
497 if (panning==-1)
498 sp->panning = (tmp[0] * 8 + 4) & 0x7f;
499 else
500 sp->panning=(byte)(panning & 0x7F);
501
502 sp->resonance=0;
503 sp->cutoff_freq=0;
504 sp->reverberation=0;
505 sp->chorusdepth=0;
506 sp->exclusiveClass=0;
507 sp->keyToModEnvHold=0;
508 sp->keyToModEnvDecay=0;
509 sp->keyToVolEnvHold=0;
510 sp->keyToVolEnvDecay=0;
511
512 if (cfg_tuning)
513 {
514 double tune_factor = (double)(cfg_tuning)/1200.0;
515 tune_factor = pow(2.0, tune_factor);
516 sp->root_freq = (u32_t)( tune_factor * (double)sp->root_freq );
517 }
518
519 /* envelope, tremolo, and vibrato */
520 if (18 != fread(tmp, 1, 18, fp)) goto fail;
521
522 if (!tmp[13] || !tmp[14])
523 {
524 sp->tremolo_sweep_increment=
525 sp->tremolo_phase_increment=sp->tremolo_depth=0;
526 ctl_msg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
527 }
528 else
529 {
530 sp->tremolo_sweep_increment=convert_tremolo_sweep(tmp[12]);
531 sp->tremolo_phase_increment=convert_tremolo_rate(tmp[13]);
532 sp->tremolo_depth=tmp[14];
533
534 ctl_msg(CMSG_INFO, VERB_DEBUG,
535 " * tremolo: sweep %d, phase %d, depth %d",
536 sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
537 sp->tremolo_depth);
538 }
539
540 if (!tmp[16] || !tmp[17])
541 {
542 sp->vibrato_sweep_increment=
543 sp->vibrato_control_ratio=sp->vibrato_depth=0;
544 ctl_msg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
545 }
546 else
547 {
548 sp->vibrato_control_ratio=convert_vibrato_rate(tmp[16]);
549 sp->vibrato_sweep_increment=
550 convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio);
551 sp->vibrato_depth=tmp[17];
552
553 ctl_msg(CMSG_INFO, VERB_DEBUG,
554 " * vibrato: sweep %d, ctl %d, depth %d",
555 sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
556 sp->vibrato_depth);
557
558 }
559
560 READ_CHAR(sp->modes);
561 READ_SHORT(sp->freq_center);
562 READ_SHORT(sp->freq_scale);
563
564 if (sf2flag)
565 {
566 READ_SHORT(sample_volume);
567 READ_CHAR(sf2delay);
568 READ_CHAR(sp->exclusiveClass);
569 skip(fp, 32);
570 }
571 else
572 {
573 skip(fp, 36);
574 }
575
576 /* Mark this as a fixed-pitch instrument if such a deed is desired. */
577 if (note_to_use!=-1)
578 sp->note_to_use=(byte)(note_to_use);
579 else
580 sp->note_to_use=0;
581
582 /* seashore.pat in the Midia patch set has no Sustain. I don't
583 understand why, and fixing it by adding the Sustain flag to
584 all looped patches probably breaks something else. We do it
585 anyway. */
586
587 if (sp->modes & MODES_LOOPING)
588 sp->modes |= MODES_SUSTAIN;
589
590 /* Strip any loops and envelopes we're permitted to */
591 if ((strip_loop==1) &&
592 (sp->modes & (MODES_SUSTAIN | MODES_LOOPING |
593 MODES_PINGPONG | MODES_REVERSE)))
594 {
595 ctl_msg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain");
596
597 sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING |
598 MODES_PINGPONG | MODES_REVERSE);
599 }
600
601 if (strip_envelope==1)
602 {
603 if (sp->modes & MODES_ENVELOPE)
604 {
605 ctl_msg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
606
607 sp->modes &= ~MODES_ENVELOPE;
608 }
609 }
610 else if (strip_envelope != 0)
611 {
612 /* Have to make a guess. */
613 if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
614 {
615 /* No loop? Then what's there to sustain? No envelope needed
616 either... */
617 sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
618
619 ctl_msg(CMSG_INFO, VERB_DEBUG,
620 " - No loop, removing sustain and envelope");
621 }
622 else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100)
623 {
624 /* Envelope rates all maxed out? Envelope end at a high "offset"?
625 That's a weird envelope. Take it out. */
626 sp->modes &= ~MODES_ENVELOPE;
627
628 ctl_msg(CMSG_INFO, VERB_DEBUG,
629 " - Weirdness, removing envelope");
630 }
631 else if (!(sp->modes & MODES_SUSTAIN))
632 {
633 /* No sustain? Then no envelope. I don't know if this is
634 justified, but patches without sustain usually don't need the
635 envelope either... at least the Gravis ones. They're mostly
636 drums. I think. */
637 sp->modes &= ~MODES_ENVELOPE;
638
639 ctl_msg(CMSG_INFO, VERB_DEBUG,
640 " - No sustain, removing envelope");
641 }
642 }
643
644 sp->attenuation = 0;
645
646 for (j=ATTACK; j<DELAY; j++)
647 {
648 sp->envelope_rate[j]=
649 (j<3)? convert_envelope_rate_attack(tmp[j], 11) : convert_envelope_rate(tmp[j]);
650 sp->envelope_offset[j]=
651 convert_envelope_offset(tmp[6+j]);
652 }
653 if (sf2flag)
654 {
655 if (sf2delay > 5) sf2delay = 5;
656 sp->envelope_rate[DELAY] = (int)( (sf2delay*play_mode_rate) / 1000 );
657 }
658 else
659 {
660 sp->envelope_rate[DELAY]=0;
661 }
662 sp->envelope_offset[DELAY]=0;
663
664 for (j=ATTACK; j<DELAY; j++)
665 {
666 sp->modulation_rate[j]=sp->envelope_rate[j];
667 sp->modulation_offset[j]=sp->envelope_offset[j];
668 }
669 sp->modulation_rate[DELAY] = sp->modulation_offset[DELAY] = 0;
670 sp->modEnvToFilterFc=0;
671 sp->modEnvToPitch=0;
672 sp->lfo_sweep_increment = 0;
673 sp->lfo_phase_increment = 0;
674 sp->modLfoToFilterFc = 0;
675 sp->vibrato_delay = 0;
676
677 /* Then read the sample data */
678 if (sp->data_length/2 > MAX_SAMPLE_SIZE)
679 {
680 goto fail;
681 }
682 sp->data = (sample_t*)safe_malloc(sp->data_length + 1);
683 lp->size += sp->data_length + 1;
684
685 if (1 != fread(sp->data, sp->data_length, 1, fp))
686 goto fail;
687
688 if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
689 {
690 int i=sp->data_length;
691 byte *cp=(byte *)(sp->data);
692 u16_t *tmp,*newdta;
693 tmp = newdta = (u16_t*) safe_malloc(sp->data_length*2 + 2);
694 while (i--)
695 *tmp++ = (u16_t)(*cp++) << 8;
696 cp=(byte *)(sp->data);
697 sp->data = (sample_t *)newdta;
698 free(cp);
699 sp->data_length *= 2;
700 sp->loop_start *= 2;
701 sp->loop_end *= 2;
702 }
703 #if EPI_BYTEORDER == EPI_BIG_ENDIAN
704 else
705 /* convert to machine byte order */
706 {
707 int i=sp->data_length/2;
708 s16_t *tmp=(s16_t *)sp->data,s;
709 while (i--)
710 {
711 s=EPI_LE_U16(*tmp);
712 *tmp++=s;
713 }
714 }
715 #endif
716
717 if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
718 {
719 int i=sp->data_length/2;
720 s16_t *tmp=(s16_t *)sp->data;
721 while (i--)
722 *tmp++ ^= 0x8000;
723 }
724
725 /* Reverse reverse loops and pass them off as normal loops */
726 if (sp->modes & MODES_REVERSE)
727 {
728 /* The GUS apparently plays reverse loops by reversing the
729 whole sample. We do the same because the GUS does not SUCK. */
730
731 ctl_msg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
732
733 reverse_data((s16_t *)sp->data, 0, sp->data_length/2);
734
735 int t = sp->loop_start;
736 sp->loop_start=sp->data_length - sp->loop_end;
737 sp->loop_end=sp->data_length - t;
738
739 sp->modes &= ~MODES_REVERSE;
740 sp->modes |= MODES_LOOPING; /* just in case */
741 }
742
743 #ifdef ADJUST_SAMPLE_VOLUMES
744 if (amp!=-1)
745 sp->volume=(double)((amp) / 100.0);
746 else if (sf2flag)
747 sp->volume=(double)((sample_volume) / 255.0);
748 else
749 {
750 /* Try to determine a volume scaling factor for the sample.
751 This is a very crude adjustment, but things sound more
752 balanced with it. Still, this should be a runtime option. */
753 u32_t i, numsamps=sp->data_length/2;
754 u32_t higher=0, highcount=0;
755 s16_t maxamp=0,a;
756 s16_t *tmp=(s16_t *)sp->data;
757 i = numsamps;
758 while (i--)
759 {
760 a=*tmp++;
761 if (a<0) a=-a;
762 if (a>maxamp)
763 maxamp=a;
764 }
765 tmp=(s16_t *)sp->data;
766 i = numsamps;
767 while (i--)
768 {
769 a=*tmp++;
770 if (a<0) a=-a;
771 if (a > 3*maxamp/4)
772 {
773 higher += a;
774 highcount++;
775 }
776 }
777 if (highcount)
778 higher /= highcount;
779 else
780 higher = 10000;
781
782 sp->volume = (32768.0 * 0.875) / (double)higher ;
783
784 ctl_msg(CMSG_INFO, VERB_DEBUG, " * volume comp: %f", sp->volume);
785 }
786 #else
787 if (amp!=-1)
788 sp->volume=(double)(amp) / 100.0;
789 else
790 sp->volume=1.0;
791 #endif
792
793 sp->data_length /= 2; /* These are in bytes. Convert into samples. */
794
795 sp->loop_start /= 2;
796 sp->loop_end /= 2;
797 sp->data[sp->data_length] = sp->data[sp->data_length-1];
798
799 /* Then fractional samples */
800 sp->data_length <<= FRACTION_BITS;
801 sp->loop_start <<= FRACTION_BITS;
802 sp->loop_end <<= FRACTION_BITS;
803
804 /* trim off zero data at end */
805 {
806 int ls = sp->loop_start>>FRACTION_BITS;
807 int le = sp->loop_end>>FRACTION_BITS;
808 int se = sp->data_length>>FRACTION_BITS;
809 while (se > 1 && !sp->data[se-1]) se--;
810 if (le > se) le = se;
811 if (ls >= le) sp->modes &= ~MODES_LOOPING;
812 sp->loop_end = le<<FRACTION_BITS;
813 sp->data_length = se<<FRACTION_BITS;
814 }
815
816 /* Adjust for fractional loop points. This is a guess. Does anyone
817 know what "fractions" really stands for? */
818 sp->loop_start |=
819 (fractions & 0x0F) << (FRACTION_BITS-4);
820 sp->loop_end |=
821 ((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
822
823 /* If this instrument will always be played on the same note,
824 and it's not looped, we can resample it now. */
825 if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
826 pre_resample(sp);
827
828 if (strip_tail==1)
829 {
830 /* Let's not really, just say we did. */
831 ctl_msg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
832 sp->data_length = sp->loop_end;
833 }
834 } /* end of sample loop */
835 } /* end of stereo layer loop */
836 } /* end of vlayer loop */
837
838
839 fclose(fp);
840 return headlp;
841 }
842
843 /* AJA: new code */
844 static const int aja_drum_remap[47][4] =
845 {
846 { 36, 40, 38, 0 }, // Acoustic Bass Drum
847 { 35, 38, 40, 0 }, // Bass Drum
848 { 75, 77, 0, 0 }, // Slide Stick
849 { 40, 35, 36, 0 }, // Acoustic Snare
850 { 58, 70, 0, 0 }, // Hand Clap
851 { 38, 36, 35, 0 }, // Electric Snare
852 { 45, 47, 0, 0 }, // Low Floor Tom
853 { 44, 46, 0, 0 }, // Closed High-Hat
854 { 50, 48, 0, 0 }, // High Floor Tom
855 { 42, 46, 0, 0 }, // Pedal High Hat
856 { 47, 41, 0, 0 }, // Low Tom
857 { 44, 42, 0, 0 }, // Open High Hat
858 { 45, 41, 0, 0 }, // Low-Mid Tom
859 { 50, 43, 0, 0 }, // High-Mid Tom
860 { 57, 51, 0, 0 }, // Crash Cymbal 1
861 { 48, 43, 0, 0 }, // High Tom
862 { 59, 0, 0, 0 }, // Ride Cymbal 1
863 { 55, 0, 0, 0 }, // Chinses Cymbal
864 { 56, 0, 0, 0 }, // Ride Bell
865 { 69, 0, 0, 0 }, // Tambourine
866 { 52, 0, 0, 0 }, // Splash Cymbal
867 { 53, 0, 0, 0 }, // Cowbell
868 { 49, 59, 0, 0 }, // Crash Cymbal 2
869 { 39, 0, 0, 0 }, // Vibraslap
870 { 51, 0, 0, 0 }, // Ride Cymbal 2
871 { 61, 0, 0, 0 }, // High Bongo
872 { 60, 0, 0, 0 }, // Low Bongo
873 { 63, 64, 0, 0 }, // Mute High Conga
874 { 62, 64, 0, 0 }, // Open High Conga
875 { 63, 62, 0, 0 }, // Low Conga
876 { 66, 67, 0, 0 }, // High Timbale
877 { 65, 68, 0, 0 }, // Low Timbale
878 { 68, 65, 0, 0 }, // High Agogo
879 { 67, 66, 0, 0 }, // Low Agogo
880 { 70, 0, 0, 0 }, // Cabasa
881 { 69, 0, 0, 0 }, // Maracas
882 { 72, 73, 0, 0 }, // Short Whistle
883 { 71, 74, 0, 0 }, // Long Whistle
884 { 74, 71, 0, 0 }, // Short Guiro
885 { 73, 72, 0, 0 }, // Long Guiro
886 { 37, 76, 0, 0 }, // Claves
887 { 77, 75, 0, 0 }, // High Wood Block
888 { 76, 75, 0, 0 }, // Low Wood Block
889 { 79, 80, 0, 0 }, // Mute Cuica
890 { 78, 81, 0, 0 }, // Open Cuica
891 { 81, 78, 0, 0 }, // Mute Triangle
892 { 80, 79, 0, 0 } // Open Triangle
893 };
894
aja_fallback_drum(int i,int dist)895 int aja_fallback_drum(int i, int dist)
896 {
897 SYS_ASSERT(dist >= 1);
898
899 if (dist > 3)
900 return -1;
901
902 for (int k = 0; k < 47; k++)
903 {
904 const int *map = &aja_drum_remap[k][0];
905
906 if (map[0] == i && map[dist] != 0)
907 return map[dist];
908 }
909
910 return -1;
911 }
912
913
aja_fallback_program(int i,int dist)914 int aja_fallback_program(int i, int dist)
915 {
916 SYS_ASSERT(dist >= 1);
917
918 if (dist > 7)
919 return -1;
920
921 int base = i & ~7;
922
923 // don't remap SYNTH / SOUND EFFECTS / PERCUSSIVE
924 if (base == 96 || base == 120 || base == 112)
925 return -1;
926
927 // try to use a lower instrument number (on the assumption
928 // that it is more "normal" than a higher one).
929
930 if (i - dist >= base)
931 return i - dist;
932
933 dist -= (i - base);
934
935 return i + dist;
936 }
937
938
try_load_instr(ToneBank * bank,int b,int dr,int i)939 static bool try_load_instr(ToneBank *bank, int b, int dr, int i)
940 {
941 SYS_ASSERT(i >= 0);
942
943 if (! bank->tone[i].name)
944 return false;
945
946 bank->tone[i].layer_copy = 0;
947 bank->tone[i].layer = load_instrument(bank->tone[i].name,
948 bank->tone[i].font_type, (dr) ? 1 : 0,
949 bank->tone[i].pan, bank->tone[i].amp, bank->tone[i].tuning,
950 (bank->tone[i].note!=-1) ? bank->tone[i].note : ((dr) ? i : -1),
951 (bank->tone[i].strip_loop!=-1) ? bank->tone[i].strip_loop : ((dr) ? 1 : -1),
952 (bank->tone[i].strip_envelope != -1) ? bank->tone[i].strip_envelope : ((dr) ? 1 : -1),
953 bank->tone[i].strip_tail, b /* bank */,
954 ((dr) ? i + 128 : i), bank->tone[i].sf_ix);
955
956 if (bank->tone[i].layer)
957 {
958 bank->tone[i].last_used = current_tune_number;
959 current_patch_memory += bank->tone[i].layer->size;
960
961 return true;
962 }
963
964 ctl_msg(CMSG_ERROR, VERB_NORMAL,
965 "Couldn't load instrument %s (%s %d, program %d)",
966 bank->tone[i].name,
967 (dr)? "drum set" : "tone bank", b, i);
968
969 return false;
970 }
971
972
fill_bank(int dr,int b)973 static int fill_bank(int dr, int b)
974 {
975 int errors=0;
976
977 ToneBank *bank = (dr) ? drumset[b] : tonebank[b];
978
979 if (!bank)
980 {
981 ctl_msg(CMSG_ERROR, VERB_NORMAL,
982 "Huh. Tried to load instruments in non-existent %s %d",
983 (dr) ? "drumset" : "tone bank", b);
984 return 0;
985 }
986
987 /* Primary Pass : try to load instruments normally */
988
989 for (int i=0; i < MAXPROG; i++)
990 {
991 if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
992 continue;
993
994 if (try_load_instr(bank, b, dr, i))
995 {
996 // OK!!
997 continue;
998 }
999
1000 errors++;
1001
1002 if (b == 0)
1003 {
1004 // standard bank : try again in second pass
1005 bank->tone[i].layer = MAGIC_LOAD_INSTRUMENT;
1006 continue;
1007 }
1008
1009 // force usage of the instrument in bank #0
1010 bank->tone[i].layer = NULL;
1011
1012 // mark the instrument in bank #0 to be loaded
1013 ToneBank *bank_zero = (dr) ? drumset[0] : tonebank[0];
1014
1015 if (! bank_zero->tone[i].layer)
1016 bank_zero->tone[i].layer = MAGIC_LOAD_INSTRUMENT;
1017 }
1018
1019 if (b != 0)
1020 return errors;
1021
1022 /* Secondary Pass for Bank #0 : fallback remaps */
1023
1024 int passes = dr ? 4 : 8;
1025
1026 for (int dist = 1; dist <= passes; dist++)
1027 {
1028 for (int i=0; i < MAXPROG; i++)
1029 {
1030 if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
1031 continue;
1032
1033 int new_i;
1034 if (dr)
1035 new_i = aja_fallback_drum(i, dist);
1036 else
1037 new_i = aja_fallback_program(i, dist);
1038
1039 if (new_i < 0)
1040 continue;
1041
1042 if (! bank->tone[new_i].name)
1043 continue;
1044
1045 if (! bank->tone[new_i].layer)
1046 {
1047 try_load_instr(bank, b, dr, new_i);
1048 }
1049
1050 if (bank->tone[new_i].layer &&
1051 bank->tone[new_i].layer != MAGIC_LOAD_INSTRUMENT &&
1052 ! bank->tone[new_i].layer_copy)
1053 {
1054 bank->tone[i].layer = bank->tone[new_i].layer;
1055 bank->tone[i].layer_copy = 1;
1056
1057 ctl_msg(CMSG_WARNING, VERB_NORMAL,
1058 "No instrument mapped to %s %d, program %d"
1059 " - remapped to program %d",
1060 (dr)? "drum set" : "tone bank", b, i, new_i);
1061 continue;
1062 }
1063
1064 } // instrument
1065 } // dist
1066
1067 /* Final Pass : find the failures */
1068
1069 for (int i=0; i < MAXPROG; i++)
1070 {
1071 if (bank->tone[i].layer == MAGIC_LOAD_INSTRUMENT)
1072 {
1073 bank->tone[i].layer = NULL;
1074
1075 ctl_msg(CMSG_WARNING, VERB_NORMAL,
1076 "No instrument mapped to %s %d, program %d%s",
1077 (dr)? "drum set" : "tone bank", b, i,
1078 (b != 0) ? "" : " - this instrument will not be heard");
1079
1080 errors++;
1081 }
1082 }
1083
1084 return errors;
1085 }
1086
1087
load_missing_instruments(void)1088 int load_missing_instruments(void)
1089 {
1090 int errors=0;
1091
1092 for (int b=MAXBANK-1; b >= 0; b--)
1093 {
1094 if (tonebank[b])
1095 errors += fill_bank(0,b);
1096
1097 if (drumset[b])
1098 errors += fill_bank(1,b);
1099 }
1100 current_tune_number++;
1101
1102 return errors;
1103 }
1104
free_instruments(void)1105 void free_instruments(void)
1106 {
1107 for (int b=MAXBANK-1; b >= 0; b--)
1108 {
1109 if (tonebank[b])
1110 free_bank(0,b);
1111
1112 if (drumset[b])
1113 free_bank(1,b);
1114 }
1115 }
1116
set_default_instrument(char * name)1117 int set_default_instrument(char *name)
1118 {
1119 InstrumentLayer *lp;
1120 /* if (!(lp=load_instrument(name, 0, -1, -1, -1, 0, 0, 0))) */
1121 if (!(lp=load_instrument(name, FONT_NORMAL, 0, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1)))
1122 return -1;
1123
1124 if (default_instrument)
1125 free_layer(default_instrument);
1126
1127 default_instrument=lp;
1128 default_program=SPECIAL_PROGRAM;
1129
1130 return 0;
1131 }
1132
1133 //--- editor settings ---
1134 // vi:ts=4:sw=4:noexpandtab
1135