/
mix.c
866 lines (796 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
Suddenly, you realize that this program is free software; you get
an overwhelming urge to redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received another copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
I bet they'll be amazed.
mix.c */
#include <math.h>
#include <stdio.h>
26
#include <stdlib.h>
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "output.h"
#include "controls.h"
#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)
{
80
81
FLOAT_T lamp=voice[v].left_amp, ramp, lramp, rramp, ceamp, lfeamp;
int32 la,ra, lra, rra, cea, lfea;
82
83
if (voice[v].panned == PANNED_MYSTERY)
{
84
lramp=voice[v].lr_amp;
85
ramp=voice[v].right_amp;
86
87
88
89
ceamp=voice[v].ce_amp;
rramp=voice[v].rr_amp;
lfeamp=voice[v].lfe_amp;
90
91
if (voice[v].tremolo_phase_increment)
{
92
93
94
95
96
97
98
FLOAT_T tv = voice[v].tremolo_volume;
lramp *= tv;
lamp *= tv;
ceamp *= tv;
ramp *= tv;
rramp *= tv;
lfeamp *= tv;
99
100
101
}
if (voice[v].sample->modes & MODES_ENVELOPE)
{
102
103
104
105
106
107
108
FLOAT_T ev = (FLOAT_T)vol_table[voice[v].envelope_volume>>23];
lramp *= ev;
lamp *= ev;
ceamp *= ev;
ramp *= ev;
rramp *= ev;
lfeamp *= ev;
109
110
}
111
112
la = (int32)FSCALE(lamp,AMP_BITS);
ra = (int32)FSCALE(ramp,AMP_BITS);
113
114
115
116
lra = (int32)FSCALE(lramp,AMP_BITS);
rra = (int32)FSCALE(rramp,AMP_BITS);
cea = (int32)FSCALE(ceamp,AMP_BITS);
lfea = (int32)FSCALE(lfeamp,AMP_BITS);
117
118
119
120
121
122
123
124
125
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);
126
voice[v].left_mix=FINAL_VOLUME(la);
127
voice[v].ce_mix=FINAL_VOLUME(cea);
128
voice[v].right_mix=FINAL_VOLUME(ra);
129
130
voice[v].rr_mix=FINAL_VOLUME(rra);
voice[v].lfe_mix=FINAL_VOLUME(lfea);
131
132
133
134
135
136
}
else
{
if (voice[v].tremolo_phase_increment)
lamp *= voice[v].tremolo_volume;
if (voice[v].sample->modes & MODES_ENVELOPE)
137
lamp *= (FLOAT_T)vol_table[voice[v].envelope_volume>>23];
138
139
la = (int32)FSCALE(lamp,AMP_BITS);
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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; */
188
voice[v].tremolo_volume = (FLOAT_T)
189
(1.0 - FSCALENEG((sine(voice[v].tremolo_phase >> RATE_SHIFT) + 1.0)
190
* depth * TREMOLO_AMPLITUDE_TUNING,
191
17));
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 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
216
217
218
219
220
221
#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)
222
223
224
{
Voice *vp = voice + v;
final_volume_t
225
left_rear=vp->lr_mix,
226
left=vp->left_mix,
227
228
229
230
center=vp->ce_mix,
right=vp->right_mix,
right_rear=vp->rr_mix,
lfe=vp->lfe_mix;
231
int cc;
232
resample_t s;
233
234
235
236
237
238
if (!(cc = vp->control_counter))
{
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
239
240
241
242
243
244
245
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;
246
247
248
249
250
251
252
253
254
}
while (count)
if (cc < count)
{
count -= cc;
while (cc--)
{
s = *sp++;
255
256
257
258
259
260
261
262
263
264
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
265
266
267
268
}
cc = control_ratio;
if (update_signal(v))
return; /* Envelope ran out */
269
left_rear = vp->lr_mix;
270
left = vp->left_mix;
271
center = vp->ce_mix;
272
right = vp->right_mix;
273
274
right_rear = vp->rr_mix;
lfe = vp->lfe_mix;
275
276
277
278
279
280
281
}
else
{
vp->control_counter = cc - count;
while (count--)
{
s = *sp++;
282
283
284
285
286
287
288
289
290
291
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
292
293
294
295
296
}
return;
}
}
297
static void mix_center_signal(resample_t *sp, int32 *lp, int v, int count)
298
299
300
301
302
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
303
resample_t s;
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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++;
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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);
}
338
339
340
341
342
343
344
345
346
347
348
349
}
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++;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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);
}
368
369
370
371
372
}
return;
}
}
373
static void mix_single_left_signal(resample_t *sp, int32 *lp, int v, int count)
374
375
376
377
378
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
379
resample_t s;
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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++;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
if (num_ochannels == 2) {
MIXATION(left);
MIXSKIP;
}
if (num_ochannels >= 4) {
MIXHALF(left);
MIXSKIP;
MIXATION(left);
MIXSKIP;
}
if (num_ochannels == 6) {
MIXSKIP;
MIXATION(left);
}
410
411
412
413
414
415
416
417
418
419
420
421
}
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++;
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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);
}
502
503
504
505
506
}
return;
}
}
507
static void mix_mono_signal(resample_t *sp, int32 *lp, int v, int count)
508
509
510
511
512
{
Voice *vp = voice + v;
final_volume_t
left=vp->left_mix;
int cc;
513
resample_t s;
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
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;
}
}
549
static void mix_mystery(resample_t *sp, int32 *lp, int v, int count)
550
551
{
final_volume_t
552
left_rear=voice[v].lr_mix,
553
left=voice[v].left_mix,
554
555
556
557
558
center=voice[v].ce_mix,
right=voice[v].right_mix,
right_rear=voice[v].rr_mix,
lfe=voice[v].lfe_mix;
resample_t s;
559
560
561
562
while (count--)
{
s = *sp++;
563
564
565
566
567
568
569
570
571
572
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
573
574
575
}
}
576
static void mix_center(resample_t *sp, int32 *lp, int v, int count)
577
578
579
{
final_volume_t
left=voice[v].left_mix;
580
resample_t s;
581
582
583
584
while (count--)
{
s = *sp++;
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
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);
}
603
604
605
}
}
606
static void mix_single_left(resample_t *sp, int32 *lp, int v, int count)
607
608
609
{
final_volume_t
left=voice[v].left_mix;
610
resample_t s;
611
612
613
614
while (count--)
{
s = *sp++;
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
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);
}
654
655
656
}
}
657
static void mix_mono(resample_t *sp, int32 *lp, int v, int count)
658
659
660
{
final_volume_t
left=voice[v].left_mix;
661
resample_t s;
662
663
664
665
666
667
668
669
670
while (count--)
{
s = *sp++;
MIXATION(left);
}
}
/* Ramp a note out in c samples */
671
static void ramp_out(resample_t *sp, int32 *lp, int v, int32 c)
672
673
674
{
/* should be final_volume_t, but uint8 gives trouble. */
675
int32 left_rear, left, center, right, right_rear, lfe, li, ri;
676
677
resample_t s = 0; /* silly warning about uninitialized s */
678
679
680
681
/* Fix by James Caldwell */
if ( c == 0 ) c = 1;
682
683
684
left = voice[v].left_mix;
li = -(left/c);
if (!li) li = -1;
685
686
687
688
689
690
691
/* 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)
{
692
693
left_rear = voice[v].lr_mix;
center=voice[v].ce_mix;
694
right=voice[v].right_mix;
695
696
697
right_rear = voice[v].rr_mix;
lfe = voice[v].lfe_mix;
698
699
700
ri=-(right/c);
while (c--)
{
701
702
703
704
705
706
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;
707
s=*sp++;
708
709
710
711
712
713
714
715
716
717
MIXATION(left);
MIXATION(right);
if (num_ochannels >= 4) {
MIXATION(left_rear);
MIXATION(right_rear);
}
if (num_ochannels == 6) {
MIXATION(center);
MIXATION(lfe);
}
718
719
720
721
722
723
724
725
726
727
}
}
else if (voice[v].panned==PANNED_CENTER)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
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);
}
746
747
748
749
750
751
752
753
754
755
756
}
}
else if (voice[v].panned==PANNED_LEFT)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
MIXATION(left);
757
758
759
760
761
762
763
764
MIXSKIP;
if (num_ochannels >= 4) {
MIXATION(left);
MIXSKIP;
} if (num_ochannels == 6) {
MIXATION(left);
MIXATION(left);
}
765
766
767
768
769
770
771
772
773
774
}
}
else if (voice[v].panned==PANNED_RIGHT)
{
while (c--)
{
left += li;
if (left<0)
return;
s=*sp++;
775
MIXSKIP;
776
MIXATION(left);
777
778
779
780
781
782
783
if (num_ochannels >= 4) {
MIXSKIP;
MIXATION(left);
} if (num_ochannels == 6) {
MIXATION(left);
MIXATION(left);
}
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
}
}
}
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;
807
808
809
int32 count=c;
resample_t *sp;
if (c<0) return;
810
811
if (vp->status==VOICE_DIE)
{
812
813
814
815
if (count>=MAX_DIE_TIME)
count=MAX_DIE_TIME;
sp=resample_voice(v, &count);
ramp_out(sp, buf, v, count);
816
817
818
819
vp->status=VOICE_FREE;
}
else
{
820
821
sp=resample_voice(v, &count);
if (count<0) return;
822
823
824
825
if (play_mode->encoding & PE_MONO)
{
/* Mono output. */
if (vp->envelope_increment || vp->tremolo_phase_increment)
826
mix_mono_signal(sp, buf, v, count);
827
else
828
mix_mono(sp, buf, v, count);
829
830
831
832
833
834
}
else
{
if (vp->panned == PANNED_MYSTERY)
{
if (vp->envelope_increment || vp->tremolo_phase_increment)
835
mix_mystery_signal(sp, buf, v, count);
836
else
837
mix_mystery(sp, buf, v, count);
838
839
840
841
}
else if (vp->panned == PANNED_CENTER)
{
if (vp->envelope_increment || vp->tremolo_phase_increment)
842
mix_center_signal(sp, buf, v, count);
843
else
844
mix_center(sp, buf, v, count);
845
846
847
848
849
850
851
}
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)
852
853
854
855
856
{
if (vp->panned == PANNED_RIGHT)
mix_single_right_signal(sp, buf, v, count);
else mix_single_left_signal(sp, buf, v, count);
}
857
else
858
859
860
861
862
{
if (vp->panned == PANNED_RIGHT)
mix_single_right(sp, buf, v, count);
else mix_single_left(sp, buf, v, count);
}
863
864
865
866
}
}
}
}