1 /*
2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 mix.c
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "timidity.h"
31 #include "common.h"
32 #include "instrum.h"
33 #include "playmidi.h"
34 #include "output.h"
35 #include "controls.h"
36 #include "tables.h"
37 #include "resample.h"
38 #include "mix.h"
39
40 #ifdef SMOOTH_MIXING
41 #ifdef LOOKUP_HACK
42 /* u2l: 0..255 -> -32256..32256
43 * shift 3 bit: -> within MAX_AMP_VALUE
44 */
45 #define FROM_FINAL_VOLUME(a) (_u2l[(uint8) ~(a)] >> 3)
46 #else
47 #define FROM_FINAL_VOLUME(a) (a)
48 #endif
49 #endif
50
51 #define OFFSET_MAX (0x3FFFFFFFL)
52
53 #if OPT_MODE != 0
54 #define VOICE_LPF
55 #endif
56
57 typedef int32 mix_t;
58
59 #ifdef LOOKUP_HACK
60 #define MIXATION(a) *lp++ += mixup[(a << 8) | (uint8) s]
61 #else
62 #define MIXATION(a) *lp++ += (a) * s
63 #endif
64
65 #define DELAYED_MIXATION(a) *lp++ += pan_delay_buf[pan_delay_spt]; \
66 if (++pan_delay_spt == PAN_DELAY_BUF_MAX) {pan_delay_spt = 0;} \
67 pan_delay_buf[pan_delay_wpt] = (a) * s; \
68 if (++pan_delay_wpt == PAN_DELAY_BUF_MAX) {pan_delay_wpt = 0;}
69
70 void mix_voice(int32 *, int, int32);
71 static inline int do_voice_filter(int, resample_t*, mix_t*, int32);
72 static inline void recalc_voice_resonance(int);
73 static inline void recalc_voice_fc(int);
74 static inline void ramp_out(mix_t *, int32 *, int, int32);
75 static inline void mix_mono_signal(mix_t *, int32 *, int, int);
76 static inline void mix_mono(mix_t *, int32 *, int, int);
77 static inline void mix_mystery_signal(mix_t *, int32 *, int, int);
78 static inline void mix_mystery(mix_t *, int32 *, int, int);
79 static inline void mix_center_signal(mix_t *, int32 *, int, int);
80 static inline void mix_center(mix_t *, int32 *, int, int);
81 static inline void mix_single_signal(mix_t *, int32 *, int, int);
82 static inline void mix_single(mix_t *, int32 *, int, int);
83 static inline int update_signal(int);
84 static inline int update_envelope(int);
85 int recompute_envelope(int);
86 static inline int update_modulation_envelope(int);
87 int apply_modulation_envelope(int);
88 int recompute_modulation_envelope(int);
89 static inline void voice_ran_out(int);
90 static inline int next_stage(int);
91 static inline int modenv_next_stage(int);
92 static inline void update_tremolo(int);
93 int apply_envelope_to_amp(int);
94 #ifdef SMOOTH_MIXING
95 static inline void compute_mix_smoothing(Voice *);
96 #endif
97
98 int min_sustain_time = 5000;
99
100 static mix_t filter_buffer[AUDIO_BUFFER_SIZE];
101
102 /**************** interface function ****************/
mix_voice(int32 * buf,int v,int32 c)103 void mix_voice(int32 *buf, int v, int32 c)
104 {
105 Voice *vp = voice + v;
106 resample_t *sp;
107
108 if (vp->status == VOICE_DIE) {
109 if (c >= MAX_DIE_TIME)
110 c = MAX_DIE_TIME;
111 sp = resample_voice(v, &c);
112 if (do_voice_filter(v, sp, filter_buffer, c)) {sp = filter_buffer;}
113 if (c > 0)
114 ramp_out(sp, buf, v, c);
115 free_voice(v);
116 } else {
117 vp->delay_counter = c;
118 if (vp->delay) {
119 if (c < vp->delay) {
120 vp->delay -= c;
121 if (vp->tremolo_phase_increment)
122 update_tremolo(v);
123 if (opt_modulation_envelope && vp->sample->modes & MODES_ENVELOPE)
124 update_modulation_envelope(v);
125 return;
126 }
127 if (play_mode->encoding & PE_MONO)
128 buf += vp->delay;
129 else
130 buf += vp->delay * 2;
131 c -= vp->delay;
132 vp->delay = 0;
133 }
134 sp = resample_voice(v, &c);
135 if (do_voice_filter(v, sp, filter_buffer, c)) {sp = filter_buffer;}
136
137 if (play_mode->encoding & PE_MONO) {
138 /* Mono output. */
139 if (vp->envelope_increment || vp->tremolo_phase_increment)
140 mix_mono_signal(sp, buf, v, c);
141 else
142 mix_mono(sp, buf, v, c);
143 } else {
144 if (vp->panned == PANNED_MYSTERY) {
145 if (vp->envelope_increment || vp->tremolo_phase_increment)
146 mix_mystery_signal(sp, buf, v, c);
147 else
148 mix_mystery(sp, buf, v, c);
149 } else if (vp->panned == PANNED_CENTER) {
150 if (vp->envelope_increment || vp->tremolo_phase_increment)
151 mix_center_signal(sp, buf, v, c);
152 else
153 mix_center(sp, buf, v, c);
154 } else {
155 /* It's either full left or full right. In either case,
156 * every other sample is 0. Just get the offset right:
157 */
158 if (vp->panned == PANNED_RIGHT)
159 buf++;
160 if (vp->envelope_increment || vp->tremolo_phase_increment)
161 mix_single_signal(sp, buf, v, c);
162 else
163 mix_single(sp, buf, v, c);
164 }
165 }
166 }
167 }
168
169 /* return 1 if filter is enabled. */
170 #ifdef __BORLANDC__
do_voice_filter(int v,resample_t * sp,mix_t * lp,int32 count)171 static int do_voice_filter(int v, resample_t *sp, mix_t *lp, int32 count)
172 #else
173 static inline int do_voice_filter(int v, resample_t *sp, mix_t *lp, int32 count)
174 #endif
175 {
176 FilterCoefficients *fc = &(voice[v].fc);
177 int32 i, f, q, p, b0, b1, b2, b3, b4, t1, t2, x;
178
179 if (fc->type == 1) { /* copy with applying Chamberlin's lowpass filter. */
180 recalc_voice_resonance(v);
181 recalc_voice_fc(v);
182 f = fc->f, q = fc->q, b0 = fc->b0, b1 = fc->b1, b2 = fc->b2;
183 for(i = 0; i < count; i++) {
184 b0 = b0 + imuldiv24(b2, f);
185 b1 = sp[i] - b0 - imuldiv24(b2, q);
186 b2 = imuldiv24(b1, f) + b2;
187 lp[i] = b0;
188 }
189 fc->b0 = b0, fc->b1 = b1, fc->b2 = b2;
190 return 1;
191 } else if(fc->type == 2) { /* copy with applying Moog lowpass VCF. */
192 recalc_voice_resonance(v);
193 recalc_voice_fc(v);
194 f = fc->f, q = fc->q, p = fc->p, b0 = fc->b0, b1 = fc->b1,
195 b2 = fc->b2, b3 = fc->b3, b4 = fc->b4;
196 for(i = 0; i < count; i++) {
197 x = sp[i] - imuldiv24(q, b4); /* feedback */
198 t1 = b1; b1 = imuldiv24(x + b0, p) - imuldiv24(b1, f);
199 t2 = b2; b2 = imuldiv24(b1 + t1, p) - imuldiv24(b2, f);
200 t1 = b3; b3 = imuldiv24(b2 + t2, p) - imuldiv24(b3, f);
201 lp[i] = b4 = imuldiv24(b3 + t1, p) - imuldiv24(b4, f);
202 b0 = x;
203 }
204 fc->b0 = b0, fc->b1 = b1, fc->b2 = b2, fc->b3 = b3, fc->b4 = b4;
205 return 1;
206 } else {
207 return 0;
208 }
209 }
210
211 //#define MOOG_RESONANCE_MAX 0.897638f
212 #define MOOG_RESONANCE_MAX 0.88f
213
recalc_voice_resonance(int v)214 static inline void recalc_voice_resonance(int v)
215 {
216 double q;
217 FilterCoefficients *fc = &(voice[v].fc);
218
219 if (fc->reso_dB != fc->last_reso_dB || fc->q == 0) {
220 fc->last_reso_dB = fc->reso_dB;
221 if(fc->type == 1) {
222 q = 1.0 / chamberlin_filter_db_to_q_table[(int)(fc->reso_dB * 4)];
223 fc->q = TIM_FSCALE(q, 24);
224 if(fc->q <= 0) {fc->q = 1;} /* must never be 0. */
225 } else if(fc->type == 2) {
226 fc->reso_lin = fc->reso_dB * MOOG_RESONANCE_MAX / 20.0f;
227 if (fc->reso_lin > MOOG_RESONANCE_MAX) {fc->reso_lin = MOOG_RESONANCE_MAX;}
228 else if(fc->reso_lin < 0) {fc->reso_lin = 0;}
229 }
230 fc->last_freq = -1;
231 }
232 }
233
recalc_voice_fc(int v)234 static inline void recalc_voice_fc(int v)
235 {
236 double f, p, q, fr;
237 FilterCoefficients *fc = &(voice[v].fc);
238
239 if (fc->freq != fc->last_freq) {
240 if(fc->type == 1) {
241 f = 2.0 * sin(M_PI * (double)fc->freq / (double)play_mode->rate);
242 fc->f = TIM_FSCALE(f, 24);
243 } else if(fc->type == 2) {
244 fr = 2.0 * (double)fc->freq / (double)play_mode->rate;
245 q = 1.0 - fr;
246 p = fr + 0.8 * fr * q;
247 f = p + p - 1.0;
248 q = fc->reso_lin * (1.0 + 0.5 * q * (1.0 - q + 5.6 * q * q));
249 fc->f = TIM_FSCALE(f, 24);
250 fc->p = TIM_FSCALE(p, 24);
251 fc->q = TIM_FSCALE(q, 24);
252 }
253 fc->last_freq = fc->freq;
254 }
255 }
256
257 /* Ramp a note out in c samples */
258 #ifdef __BORLANDC__
ramp_out(mix_t * sp,int32 * lp,int v,int32 c)259 static void ramp_out(mix_t *sp, int32 *lp, int v, int32 c)
260 #else
261 static inline void ramp_out(mix_t *sp, int32 *lp, int v, int32 c)
262 #endif
263 {
264 /* should be final_volume_t, but uint8 gives trouble. */
265 int32 left, right, li, ri, i;
266 /* silly warning about uninitialized s */
267 mix_t s = 0;
268 #ifdef ENABLE_PAN_DELAY
269 Voice *vp = &voice[v];
270 int32 pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
271 pan_delay_spt = vp->pan_delay_spt;
272 #endif
273
274 left = voice[v].left_mix;
275 li = -(left / c);
276 if (! li)
277 li = -1;
278 #if 0
279 printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li);
280 #endif
281 if (! (play_mode->encoding & PE_MONO)) {
282 if (voice[v].panned == PANNED_MYSTERY) {
283 #ifdef ENABLE_PAN_DELAY
284 right = voice[v].right_mix;
285 ri = -(right / c);
286 if(vp->pan_delay_rpt == 0) {
287 for (i = 0; i < c; i++) {
288 left += li;
289 if (left < 0)
290 left = 0;
291 right += ri;
292 if (right < 0)
293 right = 0;
294 s = *sp++;
295 MIXATION(left);
296 MIXATION(right);
297 }
298 } else if(vp->panning < 64) {
299 for (i = 0; i < c; i++) {
300 left += li;
301 if (left < 0)
302 left = 0;
303 right += ri;
304 if (right < 0)
305 right = 0;
306 s = *sp++;
307 MIXATION(left);
308 DELAYED_MIXATION(right);
309 }
310 } else {
311 for (i = 0; i < c; i++) {
312 left += li;
313 if (left < 0)
314 left = 0;
315 right += ri;
316 if (right < 0)
317 right = 0;
318 s = *sp++;
319 DELAYED_MIXATION(left);
320 MIXATION(right);
321 }
322 }
323 vp->pan_delay_wpt = pan_delay_wpt;
324 vp->pan_delay_spt = pan_delay_spt;
325 #else
326 right = voice[v].right_mix;
327 ri = -(right / c);
328 for (i = 0; i < c; i++) {
329 left += li;
330 if (left < 0)
331 left = 0;
332 right += ri;
333 if (right < 0)
334 right = 0;
335 s = *sp++;
336 MIXATION(left);
337 MIXATION(right);
338 }
339 #endif /* ENABLE_PAN_DELAY */
340 } else if (voice[v].panned == PANNED_CENTER)
341 for (i = 0; i < c; i++) {
342 left += li;
343 if (left < 0)
344 return;
345 s = *sp++;
346 MIXATION(left);
347 MIXATION(left);
348 }
349 else if (voice[v].panned == PANNED_LEFT)
350 for (i = 0; i < c; i++) {
351 left += li;
352 if (left < 0)
353 return;
354 s = *sp++;
355 MIXATION(left);
356 lp++;
357 }
358 else if (voice[v].panned == PANNED_RIGHT)
359 for (i = 0; i < c; i++) {
360 left += li;
361 if (left < 0)
362 return;
363 s = *sp++;
364 lp++;
365 MIXATION(left);
366 }
367 } else
368 /* Mono output. */
369 for (i = 0; i < c; i++) {
370 left += li;
371 if (left < 0)
372 return;
373 s = *sp++;
374 MIXATION(left);
375 }
376 }
377
378 #ifdef __BORLANDC__
mix_mono_signal(mix_t * sp,int32 * lp,int v,int count)379 static void mix_mono_signal(
380 mix_t *sp, int32 *lp, int v, int count)
381 #else
382 static inline void mix_mono_signal(
383 mix_t *sp, int32 *lp, int v, int count)
384 #endif
385 {
386 Voice *vp = voice + v;
387 final_volume_t left = vp->left_mix;
388 int cc, i;
389 mix_t s;
390 #ifdef SMOOTH_MIXING
391 int32 linear_left;
392 #endif
393
394 if (! (cc = vp->control_counter)) {
395 cc = control_ratio;
396 if (update_signal(v))
397 /* Envelope ran out */
398 return;
399 left = vp->left_mix;
400 }
401 #ifdef SMOOTH_MIXING
402 compute_mix_smoothing(vp);
403 #endif
404 while (count)
405 if (cc < count) {
406 count -= cc;
407 #ifdef SMOOTH_MIXING
408 linear_left = FROM_FINAL_VOLUME(left);
409 if (vp->left_mix_offset) {
410 linear_left += vp->left_mix_offset;
411 if (linear_left > MAX_AMP_VALUE) {
412 linear_left = MAX_AMP_VALUE;
413 vp->left_mix_offset = 0;
414 }
415 left = FINAL_VOLUME(linear_left);
416 }
417 for (i = 0; vp->left_mix_offset && i < cc; i++) {
418 s = *sp++;
419 MIXATION(left);
420 vp->left_mix_offset += vp->left_mix_inc;
421 linear_left += vp->left_mix_inc;
422 if (linear_left > MAX_AMP_VALUE) {
423 linear_left = MAX_AMP_VALUE;
424 vp->left_mix_offset = 0;
425 }
426 left = FINAL_VOLUME(linear_left);
427 }
428 vp->old_left_mix = linear_left;
429 cc -= i;
430 #endif
431 for (i = 0; i < cc; i++) {
432 s = *sp++;
433 MIXATION(left);
434 }
435 cc = control_ratio;
436 if (update_signal(v))
437 /* Envelope ran out */
438 return;
439 left = vp->left_mix;
440 #ifdef SMOOTH_MIXING
441 compute_mix_smoothing(vp);
442 #endif
443 } else {
444 vp->control_counter = cc - count;
445 #ifdef SMOOTH_MIXING
446 linear_left = FROM_FINAL_VOLUME(left);
447 if (vp->left_mix_offset) {
448 linear_left += vp->left_mix_offset;
449 if (linear_left > MAX_AMP_VALUE) {
450 linear_left = MAX_AMP_VALUE;
451 vp->left_mix_offset = 0;
452 }
453 left = FINAL_VOLUME(linear_left);
454 }
455 for (i = 0; vp->left_mix_offset && i < count; i++) {
456 s = *sp++;
457 MIXATION(left);
458 vp->left_mix_offset += vp->left_mix_inc;
459 linear_left += vp->left_mix_inc;
460 if (linear_left > MAX_AMP_VALUE) {
461 linear_left = MAX_AMP_VALUE;
462 vp->left_mix_offset = 0;
463 }
464 left = FINAL_VOLUME(linear_left);
465 }
466 vp->old_left_mix = linear_left;
467 count -= i;
468 #endif
469 for (i = 0; i < count; i++) {
470 s = *sp++;
471 MIXATION(left);
472 }
473 return;
474 }
475 }
476
477 #ifdef __BORLANDC__
mix_mono(mix_t * sp,int32 * lp,int v,int count)478 static void mix_mono(mix_t *sp, int32 *lp, int v, int count)
479 #else
480 static inline void mix_mono(mix_t *sp, int32 *lp, int v, int count)
481 #endif
482 {
483 final_volume_t left = voice[v].left_mix;
484 mix_t s;
485 int i;
486 #ifdef SMOOTH_MIXING
487 Voice *vp = voice + v;
488 int32 linear_left;
489 #endif
490
491 #ifdef SMOOTH_MIXING
492 compute_mix_smoothing(vp);
493 linear_left = FROM_FINAL_VOLUME(left);
494 if (vp->left_mix_offset) {
495 linear_left += vp->left_mix_offset;
496 if (linear_left > MAX_AMP_VALUE) {
497 linear_left = MAX_AMP_VALUE;
498 vp->left_mix_offset = 0;
499 }
500 left = FINAL_VOLUME(linear_left);
501 }
502 for (i = 0; vp->left_mix_offset && i < count; i++) {
503 s = *sp++;
504 MIXATION(left);
505 MIXATION(left);
506 vp->left_mix_offset += vp->left_mix_inc;
507 linear_left += vp->left_mix_inc;
508 if (linear_left > MAX_AMP_VALUE) {
509 linear_left = MAX_AMP_VALUE;
510 vp->left_mix_offset = 0;
511 }
512 left = FINAL_VOLUME(linear_left);
513 }
514 vp->old_left_mix = linear_left;
515 count -= i;
516 #endif
517 for (i = 0; i < count; i++) {
518 s = *sp++;
519 MIXATION(left);
520 }
521 }
522
523 #ifdef ENABLE_PAN_DELAY
524 #ifdef __BORLANDC__
mix_mystery_signal(mix_t * sp,int32 * lp,int v,int count)525 static void mix_mystery_signal(
526 mix_t *sp, int32 *lp, int v, int count)
527 #else
528 static inline void mix_mystery_signal(
529 mix_t *sp, int32 *lp, int v, int count)
530 #endif
531 {
532 Voice *vp = voice + v;
533 final_volume_t left = vp->left_mix, right = vp->right_mix;
534 int cc, i;
535 mix_t s;
536 #ifdef SMOOTH_MIXING
537 int32 linear_left, linear_right;
538 #endif
539 int32 pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
540 pan_delay_spt = vp->pan_delay_spt;
541
542 if (! (cc = vp->control_counter)) {
543 cc = control_ratio;
544 if (update_signal(v))
545 /* Envelope ran out */
546 return;
547 left = vp->left_mix;
548 right = vp->right_mix;
549 }
550 #ifdef SMOOTH_MIXING
551 compute_mix_smoothing(vp);
552 #endif
553 while (count)
554 if (cc < count) {
555 count -= cc;
556 #ifdef SMOOTH_MIXING
557 linear_left = FROM_FINAL_VOLUME(left);
558 if (vp->left_mix_offset) {
559 linear_left += vp->left_mix_offset;
560 if (linear_left > MAX_AMP_VALUE) {
561 linear_left = MAX_AMP_VALUE;
562 vp->left_mix_offset = 0;
563 }
564 left = FINAL_VOLUME(linear_left);
565 }
566 linear_right = FROM_FINAL_VOLUME(right);
567 if (vp->right_mix_offset) {
568 linear_right += vp->right_mix_offset;
569 if (linear_right > MAX_AMP_VALUE) {
570 linear_right = MAX_AMP_VALUE;
571 vp->right_mix_offset = 0;
572 }
573 right = FINAL_VOLUME(linear_right);
574 }
575 if(vp->pan_delay_rpt == 0) {
576 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
577 && i < cc; i++) {
578 s = *sp++;
579 MIXATION(left);
580 MIXATION(right);
581 if (vp->left_mix_offset) {
582 vp->left_mix_offset += vp->left_mix_inc;
583 linear_left += vp->left_mix_inc;
584 if (linear_left > MAX_AMP_VALUE) {
585 linear_left = MAX_AMP_VALUE;
586 vp->left_mix_offset = 0;
587 }
588 left = FINAL_VOLUME(linear_left);
589 }
590 if (vp->right_mix_offset) {
591 vp->right_mix_offset += vp->right_mix_inc;
592 linear_right += vp->right_mix_inc;
593 if (linear_right > MAX_AMP_VALUE) {
594 linear_right = MAX_AMP_VALUE;
595 vp->right_mix_offset = 0;
596 }
597 right = FINAL_VOLUME(linear_right);
598 }
599 }
600 } else if(vp->panning < 64) {
601 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
602 && i < cc; i++) {
603 s = *sp++;
604 MIXATION(left);
605 DELAYED_MIXATION(right);
606 if (vp->left_mix_offset) {
607 vp->left_mix_offset += vp->left_mix_inc;
608 linear_left += vp->left_mix_inc;
609 if (linear_left > MAX_AMP_VALUE) {
610 linear_left = MAX_AMP_VALUE;
611 vp->left_mix_offset = 0;
612 }
613 left = FINAL_VOLUME(linear_left);
614 }
615 if (vp->right_mix_offset) {
616 vp->right_mix_offset += vp->right_mix_inc;
617 linear_right += vp->right_mix_inc;
618 if (linear_right > MAX_AMP_VALUE) {
619 linear_right = MAX_AMP_VALUE;
620 vp->right_mix_offset = 0;
621 }
622 right = FINAL_VOLUME(linear_right);
623 }
624 }
625 } else {
626 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
627 && i < cc; i++) {
628 s = *sp++;
629 DELAYED_MIXATION(left);
630 MIXATION(right);
631 if (vp->left_mix_offset) {
632 vp->left_mix_offset += vp->left_mix_inc;
633 linear_left += vp->left_mix_inc;
634 if (linear_left > MAX_AMP_VALUE) {
635 linear_left = MAX_AMP_VALUE;
636 vp->left_mix_offset = 0;
637 }
638 left = FINAL_VOLUME(linear_left);
639 }
640 if (vp->right_mix_offset) {
641 vp->right_mix_offset += vp->right_mix_inc;
642 linear_right += vp->right_mix_inc;
643 if (linear_right > MAX_AMP_VALUE) {
644 linear_right = MAX_AMP_VALUE;
645 vp->right_mix_offset = 0;
646 }
647 right = FINAL_VOLUME(linear_right);
648 }
649 }
650 }
651 vp->old_left_mix = linear_left;
652 vp->old_right_mix = linear_right;
653 cc -= i;
654 #endif
655 if(vp->pan_delay_rpt == 0) {
656 for (i = 0; i < cc; i++) {
657 s = *sp++;
658 MIXATION(left);
659 MIXATION(right);
660 }
661 } else if(vp->panning < 64) {
662 for (i = 0; i < cc; i++) {
663 s = *sp++;
664 MIXATION(left);
665 DELAYED_MIXATION(right);
666 }
667 } else {
668 for (i = 0; i < cc; i++) {
669 s = *sp++;
670 DELAYED_MIXATION(left);
671 MIXATION(right);
672 }
673 }
674
675 cc = control_ratio;
676 if (update_signal(v))
677 /* Envelope ran out */
678 return;
679 left = vp->left_mix;
680 right = vp->right_mix;
681 #ifdef SMOOTH_MIXING
682 compute_mix_smoothing(vp);
683 #endif
684 } else {
685 vp->control_counter = cc - count;
686 #ifdef SMOOTH_MIXING
687 linear_left = FROM_FINAL_VOLUME(left);
688 if (vp->left_mix_offset) {
689 linear_left += vp->left_mix_offset;
690 if (linear_left > MAX_AMP_VALUE) {
691 linear_left = MAX_AMP_VALUE;
692 vp->left_mix_offset = 0;
693 }
694 left = FINAL_VOLUME(linear_left);
695 }
696 linear_right = FROM_FINAL_VOLUME(right);
697 if (vp->right_mix_offset) {
698 linear_right += vp->right_mix_offset;
699 if (linear_right > MAX_AMP_VALUE) {
700 linear_right = MAX_AMP_VALUE;
701 vp->right_mix_offset = 0;
702 }
703 right = FINAL_VOLUME(linear_right);
704 }
705 if(vp->pan_delay_rpt == 0) {
706 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
707 && i < count; i++) {
708 s = *sp++;
709 MIXATION(left);
710 MIXATION(right);
711 if (vp->left_mix_offset) {
712 vp->left_mix_offset += vp->left_mix_inc;
713 linear_left += vp->left_mix_inc;
714 if (linear_left > MAX_AMP_VALUE) {
715 linear_left = MAX_AMP_VALUE;
716 vp->left_mix_offset = 0;
717 }
718 left = FINAL_VOLUME(linear_left);
719 }
720 if (vp->right_mix_offset) {
721 vp->right_mix_offset += vp->right_mix_inc;
722 linear_right += vp->right_mix_inc;
723 if (linear_right > MAX_AMP_VALUE) {
724 linear_right = MAX_AMP_VALUE;
725 vp->right_mix_offset = 0;
726 }
727 right = FINAL_VOLUME(linear_right);
728 }
729 }
730 } else if(vp->panning < 64) {
731 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
732 && i < count; i++) {
733 s = *sp++;
734 MIXATION(left);
735 DELAYED_MIXATION(right);
736 if (vp->left_mix_offset) {
737 vp->left_mix_offset += vp->left_mix_inc;
738 linear_left += vp->left_mix_inc;
739 if (linear_left > MAX_AMP_VALUE) {
740 linear_left = MAX_AMP_VALUE;
741 vp->left_mix_offset = 0;
742 }
743 left = FINAL_VOLUME(linear_left);
744 }
745 if (vp->right_mix_offset) {
746 vp->right_mix_offset += vp->right_mix_inc;
747 linear_right += vp->right_mix_inc;
748 if (linear_right > MAX_AMP_VALUE) {
749 linear_right = MAX_AMP_VALUE;
750 vp->right_mix_offset = 0;
751 }
752 right = FINAL_VOLUME(linear_right);
753 }
754 }
755 } else {
756 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
757 && i < count; i++) {
758 s = *sp++;
759 DELAYED_MIXATION(left);
760 MIXATION(right);
761 if (vp->left_mix_offset) {
762 vp->left_mix_offset += vp->left_mix_inc;
763 linear_left += vp->left_mix_inc;
764 if (linear_left > MAX_AMP_VALUE) {
765 linear_left = MAX_AMP_VALUE;
766 vp->left_mix_offset = 0;
767 }
768 left = FINAL_VOLUME(linear_left);
769 }
770 if (vp->right_mix_offset) {
771 vp->right_mix_offset += vp->right_mix_inc;
772 linear_right += vp->right_mix_inc;
773 if (linear_right > MAX_AMP_VALUE) {
774 linear_right = MAX_AMP_VALUE;
775 vp->right_mix_offset = 0;
776 }
777 right = FINAL_VOLUME(linear_right);
778 }
779 }
780 }
781
782 vp->old_left_mix = linear_left;
783 vp->old_right_mix = linear_right;
784 count -= i;
785 #endif
786 if(vp->pan_delay_rpt == 0) {
787 for (i = 0; i < count; i++) {
788 s = *sp++;
789 MIXATION(left);
790 MIXATION(right);
791 }
792 } else if(vp->panning < 64) {
793 for (i = 0; i < count; i++) {
794 s = *sp++;
795 MIXATION(left);
796 DELAYED_MIXATION(right);
797 }
798 } else {
799 for (i = 0; i < count; i++) {
800 s = *sp++;
801 DELAYED_MIXATION(left);
802 MIXATION(right);
803 }
804 }
805 vp->pan_delay_wpt = pan_delay_wpt;
806 vp->pan_delay_spt = pan_delay_spt;
807 return;
808 }
809 }
810 #else /* ENABLE_PAN_DELAY */
mix_mystery_signal(mix_t * sp,int32 * lp,int v,int count)811 static inline void mix_mystery_signal(
812 mix_t *sp, int32 *lp, int v, int count)
813 {
814 Voice *vp = voice + v;
815 final_volume_t left = vp->left_mix, right = vp->right_mix;
816 int cc, i;
817 mix_t s;
818 #ifdef SMOOTH_MIXING
819 int32 linear_left, linear_right;
820 #endif
821
822 if (! (cc = vp->control_counter)) {
823 cc = control_ratio;
824 if (update_signal(v))
825 /* Envelope ran out */
826 return;
827 left = vp->left_mix;
828 right = vp->right_mix;
829 }
830 #ifdef SMOOTH_MIXING
831 compute_mix_smoothing(vp);
832 #endif
833 while (count)
834 if (cc < count) {
835 count -= cc;
836 #ifdef SMOOTH_MIXING
837 linear_left = FROM_FINAL_VOLUME(left);
838 if (vp->left_mix_offset) {
839 linear_left += vp->left_mix_offset;
840 if (linear_left > MAX_AMP_VALUE) {
841 linear_left = MAX_AMP_VALUE;
842 vp->left_mix_offset = 0;
843 }
844 left = FINAL_VOLUME(linear_left);
845 }
846 linear_right = FROM_FINAL_VOLUME(right);
847 if (vp->right_mix_offset) {
848 linear_right += vp->right_mix_offset;
849 if (linear_right > MAX_AMP_VALUE) {
850 linear_right = MAX_AMP_VALUE;
851 vp->right_mix_offset = 0;
852 }
853 right = FINAL_VOLUME(linear_right);
854 }
855 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
856 && i < cc; i++) {
857 s = *sp++;
858 MIXATION(left);
859 MIXATION(right);
860 if (vp->left_mix_offset) {
861 vp->left_mix_offset += vp->left_mix_inc;
862 linear_left += vp->left_mix_inc;
863 if (linear_left > MAX_AMP_VALUE) {
864 linear_left = MAX_AMP_VALUE;
865 vp->left_mix_offset = 0;
866 }
867 left = FINAL_VOLUME(linear_left);
868 }
869 if (vp->right_mix_offset) {
870 vp->right_mix_offset += vp->right_mix_inc;
871 linear_right += vp->right_mix_inc;
872 if (linear_right > MAX_AMP_VALUE) {
873 linear_right = MAX_AMP_VALUE;
874 vp->right_mix_offset = 0;
875 }
876 right = FINAL_VOLUME(linear_right);
877 }
878 }
879 vp->old_left_mix = linear_left;
880 vp->old_right_mix = linear_right;
881 cc -= i;
882 #endif
883 for (i = 0; i < cc; i++) {
884 s = *sp++;
885 MIXATION(left);
886 MIXATION(right);
887 }
888 cc = control_ratio;
889 if (update_signal(v))
890 /* Envelope ran out */
891 return;
892 left = vp->left_mix;
893 right = vp->right_mix;
894 #ifdef SMOOTH_MIXING
895 compute_mix_smoothing(vp);
896 #endif
897 } else {
898 vp->control_counter = cc - count;
899 #ifdef SMOOTH_MIXING
900 linear_left = FROM_FINAL_VOLUME(left);
901 if (vp->left_mix_offset) {
902 linear_left += vp->left_mix_offset;
903 if (linear_left > MAX_AMP_VALUE) {
904 linear_left = MAX_AMP_VALUE;
905 vp->left_mix_offset = 0;
906 }
907 left = FINAL_VOLUME(linear_left);
908 }
909 linear_right = FROM_FINAL_VOLUME(right);
910 if (vp->right_mix_offset) {
911 linear_right += vp->right_mix_offset;
912 if (linear_right > MAX_AMP_VALUE) {
913 linear_right = MAX_AMP_VALUE;
914 vp->right_mix_offset = 0;
915 }
916 right = FINAL_VOLUME(linear_right);
917 }
918 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
919 && i < count; i++) {
920 s = *sp++;
921 MIXATION(left);
922 MIXATION(right);
923 if (vp->left_mix_offset) {
924 vp->left_mix_offset += vp->left_mix_inc;
925 linear_left += vp->left_mix_inc;
926 if (linear_left > MAX_AMP_VALUE) {
927 linear_left = MAX_AMP_VALUE;
928 vp->left_mix_offset = 0;
929 }
930 left = FINAL_VOLUME(linear_left);
931 }
932 if (vp->right_mix_offset) {
933 vp->right_mix_offset += vp->right_mix_inc;
934 linear_right += vp->right_mix_inc;
935 if (linear_right > MAX_AMP_VALUE) {
936 linear_right = MAX_AMP_VALUE;
937 vp->right_mix_offset = 0;
938 }
939 right = FINAL_VOLUME(linear_right);
940 }
941 }
942 vp->old_left_mix = linear_left;
943 vp->old_right_mix = linear_right;
944 count -= i;
945 #endif
946 for (i = 0; i < count; i++) {
947 s = *sp++;
948 MIXATION(left);
949 MIXATION(right);
950 }
951 return;
952 }
953 }
954 #endif /* ENABLE_PAN_DELAY */
955
956 #ifdef ENABLE_PAN_DELAY
957 #ifdef __BORLANDC__
mix_mystery(mix_t * sp,int32 * lp,int v,int count)958 static void mix_mystery(mix_t *sp, int32 *lp, int v, int count)
959 #else
960 static inline void mix_mystery(mix_t *sp, int32 *lp, int v, int count)
961 #endif
962 {
963 final_volume_t left = voice[v].left_mix, right = voice[v].right_mix;
964 mix_t s;
965 int i;
966 #ifdef SMOOTH_MIXING
967 Voice *vp = voice + v;
968 int32 linear_left, linear_right;
969 #endif
970 int32 pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
971 pan_delay_spt = vp->pan_delay_spt;
972
973 #ifdef SMOOTH_MIXING
974 compute_mix_smoothing(vp);
975 linear_left = FROM_FINAL_VOLUME(left);
976 if (vp->left_mix_offset) {
977 linear_left += vp->left_mix_offset;
978 if (linear_left > MAX_AMP_VALUE) {
979 linear_left = MAX_AMP_VALUE;
980 vp->left_mix_offset = 0;
981 }
982 left = FINAL_VOLUME(linear_left);
983 }
984 linear_right = FROM_FINAL_VOLUME(right);
985 if (vp->right_mix_offset) {
986 linear_right += vp->right_mix_offset;
987 if (linear_right > MAX_AMP_VALUE) {
988 linear_right = MAX_AMP_VALUE;
989 vp->right_mix_offset = 0;
990 }
991 right = FINAL_VOLUME(linear_right);
992 }
993 if(vp->pan_delay_rpt == 0) {
994 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
995 && i < count; i++) {
996 s = *sp++;
997 MIXATION(left);
998 MIXATION(right);
999 if (vp->left_mix_offset) {
1000 vp->left_mix_offset += vp->left_mix_inc;
1001 linear_left += vp->left_mix_inc;
1002 if (linear_left > MAX_AMP_VALUE) {
1003 linear_left = MAX_AMP_VALUE;
1004 vp->left_mix_offset = 0;
1005 }
1006 left = FINAL_VOLUME(linear_left);
1007 }
1008 if (vp->right_mix_offset) {
1009 vp->right_mix_offset += vp->right_mix_inc;
1010 linear_right += vp->right_mix_inc;
1011 if (linear_right > MAX_AMP_VALUE) {
1012 linear_right = MAX_AMP_VALUE;
1013 vp->right_mix_offset = 0;
1014 }
1015 right = FINAL_VOLUME(linear_right);
1016 }
1017 }
1018 } else if(vp->panning < 64) {
1019 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
1020 && i < count; i++) {
1021 s = *sp++;
1022 MIXATION(left);
1023 DELAYED_MIXATION(right);
1024 if (vp->left_mix_offset) {
1025 vp->left_mix_offset += vp->left_mix_inc;
1026 linear_left += vp->left_mix_inc;
1027 if (linear_left > MAX_AMP_VALUE) {
1028 linear_left = MAX_AMP_VALUE;
1029 vp->left_mix_offset = 0;
1030 }
1031 left = FINAL_VOLUME(linear_left);
1032 }
1033 if (vp->right_mix_offset) {
1034 vp->right_mix_offset += vp->right_mix_inc;
1035 linear_right += vp->right_mix_inc;
1036 if (linear_right > MAX_AMP_VALUE) {
1037 linear_right = MAX_AMP_VALUE;
1038 vp->right_mix_offset = 0;
1039 }
1040 right = FINAL_VOLUME(linear_right);
1041 }
1042 }
1043 } else {
1044 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
1045 && i < count; i++) {
1046 s = *sp++;
1047 DELAYED_MIXATION(left);
1048 MIXATION(right);
1049 if (vp->left_mix_offset) {
1050 vp->left_mix_offset += vp->left_mix_inc;
1051 linear_left += vp->left_mix_inc;
1052 if (linear_left > MAX_AMP_VALUE) {
1053 linear_left = MAX_AMP_VALUE;
1054 vp->left_mix_offset = 0;
1055 }
1056 left = FINAL_VOLUME(linear_left);
1057 }
1058 if (vp->right_mix_offset) {
1059 vp->right_mix_offset += vp->right_mix_inc;
1060 linear_right += vp->right_mix_inc;
1061 if (linear_right > MAX_AMP_VALUE) {
1062 linear_right = MAX_AMP_VALUE;
1063 vp->right_mix_offset = 0;
1064 }
1065 right = FINAL_VOLUME(linear_right);
1066 }
1067 }
1068 }
1069
1070 vp->old_left_mix = linear_left;
1071 vp->old_right_mix = linear_right;
1072 count -= i;
1073 #endif
1074 if(vp->pan_delay_rpt == 0) {
1075 for (i = 0; i < count; i++) {
1076 s = *sp++;
1077 MIXATION(left);
1078 MIXATION(right);
1079 }
1080 } else if(vp->panning < 64) {
1081 for (i = 0; i < count; i++) {
1082 s = *sp++;
1083 MIXATION(left);
1084 DELAYED_MIXATION(right);
1085 }
1086 } else {
1087 for (i = 0; i < count; i++) {
1088 s = *sp++;
1089 DELAYED_MIXATION(left);
1090 MIXATION(right);
1091 }
1092 }
1093 vp->pan_delay_wpt = pan_delay_wpt;
1094 vp->pan_delay_spt = pan_delay_spt;
1095 }
1096 #else /* ENABLE_PAN_DELAY */
mix_mystery(mix_t * sp,int32 * lp,int v,int count)1097 static inline void mix_mystery(mix_t *sp, int32 *lp, int v, int count)
1098 {
1099 final_volume_t left = voice[v].left_mix, right = voice[v].right_mix;
1100 mix_t s;
1101 int i;
1102 #ifdef SMOOTH_MIXING
1103 Voice *vp = voice + v;
1104 int32 linear_left, linear_right;
1105 #endif
1106
1107 #ifdef SMOOTH_MIXING
1108 compute_mix_smoothing(vp);
1109 linear_left = FROM_FINAL_VOLUME(left);
1110 if (vp->left_mix_offset) {
1111 linear_left += vp->left_mix_offset;
1112 if (linear_left > MAX_AMP_VALUE) {
1113 linear_left = MAX_AMP_VALUE;
1114 vp->left_mix_offset = 0;
1115 }
1116 left = FINAL_VOLUME(linear_left);
1117 }
1118 linear_right = FROM_FINAL_VOLUME(right);
1119 if (vp->right_mix_offset) {
1120 linear_right += vp->right_mix_offset;
1121 if (linear_right > MAX_AMP_VALUE) {
1122 linear_right = MAX_AMP_VALUE;
1123 vp->right_mix_offset = 0;
1124 }
1125 right = FINAL_VOLUME(linear_right);
1126 }
1127 for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
1128 && i < count; i++) {
1129 s = *sp++;
1130 MIXATION(left);
1131 MIXATION(right);
1132 if (vp->left_mix_offset) {
1133 vp->left_mix_offset += vp->left_mix_inc;
1134 linear_left += vp->left_mix_inc;
1135 if (linear_left > MAX_AMP_VALUE) {
1136 linear_left = MAX_AMP_VALUE;
1137 vp->left_mix_offset = 0;
1138 }
1139 left = FINAL_VOLUME(linear_left);
1140 }
1141 if (vp->right_mix_offset) {
1142 vp->right_mix_offset += vp->right_mix_inc;
1143 linear_right += vp->right_mix_inc;
1144 if (linear_right > MAX_AMP_VALUE) {
1145 linear_right = MAX_AMP_VALUE;
1146 vp->right_mix_offset = 0;
1147 }
1148 right = FINAL_VOLUME(linear_right);
1149 }
1150 }
1151 vp->old_left_mix = linear_left;
1152 vp->old_right_mix = linear_right;
1153 count -= i;
1154 #endif
1155 for (i = 0; i < count; i++) {
1156 s = *sp++;
1157 MIXATION(left);
1158 MIXATION(right);
1159 }
1160 }
1161 #endif /* ENABLE_PAN_DELAY */
1162
1163
1164 #ifdef __BORLANDC__
mix_center_signal(mix_t * sp,int32 * lp,int v,int count)1165 static void mix_center_signal(
1166 mix_t *sp, int32 *lp, int v, int count)
1167 #else
1168 static inline void mix_center_signal(
1169 mix_t *sp, int32 *lp, int v, int count)
1170 #endif
1171 {
1172 Voice *vp = voice + v;
1173 final_volume_t left=vp->left_mix;
1174 int cc, i;
1175 mix_t s;
1176 #ifdef SMOOTH_MIXING
1177 int32 linear_left;
1178 #endif
1179
1180 if (! (cc = vp->control_counter)) {
1181 cc = control_ratio;
1182 if (update_signal(v))
1183 /* Envelope ran out */
1184 return;
1185 left = vp->left_mix;
1186 }
1187 #ifdef SMOOTH_MIXING
1188 compute_mix_smoothing(vp);
1189 #endif
1190 while (count)
1191 if (cc < count) {
1192 count -= cc;
1193 #ifdef SMOOTH_MIXING
1194 linear_left = FROM_FINAL_VOLUME(left);
1195 if (vp->left_mix_offset) {
1196 linear_left += vp->left_mix_offset;
1197 if (linear_left > MAX_AMP_VALUE) {
1198 linear_left = MAX_AMP_VALUE;
1199 vp->left_mix_offset = 0;
1200 }
1201 left = FINAL_VOLUME(linear_left);
1202 }
1203 for (i = 0; vp->left_mix_offset && i < cc; i++) {
1204 s = *sp++;
1205 MIXATION(left);
1206 MIXATION(left);
1207 vp->left_mix_offset += vp->left_mix_inc;
1208 linear_left += vp->left_mix_inc;
1209 if (linear_left > MAX_AMP_VALUE) {
1210 linear_left = MAX_AMP_VALUE;
1211 vp->left_mix_offset = 0;
1212 }
1213 left = FINAL_VOLUME(linear_left);
1214 }
1215 vp->old_left_mix = vp->old_right_mix = linear_left;
1216 cc -= i;
1217 #endif
1218 for (i = 0; i < cc; i++) {
1219 s = *sp++;
1220 MIXATION(left);
1221 MIXATION(left);
1222 }
1223 cc = control_ratio;
1224 if (update_signal(v))
1225 /* Envelope ran out */
1226 return;
1227 left = vp->left_mix;
1228 #ifdef SMOOTH_MIXING
1229 compute_mix_smoothing(vp);
1230 #endif
1231 } else {
1232 vp->control_counter = cc - count;
1233 #ifdef SMOOTH_MIXING
1234 linear_left = FROM_FINAL_VOLUME(left);
1235 if (vp->left_mix_offset) {
1236 linear_left += vp->left_mix_offset;
1237 if (linear_left > MAX_AMP_VALUE) {
1238 linear_left = MAX_AMP_VALUE;
1239 vp->left_mix_offset = 0;
1240 }
1241 left = FINAL_VOLUME(linear_left);
1242 }
1243 for (i = 0; vp->left_mix_offset && i < count; i++) {
1244 s = *sp++;
1245 MIXATION(left);
1246 MIXATION(left);
1247 vp->left_mix_offset += vp->left_mix_inc;
1248 linear_left += vp->left_mix_inc;
1249 if (linear_left > MAX_AMP_VALUE) {
1250 linear_left = MAX_AMP_VALUE;
1251 vp->left_mix_offset = 0;
1252 }
1253 left = FINAL_VOLUME(linear_left);
1254 }
1255 vp->old_left_mix = vp->old_right_mix = linear_left;
1256 count -= i;
1257 #endif
1258 for (i = 0; i < count; i++) {
1259 s = *sp++;
1260 MIXATION(left);
1261 MIXATION(left);
1262 }
1263 return;
1264 }
1265 }
1266
1267 #ifdef __BORLANDC__
mix_center(mix_t * sp,int32 * lp,int v,int count)1268 static void mix_center(mix_t *sp, int32 *lp, int v, int count)
1269 #else
1270 static inline void mix_center(mix_t *sp, int32 *lp, int v, int count)
1271 #endif
1272 {
1273 final_volume_t left = voice[v].left_mix;
1274 mix_t s;
1275 int i;
1276 #ifdef SMOOTH_MIXING
1277 Voice *vp = voice + v;
1278 int32 linear_left;
1279 #endif
1280
1281 #ifdef SMOOTH_MIXING
1282 compute_mix_smoothing(vp);
1283 linear_left = FROM_FINAL_VOLUME(left);
1284 if (vp->left_mix_offset) {
1285 linear_left += vp->left_mix_offset;
1286 if (linear_left > MAX_AMP_VALUE) {
1287 linear_left = MAX_AMP_VALUE;
1288 vp->left_mix_offset = 0;
1289 }
1290 left = FINAL_VOLUME(linear_left);
1291 }
1292 for (i = 0; vp->left_mix_offset && i < count; i++) {
1293 s = *sp++;
1294 MIXATION(left);
1295 MIXATION(left);
1296 vp->left_mix_offset += vp->left_mix_inc;
1297 linear_left += vp->left_mix_inc;
1298 if (linear_left > MAX_AMP_VALUE) {
1299 linear_left = MAX_AMP_VALUE;
1300 vp->left_mix_offset = 0;
1301 }
1302 left = FINAL_VOLUME(linear_left);
1303 }
1304 vp->old_left_mix = vp->old_right_mix = linear_left;
1305 count -= i;
1306 #endif
1307 for (i = 0; i < count; i++) {
1308 s = *sp++;
1309 MIXATION(left);
1310 MIXATION(left);
1311 }
1312 }
1313
1314 #ifdef __BORLANDC__
mix_single_signal(mix_t * sp,int32 * lp,int v,int count)1315 static void mix_single_signal(
1316 mix_t *sp, int32 *lp, int v, int count)
1317 #else
1318 static inline void mix_single_signal(
1319 mix_t *sp, int32 *lp, int v, int count)
1320 #endif
1321 {
1322 Voice *vp = voice + v;
1323 final_volume_t left = vp->left_mix;
1324 int cc, i;
1325 mix_t s;
1326 #ifdef SMOOTH_MIXING
1327 int32 linear_left;
1328 #endif
1329
1330 if (!(cc = vp->control_counter)) {
1331 cc = control_ratio;
1332 if (update_signal(v))
1333 /* Envelope ran out */
1334 return;
1335 left = vp->left_mix;
1336 }
1337 #ifdef SMOOTH_MIXING
1338 compute_mix_smoothing(vp);
1339 #endif
1340 while (count)
1341 if (cc < count) {
1342 count -= cc;
1343 #ifdef SMOOTH_MIXING
1344 linear_left = FROM_FINAL_VOLUME(left);
1345 if (vp->left_mix_offset) {
1346 linear_left += vp->left_mix_offset;
1347 if (linear_left > MAX_AMP_VALUE) {
1348 linear_left = MAX_AMP_VALUE;
1349 vp->left_mix_offset = 0;
1350 }
1351 left = FINAL_VOLUME(linear_left);
1352 }
1353 for (i = 0; vp->left_mix_offset && i < cc; i++) {
1354 s = *sp++;
1355 MIXATION(left);
1356 lp++;
1357 vp->left_mix_offset += vp->left_mix_inc;
1358 linear_left += vp->left_mix_inc;
1359 if (linear_left > MAX_AMP_VALUE) {
1360 linear_left = MAX_AMP_VALUE;
1361 vp->left_mix_offset = 0;
1362 }
1363 left = FINAL_VOLUME(linear_left);
1364 }
1365 vp->old_left_mix = linear_left;
1366 cc -= i;
1367 #endif
1368 for (i = 0; i < cc; i++) {
1369 s = *sp++;
1370 MIXATION(left);
1371 lp++;
1372 }
1373 cc = control_ratio;
1374 if (update_signal(v))
1375 /* Envelope ran out */
1376 return;
1377 left = vp->left_mix;
1378 #ifdef SMOOTH_MIXING
1379 compute_mix_smoothing(vp);
1380 #endif
1381 } else {
1382 vp->control_counter = cc - count;
1383 #ifdef SMOOTH_MIXING
1384 linear_left = FROM_FINAL_VOLUME(left);
1385 if (vp->left_mix_offset) {
1386 linear_left += vp->left_mix_offset;
1387 if (linear_left > MAX_AMP_VALUE) {
1388 linear_left = MAX_AMP_VALUE;
1389 vp->left_mix_offset = 0;
1390 }
1391 left = FINAL_VOLUME(linear_left);
1392 }
1393 for (i = 0; vp->left_mix_offset && i < count; i++) {
1394 s = *sp++;
1395 MIXATION(left);
1396 lp++;
1397 vp->left_mix_offset += vp->left_mix_inc;
1398 linear_left += vp->left_mix_inc;
1399 if (linear_left > MAX_AMP_VALUE) {
1400 linear_left = MAX_AMP_VALUE;
1401 vp->left_mix_offset = 0;
1402 }
1403 left = FINAL_VOLUME(linear_left);
1404 }
1405 vp->old_left_mix = linear_left;
1406 count -= i;
1407 #endif
1408 for (i = 0; i < count; i++) {
1409 s = *sp++;
1410 MIXATION(left);
1411 lp++;
1412 }
1413 return;
1414 }
1415 }
1416
1417 #ifdef __BORLANDC__
mix_single(mix_t * sp,int32 * lp,int v,int count)1418 static void mix_single(mix_t *sp, int32 *lp, int v, int count)
1419 #else
1420 static inline void mix_single(mix_t *sp, int32 *lp, int v, int count)
1421 #endif
1422 {
1423 final_volume_t left = voice[v].left_mix;
1424 mix_t s;
1425 int i;
1426 #ifdef SMOOTH_MIXING
1427 Voice *vp = voice + v;
1428 int32 linear_left;
1429 #endif
1430
1431 #ifdef SMOOTH_MIXING
1432 compute_mix_smoothing(vp);
1433 linear_left = FROM_FINAL_VOLUME(left);
1434 if (vp->left_mix_offset) {
1435 linear_left += vp->left_mix_offset;
1436 if (linear_left > MAX_AMP_VALUE) {
1437 linear_left = MAX_AMP_VALUE;
1438 vp->left_mix_offset = 0;
1439 }
1440 left = FINAL_VOLUME(linear_left);
1441 }
1442 for (i = 0; vp->left_mix_offset && i < count; i++) {
1443 s = *sp++;
1444 MIXATION(left);
1445 lp++;
1446 vp->left_mix_offset += vp->left_mix_inc;
1447 linear_left += vp->left_mix_inc;
1448 if (linear_left > MAX_AMP_VALUE) {
1449 linear_left = MAX_AMP_VALUE;
1450 vp->left_mix_offset = 0;
1451 }
1452 left = FINAL_VOLUME(linear_left);
1453 }
1454 vp->old_left_mix = linear_left;
1455 count -= i;
1456 #endif
1457 for (i = 0; i < count; i++) {
1458 s = *sp++;
1459 MIXATION(left);
1460 lp++;
1461 }
1462 }
1463
1464 /* Returns 1 if the note died */
1465 #ifdef __BORLANDC__
update_signal(int v)1466 static int update_signal(int v)
1467 #else
1468 static inline int update_signal(int v)
1469 #endif
1470 {
1471 Voice *vp = &voice[v];
1472
1473 if (vp->envelope_increment && update_envelope(v))
1474 return 1;
1475 if (vp->tremolo_phase_increment)
1476 update_tremolo(v);
1477 if (opt_modulation_envelope && vp->sample->modes & MODES_ENVELOPE)
1478 update_modulation_envelope(v);
1479 return apply_envelope_to_amp(v);
1480 }
1481
1482 #ifdef __BORLANDC__
update_envelope(int v)1483 static int update_envelope(int v)
1484 #else
1485 static inline int update_envelope(int v)
1486 #endif
1487 {
1488 Voice *vp = &voice[v];
1489
1490 vp->envelope_volume += vp->envelope_increment;
1491 if ((vp->envelope_increment < 0)
1492 ^ (vp->envelope_volume > vp->envelope_target)) {
1493 vp->envelope_volume = vp->envelope_target;
1494 if (recompute_envelope(v))
1495 return 1;
1496 }
1497 return 0;
1498 }
1499
get_eg_stage(int v,int stage)1500 static int get_eg_stage(int v, int stage)
1501 {
1502 int eg_stage;
1503 Voice *vp = &voice[v];
1504
1505 eg_stage = stage;
1506 if (vp->sample->inst_type == INST_SF2) {
1507 if (stage >= EG_SF_RELEASE) {
1508 eg_stage = EG_RELEASE;
1509 }
1510 } else {
1511 if (stage == EG_GUS_DECAY) {
1512 eg_stage = EG_DECAY;
1513 } else if (stage == EG_GUS_SUSTAIN) {
1514 eg_stage = EG_NULL;
1515 } else if (stage >= EG_GUS_RELEASE1) {
1516 eg_stage = EG_RELEASE;
1517 }
1518 }
1519 return eg_stage;
1520 }
1521
1522
1523 /* Returns 1 if envelope runs out */
recompute_envelope(int v)1524 int recompute_envelope(int v)
1525 {
1526 int stage, ch;
1527 double sustain_time;
1528 int32 envelope_width;
1529 Voice *vp = &voice[v];
1530
1531 stage = vp->envelope_stage;
1532 if (stage > EG_GUS_RELEASE3) {
1533 voice_ran_out(v);
1534 return 1;
1535 } else if (stage > EG_GUS_SUSTAIN && vp->envelope_volume <= 0) {
1536 /* Remove silent voice in the release stage */
1537 voice_ran_out(v);
1538 return 1;
1539 }
1540
1541 /* Routine to decay the sustain envelope
1542 *
1543 * Disabled if !min_sustain_time.
1544 * min_sustain_time is given in msec, and is the minimum
1545 * time it will take to decay a note to zero.
1546 * 2000-3000 msec seem to be decent values to use.
1547 */
1548 if (stage == EG_GUS_RELEASE1 && vp->sample->modes & MODES_ENVELOPE
1549 && vp->status & (VOICE_ON | VOICE_SUSTAINED)) {
1550
1551 int32 new_rate;
1552
1553 ch = vp->channel;
1554
1555 /* Don't adjust the current rate if VOICE_ON */
1556 if (vp->status & VOICE_ON)
1557 return 0;
1558
1559 if (min_sustain_time > 0 || channel[ch].loop_timeout > 0) {
1560 if (min_sustain_time == 1)
1561 /* The sustain stage is ignored. */
1562 return next_stage(v);
1563
1564 if (channel[ch].loop_timeout > 0 &&
1565 channel[ch].loop_timeout * 1000 < min_sustain_time) {
1566 /* timeout (See also "#extension timeout" line in *.cfg file */
1567 sustain_time = channel[ch].loop_timeout * 1000;
1568 }
1569 else {
1570 sustain_time = min_sustain_time;
1571 }
1572
1573 /* Sustain must not be 0 or else lots of dead notes! */
1574 if (channel[ch].sostenuto == 0 &&
1575 channel[ch].sustain > 0) {
1576 sustain_time *= (double)channel[ch].sustain / 127.0f;
1577 }
1578
1579 /* Calculate the width of the envelope */
1580 envelope_width = sustain_time * play_mode->rate
1581 / (1000.0f * (double)control_ratio);
1582
1583 if (vp->sample->inst_type == INST_SF2) {
1584 /* If the instrument is SoundFont, it sustains at the sustain stage. */
1585 vp->envelope_increment = -1;
1586 vp->envelope_target = vp->envelope_volume - envelope_width;
1587 if (vp->envelope_target < 0) {vp->envelope_target = 0;}
1588 } else {
1589 /* Otherwise, it decays at the sustain stage. */
1590 vp->envelope_target = 0;
1591 new_rate = vp->envelope_volume / envelope_width;
1592 /* Use the Release1 rate if slower than new rate */
1593 if (vp->sample->envelope_rate[EG_GUS_RELEASE1] &&
1594 vp->sample->envelope_rate[EG_GUS_RELEASE1] < new_rate)
1595 new_rate = vp->sample->envelope_rate[EG_GUS_RELEASE1];
1596 /* Use the Sustain rate if slower than new rate */
1597 /* (Sustain rate exists only in GUS patches) */
1598 if (vp->sample->inst_type == INST_GUS &&
1599 vp->sample->envelope_rate[EG_GUS_SUSTAIN] &&
1600 vp->sample->envelope_rate[EG_GUS_SUSTAIN] < new_rate)
1601 new_rate = vp->sample->envelope_rate[EG_GUS_SUSTAIN];
1602 /* Avoid freezing */
1603 if (!new_rate)
1604 new_rate = 1;
1605 vp->envelope_increment = -new_rate;
1606 }
1607 }
1608 return 0;
1609 }
1610 return next_stage(v);
1611 }
1612
1613 /* Envelope ran out. */
voice_ran_out(int v)1614 static inline void voice_ran_out(int v)
1615 {
1616 /* Already displayed as dead */
1617 int died = (voice[v].status == VOICE_DIE);
1618
1619 free_voice(v);
1620 if (! died)
1621 ctl_note_event(v);
1622 }
1623
1624 #ifdef __BORLANDC__
next_stage(int v)1625 static int next_stage(int v)
1626 #else
1627 static inline int next_stage(int v)
1628 #endif
1629 {
1630 int stage, ch, eg_stage;
1631 int32 offset, val;
1632 FLOAT_T rate, temp_rate;
1633 Voice *vp = &voice[v];
1634
1635 stage = vp->envelope_stage++;
1636 offset = vp->sample->envelope_offset[stage];
1637 rate = vp->sample->envelope_rate[stage];
1638 if (vp->envelope_volume == offset
1639 || (stage > EG_GUS_SUSTAIN && vp->envelope_volume < offset))
1640 return recompute_envelope(v);
1641 ch = vp->channel;
1642 /* there is some difference between GUS patch and Soundfont at envelope. */
1643 eg_stage = get_eg_stage(v, stage);
1644
1645 /* HACK -- force ramps to occur over 20 msec windows to avoid pops */
1646 /* Do not apply to attack envelope */
1647 if (eg_stage > EG_ATTACK)
1648 {
1649 temp_rate = control_ratio * (labs(vp->envelope_volume - offset) /
1650 (play_mode->rate * 0.02));
1651 if (temp_rate < 1)
1652 temp_rate = 1;
1653 if (rate < 0)
1654 temp_rate = -temp_rate;
1655 if (fabs(temp_rate) < fabs(rate))
1656 rate = temp_rate;
1657 }
1658
1659 /* envelope generator (see also playmidi.[ch]) */
1660 if (ISDRUMCHANNEL(ch))
1661 val = (channel[ch].drums[vp->note] != NULL)
1662 ? channel[ch].drums[vp->note]->drum_envelope_rate[eg_stage]
1663 : -1;
1664 else {
1665 if (vp->sample->envelope_keyf[stage]) /* envelope key-follow */
1666 rate *= pow(2.0, (double) (voice[v].note - 60)
1667 * (double)vp->sample->envelope_keyf[stage] / 1200.0f);
1668 val = channel[ch].envelope_rate[eg_stage];
1669 }
1670 if (vp->sample->envelope_velf[stage]) /* envelope velocity-follow */
1671 rate *= pow(2.0, (double) (voice[v].velocity - vp->sample->envelope_velf_bpo)
1672 * (double)vp->sample->envelope_velf[stage] / 1200.0f);
1673
1674 /* just before release-phase, some modifications are necessary */
1675 if (stage > EG_GUS_SUSTAIN) {
1676 /* adjusting release-rate for consistent release-time */
1677 rate *= (double) vp->envelope_volume
1678 / vp->sample->envelope_offset[EG_GUS_ATTACK];
1679 /* calculating current envelope scale and a inverted value for optimization */
1680 vp->envelope_scale = vp->last_envelope_volume;
1681 vp->inv_envelope_scale = TIM_FSCALE(OFFSET_MAX / (double)vp->envelope_volume, 16);
1682 }
1683
1684 /* regularizing envelope */
1685 if (offset < vp->envelope_volume) { /* decaying phase */
1686 if (val != -1) {
1687 if(eg_stage > EG_DECAY) {
1688 rate *= sc_eg_release_table[val & 0x7f];
1689 } else {
1690 rate *= sc_eg_decay_table[val & 0x7f];
1691 }
1692
1693 if (fabs(rate) > OFFSET_MAX)
1694 rate = (rate > 0) ? OFFSET_MAX : -OFFSET_MAX;
1695 else if (fabs(rate) < 1)
1696 rate = (rate > 0) ? 1 : -1;
1697 }
1698 if(stage < EG_SF_DECAY && rate > OFFSET_MAX) { /* instantaneous decay */
1699 vp->envelope_volume = offset;
1700 return recompute_envelope(v);
1701 } else if(rate > vp->envelope_volume - offset) { /* fastest decay */
1702 rate = -vp->envelope_volume + offset - 1;
1703 } else if (rate < 1) { /* slowest decay */
1704 rate = -1;
1705 }
1706 else { /* ordinary decay */
1707 rate = -rate;
1708 }
1709 } else { /* attacking phase */
1710 if (val != -1) {
1711 rate *= sc_eg_attack_table[val & 0x7f];
1712
1713 if (fabs(rate) > OFFSET_MAX)
1714 rate = (rate > 0) ? OFFSET_MAX : -OFFSET_MAX;
1715 else if (fabs(rate) < 1)
1716 rate = (rate > 0) ? 1 : -1;
1717 }
1718 if(stage < EG_SF_DECAY && rate > OFFSET_MAX) { /* instantaneous attack */
1719 vp->envelope_volume = offset;
1720 return recompute_envelope(v);
1721 } else if(rate > offset - vp->envelope_volume) { /* fastest attack */
1722 rate = offset - vp->envelope_volume + 1;
1723 } else if (rate < 1) {rate = 1;} /* slowest attack */
1724 }
1725
1726 /* HACK -- force ramps to occur over 20 msec windows to avoid pops */
1727 /* Do not apply to attack envelope */
1728 /* Must check again in case the above conditions shortened it */
1729 if (eg_stage > EG_ATTACK)
1730 {
1731 temp_rate = control_ratio * (labs(vp->envelope_volume - offset) /
1732 (play_mode->rate * 0.02));
1733 if (temp_rate < 1)
1734 temp_rate = 1;
1735 if (rate < 0)
1736 temp_rate = -temp_rate;
1737 if (fabs(temp_rate) < fabs(rate))
1738 rate = temp_rate;
1739 }
1740
1741 vp->envelope_increment = (int32)rate;
1742 vp->envelope_target = offset;
1743
1744 return 0;
1745 }
1746
1747 #ifdef __BORLANDC__
update_tremolo(int v)1748 static void update_tremolo(int v)
1749 #else
1750 static inline void update_tremolo(int v)
1751 #endif
1752 {
1753 Voice *vp = &voice[v];
1754 int32 depth = vp->tremolo_depth << 7;
1755
1756 if(vp->tremolo_delay > 0)
1757 {
1758 vp->tremolo_delay -= vp->delay_counter;
1759 if(vp->tremolo_delay > 0) {
1760 vp->tremolo_volume = 1.0;
1761 return;
1762 }
1763 vp->tremolo_delay = 0;
1764 }
1765 if (vp->tremolo_sweep) {
1766 /* Update sweep position */
1767 vp->tremolo_sweep_position += vp->tremolo_sweep;
1768 if (vp->tremolo_sweep_position >= 1 << SWEEP_SHIFT)
1769 /* Swept to max amplitude */
1770 vp->tremolo_sweep = 0;
1771 else {
1772 /* Need to adjust depth */
1773 depth *= vp->tremolo_sweep_position;
1774 depth >>= SWEEP_SHIFT;
1775 }
1776 }
1777 vp->tremolo_phase += vp->tremolo_phase_increment;
1778 #if 0
1779 if (vp->tremolo_phase >= SINE_CYCLE_LENGTH << RATE_SHIFT)
1780 vp->tremolo_phase -= SINE_CYCLE_LENGTH << RATE_SHIFT;
1781 #endif
1782
1783 if(vp->sample->inst_type == INST_SF2) {
1784 vp->tremolo_volume = 1.0 + TIM_FSCALENEG(
1785 lookup_sine(vp->tremolo_phase >> RATE_SHIFT)
1786 * depth * TREMOLO_AMPLITUDE_TUNING, 17);
1787 } else {
1788 vp->tremolo_volume = 1.0 + TIM_FSCALENEG(
1789 lookup_sine(vp->tremolo_phase >> RATE_SHIFT)
1790 * depth * TREMOLO_AMPLITUDE_TUNING, 17);
1791 }
1792 /* I'm not sure about the +1.0 there -- it makes tremoloed voices'
1793 * volumes on average the lower the higher the tremolo amplitude.
1794 */
1795 }
1796
apply_envelope_to_amp(int v)1797 int apply_envelope_to_amp(int v)
1798 {
1799 Voice *vp = &voice[v];
1800 FLOAT_T lamp = vp->left_amp, ramp,
1801 *v_table = vp->sample->inst_type == INST_SF2 ? sb_vol_table : vol_table;
1802 int32 la, ra;
1803
1804 if (vp->panned == PANNED_MYSTERY) {
1805 ramp = vp->right_amp;
1806 if (vp->tremolo_phase_increment) {
1807 lamp *= vp->tremolo_volume;
1808 ramp *= vp->tremolo_volume;
1809 }
1810 if (vp->sample->modes & MODES_ENVELOPE) {
1811 if (vp->envelope_stage > 3)
1812 vp->last_envelope_volume = v_table[
1813 imuldiv16(vp->envelope_volume,
1814 vp->inv_envelope_scale) >> 20]
1815 * vp->envelope_scale;
1816 else if (vp->envelope_stage > 1)
1817 vp->last_envelope_volume = v_table[
1818 vp->envelope_volume >> 20];
1819 else
1820 vp->last_envelope_volume = attack_vol_table[
1821 vp->envelope_volume >> 20];
1822 lamp *= vp->last_envelope_volume;
1823 ramp *= vp->last_envelope_volume;
1824 }
1825 la = TIM_FSCALE(lamp, AMP_BITS);
1826 if (la > MAX_AMP_VALUE)
1827 la = MAX_AMP_VALUE;
1828 ra = TIM_FSCALE(ramp, AMP_BITS);
1829 if (ra > MAX_AMP_VALUE)
1830 ra = MAX_AMP_VALUE;
1831 if ((vp->status & (VOICE_OFF | VOICE_SUSTAINED))
1832 && (la | ra) <= 0) {
1833 free_voice(v);
1834 ctl_note_event(v);
1835 return 1;
1836 }
1837 vp->left_mix = FINAL_VOLUME(la);
1838 vp->right_mix = FINAL_VOLUME(ra);
1839 } else {
1840 if (vp->tremolo_phase_increment)
1841 lamp *= vp->tremolo_volume;
1842 if (vp->sample->modes & MODES_ENVELOPE) {
1843 if (vp->envelope_stage > 3)
1844 vp->last_envelope_volume = v_table[
1845 imuldiv16(vp->envelope_volume,
1846 vp->inv_envelope_scale) >> 20]
1847 * vp->envelope_scale;
1848 else if (vp->envelope_stage > 1)
1849 vp->last_envelope_volume = v_table[
1850 vp->envelope_volume >> 20];
1851 else
1852 vp->last_envelope_volume = attack_vol_table[
1853 vp->envelope_volume >> 20];
1854 lamp *= vp->last_envelope_volume;
1855 }
1856 la = TIM_FSCALE(lamp, AMP_BITS);
1857 if (la > MAX_AMP_VALUE)
1858 la = MAX_AMP_VALUE;
1859 if ((vp->status & (VOICE_OFF | VOICE_SUSTAINED))
1860 && la <= 0) {
1861 free_voice(v);
1862 ctl_note_event(v);
1863 return 1;
1864 }
1865 vp->left_mix = FINAL_VOLUME(la);
1866 }
1867 return 0;
1868 }
1869
1870 #ifdef SMOOTH_MIXING
1871 #ifdef __BORLANDC__
compute_mix_smoothing(Voice * vp)1872 static inline void compute_mix_smoothing(Voice *vp)
1873 #else
1874 static inline void compute_mix_smoothing(Voice *vp)
1875 #endif
1876 {
1877 int32 max_win, delta;
1878
1879 /* reduce popping -- ramp the amp over a 20 msec window */
1880 max_win = (play_mode->rate * 0.02) / control_ratio;
1881 delta = FROM_FINAL_VOLUME(vp->left_mix) - vp->old_left_mix;
1882 if (labs(delta) > max_win) {
1883 vp->left_mix_inc = delta / max_win;
1884 vp->left_mix_offset = vp->left_mix_inc * (1 - max_win);
1885 } else if (delta) {
1886 vp->left_mix_inc = -1;
1887 if (delta > 0)
1888 vp->left_mix_inc = 1;
1889 vp->left_mix_offset = vp->left_mix_inc - delta;
1890 }
1891 delta = FROM_FINAL_VOLUME(vp->right_mix) - vp->old_right_mix;
1892 if (labs(delta) > max_win) {
1893 vp->right_mix_inc = delta / max_win;
1894 vp->right_mix_offset = vp->right_mix_inc * (1 - max_win);
1895 } else if (delta) {
1896 vp->right_mix_inc = -1;
1897 if (delta > 0)
1898 vp->right_mix_inc = 1;
1899 vp->right_mix_offset = vp->right_mix_inc - delta;
1900 }
1901 }
1902 #endif
1903
1904 #ifdef __BORLANDC__
update_modulation_envelope(int v)1905 static int update_modulation_envelope(int v)
1906 #else
1907 static inline int update_modulation_envelope(int v)
1908 #endif
1909 {
1910 Voice *vp = &voice[v];
1911
1912 if(vp->modenv_delay > 0) {
1913 vp->modenv_delay -= vp->delay_counter;
1914 if(vp->modenv_delay > 0) {return 1;}
1915 vp->modenv_delay = 0;
1916 }
1917 vp->modenv_volume += vp->modenv_increment;
1918 if ((vp->modenv_increment < 0)
1919 ^ (vp->modenv_volume > vp->modenv_target)) {
1920 vp->modenv_volume = vp->modenv_target;
1921 if (recompute_modulation_envelope(v)) {
1922 apply_modulation_envelope(v);
1923 return 1;
1924 }
1925 }
1926
1927 apply_modulation_envelope(v);
1928
1929 return 0;
1930 }
1931
apply_modulation_envelope(int v)1932 int apply_modulation_envelope(int v)
1933 {
1934 Voice *vp = &voice[v];
1935
1936 if(!opt_modulation_envelope) {return 0;}
1937
1938 if (vp->sample->modes & MODES_ENVELOPE) {
1939 vp->last_modenv_volume = modenv_vol_table[vp->modenv_volume >> 20];
1940 }
1941
1942 recompute_voice_filter(v);
1943 if(!(vp->porta_control_ratio && vp->porta_control_counter == 0)) {
1944 recompute_freq(v);
1945 }
1946 return 0;
1947 }
1948
modenv_next_stage(int v)1949 static inline int modenv_next_stage(int v)
1950 {
1951 int stage, ch, eg_stage;
1952 int32 offset, val;
1953 FLOAT_T rate;
1954 Voice *vp = &voice[v];
1955
1956 stage = vp->modenv_stage++;
1957 offset = vp->sample->modenv_offset[stage];
1958 rate = vp->sample->modenv_rate[stage];
1959 if (vp->modenv_volume == offset
1960 || (stage > EG_GUS_SUSTAIN && vp->modenv_volume < offset))
1961 return recompute_modulation_envelope(v);
1962 else if(stage < EG_SF_DECAY && rate > OFFSET_MAX) { /* instantaneous attack */
1963 vp->modenv_volume = offset;
1964 return recompute_modulation_envelope(v);
1965 }
1966 ch = vp->channel;
1967 /* there is some difference between GUS patch and Soundfont at envelope. */
1968 eg_stage = get_eg_stage(v, stage);
1969
1970 /* envelope generator (see also playmidi.[ch]) */
1971 if (ISDRUMCHANNEL(ch))
1972 val = (channel[ch].drums[vp->note] != NULL)
1973 ? channel[ch].drums[vp->note]->drum_envelope_rate[eg_stage]
1974 : -1;
1975 else {
1976 if (vp->sample->modenv_keyf[stage]) /* envelope key-follow */
1977 rate *= pow(2.0, (double) (voice[v].note - 60)
1978 * (double)vp->sample->modenv_keyf[stage] / 1200.0f);
1979 val = channel[ch].envelope_rate[eg_stage];
1980 }
1981 if (vp->sample->modenv_velf[stage])
1982 rate *= pow(2.0, (double) (voice[v].velocity - vp->sample->modenv_velf_bpo)
1983 * (double)vp->sample->modenv_velf[stage] / 1200.0f);
1984
1985 /* just before release-phase, some modifications are necessary */
1986 if (stage > EG_GUS_SUSTAIN) {
1987 /* adjusting release-rate for consistent release-time */
1988 rate *= (double) vp->modenv_volume
1989 / vp->sample->modenv_offset[EG_GUS_ATTACK];
1990 }
1991
1992 /* regularizing envelope */
1993 if (offset < vp->modenv_volume) { /* decaying phase */
1994 if (val != -1) {
1995 if(stage > EG_DECAY) {
1996 rate *= sc_eg_release_table[val & 0x7f];
1997 } else {
1998 rate *= sc_eg_decay_table[val & 0x7f];
1999 }
2000 }
2001 if(rate > vp->modenv_volume - offset) { /* fastest decay */
2002 rate = -vp->modenv_volume + offset - 1;
2003 } else if (rate < 1) { /* slowest decay */
2004 rate = -1;
2005 } else { /* ordinary decay */
2006 rate = -rate;
2007 }
2008 } else { /* attacking phase */
2009 if (val != -1)
2010 rate *= sc_eg_attack_table[val & 0x7f];
2011 if(rate > offset - vp->modenv_volume) { /* fastest attack */
2012 rate = offset - vp->modenv_volume + 1;
2013 } else if (rate < 1) {rate = 1;} /* slowest attack */
2014 }
2015
2016 vp->modenv_increment = (int32)rate;
2017 vp->modenv_target = offset;
2018
2019 return 0;
2020 }
2021
recompute_modulation_envelope(int v)2022 int recompute_modulation_envelope(int v)
2023 {
2024 int stage, ch;
2025 double sustain_time;
2026 int32 modenv_width;
2027 Voice *vp = &voice[v];
2028
2029 if(!opt_modulation_envelope) {return 0;}
2030
2031 stage = vp->modenv_stage;
2032 if (stage > EG_GUS_RELEASE3) {return 1;}
2033 else if (stage > EG_GUS_SUSTAIN && vp->modenv_volume <= 0) {
2034 return 1;
2035 }
2036
2037 /* Routine to sustain modulation envelope
2038 *
2039 * Disabled if !min_sustain_time.
2040 * min_sustain_time is given in msec, and is the minimum
2041 * time it will take to sustain a note.
2042 * 2000-3000 msec seem to be decent values to use.
2043 */
2044 if (stage == EG_GUS_RELEASE1 && vp->sample->modes & MODES_ENVELOPE
2045 && vp->status & (VOICE_ON | VOICE_SUSTAINED)) {
2046 ch = vp->channel;
2047
2048 /* Don't adjust the current rate if VOICE_ON */
2049 if (vp->status & VOICE_ON)
2050 return 0;
2051
2052 if (min_sustain_time > 0 || channel[ch].loop_timeout > 0) {
2053 if (min_sustain_time == 1)
2054 /* The sustain stage is ignored. */
2055 return modenv_next_stage(v);
2056
2057 if (channel[ch].loop_timeout > 0 &&
2058 channel[ch].loop_timeout * 1000 < min_sustain_time) {
2059 /* timeout (See also "#extension timeout" line in *.cfg file */
2060 sustain_time = channel[ch].loop_timeout * 1000;
2061 }
2062 else {
2063 sustain_time = min_sustain_time;
2064 }
2065
2066 /* Sustain must not be 0 or else lots of dead notes! */
2067 if (channel[ch].sostenuto == 0 &&
2068 channel[ch].sustain > 0) {
2069 sustain_time *= (double)channel[ch].sustain / 127.0f;
2070 }
2071
2072 /* Calculate the width of the envelope */
2073 modenv_width = sustain_time * play_mode->rate
2074 / (1000.0f * (double)control_ratio);
2075 vp->modenv_increment = -1;
2076 vp->modenv_target = vp->modenv_volume - modenv_width;
2077 if (vp->modenv_target < 0) {vp->modenv_target = 0;}
2078 }
2079 return 0;
2080 }
2081 return modenv_next_stage(v);
2082 }
2083