/
mix.c
851 lines (785 loc) · 16.9 KB
1
2
3
4
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
5
6
7
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License, available in COPYING.
*/
8
9
10
#include <math.h>
#include <stdio.h>
11
#include <stdlib.h>
12
13
14
15
16
17
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "output.h"
18
#include "ctrlmode.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "tables.h"
#include "resample.h"
#include "mix.h"
/* Returns 1 if envelope runs out */
int recompute_envelope(int v)
{
int stage;
stage = voice[v].envelope_stage;
if (stage>5)
{
/* Envelope ran out. */
int tmp=(voice[v].status == VOICE_DIE); /* Already displayed as dead */
voice[v].status = VOICE_FREE;
if(!tmp)
ctl->note(v);
return 1;
}
if (voice[v].sample->modes & MODES_ENVELOPE)
{
if (voice[v].status==VOICE_ON || voice[v].status==VOICE_SUSTAINED)
{
if (stage>2)
{
/* Freeze envelope until note turns off. Trumpets want this. */
voice[v].envelope_increment=0;
return 0;
}
}
}
voice[v].envelope_stage=stage+1;
if (voice[v].envelope_volume==voice[v].sample->envelope_offset[stage])
return recompute_envelope(v);
voice[v].envelope_target=voice[v].sample->envelope_offset[stage];
voice[v].envelope_increment = voice[v].sample->envelope_rate[stage];
if (voice[v].envelope_target<voice[v].envelope_volume)
voice[v].envelope_increment = -voice[v].envelope_increment;
return 0;
}
void apply_envelope_to_amp(int v)
{
65
66
FLOAT_T lamp=voice[v].left_amp, ramp, lramp, rramp, ceamp, lfeamp;
int32 la,ra, lra, rra, cea, lfea;
67
68
if (voice[v].panned == PANNED_MYSTERY)
{
69
lramp=voice[v].lr_amp;
70
ramp=voice[v].right_amp;
71
72
73
74
ceamp=voice[v].ce_amp;
rramp=voice[v].rr_amp;
lfeamp=voice[v].lfe_amp;
75
76
if (voice[v].tremolo_phase_increment)
{
77
78
79
80
81
82
83
FLOAT_T tv = voice[v].tremolo_volume;
lramp *= tv;
lamp *= tv;
ceamp *= tv;
ramp *= tv;
rramp *= tv;
lfeamp *= tv;
84
85
86
}
if (voice[v].sample->modes & MODES_ENVELOPE)
{
87
88
89
90
91
92
93
FLOAT_T ev = (FLOAT_T)vol_table[voice[v].envelope_volume>>23];
lramp *= ev;
lamp *= ev;
ceamp *= ev;
ramp *= ev;
rramp *= ev;
lfeamp *= ev;
94
95
}
96
97
la = (int32)FSCALE(lamp,AMP_BITS);
ra = (int32)FSCALE(ramp,AMP_BITS);
98
99
100
101
lra = (int32)FSCALE(lramp,AMP_BITS);
rra = (int32)FSCALE(rramp,AMP_BITS);
cea = (int32)FSCALE(ceamp,AMP_BITS);
lfea = (int32)FSCALE(lfeamp,AMP_BITS);
102
103
104
105
106
107
108
109
110
if (la>MAX_AMP_VALUE) la=MAX_AMP_VALUE;
if (ra>MAX_AMP_VALUE) ra=MAX_AMP_VALUE;
if (lra>MAX_AMP_VALUE) lra=MAX_AMP_VALUE;
if (rra>MAX_AMP_VALUE) rra=MAX_AMP_VALUE;
if (cea>MAX_AMP_VALUE) cea=MAX_AMP_VALUE;
if (lfea>MAX_AMP_VALUE) lfea=MAX_AMP_VALUE;
voice[v].lr_mix=FINAL_VOLUME(lra);
111
voice[v].left_mix=FINAL_VOLUME(la);
112
voice[v].ce_mix=FINAL_VOLUME(cea);
113
voice[v].right_mix=FINAL_VOLUME(ra);
114
115
voice[v].rr_mix=FINAL_VOLUME(rra);
voice[v].lfe_mix=FINAL_VOLUME(lfea);
116
117
118
119
120
121
}
else
{
if (voice[v].tremolo_phase_increment)
lamp *= voice[v].tremolo_volume;
if (voice[v].sample->modes & MODES_ENVELOPE)
122
lamp *= (FLOAT_T)vol_table[voice[v].envelope_volume>>23];
123
124
la = (int32)FSCALE(lamp,AMP_BITS);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
if (la>MAX_AMP_VALUE)
la=MAX_AMP_VALUE;
voice[v].left_mix=FINAL_VOLUME(la);
}
}
static int update_envelope(int v)
{
voice[v].envelope_volume += voice[v].envelope_increment;
/* Why is there no ^^ operator?? */
if (((voice[v].envelope_increment < 0) &&
(voice[v].envelope_volume <= voice[v].envelope_target)) ||
((voice[v].envelope_increment > 0) &&
(voice[v].envelope_volume >= voice[v].envelope_target)))
{
voice[v].envelope_volume = voice[v].envelope_target;
if (recompute_envelope(v))
return 1;
}
return 0;
}
static void update_tremolo(int v)
{
int32 depth=voice[v].sample->tremolo_depth<<7;
if (voice[v].tremolo_sweep)
{
/* Update sweep position */
voice[v].tremolo_sweep_position += voice[v].tremolo_sweep;
if (voice[v].tremolo_sweep_position>=(1<<SWEEP_SHIFT))
voice[v].tremolo_sweep=0; /* Swept to max amplitude */
else
{
/* Need to adjust depth */
depth *= voice[v].tremolo_sweep_position;
depth >>= SWEEP_SHIFT;
}
}
voice[v].tremolo_phase += voice[v].tremolo_phase_increment;
/* if (voice[v].tremolo_phase >= (SINE_CYCLE_LENGTH<<RATE_SHIFT))
voice[v].tremolo_phase -= SINE_CYCLE_LENGTH<<RATE_SHIFT; */
173
voice[v].tremolo_volume = (FLOAT_T)
174
(1.0 - FSCALENEG((sine(voice[v].tremolo_phase >> RATE_SHIFT) + 1.0)
175
* depth * TREMOLO_AMPLITUDE_TUNING,
176
17));
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* I'm not sure about the +1.0 there -- it makes tremoloed voices'
volumes on average the lower the higher the tremolo amplitude. */
}
/* Returns 1 if the note died */
static int update_signal(int v)
{
if (voice[v].envelope_increment && update_envelope(v))
return 1;
if (voice[v].tremolo_phase_increment)
update_tremolo(v);
apply_envelope_to_amp(v);
return 0;
}
#ifdef LOOKUP_HACK
# define MIXATION(a) *lp++ += mixup[(a<<8) | (uint8)s];
#else
# define MIXATION(a) *lp++ += (a)*s;
#endif
201
202
203
204
205
206
#define MIXSKIP lp++
#define MIXMAX(a,b) *lp++ += ((a>b)?a:b) * s
#define MIXCENT(a,b) *lp++ += (a/2+b/2) * s
#define MIXHALF(a) *lp++ += (a>>1)*s;
static void mix_mystery_signal(resample_t *sp, int32 *lp, int v, int count)
207
208
209
{
Voice *vp = voice + v;
final_volume_t
210
left_rear=vp->lr_mix,
211
left=vp->left_mix,
212
213
214
215
center=vp->ce_mix,
right=vp->right_mix,
right_rear=vp->rr_mix,
lfe=vp->lfe_mix;
216
int cc;
217
resample_t s;
218
219
220
221
222
223
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
224
225
226
227
228
229
230
left_rear = vp->lr_mix;
left = vp->left_mix;
center = vp->ce_mix;
right = vp->right_mix;
right_rear = vp->rr_mix;
lfe = vp->lfe_mix;
231
232
233
234
235
236
237
238
239
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
240
241
242
243
244
245
246
247
248
249
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
250
251
252
253
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
254
left_rear = vp->lr_mix;
255
left = vp->left_mix;
256
center = vp->ce_mix;
257
right = vp->right_mix;
258
259
right_rear = vp->rr_mix;
lfe = vp->lfe_mix;
260
261
262
263
264
265
266
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
267
268
269
270
271
272
273
274
275
276
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
277
278
279
280
281
}
return;
}
}
282
static void mix_center_signal(resample_t *sp, int32 *lp, int v, int count)
283
284
285
286
287
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
288
resample_t s;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
if (num_ochannels == 2) {
MIXATION(left);
MIXATION(left);
}
else if (num_ochannels == 4) {
MIXATION(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
else if (num_ochannels == 6) {
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXATION(left);
MIXATION(left);
}
323
324
325
326
327
328
329
330
331
332
333
334
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
if (num_ochannels == 2) {
MIXATION(left);
MIXATION(left);
}
else if (num_ochannels == 4) {
MIXATION(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
else if (num_ochannels == 6) {
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXATION(left);
MIXATION(left);
}
353
354
355
356
357
}
return;
}
}
358
static void mix_single_left_signal(resample_t *sp, int32 *lp, int v, int count)
359
360
361
362
363
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
364
resample_t s;
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
if (num_ochannels == 2) {
MIXATION(left);
MIXSKIP;
}
if (num_ochannels >= 4) {
MIXHALF(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
395
396
397
398
399
400
401
402
403
404
405
406
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
407
408
409
410
411
412
413
414
415
416
417
418
419
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
if (num_ochannels == 2) {
MIXATION(left);
MIXSKIP;
}
if (num_ochannels >= 4) {
MIXHALF(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
}
return;
}
}
static void mix_single_right_signal(resample_t *sp, int32 *lp, int v, int count)
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
resample_t s;
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
if (num_ochannels == 2) {
MIXSKIP;
MIXATION(left);
}
if (num_ochannels >= 4) {
MIXSKIP;
MIXHALF(left);
MIXSKIP;
MIXATION(left);
} if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
if (num_ochannels == 2) {
MIXSKIP;
MIXATION(left);
}
if (num_ochannels >= 4) {
MIXSKIP;
MIXHALF(left);
MIXSKIP;
MIXATION(left);
} if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
487
488
489
490
491
}
return;
}
}
492
static void mix_mono_signal(resample_t *sp, int32 *lp, int v, int count)
493
494
495
496
497
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
498
resample_t s;
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
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
MIXATION(left);
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
left = vp->left_mix;
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
MIXATION(left);
}
return;
}
}
534
static void mix_mystery(resample_t *sp, int32 *lp, int v, int count)
535
536
{
final_volume_t
537
left_rear=voice[v].lr_mix,
538
left=voice[v].left_mix,
539
540
541
542
543
center=voice[v].ce_mix,
right=voice[v].right_mix,
right_rear=voice[v].rr_mix,
lfe=voice[v].lfe_mix;
resample_t s;
544
545
546
547
while (count--)
{
s = *sp++;
548
549
550
551
552
553
554
555
556
557
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
558
559
560
}
}
561
static void mix_center(resample_t *sp, int32 *lp, int v, int count)
562
563
564
{
final_volume_t
left=voice[v].left_mix;
565
resample_t s;
566
567
568
569
while (count--)
{
s = *sp++;
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
if (num_ochannels == 2) {
MIXATION(left);
MIXATION(left);
}
else if (num_ochannels == 4) {
MIXATION(left);
MIXATION(left);
MIXSKIP;
MIXSKIP;
}
else if (num_ochannels == 6) {
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXATION(left);
MIXATION(left);
}
588
589
590
}
}
591
static void mix_single_left(resample_t *sp, int32 *lp, int v, int count)
592
593
594
{
final_volume_t
left=voice[v].left_mix;
595
resample_t s;
596
597
598
599
while (count--)
{
s = *sp++;
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
if (num_ochannels == 2) {
MIXATION(left);
MIXSKIP;
}
if (num_ochannels >= 4) {
MIXHALF(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
}
}
static void mix_single_right(resample_t *sp, int32 *lp, int v, int count)
{
final_volume_t
left=voice[v].left_mix;
resample_t s;
while (count--)
{
s = *sp++;
if (num_ochannels == 2) {
MIXSKIP;
MIXATION(left);
}
if (num_ochannels >= 4) {
MIXSKIP;
MIXHALF(left);
MIXSKIP;
MIXATION(left);
}
if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
639
640
641
}
}
642
static void mix_mono(resample_t *sp, int32 *lp, int v, int count)
643
644
645
{
final_volume_t
left=voice[v].left_mix;
646
resample_t s;
647
648
649
650
651
652
653
654
655
while (count--)
{
s = *sp++;
MIXATION(left);
}
}
/* Ramp a note out in c samples */
656
static void ramp_out(resample_t *sp, int32 *lp, int v, int32 c)
657
658
659
{
/* should be final_volume_t, but uint8 gives trouble. */
660
int32 left_rear, left, center, right, right_rear, lfe, li, ri;
661
662
resample_t s = 0; /* silly warning about uninitialized s */
663
664
665
666
/* Fix by James Caldwell */
if ( c == 0 ) c = 1;
667
668
669
left = voice[v].left_mix;
li = -(left/c);
if (!li) li = -1;
670
671
672
673
674
675
676
/* printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li); */
if (!(play_mode->encoding & PE_MONO))
{
if (voice[v].panned==PANNED_MYSTERY)
{
677
678
left_rear = voice[v].lr_mix;
center=voice[v].ce_mix;
679
right=voice[v].right_mix;
680
681
682
right_rear = voice[v].rr_mix;
lfe = voice[v].lfe_mix;
683
684
685
ri=-(right/c);
while (c--)
{
686
687
688
689
690
691
left_rear += li; if (left_rear<0) left_rear=0;
left += li; if (left<0) left=0;
center += li; if (center<0) center=0;
right += ri; if (right<0) right=0;
right_rear += ri; if (right_rear<0) right_rear=0;
lfe += li; if (lfe<0) lfe=0;
692
s=*sp++;
693
694
695
696
697
698
699
700
701
702
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
703
704
705
706
707
708
709
710
711
712
}
}
else if (voice[v].panned==PANNED_CENTER)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
if (num_ochannels == 2) {
MIXATION(left);
MIXATION(left);
}
else if (num_ochannels == 4) {
MIXATION(left);
MIXATION(left);
MIXSKIP;
MIXSKIP;
}
else if (num_ochannels == 6) {
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXSKIP;
MIXATION(left);
MIXATION(left);
}
731
732
733
734
735
736
737
738
739
740
741
}
}
else if (voice[v].panned==PANNED_LEFT)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
MIXATION(left);
742
743
744
745
746
747
748
749
MIXSKIP;
if (num_ochannels >= 4) {
MIXATION(left);
MIXSKIP;
} if (num_ochannels == 6) {
MIXATION(left);
MIXATION(left);
}
750
751
752
753
754
755
756
757
758
759
}
}
else if (voice[v].panned==PANNED_RIGHT)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
760
MIXSKIP;
761
MIXATION(left);
762
763
764
765
766
767
768
if (num_ochannels >= 4) {
MIXSKIP;
MIXATION(left);
} if (num_ochannels == 6) {
MIXATION(left);
MIXATION(left);
}
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
}
}
}
else
{
/* Mono output. */
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
MIXATION(left);
}
}
}
/**************** interface function ******************/
void mix_voice(int32 *buf, int v, int32 c)
{
Voice *vp=voice+v;
792
793
794
int32 count=c;
resample_t *sp;
if (c<0) return;
795
796
if (vp->status==VOICE_DIE)
{
797
798
799
800
if (count>=MAX_DIE_TIME)
count=MAX_DIE_TIME;
sp=resample_voice(v, &count);
ramp_out(sp, buf, v, count);
801
802
803
804
vp->status=VOICE_FREE;
}
else
{
805
806
sp=resample_voice(v, &count);
if (count<0) return;
807
808
809
810
if (play_mode->encoding & PE_MONO)
{
/* Mono output. */
if (vp->envelope_increment || vp->tremolo_phase_increment)
811
mix_mono_signal(sp, buf, v, count);
812
else
813
mix_mono(sp, buf, v, count);
814
815
816
817
818
819
}
else
{
if (vp->panned == PANNED_MYSTERY)
{
if (vp->envelope_increment || vp->tremolo_phase_increment)
820
mix_mystery_signal(sp, buf, v, count);
821
else
822
mix_mystery(sp, buf, v, count);
823
824
825
826
}
else if (vp->panned == PANNED_CENTER)
{
if (vp->envelope_increment || vp->tremolo_phase_increment)
827
mix_center_signal(sp, buf, v, count);
828
else
829
mix_center(sp, buf, v, count);
830
831
832
833
834
835
836
}
else
{
/* It's either full left or full right. In either case,
every other sample is 0. Just get the offset right: */
if (vp->envelope_increment || vp->tremolo_phase_increment)
837
838
839
840
841
{
if (vp->panned == PANNED_RIGHT)
mix_single_right_signal(sp, buf, v, count);
else mix_single_left_signal(sp, buf, v, count);
}
842
else
843
844
845
846
847
{
if (vp->panned == PANNED_RIGHT)
mix_single_right(sp, buf, v, count);
else mix_single_left(sp, buf, v, count);
}
848
849
850
851
}
}
}
}