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 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include "timidity.h"
28 #include "common.h"
29 #include "instrum.h"
30 #include "playmidi.h"
31 #include "tables.h"
32 #include "resample.h"
33 #include "mix.h"
34 #include "optcode.h"
35 
36 namespace TimidityPlus
37 {
38 extern float min_sustain_time;
39 
40 
41 #define FROM_FINAL_VOLUME(a) (a)
42 
43 #define OFFSET_MAX (0x3FFFFFFFL)
44 
45 typedef int32_t mix_t;
46 
47 #define MIXATION(a) *lp++ += (a) * s
48 
49 #define DELAYED_MIXATION(a) *lp++ += pan_delay_buf[pan_delay_spt];	\
50 	if (++pan_delay_spt == PAN_DELAY_BUF_MAX) {pan_delay_spt = 0;}	\
51 	pan_delay_buf[pan_delay_wpt] = (a) * s;	\
52 	if (++pan_delay_wpt == PAN_DELAY_BUF_MAX) {pan_delay_wpt = 0;}
53 
54 
55 
56 
57 /**************** interface function ****************/
mix_voice(int32_t * buf,int v,int32_t c)58 void Mixer::mix_voice(int32_t *buf, int v, int32_t c)
59 {
60 	Resampler re(player);
61 	Voice *vp = player->voice + v;
62 	resample_t *sp;
63 
64 	if (vp->status == VOICE_DIE)
65 	{
66 		if (c >= MAX_DIE_TIME)
67 			c = MAX_DIE_TIME;
68 		sp = re.resample_voice(v, &c);
69 		if (do_voice_filter(v, sp, filter_buffer, c)) { sp = filter_buffer; }
70 		if (c > 0)
71 			ramp_out(sp, buf, v, c);
72 		player->free_voice(v);
73 	}
74 	else {
75 		vp->delay_counter = c;
76 		if (vp->delay) {
77 			if (c < vp->delay) {
78 				vp->delay -= c;
79 				if (vp->tremolo_phase_increment)
80 					update_tremolo(v);
81 				if (timidity_modulation_envelope && vp->sample->modes & MODES_ENVELOPE)
82 					update_modulation_envelope(v);
83 				return;
84 			}
85 			buf += vp->delay * 2;
86 			c -= vp->delay;
87 			vp->delay = 0;
88 		}
89 		sp = re.resample_voice(v, &c);
90 		if (do_voice_filter(v, sp, filter_buffer, c)) { sp = filter_buffer; }
91 
92 		if (vp->panned == PANNED_MYSTERY) {
93 			if (vp->envelope_increment || vp->tremolo_phase_increment)
94 				mix_mystery_signal(sp, buf, v, c);
95 			else
96 				mix_mystery(sp, buf, v, c);
97 		}
98 		else if (vp->panned == PANNED_CENTER) {
99 			if (vp->envelope_increment || vp->tremolo_phase_increment)
100 				mix_center_signal(sp, buf, v, c);
101 			else
102 				mix_center(sp, buf, v, c);
103 		}
104 		else {
105 			/* It's either full left or full right. In either case,
106 			 * every other sample is 0. Just get the offset right:
107 			 */
108 			if (vp->panned == PANNED_RIGHT)
109 				buf++;
110 			if (vp->envelope_increment || vp->tremolo_phase_increment)
111 				mix_single_signal(sp, buf, v, c);
112 			else
113 				mix_single(sp, buf, v, c);
114 		}
115 	}
116 }
117 
118 /* return 1 if filter is enabled. */
do_voice_filter(int v,resample_t * sp,mix_t * lp,int32_t count)119 int Mixer::do_voice_filter(int v, resample_t *sp, mix_t *lp, int32_t count)
120 {
121 	FilterCoefficients *fc = &(player->voice[v].fc);
122 	int32_t i, f, q, p, b0, b1, b2, b3, b4, t1, t2, x;
123 
124 	if (fc->type == 1) {	/* copy with applying Chamberlin's lowpass filter. */
125 		recalc_voice_resonance(v);
126 		recalc_voice_fc(v);
127 		f = fc->f, q = fc->q, b0 = fc->b0, b1 = fc->b1, b2 = fc->b2;
128 		for(i = 0; i < count; i++) {
129 			b0 = b0 + imuldiv24(b2, f);
130 			b1 = sp[i] - b0 - imuldiv24(b2, q);
131 			b2 = imuldiv24(b1, f) + b2;
132 			lp[i] = b0;
133 		}
134 		fc->b0 = b0, fc->b1 = b1, fc->b2 = b2;
135 		return 1;
136 	} else if(fc->type == 2) {	/* copy with applying Moog lowpass VCF. */
137 		recalc_voice_resonance(v);
138 		recalc_voice_fc(v);
139 		f = fc->f, q = fc->q, p = fc->p, b0 = fc->b0, b1 = fc->b1,
140 			b2 = fc->b2, b3 = fc->b3, b4 = fc->b4;
141 		for(i = 0; i < count; i++) {
142 			x = sp[i] - imuldiv24(q, b4);	/* feedback */
143 			t1 = b1;  b1 = imuldiv24(x + b0, p) - imuldiv24(b1, f);
144 			t2 = b2;  b2 = imuldiv24(b1 + t1, p) - imuldiv24(b2, f);
145 			t1 = b3;  b3 = imuldiv24(b2 + t2, p) - imuldiv24(b3, f);
146 			lp[i] = b4 = imuldiv24(b3 + t1, p) - imuldiv24(b4, f);
147 			b0 = x;
148 		}
149 		fc->b0 = b0, fc->b1 = b1, fc->b2 = b2, fc->b3 = b3, fc->b4 = b4;
150 		return 1;
151 	} else {
152 		return 0;
153 	}
154 }
155 
156 //#define MOOG_RESONANCE_MAX 0.897638f
157 #define MOOG_RESONANCE_MAX 0.88f
158 
recalc_voice_resonance(int v)159 void Mixer::recalc_voice_resonance(int v)
160 {
161 	double q;
162 	FilterCoefficients *fc = &(player->voice[v].fc);
163 
164 	if (fc->reso_dB != fc->last_reso_dB || fc->q == 0) {
165 		fc->last_reso_dB = fc->reso_dB;
166 		if(fc->type == 1) {
167 			q = 1.0 / chamberlin_filter_db_to_q_table[(int)(fc->reso_dB * 4)];
168 			fc->q = TIM_FSCALE(q, 24);
169 			if(fc->q <= 0) {fc->q = 1;}	/* must never be 0. */
170 		} else if(fc->type == 2) {
171 			fc->reso_lin = fc->reso_dB * MOOG_RESONANCE_MAX / 20.0f;
172 			if (fc->reso_lin > MOOG_RESONANCE_MAX) {fc->reso_lin = MOOG_RESONANCE_MAX;}
173 			else if(fc->reso_lin < 0) {fc->reso_lin = 0;}
174 		}
175 		fc->last_freq = -1;
176 	}
177 }
178 
recalc_voice_fc(int v)179 void Mixer::recalc_voice_fc(int v)
180 {
181 	double f, p, q, fr;
182 	FilterCoefficients *fc = &(player->voice[v].fc);
183 
184 	if (fc->freq != fc->last_freq) {
185 		if(fc->type == 1) {
186 			f = 2.0 * sin(M_PI * (double)fc->freq / (double)playback_rate);
187 			fc->f = TIM_FSCALE(f, 24);
188 		} else if(fc->type == 2) {
189 			fr = 2.0 * (double)fc->freq / (double)playback_rate;
190 			q = 1.0 - fr;
191 			p = fr + 0.8 * fr * q;
192 			f = p + p - 1.0;
193 			q = fc->reso_lin * (1.0 + 0.5 * q * (1.0 - q + 5.6 * q * q));
194 			fc->f = TIM_FSCALE(f, 24);
195 			fc->p = TIM_FSCALE(p, 24);
196 			fc->q = TIM_FSCALE(q, 24);
197 		}
198 		fc->last_freq = fc->freq;
199 	}
200 }
201 
202 /* Ramp a note out in c samples */
ramp_out(mix_t * sp,int32_t * lp,int v,int32_t c)203 void Mixer::ramp_out(mix_t *sp, int32_t *lp, int v, int32_t c)
204 {
205 	/* should be final_volume_t, but uint8_t gives trouble. */
206 	int32_t left, right, li, ri, i;
207 	/* silly warning about uninitialized s */
208 	mix_t s = 0;
209 	Voice *vp = &player->voice[v];
210 	int32_t pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
211 		pan_delay_spt = vp->pan_delay_spt;
212 
213 	left = player->voice[v].left_mix;
214 	li = -(left / c);
215 	if (! li)
216 		li = -1;
217 	if (true) {
218 		if (player->voice[v].panned == PANNED_MYSTERY) {
219 			right = player->voice[v].right_mix;
220 			ri = -(right / c);
221 			if(vp->pan_delay_rpt == 0) {
222 				for (i = 0; i < c; i++) {
223 					left += li;
224 					if (left < 0)
225 						left = 0;
226 					right += ri;
227 					if (right < 0)
228 						right = 0;
229 					s = *sp++;
230 					MIXATION(left);
231 					MIXATION(right);
232 				}
233 			} else if(vp->panning < 64) {
234 				for (i = 0; i < c; i++) {
235 					left += li;
236 					if (left < 0)
237 						left = 0;
238 					right += ri;
239 					if (right < 0)
240 						right = 0;
241 					s = *sp++;
242 					MIXATION(left);
243 					DELAYED_MIXATION(right);
244 				}
245 			} else {
246 				for (i = 0; i < c; i++) {
247 					left += li;
248 					if (left < 0)
249 						left = 0;
250 					right += ri;
251 					if (right < 0)
252 						right = 0;
253 					s = *sp++;
254 					DELAYED_MIXATION(left);
255 					MIXATION(right);
256 				}
257 			}
258 			vp->pan_delay_wpt = pan_delay_wpt;
259 			vp->pan_delay_spt = pan_delay_spt;
260 		} else if (player->voice[v].panned == PANNED_CENTER)
261 			for (i = 0; i < c; i++) {
262 				left += li;
263 				if (left < 0)
264 					return;
265 				s = *sp++;
266 				MIXATION(left);
267 				MIXATION(left);
268 			}
269 		else if (player->voice[v].panned == PANNED_LEFT)
270 			for (i = 0; i < c; i++) {
271 				left += li;
272 				if (left < 0)
273 					return;
274 				s = *sp++;
275 				MIXATION(left);
276 				lp++;
277 			}
278 		else if (player->voice[v].panned == PANNED_RIGHT)
279 			for (i = 0; i < c; i++) {
280 				left += li;
281 				if (left < 0)
282 					return;
283 				s = *sp++;
284 				lp++;
285 				MIXATION(left);
286 			}
287 	} else
288 		/* Mono output. */
289 		for (i = 0; i < c; i++) {
290 			left += li;
291 			if (left < 0)
292 				return;
293 			s = *sp++;
294 			MIXATION(left);
295 		}
296 }
297 
mix_mono_signal(mix_t * sp,int32_t * lp,int v,int count)298 void Mixer::mix_mono_signal(
299 		mix_t *sp, int32_t *lp, int v, int count)
300 {
301 	Voice *vp = player->voice + v;
302 	final_volume_t left = vp->left_mix;
303 	int cc, i;
304 	mix_t s;
305 	int32_t linear_left;
306 
307 	if (! (cc = vp->control_counter)) {
308 		cc = control_ratio;
309 		if (update_signal(v))
310 			/* Envelope ran out */
311 			return;
312 		left = vp->left_mix;
313 	}
314 	compute_mix_smoothing(vp);
315 	while (count)
316 		if (cc < count) {
317 			count -= cc;
318 			linear_left = FROM_FINAL_VOLUME(left);
319 			if (vp->left_mix_offset) {
320 				linear_left += vp->left_mix_offset;
321 				if (linear_left > MAX_AMP_VALUE) {
322 					linear_left = MAX_AMP_VALUE;
323 					vp->left_mix_offset = 0;
324 				}
325 				left = FINAL_VOLUME(linear_left);
326 			}
327 			for (i = 0; vp->left_mix_offset && i < cc; i++) {
328 				s = *sp++;
329 				MIXATION(left);
330 				vp->left_mix_offset += vp->left_mix_inc;
331 				linear_left += vp->left_mix_inc;
332 				if (linear_left > MAX_AMP_VALUE) {
333 					linear_left = MAX_AMP_VALUE;
334 					vp->left_mix_offset = 0;
335 				}
336 				left = FINAL_VOLUME(linear_left);
337 			}
338 			vp->old_left_mix = linear_left;
339 			cc -= i;
340 			for (i = 0; i < cc; i++) {
341 				s = *sp++;
342 				MIXATION(left);
343 			}
344 			cc = control_ratio;
345 			if (update_signal(v))
346 				/* Envelope ran out */
347 				return;
348 			left = vp->left_mix;
349 			compute_mix_smoothing(vp);
350 		} else {
351 			vp->control_counter = cc - count;
352 			linear_left = FROM_FINAL_VOLUME(left);
353 			if (vp->left_mix_offset) {
354 				linear_left += vp->left_mix_offset;
355 				if (linear_left > MAX_AMP_VALUE) {
356 					linear_left = MAX_AMP_VALUE;
357 					vp->left_mix_offset = 0;
358 				}
359 				left = FINAL_VOLUME(linear_left);
360 			}
361 			for (i = 0; vp->left_mix_offset && i < count; i++) {
362 				s = *sp++;
363 				MIXATION(left);
364 				vp->left_mix_offset += vp->left_mix_inc;
365 				linear_left += vp->left_mix_inc;
366 				if (linear_left > MAX_AMP_VALUE) {
367 					linear_left = MAX_AMP_VALUE;
368 					vp->left_mix_offset = 0;
369 				}
370 				left = FINAL_VOLUME(linear_left);
371 			}
372 			vp->old_left_mix = linear_left;
373 			count -= i;
374 			for (i = 0; i < count; i++) {
375 				s = *sp++;
376 				MIXATION(left);
377 			}
378 			return;
379 		}
380 }
381 
382 
mix_mystery_signal(mix_t * sp,int32_t * lp,int v,int count)383 void Mixer::mix_mystery_signal(
384 		mix_t *sp, int32_t *lp, int v, int count)
385 {
386 	Voice *vp = player->voice + v;
387 	final_volume_t left = vp->left_mix, right = vp->right_mix;
388 	int cc, i;
389 	mix_t s;
390 	int32_t linear_left, linear_right;
391 	int32_t pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
392 		pan_delay_spt = vp->pan_delay_spt;
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 		right = vp->right_mix;
401 	}
402 	compute_mix_smoothing(vp);
403 	while (count)
404 		if (cc < count) {
405 			count -= cc;
406 			linear_left = FROM_FINAL_VOLUME(left);
407 			if (vp->left_mix_offset) {
408 				linear_left += vp->left_mix_offset;
409 				if (linear_left > MAX_AMP_VALUE) {
410 					linear_left = MAX_AMP_VALUE;
411 					vp->left_mix_offset = 0;
412 				}
413 				left = FINAL_VOLUME(linear_left);
414 			}
415 			linear_right = FROM_FINAL_VOLUME(right);
416 			if (vp->right_mix_offset) {
417 				linear_right += vp->right_mix_offset;
418 				if (linear_right > MAX_AMP_VALUE) {
419 					linear_right = MAX_AMP_VALUE;
420 					vp->right_mix_offset = 0;
421 				}
422 				right = FINAL_VOLUME(linear_right);
423 			}
424 			if(vp->pan_delay_rpt == 0) {
425 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
426 						&& i < cc; i++) {
427 					s = *sp++;
428 					MIXATION(left);
429 					MIXATION(right);
430 					if (vp->left_mix_offset) {
431 						vp->left_mix_offset += vp->left_mix_inc;
432 						linear_left += vp->left_mix_inc;
433 						if (linear_left > MAX_AMP_VALUE) {
434 							linear_left = MAX_AMP_VALUE;
435 							vp->left_mix_offset = 0;
436 						}
437 						left = FINAL_VOLUME(linear_left);
438 					}
439 					if (vp->right_mix_offset) {
440 						vp->right_mix_offset += vp->right_mix_inc;
441 						linear_right += vp->right_mix_inc;
442 						if (linear_right > MAX_AMP_VALUE) {
443 							linear_right = MAX_AMP_VALUE;
444 							vp->right_mix_offset = 0;
445 						}
446 						right = FINAL_VOLUME(linear_right);
447 					}
448 				}
449 			} else if(vp->panning < 64) {
450 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
451 						&& i < cc; i++) {
452 					s = *sp++;
453 					MIXATION(left);
454 					DELAYED_MIXATION(right);
455 					if (vp->left_mix_offset) {
456 						vp->left_mix_offset += vp->left_mix_inc;
457 						linear_left += vp->left_mix_inc;
458 						if (linear_left > MAX_AMP_VALUE) {
459 							linear_left = MAX_AMP_VALUE;
460 							vp->left_mix_offset = 0;
461 						}
462 						left = FINAL_VOLUME(linear_left);
463 					}
464 					if (vp->right_mix_offset) {
465 						vp->right_mix_offset += vp->right_mix_inc;
466 						linear_right += vp->right_mix_inc;
467 						if (linear_right > MAX_AMP_VALUE) {
468 							linear_right = MAX_AMP_VALUE;
469 							vp->right_mix_offset = 0;
470 						}
471 						right = FINAL_VOLUME(linear_right);
472 					}
473 				}
474 			} else {
475 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
476 						&& i < cc; i++) {
477 					s = *sp++;
478 					DELAYED_MIXATION(left);
479 					MIXATION(right);
480 					if (vp->left_mix_offset) {
481 						vp->left_mix_offset += vp->left_mix_inc;
482 						linear_left += vp->left_mix_inc;
483 						if (linear_left > MAX_AMP_VALUE) {
484 							linear_left = MAX_AMP_VALUE;
485 							vp->left_mix_offset = 0;
486 						}
487 						left = FINAL_VOLUME(linear_left);
488 					}
489 					if (vp->right_mix_offset) {
490 						vp->right_mix_offset += vp->right_mix_inc;
491 						linear_right += vp->right_mix_inc;
492 						if (linear_right > MAX_AMP_VALUE) {
493 							linear_right = MAX_AMP_VALUE;
494 							vp->right_mix_offset = 0;
495 						}
496 						right = FINAL_VOLUME(linear_right);
497 					}
498 				}
499 			}
500 			vp->old_left_mix = linear_left;
501 			vp->old_right_mix = linear_right;
502 			cc -= i;
503 			if(vp->pan_delay_rpt == 0) {
504 				for (i = 0; i < cc; i++) {
505 					s = *sp++;
506 					MIXATION(left);
507 					MIXATION(right);
508 				}
509 			} else if(vp->panning < 64) {
510 				for (i = 0; i < cc; i++) {
511 					s = *sp++;
512 					MIXATION(left);
513 					DELAYED_MIXATION(right);
514 				}
515 			} else {
516 				for (i = 0; i < cc; i++) {
517 					s = *sp++;
518 					DELAYED_MIXATION(left);
519 					MIXATION(right);
520 				}
521 			}
522 
523 			cc = control_ratio;
524 			if (update_signal(v))
525 				/* Envelope ran out */
526 				return;
527 			left = vp->left_mix;
528 			right = vp->right_mix;
529 			compute_mix_smoothing(vp);
530 		} else {
531 			vp->control_counter = cc - count;
532 			linear_left = FROM_FINAL_VOLUME(left);
533 			if (vp->left_mix_offset) {
534 				linear_left += vp->left_mix_offset;
535 				if (linear_left > MAX_AMP_VALUE) {
536 					linear_left = MAX_AMP_VALUE;
537 					vp->left_mix_offset = 0;
538 				}
539 				left = FINAL_VOLUME(linear_left);
540 			}
541 			linear_right = FROM_FINAL_VOLUME(right);
542 			if (vp->right_mix_offset) {
543 				linear_right += vp->right_mix_offset;
544 				if (linear_right > MAX_AMP_VALUE) {
545 					linear_right = MAX_AMP_VALUE;
546 					vp->right_mix_offset = 0;
547 				}
548 				right = FINAL_VOLUME(linear_right);
549 			}
550 			if(vp->pan_delay_rpt == 0) {
551 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
552 						&& i < count; i++) {
553 					s = *sp++;
554 					MIXATION(left);
555 					MIXATION(right);
556 					if (vp->left_mix_offset) {
557 						vp->left_mix_offset += vp->left_mix_inc;
558 						linear_left += vp->left_mix_inc;
559 						if (linear_left > MAX_AMP_VALUE) {
560 							linear_left = MAX_AMP_VALUE;
561 							vp->left_mix_offset = 0;
562 						}
563 						left = FINAL_VOLUME(linear_left);
564 					}
565 					if (vp->right_mix_offset) {
566 						vp->right_mix_offset += vp->right_mix_inc;
567 						linear_right += vp->right_mix_inc;
568 						if (linear_right > MAX_AMP_VALUE) {
569 							linear_right = MAX_AMP_VALUE;
570 							vp->right_mix_offset = 0;
571 						}
572 						right = FINAL_VOLUME(linear_right);
573 					}
574 				}
575 			} else if(vp->panning < 64) {
576 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
577 						&& i < count; i++) {
578 					s = *sp++;
579 					MIXATION(left);
580 					DELAYED_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 {
601 				for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
602 						&& i < count; i++) {
603 					s = *sp++;
604 					DELAYED_MIXATION(left);
605 					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 			}
626 
627 			vp->old_left_mix = linear_left;
628 			vp->old_right_mix = linear_right;
629 			count -= i;
630 			if(vp->pan_delay_rpt == 0) {
631 				for (i = 0; i < count; i++) {
632 					s = *sp++;
633 					MIXATION(left);
634 					MIXATION(right);
635 				}
636 			} else if(vp->panning < 64) {
637 				for (i = 0; i < count; i++) {
638 					s = *sp++;
639 					MIXATION(left);
640 					DELAYED_MIXATION(right);
641 				}
642 			} else {
643 				for (i = 0; i < count; i++) {
644 					s = *sp++;
645 					DELAYED_MIXATION(left);
646 					MIXATION(right);
647 				}
648 			}
649 			vp->pan_delay_wpt = pan_delay_wpt;
650 			vp->pan_delay_spt = pan_delay_spt;
651 			return;
652 		}
653 }
654 
mix_mystery(mix_t * sp,int32_t * lp,int v,int count)655 void Mixer::mix_mystery(mix_t *sp, int32_t *lp, int v, int count)
656 {
657 	final_volume_t left = player->voice[v].left_mix, right = player->voice[v].right_mix;
658 	mix_t s;
659 	int i;
660 	Voice *vp = player->voice + v;
661 	int32_t linear_left, linear_right;
662 	int32_t pan_delay_wpt = vp->pan_delay_wpt, *pan_delay_buf = vp->pan_delay_buf,
663 		pan_delay_spt = vp->pan_delay_spt;
664 
665 	compute_mix_smoothing(vp);
666 	linear_left = FROM_FINAL_VOLUME(left);
667 	if (vp->left_mix_offset) {
668 		linear_left += vp->left_mix_offset;
669 		if (linear_left > MAX_AMP_VALUE) {
670 			linear_left = MAX_AMP_VALUE;
671 			vp->left_mix_offset = 0;
672 		}
673 		left = FINAL_VOLUME(linear_left);
674 	}
675 	linear_right = FROM_FINAL_VOLUME(right);
676 	if (vp->right_mix_offset) {
677 		linear_right += vp->right_mix_offset;
678 		if (linear_right > MAX_AMP_VALUE) {
679 			linear_right = MAX_AMP_VALUE;
680 			vp->right_mix_offset = 0;
681 		}
682 		right = FINAL_VOLUME(linear_right);
683 	}
684 	if(vp->pan_delay_rpt == 0) {
685 		for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
686 				&& i < count; i++) {
687 			s = *sp++;
688 			MIXATION(left);
689 			MIXATION(right);
690 			if (vp->left_mix_offset) {
691 				vp->left_mix_offset += vp->left_mix_inc;
692 				linear_left += vp->left_mix_inc;
693 				if (linear_left > MAX_AMP_VALUE) {
694 					linear_left = MAX_AMP_VALUE;
695 					vp->left_mix_offset = 0;
696 				}
697 				left = FINAL_VOLUME(linear_left);
698 			}
699 			if (vp->right_mix_offset) {
700 				vp->right_mix_offset += vp->right_mix_inc;
701 				linear_right += vp->right_mix_inc;
702 				if (linear_right > MAX_AMP_VALUE) {
703 					linear_right = MAX_AMP_VALUE;
704 					vp->right_mix_offset = 0;
705 				}
706 				right = FINAL_VOLUME(linear_right);
707 			}
708 		}
709 	} else if(vp->panning < 64) {
710 		for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
711 				&& i < count; i++) {
712 			s = *sp++;
713 			MIXATION(left);
714 			DELAYED_MIXATION(right);
715 			if (vp->left_mix_offset) {
716 				vp->left_mix_offset += vp->left_mix_inc;
717 				linear_left += vp->left_mix_inc;
718 				if (linear_left > MAX_AMP_VALUE) {
719 					linear_left = MAX_AMP_VALUE;
720 					vp->left_mix_offset = 0;
721 				}
722 				left = FINAL_VOLUME(linear_left);
723 			}
724 			if (vp->right_mix_offset) {
725 				vp->right_mix_offset += vp->right_mix_inc;
726 				linear_right += vp->right_mix_inc;
727 				if (linear_right > MAX_AMP_VALUE) {
728 					linear_right = MAX_AMP_VALUE;
729 					vp->right_mix_offset = 0;
730 				}
731 				right = FINAL_VOLUME(linear_right);
732 			}
733 		}
734 	} else {
735 		for (i = 0; (vp->left_mix_offset | vp->right_mix_offset)
736 				&& i < count; i++) {
737 			s = *sp++;
738 			DELAYED_MIXATION(left);
739 			MIXATION(right);
740 			if (vp->left_mix_offset) {
741 				vp->left_mix_offset += vp->left_mix_inc;
742 				linear_left += vp->left_mix_inc;
743 				if (linear_left > MAX_AMP_VALUE) {
744 					linear_left = MAX_AMP_VALUE;
745 					vp->left_mix_offset = 0;
746 				}
747 				left = FINAL_VOLUME(linear_left);
748 			}
749 			if (vp->right_mix_offset) {
750 				vp->right_mix_offset += vp->right_mix_inc;
751 				linear_right += vp->right_mix_inc;
752 				if (linear_right > MAX_AMP_VALUE) {
753 					linear_right = MAX_AMP_VALUE;
754 					vp->right_mix_offset = 0;
755 				}
756 				right = FINAL_VOLUME(linear_right);
757 			}
758 		}
759 	}
760 
761 	vp->old_left_mix = linear_left;
762 	vp->old_right_mix = linear_right;
763 	count -= i;
764 	if(vp->pan_delay_rpt == 0) {
765 		for (i = 0; i < count; i++) {
766 			s = *sp++;
767 			MIXATION(left);
768 			MIXATION(right);
769 		}
770 	} else if(vp->panning < 64) {
771 		for (i = 0; i < count; i++) {
772 			s = *sp++;
773 			MIXATION(left);
774 			DELAYED_MIXATION(right);
775 		}
776 	} else {
777 		for (i = 0; i < count; i++) {
778 			s = *sp++;
779 			DELAYED_MIXATION(left);
780 			MIXATION(right);
781 		}
782 	}
783 	vp->pan_delay_wpt = pan_delay_wpt;
784 	vp->pan_delay_spt = pan_delay_spt;
785 }
786 
787 
mix_center_signal(mix_t * sp,int32_t * lp,int v,int count)788 void Mixer::mix_center_signal(
789 		mix_t *sp, int32_t *lp, int v, int count)
790 {
791 	Voice *vp = player->voice + v;
792 	final_volume_t left=vp->left_mix;
793 	int cc, i;
794 	mix_t s;
795 	int32_t linear_left;
796 
797 	if (! (cc = vp->control_counter)) {
798 		cc = control_ratio;
799 		if (update_signal(v))
800 			/* Envelope ran out */
801 			return;
802 		left = vp->left_mix;
803 	}
804 	compute_mix_smoothing(vp);
805 	while (count)
806 		if (cc < count) {
807 			count -= cc;
808 			linear_left = FROM_FINAL_VOLUME(left);
809 			if (vp->left_mix_offset) {
810 				linear_left += vp->left_mix_offset;
811 				if (linear_left > MAX_AMP_VALUE) {
812 					linear_left = MAX_AMP_VALUE;
813 					vp->left_mix_offset = 0;
814 				}
815 				left = FINAL_VOLUME(linear_left);
816 			}
817 			for (i = 0; vp->left_mix_offset && i < cc; i++) {
818 				s = *sp++;
819 				MIXATION(left);
820 				MIXATION(left);
821 				vp->left_mix_offset += vp->left_mix_inc;
822 				linear_left += vp->left_mix_inc;
823 				if (linear_left > MAX_AMP_VALUE) {
824 					linear_left = MAX_AMP_VALUE;
825 					vp->left_mix_offset = 0;
826 				}
827 				left = FINAL_VOLUME(linear_left);
828 			}
829 			vp->old_left_mix = vp->old_right_mix = linear_left;
830 			cc -= i;
831 			for (i = 0; i < cc; i++) {
832 				s = *sp++;
833 				MIXATION(left);
834 				MIXATION(left);
835 			}
836 			cc = control_ratio;
837 			if (update_signal(v))
838 				/* Envelope ran out */
839 				return;
840 			left = vp->left_mix;
841 			compute_mix_smoothing(vp);
842 		} else {
843 			vp->control_counter = cc - count;
844 			linear_left = FROM_FINAL_VOLUME(left);
845 			if (vp->left_mix_offset) {
846 				linear_left += vp->left_mix_offset;
847 				if (linear_left > MAX_AMP_VALUE) {
848 					linear_left = MAX_AMP_VALUE;
849 					vp->left_mix_offset = 0;
850 				}
851 				left = FINAL_VOLUME(linear_left);
852 			}
853 			for (i = 0; vp->left_mix_offset && i < count; i++) {
854 				s = *sp++;
855 				MIXATION(left);
856 				MIXATION(left);
857 				vp->left_mix_offset += vp->left_mix_inc;
858 				linear_left += vp->left_mix_inc;
859 				if (linear_left > MAX_AMP_VALUE) {
860 					linear_left = MAX_AMP_VALUE;
861 					vp->left_mix_offset = 0;
862 				}
863 				left = FINAL_VOLUME(linear_left);
864 			}
865 			vp->old_left_mix = vp->old_right_mix = linear_left;
866 			count -= i;
867 			for (i = 0; i < count; i++) {
868 				s = *sp++;
869 				MIXATION(left);
870 				MIXATION(left);
871 			}
872 			return;
873 		}
874 }
875 
mix_center(mix_t * sp,int32_t * lp,int v,int count)876 void Mixer::mix_center(mix_t *sp, int32_t *lp, int v, int count)
877 {
878 	final_volume_t left = player->voice[v].left_mix;
879 	mix_t s;
880 	int i;
881 	Voice *vp = player->voice + v;
882 	int32_t linear_left;
883 
884 	compute_mix_smoothing(vp);
885 	linear_left = FROM_FINAL_VOLUME(left);
886 	if (vp->left_mix_offset) {
887 		linear_left += vp->left_mix_offset;
888 		if (linear_left > MAX_AMP_VALUE) {
889 			linear_left = MAX_AMP_VALUE;
890 			vp->left_mix_offset = 0;
891 		}
892 		left = FINAL_VOLUME(linear_left);
893 	}
894 	for (i = 0; vp->left_mix_offset && i < count; i++) {
895 		s = *sp++;
896 		MIXATION(left);
897 		MIXATION(left);
898 		vp->left_mix_offset += vp->left_mix_inc;
899 		linear_left += vp->left_mix_inc;
900 		if (linear_left > MAX_AMP_VALUE) {
901 			linear_left = MAX_AMP_VALUE;
902 			vp->left_mix_offset = 0;
903 		}
904 		left = FINAL_VOLUME(linear_left);
905 	}
906 	vp->old_left_mix = vp->old_right_mix = linear_left;
907 	count -= i;
908 	for (i = 0; i < count; i++) {
909 		s = *sp++;
910 		MIXATION(left);
911 		MIXATION(left);
912 	}
913 }
914 
mix_single_signal(mix_t * sp,int32_t * lp,int v,int count)915 void Mixer::mix_single_signal(mix_t *sp, int32_t *lp, int v, int count)
916 {
917 	Voice *vp = player->voice + v;
918 	final_volume_t left = vp->left_mix;
919 	int cc, i;
920 	mix_t s;
921 	int32_t linear_left;
922 
923 	if (!(cc = vp->control_counter)) {
924 		cc = control_ratio;
925 		if (update_signal(v))
926 			/* Envelope ran out */
927 			return;
928 		left = vp->left_mix;
929 	}
930 	compute_mix_smoothing(vp);
931 	while (count)
932 		if (cc < count) {
933 			count -= cc;
934 			linear_left = FROM_FINAL_VOLUME(left);
935 			if (vp->left_mix_offset) {
936 				linear_left += vp->left_mix_offset;
937 				if (linear_left > MAX_AMP_VALUE) {
938 					linear_left = MAX_AMP_VALUE;
939 					vp->left_mix_offset = 0;
940 				}
941 				left = FINAL_VOLUME(linear_left);
942 			}
943 			for (i = 0; vp->left_mix_offset && i < cc; i++) {
944 				s = *sp++;
945 				MIXATION(left);
946 				lp++;
947 				vp->left_mix_offset += vp->left_mix_inc;
948 				linear_left += vp->left_mix_inc;
949 				if (linear_left > MAX_AMP_VALUE) {
950 					linear_left = MAX_AMP_VALUE;
951 					vp->left_mix_offset = 0;
952 				}
953 				left = FINAL_VOLUME(linear_left);
954 			}
955 			vp->old_left_mix = linear_left;
956 			cc -= i;
957 			for (i = 0; i < cc; i++) {
958 				s = *sp++;
959 				MIXATION(left);
960 				lp++;
961 			}
962 			cc = control_ratio;
963 			if (update_signal(v))
964 				/* Envelope ran out */
965 				return;
966 			left = vp->left_mix;
967 			compute_mix_smoothing(vp);
968 		} else {
969 			vp->control_counter = cc - count;
970 			linear_left = FROM_FINAL_VOLUME(left);
971 			if (vp->left_mix_offset) {
972 				linear_left += vp->left_mix_offset;
973 				if (linear_left > MAX_AMP_VALUE) {
974 					linear_left = MAX_AMP_VALUE;
975 					vp->left_mix_offset = 0;
976 				}
977 				left = FINAL_VOLUME(linear_left);
978 			}
979 			for (i = 0; vp->left_mix_offset && i < count; i++) {
980 				s = *sp++;
981 				MIXATION(left);
982 				lp++;
983 				vp->left_mix_offset += vp->left_mix_inc;
984 				linear_left += vp->left_mix_inc;
985 				if (linear_left > MAX_AMP_VALUE) {
986 					linear_left = MAX_AMP_VALUE;
987 					vp->left_mix_offset = 0;
988 				}
989 				left = FINAL_VOLUME(linear_left);
990 			}
991 			vp->old_left_mix = linear_left;
992 			count -= i;
993 			for (i = 0; i < count; i++) {
994 				s = *sp++;
995 				MIXATION(left);
996 				lp++;
997 			}
998 			return;
999 		}
1000 }
1001 
mix_single(mix_t * sp,int32_t * lp,int v,int count)1002 void Mixer::mix_single(mix_t *sp, int32_t *lp, int v, int count)
1003 {
1004 	final_volume_t left = player->voice[v].left_mix;
1005 	mix_t s;
1006 	int i;
1007 	Voice *vp = player->voice + v;
1008 	int32_t linear_left;
1009 
1010 	compute_mix_smoothing(vp);
1011 	linear_left = FROM_FINAL_VOLUME(left);
1012 	if (vp->left_mix_offset) {
1013 		linear_left += vp->left_mix_offset;
1014 		if (linear_left > MAX_AMP_VALUE) {
1015 			linear_left = MAX_AMP_VALUE;
1016 			vp->left_mix_offset = 0;
1017 		}
1018 		left = FINAL_VOLUME(linear_left);
1019 	}
1020 	for (i = 0; vp->left_mix_offset && i < count; i++) {
1021 		s = *sp++;
1022 		MIXATION(left);
1023 		lp++;
1024 		vp->left_mix_offset += vp->left_mix_inc;
1025 		linear_left += vp->left_mix_inc;
1026 		if (linear_left > MAX_AMP_VALUE) {
1027 			linear_left = MAX_AMP_VALUE;
1028 			vp->left_mix_offset = 0;
1029 		}
1030 		left = FINAL_VOLUME(linear_left);
1031 	}
1032 	vp->old_left_mix = linear_left;
1033 	count -= i;
1034 	for (i = 0; i < count; i++) {
1035 		s = *sp++;
1036 		MIXATION(left);
1037 		lp++;
1038 	}
1039 }
1040 
1041 /* Returns 1 if the note died */
update_signal(int v)1042 int Mixer::update_signal(int v)
1043 {
1044 	Voice *vp = &player->voice[v];
1045 
1046 	if (vp->envelope_increment && update_envelope(v))
1047 		return 1;
1048 	if (vp->tremolo_phase_increment)
1049 		update_tremolo(v);
1050 	if (timidity_modulation_envelope && vp->sample->modes & MODES_ENVELOPE)
1051 		update_modulation_envelope(v);
1052 	return apply_envelope_to_amp(v);
1053 }
1054 
update_envelope(int v)1055 int Mixer::update_envelope(int v)
1056 {
1057 	Voice *vp = &player->voice[v];
1058 
1059 	vp->envelope_volume += vp->envelope_increment;
1060 	if ((vp->envelope_increment < 0)
1061 			^ (vp->envelope_volume > vp->envelope_target)) {
1062 		vp->envelope_volume = vp->envelope_target;
1063 		if (recompute_envelope(v))
1064 			return 1;
1065 	}
1066 	return 0;
1067 }
1068 
get_eg_stage(int v,int stage)1069 int Mixer::get_eg_stage(int v, int stage)
1070 {
1071 	int eg_stage;
1072 	Voice *vp = &player->voice[v];
1073 
1074 	eg_stage = stage;
1075 	if (vp->sample->inst_type == INST_SF2) {
1076 		if (stage >= EG_SF_RELEASE) {
1077 			eg_stage = EG_RELEASE;
1078 		}
1079 	} else {
1080 		if (stage == EG_GUS_DECAY) {
1081 			eg_stage = EG_DECAY;
1082 		} else if (stage == EG_GUS_SUSTAIN) {
1083 			eg_stage = EG_NULL;
1084 		} else if (stage >= EG_GUS_RELEASE1) {
1085 			eg_stage = EG_RELEASE;
1086 		}
1087 	}
1088 	return eg_stage;
1089 }
1090 
1091 
1092 /* Returns 1 if envelope runs out */
recompute_envelope(int v)1093 int Mixer::recompute_envelope(int v)
1094 {
1095 	int stage, ch;
1096 	double sustain_time;
1097 	int32_t envelope_width;
1098 	Voice *vp = &player->voice[v];
1099 
1100 	stage = vp->envelope_stage;
1101 	if (stage > EG_GUS_RELEASE3) {
1102 		voice_ran_out(v);
1103 		return 1;
1104 	} else if (stage > EG_GUS_SUSTAIN && vp->envelope_volume <= 0) {
1105 		/* Remove silent player->voice in the release stage */
1106 		voice_ran_out(v);
1107 		return 1;
1108 	}
1109 
1110 	/* Routine to decay the sustain envelope
1111 	 *
1112 	 * Disabled if !min_sustain_time.
1113 	 * min_sustain_time is given in msec, and is the minimum
1114 	 *  time it will take to decay a note to zero.
1115 	 * 2000-3000 msec seem to be decent values to use.
1116 	 */
1117 	if (stage == EG_GUS_RELEASE1 && vp->sample->modes & MODES_ENVELOPE
1118 	    && vp->status & (VOICE_ON | VOICE_SUSTAINED)) {
1119 
1120 		int32_t new_rate;
1121 
1122 		ch = vp->channel;
1123 
1124 		/* Don't adjust the current rate if VOICE_ON */
1125 		if (vp->status & VOICE_ON)
1126 			return 0;
1127 
1128 		if (min_sustain_time > 0 || player->channel[ch].loop_timeout > 0) {
1129 			if (min_sustain_time == 1)
1130 				/* The sustain stage is ignored. */
1131 				return next_stage(v);
1132 
1133 			if (player->channel[ch].loop_timeout > 0 &&
1134 				player->channel[ch].loop_timeout * 1000 < min_sustain_time) {
1135 				/* timeout (See also "#extension timeout" line in *.cfg file */
1136 				sustain_time = player->channel[ch].loop_timeout * 1000;
1137 			}
1138 			else {
1139 				sustain_time = min_sustain_time;
1140 			}
1141 
1142 			/* Sustain must not be 0 or else lots of dead notes! */
1143 			if (player->channel[ch].sostenuto == 0 &&
1144 				player->channel[ch].sustain > 0) {
1145 				sustain_time *= (double)player->channel[ch].sustain / 127.0f;
1146 			}
1147 
1148 			/* Calculate the width of the envelope */
1149 			envelope_width = sustain_time * playback_rate
1150 					 / (1000.0f * (double)control_ratio);
1151 
1152 			if (vp->sample->inst_type == INST_SF2) {
1153 				/* If the instrument is SoundFont, it sustains at the sustain stage. */
1154 				vp->envelope_increment = -1;
1155 				vp->envelope_target = vp->envelope_volume - envelope_width;
1156 				if (vp->envelope_target < 0) {vp->envelope_target = 0;}
1157 			} else {
1158 				/* Otherwise, it decays at the sustain stage. */
1159 				vp->envelope_target = 0;
1160 				new_rate = vp->envelope_volume / envelope_width;
1161 				/* Use the Release1 rate if slower than new rate */
1162 				if (vp->sample->envelope_rate[EG_GUS_RELEASE1] &&
1163 					vp->sample->envelope_rate[EG_GUS_RELEASE1] < new_rate)
1164 						new_rate = vp->sample->envelope_rate[EG_GUS_RELEASE1];
1165 				/* Use the Sustain rate if slower than new rate */
1166 				/* (Sustain rate exists only in GUS patches) */
1167 				if (vp->sample->inst_type == INST_GUS &&
1168 					vp->sample->envelope_rate[EG_GUS_SUSTAIN] &&
1169 					vp->sample->envelope_rate[EG_GUS_SUSTAIN] < new_rate)
1170 						new_rate = vp->sample->envelope_rate[EG_GUS_SUSTAIN];
1171 				/* Avoid freezing */
1172 				if (!new_rate)
1173 					new_rate = 1;
1174 				vp->envelope_increment = -new_rate;
1175 			}
1176 		}
1177 		return 0;
1178 	}
1179 	return next_stage(v);
1180 }
1181 
1182 /* Envelope ran out. */
voice_ran_out(int v)1183 void Mixer::voice_ran_out(int v)
1184 {
1185 	player->free_voice(v);
1186 }
1187 
next_stage(int v)1188 int Mixer::next_stage(int v)
1189 {
1190 	int stage, ch, eg_stage;
1191 	int32_t offset, val;
1192 	double rate, temp_rate;
1193 	Voice *vp = &player->voice[v];
1194 
1195 	stage = vp->envelope_stage++;
1196 	offset = vp->sample->envelope_offset[stage];
1197 	rate = vp->sample->envelope_rate[stage];
1198 	if (vp->envelope_volume == offset
1199 			|| (stage > EG_GUS_SUSTAIN && vp->envelope_volume < offset))
1200 		return recompute_envelope(v);
1201 	ch = vp->channel;
1202 	/* there is some difference between GUS patch and Soundfont at envelope. */
1203 	eg_stage = get_eg_stage(v, stage);
1204 
1205 	/* HACK -- force ramps to occur over 20 msec windows to avoid pops */
1206 	/* Do not apply to attack envelope */
1207 	if (eg_stage > EG_ATTACK)
1208 	{
1209 		temp_rate = control_ratio * (labs(vp->envelope_volume - offset) /
1210 					(playback_rate * 0.02));
1211 		if (temp_rate < 1)
1212 			temp_rate = 1;
1213 		if (rate < 0)
1214 			temp_rate = -temp_rate;
1215 		if (fabs(temp_rate) < fabs(rate))
1216 			rate = temp_rate;
1217 	}
1218 
1219 	/* envelope generator (see also playmidi.[ch]) */
1220 	if (player->ISDRUMCHANNEL(ch))
1221 		val = (player->channel[ch].drums[vp->note] != NULL)
1222 				? player->channel[ch].drums[vp->note]->drum_envelope_rate[eg_stage]
1223 				: -1;
1224 	else {
1225 		if (vp->sample->envelope_keyf[stage])	/* envelope key-follow */
1226 			rate *= pow(2.0, (double) (player->voice[v].note - 60)
1227 					* (double)vp->sample->envelope_keyf[stage] / 1200.0f);
1228 		val = player->channel[ch].envelope_rate[eg_stage];
1229 	}
1230 	if (vp->sample->envelope_velf[stage])	/* envelope velocity-follow */
1231 		rate *= pow(2.0, (double) (player->voice[v].velocity - vp->sample->envelope_velf_bpo)
1232 				* (double)vp->sample->envelope_velf[stage] / 1200.0f);
1233 
1234 	/* just before release-phase, some modifications are necessary */
1235 	if (stage > EG_GUS_SUSTAIN) {
1236 		/* adjusting release-rate for consistent release-time */
1237 		rate *= (double) vp->envelope_volume
1238 				/ vp->sample->envelope_offset[EG_GUS_ATTACK];
1239 		/* calculating current envelope scale and a inverted value for optimization */
1240 		vp->envelope_scale = vp->last_envelope_volume;
1241 		vp->inv_envelope_scale = TIM_FSCALE(OFFSET_MAX / (double)vp->envelope_volume, 16);
1242 	}
1243 
1244 	/* regularizing envelope */
1245 	if (offset < vp->envelope_volume) {	/* decaying phase */
1246 		if (val != -1) {
1247 			if(eg_stage > EG_DECAY) {
1248 				rate *= sc_eg_release_table[val & 0x7f];
1249 			} else {
1250 				rate *= sc_eg_decay_table[val & 0x7f];
1251 			}
1252 
1253 			if (fabs(rate) > OFFSET_MAX)
1254 				rate = (rate > 0) ? OFFSET_MAX : -OFFSET_MAX;
1255 			else if (fabs(rate) < 1)
1256 				rate = (rate > 0) ? 1 : -1;
1257 		}
1258 		if(stage < EG_SF_DECAY && rate > OFFSET_MAX) {	/* instantaneous decay */
1259 			vp->envelope_volume = offset;
1260 			return recompute_envelope(v);
1261 		} else if(rate > vp->envelope_volume - offset) {	/* fastest decay */
1262 			rate = -vp->envelope_volume + offset - 1;
1263 		} else if (rate < 1) {	/* slowest decay */
1264 			rate = -1;
1265 		}
1266 		else {	/* ordinary decay */
1267 			rate = -rate;
1268 		}
1269 	} else {	/* attacking phase */
1270 		if (val != -1) {
1271 			rate *= sc_eg_attack_table[val & 0x7f];
1272 
1273 			if (fabs(rate) > OFFSET_MAX)
1274 				rate = (rate > 0) ? OFFSET_MAX : -OFFSET_MAX;
1275 			else if (fabs(rate) < 1)
1276 				rate = (rate > 0) ? 1 : -1;
1277 		}
1278 		if(stage < EG_SF_DECAY && rate > OFFSET_MAX) {	/* instantaneous attack */
1279 			vp->envelope_volume = offset;
1280 			return recompute_envelope(v);
1281 		} else if(rate > offset - vp->envelope_volume) {	/* fastest attack */
1282 			rate = offset - vp->envelope_volume + 1;
1283 		} else if (rate < 1) {rate =  1;}	/* slowest attack */
1284 	}
1285 
1286 	/* HACK -- force ramps to occur over 20 msec windows to avoid pops */
1287 	/* Do not apply to attack envelope */
1288 	/* Must check again in case the above conditions shortened it */
1289 	if (eg_stage > EG_ATTACK)
1290 	{
1291 		temp_rate = control_ratio * (labs(vp->envelope_volume - offset) /
1292 					(playback_rate * 0.02));
1293 		if (temp_rate < 1)
1294 			temp_rate = 1;
1295 		if (rate < 0)
1296 			temp_rate = -temp_rate;
1297 		if (fabs(temp_rate) < fabs(rate))
1298 			rate = temp_rate;
1299 	}
1300 
1301 	vp->envelope_increment = (int32_t)rate;
1302 	vp->envelope_target = offset;
1303 
1304 	return 0;
1305 }
1306 
update_tremolo(int v)1307 void Mixer::update_tremolo(int v)
1308 {
1309 	Voice *vp = &player->voice[v];
1310 	int32_t depth = vp->tremolo_depth << 7;
1311 
1312 	if(vp->tremolo_delay > 0)
1313 	{
1314 		vp->tremolo_delay -= vp->delay_counter;
1315 		if(vp->tremolo_delay > 0) {
1316 			vp->tremolo_volume = 1.0;
1317 			return;
1318 		}
1319 		vp->tremolo_delay = 0;
1320 	}
1321 	if (vp->tremolo_sweep) {
1322 		/* Update sweep position */
1323 		vp->tremolo_sweep_position += vp->tremolo_sweep;
1324 		if (vp->tremolo_sweep_position >= 1 << SWEEP_SHIFT)
1325 			/* Swept to max amplitude */
1326 			vp->tremolo_sweep = 0;
1327 		else {
1328 			/* Need to adjust depth */
1329 			depth *= vp->tremolo_sweep_position;
1330 			depth >>= SWEEP_SHIFT;
1331 		}
1332 	}
1333 	vp->tremolo_phase += vp->tremolo_phase_increment;
1334 
1335 	if(vp->sample->inst_type == INST_SF2) {
1336 	vp->tremolo_volume = 1.0 + TIM_FSCALENEG(
1337 			lookup_sine(vp->tremolo_phase >> RATE_SHIFT)
1338 			* depth * TREMOLO_AMPLITUDE_TUNING, 17);
1339 	} else {
1340 	vp->tremolo_volume = 1.0 + TIM_FSCALENEG(
1341 			lookup_sine(vp->tremolo_phase >> RATE_SHIFT)
1342 			* depth * TREMOLO_AMPLITUDE_TUNING, 17);
1343 	}
1344 	/* I'm not sure about the +1.0 there -- it makes tremoloed voices'
1345 	 *  volumes on average the lower the higher the tremolo amplitude.
1346 	 */
1347 }
1348 
apply_envelope_to_amp(int v)1349 int Mixer::apply_envelope_to_amp(int v)
1350 {
1351 	Voice *vp = &player->voice[v];
1352 	double lamp = vp->left_amp, ramp,
1353 		*v_table = vp->sample->inst_type == INST_SF2 ? sb_vol_table : player->vol_table;
1354 	int32_t la, ra;
1355 
1356 	if (vp->panned == PANNED_MYSTERY) {
1357 		ramp = vp->right_amp;
1358 		if (vp->tremolo_phase_increment) {
1359 			lamp *= vp->tremolo_volume;
1360 			ramp *= vp->tremolo_volume;
1361 		}
1362 		if (vp->sample->modes & MODES_ENVELOPE) {
1363 			if (vp->envelope_stage > 3)
1364 				vp->last_envelope_volume = v_table[
1365 						imuldiv16(vp->envelope_volume,
1366 						vp->inv_envelope_scale) >> 20]
1367 						* vp->envelope_scale;
1368 			else if (vp->envelope_stage > 1)
1369 				vp->last_envelope_volume = v_table[
1370 						vp->envelope_volume >> 20];
1371 			else
1372 				vp->last_envelope_volume = attack_vol_table[
1373 				vp->envelope_volume >> 20];
1374 			lamp *= vp->last_envelope_volume;
1375 			ramp *= vp->last_envelope_volume;
1376 		}
1377 		la = TIM_FSCALE(lamp, AMP_BITS);
1378 		if (la > MAX_AMP_VALUE)
1379 			la = MAX_AMP_VALUE;
1380 		ra = TIM_FSCALE(ramp, AMP_BITS);
1381 		if (ra > MAX_AMP_VALUE)
1382 			ra = MAX_AMP_VALUE;
1383 		if ((vp->status & (VOICE_OFF | VOICE_SUSTAINED))
1384 				&& (la | ra) <= 0) {
1385 			player->free_voice(v);
1386 			return 1;
1387 		}
1388 		vp->left_mix = FINAL_VOLUME(la);
1389 		vp->right_mix = FINAL_VOLUME(ra);
1390 	} else {
1391 		if (vp->tremolo_phase_increment)
1392 			lamp *= vp->tremolo_volume;
1393 		if (vp->sample->modes & MODES_ENVELOPE) {
1394 			if (vp->envelope_stage > 3)
1395 				vp->last_envelope_volume = v_table[
1396 						imuldiv16(vp->envelope_volume,
1397 						vp->inv_envelope_scale) >> 20]
1398 						* vp->envelope_scale;
1399 			else if (vp->envelope_stage > 1)
1400 				vp->last_envelope_volume = v_table[
1401 						vp->envelope_volume >> 20];
1402 			else
1403 				vp->last_envelope_volume = attack_vol_table[
1404 				vp->envelope_volume >> 20];
1405 			lamp *= vp->last_envelope_volume;
1406 		}
1407 		la = TIM_FSCALE(lamp, AMP_BITS);
1408 		if (la > MAX_AMP_VALUE)
1409 		la = MAX_AMP_VALUE;
1410 		if ((vp->status & (VOICE_OFF | VOICE_SUSTAINED))
1411 				&& la <= 0) {
1412 			player->free_voice(v);
1413 			return 1;
1414 		}
1415 		vp->left_mix = FINAL_VOLUME(la);
1416 	}
1417 	return 0;
1418 }
1419 
compute_mix_smoothing(Voice * vp)1420 void Mixer::compute_mix_smoothing(Voice *vp)
1421 {
1422 	int32_t max_win, delta;
1423 
1424 	/* reduce popping -- ramp the amp over a 20 msec window */
1425 	max_win = (playback_rate * 0.02) / control_ratio;
1426 	delta = FROM_FINAL_VOLUME(vp->left_mix) - vp->old_left_mix;
1427 	if (labs(delta) > max_win) {
1428 		vp->left_mix_inc = delta / max_win;
1429 		vp->left_mix_offset = vp->left_mix_inc * (1 - max_win);
1430 	} else if (delta) {
1431 		vp->left_mix_inc = -1;
1432 		if (delta > 0)
1433 			vp->left_mix_inc = 1;
1434 		vp->left_mix_offset = vp->left_mix_inc - delta;
1435 	}
1436 	delta = FROM_FINAL_VOLUME(vp->right_mix) - vp->old_right_mix;
1437 	if (labs(delta) > max_win) {
1438 		vp->right_mix_inc = delta / max_win;
1439 		vp->right_mix_offset = vp->right_mix_inc * (1 - max_win);
1440 	} else if (delta) {
1441 		vp->right_mix_inc = -1;
1442 		if (delta > 0)
1443 			vp->right_mix_inc = 1;
1444 		vp->right_mix_offset = vp->right_mix_inc - delta;
1445 	}
1446 }
1447 
update_modulation_envelope(int v)1448 int Mixer::update_modulation_envelope(int v)
1449 {
1450 	Voice *vp = &player->voice[v];
1451 
1452 	if(vp->modenv_delay > 0) {
1453 		vp->modenv_delay -= vp->delay_counter;
1454 		if(vp->modenv_delay > 0) {return 1;}
1455 		vp->modenv_delay = 0;
1456 	}
1457 	vp->modenv_volume += vp->modenv_increment;
1458 	if ((vp->modenv_increment < 0)
1459 			^ (vp->modenv_volume > vp->modenv_target)) {
1460 		vp->modenv_volume = vp->modenv_target;
1461 		if (recompute_modulation_envelope(v)) {
1462 			apply_modulation_envelope(v);
1463 			return 1;
1464 		}
1465 	}
1466 
1467 	apply_modulation_envelope(v);
1468 
1469 	return 0;
1470 }
1471 
apply_modulation_envelope(int v)1472 int Mixer::apply_modulation_envelope(int v)
1473 {
1474 	Voice *vp = &player->voice[v];
1475 
1476 	if(!timidity_modulation_envelope) {return 0;}
1477 
1478 	if (vp->sample->modes & MODES_ENVELOPE) {
1479 		vp->last_modenv_volume = modenv_vol_table[vp->modenv_volume >> 20];
1480 	}
1481 
1482 	player->recompute_voice_filter(v);
1483 	if(!(vp->porta_control_ratio && vp->porta_control_counter == 0)) {
1484 		player->recompute_freq(v);
1485 	}
1486 	return 0;
1487 }
1488 
modenv_next_stage(int v)1489 int Mixer::modenv_next_stage(int v)
1490 {
1491 	int stage, ch, eg_stage;
1492 	int32_t offset, val;
1493 	double rate;
1494 	Voice *vp = &player->voice[v];
1495 
1496 	stage = vp->modenv_stage++;
1497 	offset = vp->sample->modenv_offset[stage];
1498 	rate = vp->sample->modenv_rate[stage];
1499 	if (vp->modenv_volume == offset
1500 			|| (stage > EG_GUS_SUSTAIN && vp->modenv_volume < offset))
1501 		return recompute_modulation_envelope(v);
1502 	else if(stage < EG_SF_DECAY && rate > OFFSET_MAX) {	/* instantaneous attack */
1503 			vp->modenv_volume = offset;
1504 			return recompute_modulation_envelope(v);
1505 	}
1506 	ch = vp->channel;
1507 	/* there is some difference between GUS patch and Soundfont at envelope. */
1508 	eg_stage = get_eg_stage(v, stage);
1509 
1510 	/* envelope generator (see also playmidi.[ch]) */
1511 	if (player->ISDRUMCHANNEL(ch))
1512 		val = (player->channel[ch].drums[vp->note] != NULL)
1513 				? player->channel[ch].drums[vp->note]->drum_envelope_rate[eg_stage]
1514 				: -1;
1515 	else {
1516 		if (vp->sample->modenv_keyf[stage])	/* envelope key-follow */
1517 			rate *= pow(2.0, (double) (player->voice[v].note - 60)
1518 					* (double)vp->sample->modenv_keyf[stage] / 1200.0f);
1519 		val = player->channel[ch].envelope_rate[eg_stage];
1520 	}
1521 	if (vp->sample->modenv_velf[stage])
1522 		rate *= pow(2.0, (double) (player->voice[v].velocity - vp->sample->modenv_velf_bpo)
1523 				* (double)vp->sample->modenv_velf[stage] / 1200.0f);
1524 
1525 	/* just before release-phase, some modifications are necessary */
1526 	if (stage > EG_GUS_SUSTAIN) {
1527 		/* adjusting release-rate for consistent release-time */
1528 		rate *= (double) vp->modenv_volume
1529 				/ vp->sample->modenv_offset[EG_GUS_ATTACK];
1530 	}
1531 
1532 	/* regularizing envelope */
1533 	if (offset < vp->modenv_volume) {	/* decaying phase */
1534 		if (val != -1) {
1535 			if(stage > EG_DECAY) {
1536 				rate *= sc_eg_release_table[val & 0x7f];
1537 			} else {
1538 				rate *= sc_eg_decay_table[val & 0x7f];
1539 			}
1540 		}
1541 		if(rate > vp->modenv_volume - offset) {	/* fastest decay */
1542 			rate = -vp->modenv_volume + offset - 1;
1543 		} else if (rate < 1) {	/* slowest decay */
1544 			rate = -1;
1545 		} else {	/* ordinary decay */
1546 			rate = -rate;
1547 		}
1548 	} else {	/* attacking phase */
1549 		if (val != -1)
1550 			rate *= sc_eg_attack_table[val & 0x7f];
1551 		if(rate > offset - vp->modenv_volume) {	/* fastest attack */
1552 			rate = offset - vp->modenv_volume + 1;
1553 		} else if (rate < 1) {rate = 1;}	/* slowest attack */
1554 	}
1555 
1556 	vp->modenv_increment = (int32_t)rate;
1557 	vp->modenv_target = offset;
1558 
1559 	return 0;
1560 }
1561 
recompute_modulation_envelope(int v)1562 int Mixer::recompute_modulation_envelope(int v)
1563 {
1564 	int stage, ch;
1565 	double sustain_time;
1566 	int32_t modenv_width;
1567 	Voice *vp = &player->voice[v];
1568 
1569 	if(!timidity_modulation_envelope) {return 0;}
1570 
1571 	stage = vp->modenv_stage;
1572 	if (stage > EG_GUS_RELEASE3) {return 1;}
1573 	else if (stage > EG_GUS_SUSTAIN && vp->modenv_volume <= 0) {
1574 		return 1;
1575 	}
1576 
1577 	/* Routine to sustain modulation envelope
1578 	 *
1579 	 * Disabled if !min_sustain_time.
1580 	 * min_sustain_time is given in msec, and is the minimum
1581 	 *  time it will take to sustain a note.
1582 	 * 2000-3000 msec seem to be decent values to use.
1583 	 */
1584 	if (stage == EG_GUS_RELEASE1 && vp->sample->modes & MODES_ENVELOPE
1585 	    && vp->status & (VOICE_ON | VOICE_SUSTAINED)) {
1586 		ch = vp->channel;
1587 
1588 		/* Don't adjust the current rate if VOICE_ON */
1589 		if (vp->status & VOICE_ON)
1590 			return 0;
1591 
1592 		if (min_sustain_time > 0 || player->channel[ch].loop_timeout > 0) {
1593 			if (min_sustain_time == 1)
1594 				/* The sustain stage is ignored. */
1595 				return modenv_next_stage(v);
1596 
1597 			if (player->channel[ch].loop_timeout > 0 &&
1598 				player->channel[ch].loop_timeout * 1000 < min_sustain_time) {
1599 				/* timeout (See also "#extension timeout" line in *.cfg file */
1600 				sustain_time = player->channel[ch].loop_timeout * 1000;
1601 			}
1602 			else {
1603 				sustain_time = min_sustain_time;
1604 			}
1605 
1606 			/* Sustain must not be 0 or else lots of dead notes! */
1607 			if (player->channel[ch].sostenuto == 0 &&
1608 				player->channel[ch].sustain > 0) {
1609 				sustain_time *= (double)player->channel[ch].sustain / 127.0f;
1610 			}
1611 
1612 			/* Calculate the width of the envelope */
1613 			modenv_width = sustain_time * playback_rate
1614 				       / (1000.0f * (double)control_ratio);
1615 			vp->modenv_increment = -1;
1616 			vp->modenv_target = vp->modenv_volume - modenv_width;
1617 			if (vp->modenv_target < 0) {vp->modenv_target = 0;}
1618 		}
1619 		return 0;
1620 	}
1621 	return modenv_next_stage(v);
1622 }
1623 
1624 }
1625