1 /*
2     TiMidity -- Experimental MIDI to WAVE converter
3     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
4 
5     Suddenly, you realize that this program is free software; you get
6     an overwhelming urge to redistribute it and/or modify it under the
7     terms of the GNU General Public License as published by the Free
8     Software Foundation; either version 2 of the License, or (at your
9     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 another copy of the GNU General Public
17     License along with this program; if not, write to the Free
18     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     I bet they'll be amazed.
20 
21     mix.c (+ output.c)
22 */
23 
24 #include "config.h"
25 
26 #include "common.h"
27 #include "instrum.h"
28 #include "playmidi.h"
29 #include "ctrlmode.h"
30 #include "tables.h"
31 #include "resample.h"
32 #include "mix.h"
33 
34 /* Returns 1 if envelope runs out */
recompute_envelope(int v)35 int recompute_envelope(int v)
36 {
37 	int stage = voice[v].envelope_stage;
38 
39 	if (stage > 5)
40 	{
41 		/* Envelope ran out. */
42 		int tmp=(voice[v].status == VOICE_DIE); /* Already displayed as dead */
43 		voice[v].status = VOICE_FREE;
44 		if(!tmp)
45 			return 1;
46 	}
47 
48 	if (voice[v].sample->modes & MODES_ENVELOPE)
49 	{
50 		if (voice[v].status==VOICE_ON || voice[v].status==VOICE_SUSTAINED)
51 		{
52 			if (stage > 2)
53 			{
54 				/* Freeze envelope until note turns off. Trumpets want this. */
55 				voice[v].envelope_increment=0;
56 				return 0;
57 			}
58 		}
59 	}
60 	voice[v].envelope_stage=stage+1;
61 
62 	if (voice[v].envelope_volume==voice[v].sample->envelope_offset[stage])
63 		return recompute_envelope(v);
64 
65 	voice[v].envelope_target    = voice[v].sample->envelope_offset[stage];
66 	voice[v].envelope_increment = voice[v].sample->envelope_rate[stage];
67 
68 	if (voice[v].envelope_target < voice[v].envelope_volume)
69 		voice[v].envelope_increment = -voice[v].envelope_increment;
70 
71 	return 0;
72 }
73 
apply_envelope_to_amp(int v)74 void apply_envelope_to_amp(int v)
75 {
76 	double lamp = voice[v].left_amp;
77 
78 	if (voice[v].panned == PANNED_MYSTERY)
79 	{
80 		double lramp=voice[v].lr_amp;
81 		double ramp=voice[v].right_amp;
82 		double ceamp=voice[v].ce_amp;
83 		double rramp=voice[v].rr_amp;
84 		double lfeamp=voice[v].lfe_amp;
85 
86 		if (voice[v].tremolo_phase_increment)
87 		{
88 			double tv = voice[v].tremolo_volume;
89 
90 			lramp  *= tv;
91 			lamp   *= tv;
92 			ceamp  *= tv;
93 			ramp   *= tv;
94 			rramp  *= tv;
95 			lfeamp *= tv;
96 		}
97 
98 		if (voice[v].sample->modes & MODES_ENVELOPE)
99 		{
100 			double ev = (double)vol_table[voice[v].envelope_volume>>23];
101 
102 			lramp  *= ev;
103 			lamp   *= ev;
104 			ceamp  *= ev;
105 			ramp   *= ev;
106 			rramp  *= ev;
107 			lfeamp *= ev;
108 		}
109 
110 		s32_t la   = (s32_t)FSCALE(lamp,AMP_BITS);
111 		s32_t ra   = (s32_t)FSCALE(ramp,AMP_BITS);
112 		s32_t lra  = (s32_t)FSCALE(lramp,AMP_BITS);
113 		s32_t rra  = (s32_t)FSCALE(rramp,AMP_BITS);
114 		s32_t cea  = (s32_t)FSCALE(ceamp,AMP_BITS);
115 		s32_t lfea = (s32_t)FSCALE(lfeamp,AMP_BITS);
116 
117 		if (la>MAX_AMP_VALUE) la=MAX_AMP_VALUE;
118 		if (ra>MAX_AMP_VALUE) ra=MAX_AMP_VALUE;
119 		if (lra>MAX_AMP_VALUE) lra=MAX_AMP_VALUE;
120 		if (rra>MAX_AMP_VALUE) rra=MAX_AMP_VALUE;
121 		if (cea>MAX_AMP_VALUE) cea=MAX_AMP_VALUE;
122 		if (lfea>MAX_AMP_VALUE) lfea=MAX_AMP_VALUE;
123 
124 		voice[v].lr_mix=FINAL_VOLUME(lra);
125 		voice[v].left_mix=FINAL_VOLUME(la);
126 		voice[v].ce_mix=FINAL_VOLUME(cea);
127 		voice[v].right_mix=FINAL_VOLUME(ra);
128 		voice[v].rr_mix=FINAL_VOLUME(rra);
129 		voice[v].lfe_mix=FINAL_VOLUME(lfea);
130 	}
131 	else
132 	{
133 		if (voice[v].tremolo_phase_increment)
134 			lamp *= voice[v].tremolo_volume;
135 
136 		if (voice[v].sample->modes & MODES_ENVELOPE)
137 			lamp *= (double)vol_table[voice[v].envelope_volume>>23];
138 
139 		s32_t la = (s32_t)FSCALE(lamp,AMP_BITS);
140 
141 		if (la > MAX_AMP_VALUE)
142 			la = MAX_AMP_VALUE;
143 
144 		voice[v].left_mix=FINAL_VOLUME(la);
145 	}
146 }
147 
update_envelope(int v)148 static int update_envelope(int v)
149 {
150 	voice[v].envelope_volume += voice[v].envelope_increment;
151 
152 	/* Why is there no ^^ operator?? */
153 	if (((voice[v].envelope_increment < 0) &&
154 		 (voice[v].envelope_volume <= voice[v].envelope_target)) ||
155 		((voice[v].envelope_increment > 0) &&
156 		 (voice[v].envelope_volume >= voice[v].envelope_target)))
157 	{
158 		voice[v].envelope_volume = voice[v].envelope_target;
159 
160 		if (recompute_envelope(v))
161 			return 1;
162 	}
163 
164 	return 0;
165 }
166 
update_tremolo(int v)167 static void update_tremolo(int v)
168 {
169 	s32_t depth=voice[v].sample->tremolo_depth<<7;
170 
171 	if (voice[v].tremolo_sweep)
172 	{
173 		/* Update sweep position */
174 
175 		voice[v].tremolo_sweep_position += voice[v].tremolo_sweep;
176 
177 		if (voice[v].tremolo_sweep_position >= (1<<SWEEP_SHIFT))
178 			voice[v].tremolo_sweep=0; /* Swept to max amplitude */
179 		else
180 		{
181 			/* Need to adjust depth */
182 			depth *= voice[v].tremolo_sweep_position;
183 			depth >>= SWEEP_SHIFT;
184 		}
185 	}
186 
187 	voice[v].tremolo_phase += voice[v].tremolo_phase_increment;
188 
189 	/* if (voice[v].tremolo_phase >= (SINE_CYCLE_LENGTH<<RATE_SHIFT))
190 	   voice[v].tremolo_phase -= SINE_CYCLE_LENGTH<<RATE_SHIFT;  */
191 
192 	double sineval = sine(voice[v].tremolo_phase >> RATE_SHIFT);
193 
194 	voice[v].tremolo_volume = (double)
195 		(1.0 - FSCALENEG((sineval+1.0) * depth * TREMOLO_AMPLITUDE_TUNING, 17));
196 
197 	/* I'm not sure about the +1.0 there -- it makes tremoloed voices'
198 	   volumes on average the lower the higher the tremolo amplitude. */
199 }
200 
201 /* Returns 1 if the note died */
update_signal(int v)202 static int update_signal(int v)
203 {
204 	if (voice[v].envelope_increment && update_envelope(v))
205 		return 1;
206 
207 	if (voice[v].tremolo_phase_increment)
208 		update_tremolo(v);
209 
210 	apply_envelope_to_amp(v);
211 	return 0;
212 }
213 
214 
215 //------------------------------------------------------------------------
216 
217 #define MIXATION(a)  *lp++ += (a)*s;
218 
219 #define MIXSKIP lp++
220 #define MIXMAX(a,b) *lp++ += ((a>b)?a:b) * s
221 #define MIXCENT(a,b) *lp++ += (a/2+b/2) * s
222 #define MIXHALF(a)	*lp++ += (a>>1)*s;
223 
mix_mystery_signal(const resample_t * sp,s32_t * lp,int v,int count)224 static void mix_mystery_signal(const resample_t *sp, s32_t *lp, int v, int count)
225 {
226 	Voice *vp = voice + v;
227 
228 	final_volume_t left_rear = vp->lr_mix;
229 	final_volume_t left = vp->left_mix;
230 	final_volume_t center = vp->ce_mix;
231 	final_volume_t right = vp->right_mix;
232 	final_volume_t right_rear = vp->rr_mix;
233 	final_volume_t lfe = vp->lfe_mix;
234 
235 	int cc;
236 
237 	if (! (cc = vp->control_counter))
238 	{
239 		cc = control_ratio;
240 
241 		if (update_signal(v))
242 			return;	/* Envelope ran out */
243 
244 		left_rear = vp->lr_mix;
245 		left = vp->left_mix;
246 		center = vp->ce_mix;
247 		right = vp->right_mix;
248 		right_rear = vp->rr_mix;
249 		lfe = vp->lfe_mix;
250 	}
251 
252 	while (count)
253 	{
254 		if (cc < count)
255 		{
256 			count -= cc;
257 			while (cc--)
258 			{
259 				resample_t s = *sp++;
260 
261 				MIXATION(left);
262 				MIXATION(right);
263 
264 				if (num_ochannels >= 4)
265 				{
266 					MIXATION(left_rear);
267 					MIXATION(right_rear);
268 				}
269 				if (num_ochannels == 6)
270 				{
271 					MIXATION(center);
272 					MIXATION(lfe);
273 				}
274 			}
275 			cc = control_ratio;
276 
277 			if (update_signal(v))
278 				return;	/* Envelope ran out */
279 
280 			left_rear = vp->lr_mix;
281 			left = vp->left_mix;
282 			center = vp->ce_mix;
283 			right = vp->right_mix;
284 			right_rear = vp->rr_mix;
285 			lfe = vp->lfe_mix;
286 		}
287 		else
288 		{
289 			vp->control_counter = cc - count;
290 
291 			while (count--)
292 			{
293 				resample_t s = *sp++;
294 
295 				MIXATION(left);
296 				MIXATION(right);
297 
298 				if (num_ochannels >= 4)
299 				{
300 					MIXATION(left_rear);
301 					MIXATION(right_rear);
302 				}
303 				if (num_ochannels == 6)
304 				{
305 					MIXATION(center);
306 					MIXATION(lfe);
307 				}
308 			}
309 			return;
310 		}
311 	}
312 }
313 
mix_center_signal(const resample_t * sp,s32_t * lp,int v,int count)314 static void mix_center_signal(const resample_t *sp, s32_t *lp, int v, int count)
315 {
316 	Voice *vp = voice + v;
317 	final_volume_t left = vp->left_mix;
318 	int cc;
319 
320 	if (!(cc = vp->control_counter))
321 	{
322 		cc = control_ratio;
323 		if (update_signal(v))
324 			return;	/* Envelope ran out */
325 		left = vp->left_mix;
326 	}
327 
328 	while (count)
329 	{
330 		if (cc < count)
331 		{
332 			count -= cc;
333 			while (cc--)
334 			{
335 				resample_t s = *sp++;
336 
337 				if (num_ochannels == 2)
338 				{
339 					MIXATION(left);
340 					MIXATION(left);
341 				}
342 				else if (num_ochannels == 4)
343 				{
344 					MIXATION(left);
345 					MIXSKIP;
346 					MIXATION(left);
347 					MIXSKIP;
348 				}
349 				else if (num_ochannels == 6)
350 				{
351 					MIXSKIP;
352 					MIXSKIP;
353 					MIXSKIP;
354 					MIXSKIP;
355 					MIXATION(left);
356 					MIXATION(left);
357 				}
358 			}
359 			cc = control_ratio;
360 
361 			if (update_signal(v))
362 				return;	/* Envelope ran out */
363 
364 			left = vp->left_mix;
365 		}
366 		else
367 		{
368 			vp->control_counter = cc - count;
369 
370 			while (count--)
371 			{
372 				resample_t s = *sp++;
373 
374 				if (num_ochannels == 2)
375 				{
376 					MIXATION(left);
377 					MIXATION(left);
378 				}
379 				else if (num_ochannels == 4)
380 				{
381 					MIXATION(left);
382 					MIXSKIP;
383 					MIXATION(left);
384 					MIXSKIP;
385 				}
386 				else if (num_ochannels == 6)
387 				{
388 					MIXSKIP;
389 					MIXSKIP;
390 					MIXSKIP;
391 					MIXSKIP;
392 					MIXATION(left);
393 					MIXATION(left);
394 				}
395 			}
396 			return;
397 		}
398 	}
399 }
400 
mix_single_left_signal(const resample_t * sp,s32_t * lp,int v,int count)401 static void mix_single_left_signal(const resample_t *sp, s32_t *lp, int v, int count)
402 {
403 	Voice *vp = voice + v;
404 	final_volume_t left = vp->left_mix;
405 	int cc;
406 
407 	if (!(cc = vp->control_counter))
408 	{
409 		cc = control_ratio;
410 		if (update_signal(v))
411 			return;	/* Envelope ran out */
412 		left = vp->left_mix;
413 	}
414 
415 	while (count)
416 	{
417 		if (cc < count)
418 		{
419 			count -= cc;
420 			while (cc--)
421 			{
422 				resample_t s = *sp++;
423 
424 				if (num_ochannels == 2)
425 				{
426 					MIXATION(left);
427 					MIXSKIP;
428 				}
429 				if (num_ochannels >= 4)
430 				{
431 					MIXHALF(left);
432 					MIXSKIP;
433 					MIXATION(left);
434 					MIXSKIP;
435 				}
436 				if (num_ochannels == 6)
437 				{
438 					MIXSKIP;
439 					MIXATION(left);
440 				}
441 			}
442 			cc = control_ratio;
443 
444 			if (update_signal(v))
445 				return;	/* Envelope ran out */
446 
447 			left = vp->left_mix;
448 		}
449 		else
450 		{
451 			vp->control_counter = cc - count;
452 
453 			while (count--)
454 			{
455 				resample_t s = *sp++;
456 
457 				if (num_ochannels == 2)
458 				{
459 					MIXATION(left);
460 					MIXSKIP;
461 				}
462 				if (num_ochannels >= 4)
463 				{
464 					MIXHALF(left);
465 					MIXSKIP;
466 					MIXATION(left);
467 					MIXSKIP;
468 				}
469 				if (num_ochannels == 6)
470 				{
471 					MIXSKIP;
472 					MIXATION(left);
473 				}
474 			}
475 			return;
476 		}
477 	}
478 }
479 
mix_single_right_signal(const resample_t * sp,s32_t * lp,int v,int count)480 static void mix_single_right_signal(const resample_t *sp, s32_t *lp, int v, int count)
481 {
482 	Voice *vp = voice + v;
483 	final_volume_t left = vp->left_mix;
484 	int cc;
485 
486 	if (! (cc = vp->control_counter))
487 	{
488 		cc = control_ratio;
489 
490 		if (update_signal(v))
491 			return;	/* Envelope ran out */
492 
493 		left = vp->left_mix;
494 	}
495 
496 	while (count)
497 	{
498 		if (cc < count)
499 		{
500 			count -= cc;
501 			while (cc--)
502 			{
503 				resample_t s = *sp++;
504 
505 				if (num_ochannels == 2)
506 				{
507 					MIXSKIP;
508 					MIXATION(left);
509 				}
510 				if (num_ochannels >= 4)
511 				{
512 					MIXSKIP;
513 					MIXHALF(left);
514 					MIXSKIP;
515 					MIXATION(left);
516 				}
517 				if (num_ochannels == 6)
518 				{
519 					MIXSKIP;
520 					MIXATION(left);
521 				}
522 			}
523 			cc = control_ratio;
524 
525 			if (update_signal(v))
526 				return;	/* Envelope ran out */
527 
528 			left = vp->left_mix;
529 		}
530 		else
531 		{
532 			vp->control_counter = cc - count;
533 
534 			while (count--)
535 			{
536 				resample_t s = *sp++;
537 
538 				if (num_ochannels == 2)
539 				{
540 					MIXSKIP;
541 					MIXATION(left);
542 				}
543 				if (num_ochannels >= 4)
544 				{
545 					MIXSKIP;
546 					MIXHALF(left);
547 					MIXSKIP;
548 					MIXATION(left);
549 				}
550 				if (num_ochannels == 6)
551 				{
552 					MIXSKIP;
553 					MIXATION(left);
554 				}
555 			}
556 			return;
557 		}
558 	}
559 }
560 
mix_mono_signal(const resample_t * sp,s32_t * lp,int v,int count)561 static void mix_mono_signal(const resample_t *sp, s32_t *lp, int v, int count)
562 {
563 	Voice *vp = voice + v;
564 	final_volume_t left = vp->left_mix;
565 	int cc;
566 
567 	if (! (cc = vp->control_counter))
568 	{
569 		cc = control_ratio;
570 
571 		if (update_signal(v))
572 			return;	/* Envelope ran out */
573 
574 		left = vp->left_mix;
575 	}
576 
577 	while (count)
578 	{
579 		if (cc < count)
580 		{
581 			count -= cc;
582 			while (cc--)
583 			{
584 				resample_t s = *sp++;
585 
586 				MIXATION(left);
587 			}
588 			cc = control_ratio;
589 
590 			if (update_signal(v))
591 				return;	/* Envelope ran out */
592 
593 			left = vp->left_mix;
594 		}
595 		else
596 		{
597 			vp->control_counter = cc - count;
598 
599 			while (count--)
600 			{
601 				resample_t s = *sp++;
602 
603 				MIXATION(left);
604 			}
605 			return;
606 		}
607 	}
608 }
609 
mix_mystery(const resample_t * sp,s32_t * lp,int v,int count)610 static void mix_mystery(const resample_t *sp, s32_t *lp, int v, int count)
611 {
612 	final_volume_t left_rear=voice[v].lr_mix;
613 	final_volume_t left=voice[v].left_mix;
614 	final_volume_t center=voice[v].ce_mix;
615 	final_volume_t right=voice[v].right_mix;
616 	final_volume_t right_rear=voice[v].rr_mix;
617 	final_volume_t lfe=voice[v].lfe_mix;
618 
619 	while (count--)
620 	{
621 		resample_t s = *sp++;
622 
623 		MIXATION(left);
624 		MIXATION(right);
625 
626 		if (num_ochannels >= 4)
627 		{
628 			MIXATION(left_rear);
629 			MIXATION(right_rear);
630 		}
631 		if (num_ochannels == 6)
632 		{
633 			MIXATION(center);
634 			MIXATION(lfe);
635 		}
636 	}
637 }
638 
mix_center(const resample_t * sp,s32_t * lp,int v,int count)639 static void mix_center(const resample_t *sp, s32_t *lp, int v, int count)
640 {
641 	final_volume_t left=voice[v].left_mix;
642 
643 	while (count--)
644 	{
645 		resample_t s = *sp++;
646 
647 		if (num_ochannels == 2)
648 		{
649 			MIXATION(left);
650 			MIXATION(left);
651 		}
652 		else if (num_ochannels == 4)
653 		{
654 			MIXATION(left);
655 			MIXATION(left);
656 			MIXSKIP;
657 			MIXSKIP;
658 		}
659 		else if (num_ochannels == 6)
660 		{
661 			MIXSKIP;
662 			MIXSKIP;
663 			MIXSKIP;
664 			MIXSKIP;
665 			MIXATION(left);
666 			MIXATION(left);
667 		}
668 	}
669 }
670 
mix_single_left(const resample_t * sp,s32_t * lp,int v,int count)671 static void mix_single_left(const resample_t *sp, s32_t *lp, int v, int count)
672 {
673 	final_volume_t left=voice[v].left_mix;
674 
675 	while (count--)
676 	{
677 		resample_t s = *sp++;
678 
679 		if (num_ochannels == 2)
680 		{
681 			MIXATION(left);
682 			MIXSKIP;
683 		}
684 		if (num_ochannels >= 4)
685 		{
686 			MIXHALF(left);
687 			MIXSKIP;
688 			MIXATION(left);
689 			MIXSKIP;
690 		}
691 		if (num_ochannels == 6)
692 		{
693 			MIXSKIP;
694 			MIXATION(left);
695 		}
696 	}
697 }
mix_single_right(const resample_t * sp,s32_t * lp,int v,int count)698 static void mix_single_right(const resample_t *sp, s32_t *lp, int v, int count)
699 {
700 	final_volume_t left=voice[v].left_mix;
701 
702 	while (count--)
703 	{
704 		resample_t s = *sp++;
705 
706 		if (num_ochannels == 2)
707 		{
708 			MIXSKIP;
709 			MIXATION(left);
710 		}
711 		if (num_ochannels >= 4)
712 		{
713 			MIXSKIP;
714 			MIXHALF(left);
715 			MIXSKIP;
716 			MIXATION(left);
717 		}
718 		if (num_ochannels == 6)
719 		{
720 			MIXSKIP;
721 			MIXATION(left);
722 		}
723 	}
724 }
725 
mix_mono(const resample_t * sp,s32_t * lp,int v,int count)726 static void mix_mono(const resample_t *sp, s32_t *lp, int v, int count)
727 {
728 	final_volume_t left=voice[v].left_mix;
729 
730 	while (count--)
731 	{
732 		resample_t s = *sp++;
733 
734 		MIXATION(left);
735 	}
736 }
737 
738 /* Ramp a note out in c samples */
ramp_out(const resample_t * sp,s32_t * lp,int v,int count)739 static void ramp_out(const resample_t *sp, s32_t *lp, int v, int count)
740 {
741 	/* should be final_volume_t, but uint8 gives trouble. */
742 	s32_t left_rear, left, center, right, right_rear, lfe, li, ri;
743 
744 	/* Fix by James Caldwell */
745 	if (count == 0)
746 		count = 1;
747 
748 	left = voice[v].left_mix;
749 
750 	li = -(left/count);
751 	if (!li)
752 		li = -1;
753 
754 	/* printf("Ramping out: left=%d, c=%d, li=%d\n", left, count, li); */
755 
756 	if (play_mode_encoding & PE_MONO)
757 	{
758 		/* Mono output */
759 		while (count--)
760 		{
761 			left += li;
762 			if (left<0)
763 				return;
764 
765 			resample_t s = *sp++;
766 
767 			MIXATION(left);
768 		}
769 		return;
770 	}
771 
772 	if (voice[v].panned==PANNED_MYSTERY)
773 	{
774 		left_rear = voice[v].lr_mix;
775 		center=voice[v].ce_mix;
776 		right=voice[v].right_mix;
777 		right_rear = voice[v].rr_mix;
778 		lfe = voice[v].lfe_mix;
779 		ri = -(right/count);
780 
781 		while (count--)
782 		{
783 			left_rear += li; if (left_rear<0) left_rear=0;
784 			left += li; if (left<0) left=0;
785 			center += li; if (center<0) center=0;
786 			right += ri; if (right<0) right=0;
787 			right_rear += ri; if (right_rear<0) right_rear=0;
788 			lfe += li; if (lfe<0) lfe=0;
789 
790 			resample_t s = *sp++;
791 
792 			MIXATION(left);
793 			MIXATION(right);
794 
795 			if (num_ochannels >= 4)
796 			{
797 				MIXATION(left_rear);
798 				MIXATION(right_rear);
799 			}
800 			if (num_ochannels == 6)
801 			{
802 				MIXATION(center);
803 				MIXATION(lfe);
804 			}
805 		}
806 	}
807 	else if (voice[v].panned==PANNED_CENTER)
808 	{
809 		while (count--)
810 		{
811 			left += li;
812 			if (left<0)
813 				return;
814 
815 			resample_t s = *sp++;
816 
817 			if (num_ochannels == 2)
818 			{
819 				MIXATION(left);
820 				MIXATION(left);
821 			}
822 			else if (num_ochannels == 4)
823 			{
824 				MIXATION(left);
825 				MIXATION(left);
826 				MIXSKIP;
827 				MIXSKIP;
828 			}
829 			else if (num_ochannels == 6)
830 			{
831 				MIXSKIP;
832 				MIXSKIP;
833 				MIXSKIP;
834 				MIXSKIP;
835 				MIXATION(left);
836 				MIXATION(left);
837 			}
838 		}
839 	}
840 	else if (voice[v].panned==PANNED_LEFT)
841 	{
842 		while (count--)
843 		{
844 			left += li;
845 			if (left<0)
846 				return;
847 
848 			resample_t s = *sp++;
849 
850 			MIXATION(left);
851 			MIXSKIP;
852 
853 			if (num_ochannels >= 4)
854 			{
855 				MIXATION(left);
856 				MIXSKIP;
857 			}
858 			if (num_ochannels == 6)
859 			{
860 				MIXATION(left);
861 				MIXATION(left);
862 			}
863 		}
864 	}
865 	else if (voice[v].panned==PANNED_RIGHT)
866 	{
867 		while (count--)
868 		{
869 			left += li;
870 			if (left<0)
871 				return;
872 
873 			resample_t s = *sp++;
874 
875 			MIXSKIP;
876 			MIXATION(left);
877 
878 			if (num_ochannels >= 4)
879 			{
880 				MIXSKIP;
881 				MIXATION(left);
882 			}
883 			if (num_ochannels == 6)
884 			{
885 				MIXATION(left);
886 				MIXATION(left);
887 			}
888 		}
889 	}
890 }
891 
892 
893 /**************** interface function ******************/
894 
mix_voice(s32_t * buf,int v,int count)895 void mix_voice(s32_t *buf, int v, int count)
896 {
897 	Voice *vp = voice+v;
898 
899 	if (count < 0)
900 		return;
901 
902 	if (vp->status==VOICE_DIE)
903 	{
904 		if (count > MAX_DIE_TIME)
905 			count = MAX_DIE_TIME;
906 
907 		resample_t *sp = resample_voice(v, &count);
908 		ramp_out(sp, buf, v, count);
909 
910 		vp->status=VOICE_FREE;
911 		return;
912 	}
913 
914 	resample_t *sp = resample_voice(v, &count);
915 
916 	if (count < 0)
917 		return;
918 
919 	if (play_mode_encoding & PE_MONO)
920 	{
921 		/* Mono output. */
922 		if (vp->envelope_increment || vp->tremolo_phase_increment)
923 			mix_mono_signal(sp, buf, v, count);
924 		else
925 			mix_mono(sp, buf, v, count);
926 
927 		return;
928 	}
929 
930 	if (vp->panned == PANNED_MYSTERY)
931 	{
932 		if (vp->envelope_increment || vp->tremolo_phase_increment)
933 			mix_mystery_signal(sp, buf, v, count);
934 		else
935 			mix_mystery(sp, buf, v, count);
936 
937 		return;
938 	}
939 	else if (vp->panned == PANNED_CENTER)
940 	{
941 		if (vp->envelope_increment || vp->tremolo_phase_increment)
942 			mix_center_signal(sp, buf, v, count);
943 		else
944 			mix_center(sp, buf, v, count);
945 
946 		return;
947 	}
948 
949 	/* It's either full left or full right. In either case,
950 	   every other sample is 0. Just get the offset right: */
951 
952 	if (vp->envelope_increment || vp->tremolo_phase_increment)
953 	{
954 		if (vp->panned == PANNED_RIGHT)
955 			mix_single_right_signal(sp, buf, v, count);
956 		else
957 			mix_single_left_signal(sp, buf, v, count);
958 	}
959 	else
960 	{
961 		if (vp->panned == PANNED_RIGHT)
962 			mix_single_right(sp, buf, v, count);
963 		else
964 			mix_single_left(sp, buf, v, count);
965 	}
966 }
967 
968 
969 /*****************************************************************/
970 /* Some functions to convert signed 32-bit data to other formats */
971 
972 #define CLIP_THRESHHOLD  ((1L << (31-GUARD_BITS)) - 1)
973 
974 
s32tos8(void * dp,const s32_t * lp,int count)975 void s32tos8(void *dp, const s32_t *lp, int count)
976 {
977 	s8_t *dest = (s8_t *)dp;
978 
979 	while (count--)
980 	{
981 		s32_t val = *lp++;
982 
983 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
984 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
985 
986 		*dest++ = (s8_t) (val >> (32-8-GUARD_BITS));
987 	}
988 }
989 
s32tou8(void * dp,const s32_t * lp,int count)990 void s32tou8(void *dp, const s32_t *lp, int count)
991 {
992 	u8_t *dest = (u8_t *)dp;
993 
994 	while (count--)
995 	{
996 		s32_t val = *lp++;
997 
998 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
999 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1000 
1001 		*dest++ = (u8_t) (0x80 ^ (val >> (32-8-GUARD_BITS)));
1002 	}
1003 }
1004 
s32tos16(void * dp,const s32_t * lp,int count)1005 void s32tos16(void *dp, const s32_t *lp, int count)
1006 {
1007 	s16_t *dest = (s16_t *)dp;
1008 
1009 	while (count--)
1010 	{
1011 		s32_t val = *lp++;
1012 
1013 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
1014 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1015 
1016 		*dest++ = (s16_t)(val >> (32-16-GUARD_BITS));
1017 	}
1018 }
1019 
s32tou16(void * dp,const s32_t * lp,int count)1020 void s32tou16(void *dp, const s32_t *lp, int count)
1021 {
1022 	u16_t *dest = (u16_t *)dp;
1023 
1024 	while (count--)
1025 	{
1026 		s32_t val = *lp++;
1027 
1028 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
1029 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1030 
1031 		*dest++ = (u16_t) (0x8000 ^ (val >> (32-16-GUARD_BITS)));
1032 	}
1033 }
1034 
s32tos16x(void * dp,const s32_t * lp,int count)1035 void s32tos16x(void *dp, const s32_t *lp, int count)
1036 {
1037 	s16_t *dest = (s16_t *)dp;
1038 
1039 	while (count--)
1040 	{
1041 		s32_t val = *lp++;
1042 
1043 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
1044 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1045 
1046 		val >>= (32-16-GUARD_BITS);
1047 
1048 		*dest++ = (s16_t)EPI_Swap16((u16_t)val);
1049 	}
1050 }
1051 
s32tou16x(void * dp,const s32_t * lp,int count)1052 void s32tou16x(void *dp, const s32_t *lp, int count)
1053 {
1054 	u16_t *dest = (u16_t *)dp;
1055 
1056 	while (count--)
1057 	{
1058 		s32_t val = *lp++;
1059 
1060 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
1061 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1062 
1063 		val >>= (32-16-GUARD_BITS);
1064 
1065 		*dest++ = (u16_t) (0x8000 ^ EPI_Swap16((u16_t)val));
1066 	}
1067 }
1068 
s32toulaw(void * dp,const s32_t * lp,int count)1069 void s32toulaw(void *dp, const s32_t *lp, int count)
1070 {
1071 	u8_t *dest = (u8_t *)dp;
1072 
1073 	while (count--)
1074 	{
1075 		s32_t val = *lp++;
1076 
1077 		     if (val >  CLIP_THRESHHOLD) val =  CLIP_THRESHHOLD;
1078 		else if (val < -CLIP_THRESHHOLD) val = -CLIP_THRESHHOLD;
1079 
1080 		val >>= (32-13-GUARD_BITS);
1081 
1082 		// Note: _l2u has been offsetted by 4096 to allow for
1083 		//       the negative values here.  (-AJA- ick!)
1084 		*dest++ = _l2u[val];
1085 	}
1086 }
1087 
1088 
1089 //--- editor settings ---
1090 // vi:ts=4:sw=4:noexpandtab
1091