Skip to content

Latest commit

 

History

History
1747 lines (1512 loc) · 43 KB

playmidi.c

File metadata and controls

1747 lines (1512 loc) · 43 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Jul 4, 2005
Jul 4, 2005
13
14
#include <SDL_rwops.h>
Oct 21, 1999
Oct 21, 1999
15
16
17
18
19
20
21
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "output.h"
#include "mix.h"
May 14, 2006
May 14, 2006
22
#include "ctrlmode.h"
Oct 21, 1999
Oct 21, 1999
23
24
25
26
#include "timidity.h"
#include "tables.h"
Aug 21, 2004
Aug 21, 2004
27
28
29
30
31
32
33
static int opt_expression_curve = 2;
static int opt_volume_curve = 2;
static int opt_stereo_surround = 0;
Channel channel[MAXCHAN];
Oct 21, 1999
Oct 21, 1999
34
Voice voice[MAX_VOICES];
Aug 21, 2004
Aug 21, 2004
35
36
37
38
signed char drumvolume[MAXCHAN][MAXNOTE];
signed char drumpanpot[MAXCHAN][MAXNOTE];
signed char drumreverberation[MAXCHAN][MAXNOTE];
signed char drumchorusdepth[MAXCHAN][MAXNOTE];
Oct 21, 1999
Oct 21, 1999
39
40
41
42
43
44
45
46
int
voices=DEFAULT_VOICES;
int32
control_ratio=0,
amplification=DEFAULT_AMPLIFICATION;
Aug 21, 2004
Aug 21, 2004
47
FLOAT_T
Oct 21, 1999
Oct 21, 1999
48
49
50
51
52
53
54
55
56
57
58
59
60
61
master_volume;
int32 drumchannels=DEFAULT_DRUMCHANNELS;
int adjust_panning_immediately=0;
struct _MidiSong {
int32 samples;
MidiEvent *events;
};
static int midi_playing = 0;
static int32 lost_notes, cut_notes;
static int32 *buffer_pointer;
static int32 buffered_count;
extern int32 *common_buffer;
Jul 4, 2005
Jul 4, 2005
62
extern resample_t *resample_buffer; /* to free it on Timidity_Close */
Oct 21, 1999
Oct 21, 1999
63
64
65
66
static MidiEvent *event_list, *current_event;
static int32 sample_count, current_sample;
Aug 21, 2004
Aug 21, 2004
67
68
69
70
71
72
73
74
int GM_System_On=0;
int XG_System_On=0;
int GS_System_On=0;
int XG_System_reverb_type;
int XG_System_chorus_type;
int XG_System_variation_type;
Oct 21, 1999
Oct 21, 1999
75
76
static void adjust_amplification(void)
{
Aug 21, 2004
Aug 21, 2004
77
78
79
80
81
82
83
84
85
master_volume = (FLOAT_T)(amplification) / (FLOAT_T)100.0;
master_volume /= 2;
}
static void adjust_master_volume(int32 vol)
{
master_volume = (double)(vol*amplification) / 1638400.0L;
master_volume /= 2;
Oct 21, 1999
Oct 21, 1999
86
87
}
Aug 21, 2004
Aug 21, 2004
88
Oct 21, 1999
Oct 21, 1999
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
static void reset_voices(void)
{
int i;
for (i=0; i<MAX_VOICES; i++)
voice[i].status=VOICE_FREE;
}
/* Process the Reset All Controllers event */
static void reset_controllers(int c)
{
channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
channel[c].expression=127; /* SCC-1 does this. */
channel[c].sustain=0;
channel[c].pitchbend=0x2000;
channel[c].pitchfactor=0; /* to be computed */
Aug 21, 2004
Aug 21, 2004
104
105
106
channel[c].reverberation = 0;
channel[c].chorusdepth = 0;
Oct 21, 1999
Oct 21, 1999
107
108
109
110
111
112
113
114
115
116
117
118
119
}
static void redraw_controllers(int c)
{
ctl->volume(c, channel[c].volume);
ctl->expression(c, channel[c].expression);
ctl->sustain(c, channel[c].sustain);
ctl->pitch_bend(c, channel[c].pitchbend);
}
static void reset_midi(void)
{
int i;
Aug 21, 2004
Aug 21, 2004
120
for (i=0; i<MAXCHAN; i++)
Oct 21, 1999
Oct 21, 1999
121
122
123
124
125
126
127
{
reset_controllers(i);
/* The rest of these are unaffected by the Reset All Controllers event */
channel[i].program=default_program;
channel[i].panning=NO_PANNING;
channel[i].pitchsens=2;
channel[i].bank=0; /* tone bank or drum set */
Aug 21, 2004
Aug 21, 2004
128
129
130
131
132
channel[i].harmoniccontent=64,
channel[i].releasetime=64,
channel[i].attacktime=64,
channel[i].brightness=64,
channel[i].sfx=0;
Oct 21, 1999
Oct 21, 1999
133
134
135
136
137
138
}
reset_voices();
}
static void select_sample(int v, Instrument *ip)
{
Aug 21, 2004
Aug 21, 2004
139
int32 f, cdiff, diff, midfreq;
Oct 21, 1999
Oct 21, 1999
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
int s,i;
Sample *sp, *closest;
s=ip->samples;
sp=ip->sample;
if (s==1)
{
voice[v].sample=sp;
return;
}
f=voice[v].orig_frequency;
/*
No suitable sample found! We'll select the sample whose root
frequency is closest to the one we want. (Actually we should
probably convert the low, high, and root frequencies to MIDI note
values and compare those.) */
cdiff=0x7FFFFFFF;
closest=sp=ip->sample;
Aug 21, 2004
Aug 21, 2004
161
midfreq = (sp->low_freq + sp->high_freq) / 2;
Oct 21, 1999
Oct 21, 1999
162
163
164
for(i=0; i<s; i++)
{
diff=sp->root_freq - f;
Aug 21, 2004
Aug 21, 2004
165
166
167
168
/* But the root freq. can perfectly well lie outside the keyrange
* frequencies, so let's try:
*/
/* diff=midfreq - f; */
Oct 21, 1999
Oct 21, 1999
169
170
171
172
173
174
175
176
177
178
179
180
if (diff<0) diff=-diff;
if (diff<cdiff)
{
cdiff=diff;
closest=sp;
}
sp++;
}
voice[v].sample=closest;
return;
}
Aug 21, 2004
Aug 21, 2004
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
static void select_stereo_samples(int v, InstrumentLayer *lp)
{
Instrument *ip;
InstrumentLayer *nlp, *bestvel;
int diffvel, midvel, mindiff;
/* select closest velocity */
bestvel = lp;
mindiff = 500;
for (nlp = lp; nlp; nlp = nlp->next) {
midvel = (nlp->hi + nlp->lo)/2;
if (!midvel) diffvel = 127;
else if (voice[v].velocity < nlp->lo || voice[v].velocity > nlp->hi)
diffvel = 200;
else diffvel = voice[v].velocity - midvel;
if (diffvel < 0) diffvel = -diffvel;
if (diffvel < mindiff) {
mindiff = diffvel;
bestvel = nlp;
}
}
ip = bestvel->instrument;
if (ip->right_sample) {
ip->sample = ip->right_sample;
ip->samples = ip->right_samples;
select_sample(v, ip);
voice[v].right_sample = voice[v].sample;
}
else voice[v].right_sample = 0;
ip->sample = ip->left_sample;
ip->samples = ip->left_samples;
select_sample(v, ip);
}
Oct 21, 1999
Oct 21, 1999
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
static void recompute_freq(int v)
{
int
sign=(voice[v].sample_increment < 0), /* for bidirectional loops */
pb=channel[voice[v].channel].pitchbend;
double a;
if (!voice[v].sample->sample_rate)
return;
if (voice[v].vibrato_control_ratio)
{
/* This instrument has vibrato. Invalidate any precomputed
sample_increments. */
int i=VIBRATO_SAMPLE_INCREMENTS;
while (i--)
voice[v].vibrato_sample_increment[i]=0;
}
if (pb==0x2000 || pb<0 || pb>0x3FFF)
voice[v].frequency=voice[v].orig_frequency;
else
{
pb-=0x2000;
if (!(channel[voice[v].channel].pitchfactor))
{
/* Damn. Somebody bent the pitch. */
int32 i=pb*channel[voice[v].channel].pitchsens;
if (pb<0)
i=-i;
channel[voice[v].channel].pitchfactor=
Aug 21, 2004
Aug 21, 2004
251
(FLOAT_T)(bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13]);
Oct 21, 1999
Oct 21, 1999
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
}
if (pb>0)
voice[v].frequency=
(int32)(channel[voice[v].channel].pitchfactor *
(double)(voice[v].orig_frequency));
else
voice[v].frequency=
(int32)((double)(voice[v].orig_frequency) /
channel[voice[v].channel].pitchfactor);
}
a = FSCALE(((double)(voice[v].sample->sample_rate) *
(double)(voice[v].frequency)) /
((double)(voice[v].sample->root_freq) *
(double)(play_mode->rate)),
FRACTION_BITS);
if (sign)
a = -a; /* need to preserve the loop direction */
voice[v].sample_increment = (int32)(a);
}
Aug 21, 2004
Aug 21, 2004
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
static int expr_curve[128] = {
7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11,
11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15,
15, 16, 16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 22,
22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31,
32, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 59, 60, 61, 63,
64, 65, 67, 68, 70, 71, 73, 75, 76, 78, 80, 82, 83, 85, 87, 89,
91, 93, 95, 97, 99, 102, 104, 106, 109, 111, 113, 116, 118, 121,
124, 127
};
static int panf(int pan, int speaker, int separation)
{
int val;
val = abs(pan - speaker);
val = (val * 127) / separation;
val = 127 - val;
if (val < 0) val = 0;
if (val > 127) val = 127;
return expr_curve[val];
}
static int vcurve[128] = {
0,0,18,29,36,42,47,51,55,58,
60,63,65,67,69,71,73,74,76,77,
79,80,81,82,83,84,85,86,87,88,
89,90,91,92,92,93,94,95,95,96,
97,97,98,99,99,100,100,101,101,102,
103,103,104,104,105,105,106,106,106,107,
107,108,108,109,109,109,110,110,111,111,
111,112,112,112,113,113,114,114,114,115,
115,115,116,116,116,116,117,117,117,118,
118,118,119,119,119,119,120,120,120,120,
121,121,121,122,122,122,122,123,123,123,
123,123,124,124,124,124,125,125,125,125,
126,126,126,126,126,127,127,127
};
Oct 21, 1999
Oct 21, 1999
315
316
317
static void recompute_amp(int v)
{
int32 tempamp;
Aug 21, 2004
Aug 21, 2004
318
319
320
321
322
323
324
325
326
327
328
int chan = voice[v].channel;
int panning = voice[v].panning;
int vol = channel[chan].volume;
int expr = channel[chan].expression;
int vel = vcurve[voice[v].velocity];
FLOAT_T curved_expression, curved_volume;
if (channel[chan].kit)
{
int note = voice[v].sample->note_to_use;
if (note>0 && drumvolume[chan][note]>=0) vol = drumvolume[chan][note];
Oct 6, 2018
Oct 6, 2018
329
if (note>0 && drumpanpot[chan][note]>=0) panning = drumpanpot[chan][note];
Aug 21, 2004
Aug 21, 2004
330
331
332
333
334
335
336
337
338
339
340
}
if (opt_expression_curve == 2) curved_expression = 127.0 * vol_table[expr];
else if (opt_expression_curve == 1) curved_expression = 127.0 * expr_table[expr];
else curved_expression = (FLOAT_T)expr;
if (opt_volume_curve == 2) curved_volume = 127.0 * vol_table[vol];
else if (opt_volume_curve == 1) curved_volume = 127.0 * expr_table[vol];
else curved_volume = (FLOAT_T)vol;
tempamp= (int32)((FLOAT_T)vel * curved_volume * curved_expression); /* 21 bits */
Oct 21, 1999
Oct 21, 1999
341
342
343
/* TODO: use fscale */
Aug 21, 2004
Aug 21, 2004
344
if (num_ochannels > 1)
Oct 21, 1999
Oct 21, 1999
345
{
Aug 21, 2004
Aug 21, 2004
346
if (panning > 60 && panning < 68)
Oct 21, 1999
Oct 21, 1999
347
348
349
{
voice[v].panned=PANNED_CENTER;
Aug 21, 2004
Aug 21, 2004
350
351
352
353
354
355
if (num_ochannels == 6) voice[v].left_amp =
FSCALENEG((double) (tempamp) * voice[v].sample->volume *
master_volume, 20);
else voice[v].left_amp=
FSCALENEG((double)(tempamp) * voice[v].sample->volume *
master_volume, 21);
Oct 21, 1999
Oct 21, 1999
356
}
Aug 21, 2004
Aug 21, 2004
357
else if (panning<5)
Oct 21, 1999
Oct 21, 1999
358
359
360
361
362
363
364
{
voice[v].panned = PANNED_LEFT;
voice[v].left_amp=
FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
20);
}
Aug 21, 2004
Aug 21, 2004
365
else if (panning>123)
Oct 21, 1999
Oct 21, 1999
366
367
368
369
370
371
372
373
374
{
voice[v].panned = PANNED_RIGHT;
voice[v].left_amp= /* left_amp will be used */
FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
20);
}
else
{
Aug 21, 2004
Aug 21, 2004
375
376
377
378
379
FLOAT_T refv = (double)(tempamp) * voice[v].sample->volume * master_volume;
int wide_panning = 64;
if (num_ochannels == 4) wide_panning = 95;
Oct 21, 1999
Oct 21, 1999
380
voice[v].panned = PANNED_MYSTERY;
Aug 21, 2004
Aug 21, 2004
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
voice[v].lfe_amp = FSCALENEG(refv * 64, 27);
switch (num_ochannels)
{
case 2:
voice[v].lr_amp = 0;
voice[v].left_amp = FSCALENEG(refv * (128-panning), 27);
voice[v].ce_amp = 0;
voice[v].right_amp = FSCALENEG(refv * panning, 27);
voice[v].rr_amp = 0;
break;
case 4:
voice[v].lr_amp = FSCALENEG(refv * panf(panning, 0, wide_panning), 27);
voice[v].left_amp = FSCALENEG(refv * panf(panning, 32, wide_panning), 27);
voice[v].ce_amp = 0;
voice[v].right_amp = FSCALENEG(refv * panf(panning, 95, wide_panning), 27);
voice[v].rr_amp = FSCALENEG(refv * panf(panning, 128, wide_panning), 27);
break;
case 6:
voice[v].lr_amp = FSCALENEG(refv * panf(panning, 0, wide_panning), 27);
voice[v].left_amp = FSCALENEG(refv * panf(panning, 32, wide_panning), 27);
voice[v].ce_amp = FSCALENEG(refv * panf(panning, 64, wide_panning), 27);
voice[v].right_amp = FSCALENEG(refv * panf(panning, 95, wide_panning), 27);
voice[v].rr_amp = FSCALENEG(refv * panf(panning, 128, wide_panning), 27);
break;
}
Oct 21, 1999
Oct 21, 1999
407
408
409
410
411
412
413
414
415
416
417
418
419
}
}
else
{
voice[v].panned=PANNED_CENTER;
voice[v].left_amp=
FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
21);
}
}
Aug 21, 2004
Aug 21, 2004
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#define NOT_CLONE 0
#define STEREO_CLONE 1
#define REVERB_CLONE 2
#define CHORUS_CLONE 3
/* just a variant of note_on() */
static int vc_alloc(int j)
{
int i=voices;
while (i--)
{
if (i == j) continue;
if (voice[i].status & VOICE_FREE) {
return i;
}
}
return -1;
}
static void kill_note(int i);
static void kill_others(int i)
{
int j=voices;
if (!voice[i].sample->exclusiveClass) return;
while (j--)
{
if (voice[j].status & (VOICE_FREE|VOICE_OFF|VOICE_DIE)) continue;
if (i == j) continue;
if (voice[i].channel != voice[j].channel) continue;
if (voice[j].sample->note_to_use)
{
if (voice[j].sample->exclusiveClass != voice[i].sample->exclusiveClass) continue;
kill_note(j);
}
}
}
static void clone_voice(Instrument *ip, int v, MidiEvent *e, int clone_type, int variationbank)
{
Nov 15, 2004
Nov 15, 2004
466
int w, played_note, chorus=0, reverb=0, milli;
Aug 21, 2004
Aug 21, 2004
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
740
741
742
int chan = voice[v].channel;
if (clone_type == STEREO_CLONE) {
if (!voice[v].right_sample && variationbank != 3) return;
if (variationbank == 6) return;
}
if (channel[chan].kit) {
reverb = drumreverberation[chan][voice[v].note];
chorus = drumchorusdepth[chan][voice[v].note];
}
else {
reverb = channel[chan].reverberation;
chorus = channel[chan].chorusdepth;
}
if (clone_type == REVERB_CLONE) chorus = 0;
else if (clone_type == CHORUS_CLONE) reverb = 0;
else if (clone_type == STEREO_CLONE) reverb = chorus = 0;
if (reverb > 127) reverb = 127;
if (chorus > 127) chorus = 127;
if (clone_type == CHORUS_CLONE) {
if (variationbank == 32) chorus = 30;
else if (variationbank == 33) chorus = 60;
else if (variationbank == 34) chorus = 90;
}
chorus /= 2; /* This is an ad hoc adjustment. */
if (!reverb && !chorus && clone_type != STEREO_CLONE) return;
if ( (w = vc_alloc(v)) < 0 ) return;
voice[w] = voice[v];
if (clone_type==STEREO_CLONE) voice[v].clone_voice = w;
voice[w].clone_voice = v;
voice[w].clone_type = clone_type;
voice[w].sample = voice[v].right_sample;
voice[w].velocity= e->b;
milli = play_mode->rate/1000;
if (clone_type == STEREO_CLONE) {
int left, right, leftpan, rightpan;
int panrequest = voice[v].panning;
if (variationbank == 3) {
voice[v].panning = 0;
voice[w].panning = 127;
}
else {
if (voice[v].sample->panning > voice[w].sample->panning) {
left = w;
right = v;
}
else {
left = v;
right = w;
}
#define INSTRUMENT_SEPARATION 12
leftpan = panrequest - INSTRUMENT_SEPARATION / 2;
rightpan = leftpan + INSTRUMENT_SEPARATION;
if (leftpan < 0) {
leftpan = 0;
rightpan = leftpan + INSTRUMENT_SEPARATION;
}
if (rightpan > 127) {
rightpan = 127;
leftpan = rightpan - INSTRUMENT_SEPARATION;
}
voice[left].panning = leftpan;
voice[right].panning = rightpan;
voice[right].echo_delay = 20 * milli;
}
}
voice[w].volume = voice[w].sample->volume;
if (reverb) {
if (opt_stereo_surround) {
if (voice[w].panning > 64) voice[w].panning = 127;
else voice[w].panning = 0;
}
else {
if (voice[v].panning < 64) voice[w].panning = 64 + reverb/2;
else voice[w].panning = 64 - reverb/2;
}
/* try 98->99 for melodic instruments ? (bit much for percussion) */
voice[w].volume *= vol_table[(127-reverb)/8 + 98];
voice[w].echo_delay += reverb * milli;
voice[w].envelope_rate[DECAY] *= 2;
voice[w].envelope_rate[RELEASE] /= 2;
if (XG_System_reverb_type >= 0) {
int subtype = XG_System_reverb_type & 0x07;
int rtype = XG_System_reverb_type >>3;
switch (rtype) {
case 0: /* no effect */
break;
case 1: /* hall */
if (subtype) voice[w].echo_delay += 100 * milli;
break;
case 2: /* room */
voice[w].echo_delay /= 2;
break;
case 3: /* stage */
voice[w].velocity = voice[v].velocity;
break;
case 4: /* plate */
voice[w].panning = voice[v].panning;
break;
case 16: /* white room */
voice[w].echo_delay = 0;
break;
case 17: /* tunnel */
voice[w].echo_delay *= 2;
voice[w].velocity /= 2;
break;
case 18: /* canyon */
voice[w].echo_delay *= 2;
break;
case 19: /* basement */
voice[w].velocity /= 2;
break;
default: break;
}
}
}
played_note = voice[w].sample->note_to_use;
if (!played_note) {
played_note = e->a & 0x7f;
if (variationbank == 35) played_note += 12;
else if (variationbank == 36) played_note -= 12;
else if (variationbank == 37) played_note += 7;
else if (variationbank == 36) played_note -= 7;
}
#if 0
played_note = ( (played_note - voice[w].sample->freq_center) * voice[w].sample->freq_scale ) / 1024 +
voice[w].sample->freq_center;
#endif
voice[w].note = played_note;
voice[w].orig_frequency = freq_table[played_note];
if (chorus) {
if (opt_stereo_surround) {
if (voice[v].panning < 64) voice[w].panning = voice[v].panning + 32;
else voice[w].panning = voice[v].panning - 32;
}
if (!voice[w].vibrato_control_ratio) {
voice[w].vibrato_control_ratio = 100;
voice[w].vibrato_depth = 6;
voice[w].vibrato_sweep = 74;
}
voice[w].volume *= 0.40;
voice[v].volume = voice[w].volume;
recompute_amp(v);
apply_envelope_to_amp(v);
voice[w].vibrato_sweep = chorus/2;
voice[w].vibrato_depth /= 2;
if (!voice[w].vibrato_depth) voice[w].vibrato_depth = 2;
voice[w].vibrato_control_ratio /= 2;
voice[w].echo_delay += 30 * milli;
if (XG_System_chorus_type >= 0) {
int subtype = XG_System_chorus_type & 0x07;
int chtype = 0x0f & (XG_System_chorus_type >> 3);
switch (chtype) {
case 0: /* no effect */
break;
case 1: /* chorus */
chorus /= 3;
if(channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency * bend_fine[chorus] );
else voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency / bend_fine[chorus] );
if (subtype) voice[w].vibrato_depth *= 2;
break;
case 2: /* celeste */
voice[w].orig_frequency += (voice[w].orig_frequency/128) * chorus;
break;
case 3: /* flanger */
voice[w].vibrato_control_ratio = 10;
voice[w].vibrato_depth = 100;
voice[w].vibrato_sweep = 8;
voice[w].echo_delay += 200 * milli;
break;
case 4: /* symphonic : cf Children of the Night /128 bad, /1024 ok */
voice[w].orig_frequency += (voice[w].orig_frequency/512) * chorus;
voice[v].orig_frequency -= (voice[v].orig_frequency/512) * chorus;
recompute_freq(v);
break;
case 8: /* phaser */
break;
default:
break;
}
}
else {
chorus /= 3;
if(channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency * bend_fine[chorus] );
else voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency / bend_fine[chorus] );
}
}
#if 0
voice[w].loop_start = voice[w].sample->loop_start;
voice[w].loop_end = voice[w].sample->loop_end;
#endif
voice[w].echo_delay_count = voice[w].echo_delay;
if (reverb) voice[w].echo_delay *= 2;
recompute_freq(w);
recompute_amp(w);
if (voice[w].sample->modes & MODES_ENVELOPE)
{
/* Ramp up from 0 */
voice[w].envelope_stage=ATTACK;
voice[w].modulation_stage=ATTACK;
voice[w].envelope_volume=0;
voice[w].modulation_volume=0;
voice[w].control_counter=0;
voice[w].modulation_counter=0;
recompute_envelope(w);
/*recompute_modulation(w);*/
}
else
{
voice[w].envelope_increment=0;
voice[w].modulation_increment=0;
}
apply_envelope_to_amp(w);
}
static void xremap(int *banknumpt, int *this_notept, int this_kit) {
int i, newmap;
int banknum = *banknumpt;
int this_note = *this_notept;
int newbank, newnote;
if (!this_kit) {
if (banknum == SFXBANK && tonebank[SFXBANK]) return;
if (banknum == SFXBANK && tonebank[120]) *banknumpt = 120;
return;
}
if (this_kit != 127 && this_kit != 126) return;
for (i = 0; i < XMAPMAX; i++) {
newmap = xmap[i][0];
if (!newmap) return;
if (this_kit == 127 && newmap != XGDRUM) continue;
if (this_kit == 126 && newmap != SFXDRUM1) continue;
if (xmap[i][1] != banknum) continue;
if (xmap[i][3] != this_note) continue;
newbank = xmap[i][2];
newnote = xmap[i][4];
if (newbank == banknum && newnote == this_note) return;
if (!drumset[newbank]) return;
if (!drumset[newbank]->tone[newnote].layer) return;
if (drumset[newbank]->tone[newnote].layer == MAGIC_LOAD_INSTRUMENT) return;
*banknumpt = newbank;
*this_notept = newnote;
return;
}
}
Oct 21, 1999
Oct 21, 1999
743
744
static void start_note(MidiEvent *e, int i)
{
Aug 21, 2004
Aug 21, 2004
745
InstrumentLayer *lp;
Oct 21, 1999
Oct 21, 1999
746
Instrument *ip;
Aug 21, 2004
Aug 21, 2004
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
int j, banknum, ch=e->channel;
int played_note, drumpan=NO_PANNING;
int32 rt;
int attacktime, releasetime, decaytime, variationbank;
int brightness = channel[ch].brightness;
int harmoniccontent = channel[ch].harmoniccontent;
int this_note = e->a;
int this_velocity = e->b;
int drumsflag = channel[ch].kit;
int this_prog = channel[ch].program;
if (channel[ch].sfx) banknum=channel[ch].sfx;
else banknum=channel[ch].bank;
voice[i].velocity=this_velocity;
if (XG_System_On) xremap(&banknum, &this_note, drumsflag);
/* if (current_config_pc42b) pcmap(&banknum, &this_note, &this_prog, &drumsflag); */
if (drumsflag)
Oct 21, 1999
Oct 21, 1999
767
{
Aug 21, 2004
Aug 21, 2004
768
if (!(lp=drumset[banknum]->tone[this_note].layer))
Oct 21, 1999
Oct 21, 1999
769
{
Aug 21, 2004
Aug 21, 2004
770
if (!(lp=drumset[0]->tone[this_note].layer))
Oct 21, 1999
Oct 21, 1999
771
772
return; /* No instrument? Then we can't play. */
}
Aug 21, 2004
Aug 21, 2004
773
774
ip = lp->instrument;
if (ip->type == INST_GUS && ip->samples != 1)
Oct 21, 1999
Oct 21, 1999
775
776
777
778
779
780
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Strange: percussion instrument with %d samples!", ip->samples);
}
if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
Aug 21, 2004
Aug 21, 2004
781
782
783
784
{
voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
drumpan=drumpanpot[ch][(int)ip->sample->note_to_use];
}
Oct 21, 1999
Oct 21, 1999
785
else
Aug 21, 2004
Aug 21, 2004
786
787
voice[i].orig_frequency=freq_table[this_note & 0x7F];
Oct 21, 1999
Oct 21, 1999
788
789
790
}
else
{
Aug 21, 2004
Aug 21, 2004
791
792
793
794
if (channel[ch].program==SPECIAL_PROGRAM)
lp=default_instrument;
else if (!(lp=tonebank[channel[ch].bank]->
tone[channel[ch].program].layer))
Oct 21, 1999
Oct 21, 1999
795
{
Aug 21, 2004
Aug 21, 2004
796
if (!(lp=tonebank[0]->tone[this_prog].layer))
Oct 21, 1999
Oct 21, 1999
797
798
return; /* No instrument? Then we can't play. */
}
Aug 21, 2004
Aug 21, 2004
799
ip = lp->instrument;
Oct 21, 1999
Oct 21, 1999
800
801
802
if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
else
Aug 21, 2004
Aug 21, 2004
803
voice[i].orig_frequency=freq_table[this_note & 0x7F];
Oct 21, 1999
Oct 21, 1999
804
805
}
Aug 21, 2004
Aug 21, 2004
806
807
808
809
810
811
812
813
814
815
select_stereo_samples(i, lp);
voice[i].starttime = e->time;
played_note = voice[i].sample->note_to_use;
if (!played_note || !drumsflag) played_note = this_note & 0x7f;
#if 0
played_note = ( (played_note - voice[i].sample->freq_center) * voice[i].sample->freq_scale ) / 1024 +
voice[i].sample->freq_center;
#endif
Oct 21, 1999
Oct 21, 1999
816
voice[i].status=VOICE_ON;
Aug 21, 2004
Aug 21, 2004
817
818
819
voice[i].channel=ch;
voice[i].note=played_note;
voice[i].velocity=this_velocity;
Oct 21, 1999
Oct 21, 1999
820
821
822
823
824
825
826
827
828
829
voice[i].sample_offset=0;
voice[i].sample_increment=0; /* make sure it isn't negative */
voice[i].tremolo_phase=0;
voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment;
voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment;
voice[i].tremolo_sweep_position=0;
voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment;
voice[i].vibrato_sweep_position=0;
Aug 21, 2004
Aug 21, 2004
830
voice[i].vibrato_depth=voice[i].sample->vibrato_depth;
Oct 21, 1999
Oct 21, 1999
831
832
voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio;
voice[i].vibrato_control_counter=voice[i].vibrato_phase=0;
Aug 21, 2004
Aug 21, 2004
833
834
835
836
voice[i].vibrato_delay = voice[i].sample->vibrato_delay;
kill_others(i);
Oct 21, 1999
Oct 21, 1999
837
838
839
for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
voice[i].vibrato_sample_increment[j]=0;
Aug 21, 2004
Aug 21, 2004
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
attacktime = channel[ch].attacktime;
releasetime = channel[ch].releasetime;
decaytime = 64;
variationbank = channel[ch].variationbank;
switch (variationbank) {
case 8:
attacktime = 64+32;
break;
case 12:
decaytime = 64-32;
break;
case 16:
brightness = 64+16;
break;
case 17:
brightness = 64+32;
break;
case 18:
brightness = 64-16;
break;
case 19:
brightness = 64-32;
break;
case 20:
harmoniccontent = 64+16;
break;
#if 0
case 24:
voice[i].modEnvToFilterFc=2.0;
voice[i].sample->cutoff_freq = 800;
break;
case 25:
voice[i].modEnvToFilterFc=-2.0;
voice[i].sample->cutoff_freq = 800;
break;
case 27:
voice[i].modLfoToFilterFc=2.0;
voice[i].lfo_phase_increment=109;
voice[i].lfo_sweep=122;
voice[i].sample->cutoff_freq = 800;
break;
case 28:
voice[i].modLfoToFilterFc=-2.0;
voice[i].lfo_phase_increment=109;
voice[i].lfo_sweep=122;
voice[i].sample->cutoff_freq = 800;
break;
#endif
default:
break;
}
for (j=ATTACK; j<MAXPOINT; j++)
{
voice[i].envelope_rate[j]=voice[i].sample->envelope_rate[j];
voice[i].envelope_offset[j]=voice[i].sample->envelope_offset[j];
}
voice[i].echo_delay=voice[i].envelope_rate[DELAY];
voice[i].echo_delay_count = voice[i].echo_delay;
if (attacktime!=64)
{
rt = voice[i].envelope_rate[ATTACK];
rt = rt + ( (64-attacktime)*rt ) / 100;
if (rt > 1000) voice[i].envelope_rate[ATTACK] = rt;
}
if (releasetime!=64)
{
rt = voice[i].envelope_rate[RELEASE];
rt = rt + ( (64-releasetime)*rt ) / 100;
if (rt > 1000) voice[i].envelope_rate[RELEASE] = rt;
}
if (decaytime!=64)
{
rt = voice[i].envelope_rate[DECAY];
rt = rt + ( (64-decaytime)*rt ) / 100;
if (rt > 1000) voice[i].envelope_rate[DECAY] = rt;
}
if (channel[ch].panning != NO_PANNING)
voice[i].panning=channel[ch].panning;
Oct 21, 1999
Oct 21, 1999
925
926
else
voice[i].panning=voice[i].sample->panning;
Aug 21, 2004
Aug 21, 2004
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
if (drumpan != NO_PANNING)
voice[i].panning=drumpan;
if (variationbank == 1) {
int pan = voice[i].panning;
int disturb = 0;
/* If they're close up (no reverb) and you are behind the pianist,
* high notes come from the right, so we'll spread piano etc. notes
* out horizontally according to their pitches.
*/
if (this_prog < 21) {
int n = voice[i].velocity - 32;
if (n < 0) n = 0;
if (n > 64) n = 64;
pan = pan/2 + n;
}
/* For other types of instruments, the music sounds more alive if
* notes come from slightly different directions. However, instruments
* do drift around in a sometimes disconcerting way, so the following
* might not be such a good idea.
*/
else disturb = (voice[i].velocity/32 % 8) +
(voice[i].note % 8); /* /16? */
if (pan < 64) pan += disturb;
else pan -= disturb;
if (pan < 0) pan = 0;
else if (pan > 127) pan = 127;
voice[i].panning = pan;
}
Oct 21, 1999
Oct 21, 1999
957
958
959
960
961
962
recompute_freq(i);
recompute_amp(i);
if (voice[i].sample->modes & MODES_ENVELOPE)
{
/* Ramp up from 0 */
Aug 21, 2004
Aug 21, 2004
963
voice[i].envelope_stage=ATTACK;
Oct 21, 1999
Oct 21, 1999
964
965
966
967
968
969
970
971
voice[i].envelope_volume=0;
voice[i].control_counter=0;
recompute_envelope(i);
}
else
{
voice[i].envelope_increment=0;
}
Aug 21, 2004
Aug 21, 2004
972
973
974
975
976
977
978
979
980
apply_envelope_to_amp(i);
voice[i].clone_voice = -1;
voice[i].clone_type = NOT_CLONE;
clone_voice(ip, i, e, STEREO_CLONE, variationbank);
clone_voice(ip, i, e, CHORUS_CLONE, variationbank);
clone_voice(ip, i, e, REVERB_CLONE, variationbank);
Oct 21, 1999
Oct 21, 1999
981
982
983
984
985
986
ctl->note(i);
}
static void kill_note(int i)
{
voice[i].status=VOICE_DIE;
Aug 21, 2004
Aug 21, 2004
987
988
if (voice[i].clone_voice >= 0)
voice[ voice[i].clone_voice ].status=VOICE_DIE;
Oct 21, 1999
Oct 21, 1999
989
990
991
ctl->note(i);
}
Aug 21, 2004
Aug 21, 2004
992
Oct 21, 1999
Oct 21, 1999
993
994
995
996
997
998
999
1000
/* Only one instance of a note can be playing on a single channel. */
static void note_on(MidiEvent *e)
{
int i=voices, lowest=-1;
int32 lv=0x7FFFFFFF, v;
while (i--)
{