Skip to content

Latest commit

 

History

History
1009 lines (893 loc) · 24.8 KB

instrum.c

File metadata and controls

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