1 /*(LGPL)
2 ---------------------------------------------------------------------------
3 	a_voice.c - Audio Engine low level mixer voices
4 ---------------------------------------------------------------------------
5  * Copyright (C) 2001-2003, David Olofson
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 
26 #include "kobolog.h"
27 #include "a_voice.h"
28 #include "a_struct.h"
29 #include "a_globals.h"
30 #include "a_tools.h"
31 #include "a_control.h"
32 
33 #define	LDBG(x)
34 #define	EVDBG(x)
35 #define	CHECKPOINTS
36 
37 
38 /* Random number generator state for randtrig etc */
39 static int rnd = 16576;
40 #define	UPDATE_RND	rnd *= 1566083941UL; rnd++; rnd &= 0x7fffffffUL;
41 
42 /* Last allocated voice (good starting point!) */
43 static int last_voice = 0;
44 
45 
voice_kill(audio_voice_t * v)46 void voice_kill(audio_voice_t *v)
47 {
48 	v->vu = 65535;		/* Newly allocated voices are harder to steal */
49 	aev_flush(&v->port);
50 	if(v->channel)
51 	{
52 		--v->channel->playing;
53 		chan_unlink_voice(v);
54 	}
55 	v->state = VS_STOPPED;
56 }
57 
58 
voice_alloc(audio_channel_t * c)59 int voice_alloc(audio_channel_t *c)
60 {
61 	int lv = 0;
62 	int i, pri, v, vol;
63 
64 	/* Pass 1: Look for an unused voice. */
65 	for(v = 0; v < AUDIO_MAX_VOICES; ++v)
66 	{
67 		if(voicetab[v].state != VS_STOPPED)
68 			continue;	/* Not interesting here... */
69 		last_voice = v;
70 		chan_link_voice(c, &voicetab[v]);
71 		voicetab[v].priority = c->ctl[ACC_PRIORITY];
72 		voicetab[v].state = VS_RESERVED;
73 		return v;
74 	}
75 
76 	/*
77 	 * Pass 2: Look for the most silent voice with
78 	 *         same or lower priority.
79 	 */
80 	lv = last_voice;
81 	vol = 2000000000;
82 	v = -1;
83 	for(i = 0; i < AUDIO_MAX_VOICES; ++i)
84 	{
85 		int vu;
86 		if(++lv >= AUDIO_MAX_VOICES)
87 			lv = 0;
88 		if(voicetab[lv].priority < c->ctl[ACC_PRIORITY])
89 			continue;
90 #ifdef AUDIO_USE_VU
91 		vu = voicetab[lv].vu;
92 		vu *= (voicetab[lv].ic[VIC_LVOL].v +
93 				voicetab[lv].ic[VIC_RVOL].v +
94 				voicetab[lv].ic[VIC_LSEND].v +
95 				voicetab[lv].ic[VIC_RSEND].v) >> (RAMP_BITS + 2);
96 		vu >>= VOL_BITS;
97 #else
98 		vu = (voicetab[lv].ic[VIC_LVOL].v +
99 				voicetab[lv].ic[VIC_RVOL].v +
100 				voicetab[lv].ic[VIC_LSEND].v +
101 				voicetab[lv].ic[VIC_RSEND].v) >> (RAMP_BITS + 2);
102 #endif
103 		if(vu < vol)
104 		{
105 			vol = vu;
106 			v = lv;
107 		}
108 	}
109 	if(v >= 0)
110 	{
111 		voice_kill(&voicetab[v]);
112 		chan_link_voice(c, &voicetab[v]);
113 		last_voice = v;
114 		voicetab[v].priority = c->ctl[ACC_PRIORITY];
115 		voicetab[v].state = VS_RESERVED;
116 		return v;
117 	}
118 
119 	/* Pass 3: Grab voice with lowest priority. */
120 	lv = last_voice;
121 	pri = c->ctl[ACC_PRIORITY];
122 	v = -1;
123 	for(i = 0; i < AUDIO_MAX_VOICES; ++i)
124 	{
125 		if(++lv >= AUDIO_MAX_VOICES)
126 			lv = 0;
127 		if(voicetab[lv].priority > pri)
128 		{
129 			pri = voicetab[lv].priority;
130 			v = lv;
131 		}
132 	}
133 	if(v >= 0)
134 	{
135 		voice_kill(&voicetab[v]);
136 		chan_link_voice(c, &voicetab[v]);
137 		last_voice = v;
138 		voicetab[v].priority = c->ctl[ACC_PRIORITY];
139 		voicetab[v].state = VS_RESERVED;
140 		return v;
141 	}
142 
143 	return -1;
144 }
145 
146 
voice_start(audio_voice_t * v,int wid)147 void voice_start(audio_voice_t *v, int wid)
148 {
149 	int retrig, randtrig;
150 
151 	v->wave = v->c[VC_WAVE] = wid;
152 	v->c[VC_LOOP] = wavetab[wid].looped;
153 
154 	v->position = 0;
155 	v->position_frac = 0;
156 	if(!wavetab[wid].data.si8)
157 	{
158 		voice_kill(v);
159 		return;
160 	}
161 
162 	/* Set up retrig and looping */
163 	randtrig = (int)v->c[VC_RANDTRIG];
164 	retrig = (int)v->c[VC_RETRIG];
165 	if(randtrig)
166 	{
167 		UPDATE_RND
168 		randtrig = rnd % (randtrig<<1) - randtrig;
169 		randtrig = retrig * randtrig >> 16;
170 		retrig += randtrig;
171 	}
172 
173 	if(retrig > 0)
174 	{
175 		if((unsigned)retrig > wavetab[wid].play_samples)
176 			v->section_end = wavetab[wid].play_samples;
177 		else
178 			v->section_end = (unsigned)retrig;
179 	}
180 	else
181 		v->section_end = wavetab[wid].play_samples;
182 
183 	/* Start voice! */
184 	v->state = VS_PLAYING;
185 }
186 
187 
__handle_looping(audio_voice_t * v)188 static inline int __handle_looping(audio_voice_t *v)
189 {
190 	unsigned int samples = wavetab[v->c[VC_WAVE]].play_samples;
191 	int randtrig = v->c[VC_RANDTRIG];
192 	int retrig = v->c[VC_RETRIG];
193 
194 	/* Latch (new) waveform index */
195 	v->wave = v->c[VC_WAVE];
196 
197 	if(randtrig)
198 	{
199 		UPDATE_RND
200 		randtrig = rnd % (randtrig<<1) - randtrig;
201 		randtrig = retrig * randtrig >> 16;
202 		retrig += randtrig;
203 	}
204 
205 	if(retrig > 0)
206 	{
207 		unsigned int old_se = v->section_end;
208 
209 		if((unsigned)retrig > samples)
210 			v->section_end = samples;
211 		else
212 			v->section_end = (unsigned)retrig;
213 
214 		if(old_se > v->position)
215 		{
216 			/* Force instant initial retrig */
217 			v->position = 0;
218 			v->position_frac = 0;
219 		}
220 		else
221 		{
222 			/* Wrap loop */
223 			if(v->position >= old_se)
224 				v->position = 0;
225 			else
226 				v->position -= old_se;
227 		}
228 	}
229 	else
230 	{
231 		if(v->c[VC_LOOP])
232 		{
233 			v->position -= v->section_end;
234 			v->section_end = samples;
235 		}
236 		else
237 			return 0;	/* Stop playing! */
238 	}
239 	return 1;
240 }
241 
242 
voice_check_retrig(audio_voice_t * v)243 void voice_check_retrig(audio_voice_t *v)
244 {
245 	if(wavetab[v->wave].data.si8)
246 	{
247 		int retrig_max = v->c[VC_RETRIG] * v->c[VC_RANDTRIG] >> 16;
248 		retrig_max += v->c[VC_RETRIG];
249 		if(v->position > (unsigned)retrig_max)
250 			__handle_looping(v);
251 	}
252 }
253 
254 
255 /*
256  * Macro Mayhem! Create all the mixer variants...
257  */
258 
__mix_m8(audio_voice_t * v,int * out,unsigned frames)259 static inline void __mix_m8(audio_voice_t *v, int *out, unsigned frames)
260 {
261 #undef	__SEND
262 #undef	__STEREO
263 #undef	__16BIT
264 #include "a_mixers.h"
265 }
266 
__mix_s8(audio_voice_t * v,int * out,unsigned frames)267 static inline void __mix_s8(audio_voice_t *v, int *out, unsigned frames)
268 {
269 #undef	__SEND
270 #define	__STEREO
271 #undef	__16BIT
272 #include "a_mixers.h"
273 }
274 
__mix_m16(audio_voice_t * v,int * out,unsigned frames)275 static inline void __mix_m16(audio_voice_t *v, int *out, unsigned frames)
276 {
277 #undef	__SEND
278 #undef	__STEREO
279 #define	__16BIT
280 #include "a_mixers.h"
281 }
282 
__mix_s16(audio_voice_t * v,int * out,unsigned frames)283 static inline void __mix_s16(audio_voice_t *v, int *out, unsigned frames)
284 {
285 #undef	__SEND
286 #define	__STEREO
287 #define	__16BIT
288 #include "a_mixers.h"
289 }
290 
291 
__mix_m8d(audio_voice_t * v,int * out,int * sout,unsigned frames)292 static inline void __mix_m8d(audio_voice_t *v, int *out, int *sout, unsigned frames)
293 {
294 #define	__SEND
295 #undef	__STEREO
296 #undef	__16BIT
297 #include "a_mixers.h"
298 }
299 
__mix_s8d(audio_voice_t * v,int * out,int * sout,unsigned frames)300 static inline void __mix_s8d(audio_voice_t *v, int *out, int *sout, unsigned frames)
301 {
302 #define	__SEND
303 #define	__STEREO
304 #undef	__16BIT
305 #include "a_mixers.h"
306 }
307 
__mix_m16d(audio_voice_t * v,int * out,int * sout,unsigned frames)308 static inline void __mix_m16d(audio_voice_t *v, int *out, int *sout, unsigned frames)
309 {
310 #define	__SEND
311 #undef	__STEREO
312 #define	__16BIT
313 #include "a_mixers.h"
314 }
315 
__mix_s16d(audio_voice_t * v,int * out,int * sout,unsigned frames)316 static inline void __mix_s16d(audio_voice_t *v, int *out, int *sout, unsigned frames)
317 {
318 #define	__SEND
319 #define	__STEREO
320 #define	__16BIT
321 #include "a_mixers.h"
322 }
323 
324 #undef	__SEND
325 #undef	__STEREO
326 #undef	__16BIT
327 
328 
329 /*
330  * Calculates resampling input "step", and selects resampling mode.
331  */
__calc_step(audio_voice_t * v)332 static inline unsigned int __calc_step(audio_voice_t *v)
333 {
334 	audio_resample_t mode = AR_LINEAR;
335 	/* Resampling factor */
336 	int pitch = v->c[VC_PITCH];
337 	unsigned step = (unsigned)fixmul(ptab_convert(pitch),
338 			wavetab[v->wave].speed);
339 #if (FREQ_BITS < 16)
340 	step >>= 16 - FREQ_BITS;
341 #elif (FREQ_BITS > 16)
342 	step <<= FREQ_BITS - 16;
343 #endif
344 
345 	/*
346 	 * We must prevent high pithes from locking the mixer in
347 	 * an infinite loop with short looped waveforms...
348 	 */
349 	if(step > MAX_STEP)
350 	{
351 #ifdef DEBUG
352 		log_printf(ELOG, "Too high pitch!\n");
353 #endif
354 		while(step > MAX_STEP)
355 			step >>= 1;
356 	}
357 
358 	switch(a_settings.quality)
359 	{
360 	  case AQ_VERY_LOW:
361 		mode = AR_NEAREST;
362 		break;
363 	  case AQ_LOW:
364 		mode = AR_NEAREST_4X;
365 		break;
366 	  case AQ_NORMAL:
367 		mode = AR_LINEAR;
368 		break;
369 	  case AQ_HIGH:
370 		/* Select resampling method based on in/out ratio */
371 		if(step > (unsigned)(6 << FREQ_BITS))
372 			mode = AR_LINEAR_8X_R;	/* Above 6:1 */
373 		else if(step > (unsigned)(3 << FREQ_BITS))
374 			mode = AR_LINEAR_4X_R;	/* Above 3:1 */
375 		else
376 			mode = AR_LINEAR_2X_R;	/* Below 2:1 */
377 		break;
378 	  case AQ_VERY_HIGH:
379 		/* Select resampling method based on in/out ratio */
380 		if(step > (unsigned)(4 << FREQ_BITS))
381 			mode = AR_LINEAR_16X_R;	/* Above 4:1 */
382 		else if(step > (unsigned)(3 << FREQ_BITS))
383 			mode = AR_LINEAR_8X_R;	/* Above 3:1 */
384 		else if(step > (unsigned)(2 << FREQ_BITS))
385 			mode = AR_LINEAR_4X_R;	/* Above 2:1 */
386 		else if(step > (unsigned)(3 << (FREQ_BITS-1)))
387 			mode = AR_LINEAR_2X_R;	/* Above 1.5:1 */
388 		else
389 			mode = AR_CUBIC_R;	/* Below 1.5:1 */
390 		break;
391 	}
392 	v->c[VC_RESAMPLE] = mode;
393 	return step;
394 }
395 
396 
397 /*
398  * Calculates # of output frames to the nearest of 'frames',
399  * end of segment and the "fragment span limit".
400  */
__endframes(audio_voice_t * v,unsigned int frames)401 static inline unsigned int __endframes(audio_voice_t *v, unsigned int frames)
402 {
403 #ifdef A_USE_INT64
404 	Uint64 n, n2;
405 #else
406 	double n, n2;
407 #endif
408 	if(!v->step)
409 		return frames;
410 
411 #ifdef A_USE_INT64
412 	n = ((Uint64)(v->position) << 32) | (Uint64)(v->position_frac);
413 	n >>= 32 - FREQ_BITS;
414 	n = ((Uint64)(v->section_end) << FREQ_BITS) - n + v->step - 1;
415 	n /= v->step;
416 #else
417 	n = (double)(v->position) + (double)(v->position_frac) / 4294967296.0;
418 	n = (double)(v->section_end) - n;
419 	n /= (double)v->step / (double)(1<<FREQ_BITS);
420 	n = ceil(n);
421 #endif
422 	if(n > 0xffffffff)
423 		n = 0xffffffff;
424 #ifdef A_USE_INT64
425 	if(n > (Uint64)frames)
426 		n = (Uint64)frames;
427 #else
428 	if(n > (double)frames)
429 		n = (double)frames;
430 #endif
431 	/*
432 	 * Restrict fragment size to prevent read position overflows.
433 	 *
434 	 * (In order to maximize pitch accuracy, voice mixers can only
435 	 * handle a very limited number of input samples without
436 	 * recalculating their "base pointers".)
437 	 */
438 #ifdef A_USE_INT64
439 	n2 = (Uint64)MAX_FRAGMENT_SPAN << FREQ_BITS;
440 	n2 /= (Uint64)v->step * (Uint64)frames;
441 #else
442 	n2 = (double)MAX_FRAGMENT_SPAN * (1 << FREQ_BITS);
443 	n2 /= (double)v->step * (double)frames;
444 #endif
445 	if(n > n2)
446 		n = n2;
447 
448 #ifdef	CHECKPOINTS
449 	if(!frames)
450 	{
451 		voice_kill(v);
452 		log_printf(ELOG, "Voice locked up! (Too high pitch "
453 				"resulted in zero fragment size.)\n");
454 	}
455 #endif
456 	return (unsigned int)n;
457 }
458 
459 
__fragment_single(audio_voice_t * v,int * out,unsigned int frames)460 static inline void __fragment_single(audio_voice_t *v, int *out,
461 		unsigned int frames)
462 {
463 	switch(wavetab[v->wave].format)
464 	{
465 	  case AF_MONO8:
466 		__mix_m8(v, out, frames);
467 		break;
468 	  case AF_STEREO8:
469 		__mix_s8(v, out, frames);
470 		break;
471 	  case AF_MONO16:
472 		__mix_m16(v, out, frames);
473 		break;
474 	  case AF_STEREO16:
475 		__mix_s16(v, out, frames);
476 		break;
477 	  case AF_MONO32:
478 		/*__mix_m32(v, out, frames);*/
479 		break;
480 	  case AF_STEREO32:
481 		/*__mix_s32(v, out, frames);*/
482 	  case AF_MIDI:	/* warning eliminator */
483 		break;
484 	}
485 }
486 
487 
__fragment_double(audio_voice_t * v,int * out,int * sout,unsigned int frames)488 static inline void __fragment_double(audio_voice_t * v, int *out, int *sout,
489 			unsigned int frames)
490 {
491 	switch(wavetab[v->wave].format)
492 	{
493 	  case AF_MONO8:
494 		__mix_m8d(v, out, sout, frames);
495 		break;
496 	  case AF_STEREO8:
497 		__mix_s8d(v, out, sout, frames);
498 		break;
499 	  case AF_MONO16:
500 		__mix_m16d(v, out, sout, frames);
501 		break;
502 	  case AF_STEREO16:
503 		__mix_s16d(v, out, sout, frames);
504 		break;
505 	  case AF_MONO32:
506 		/*__mix_m32d(v, out, sout, frames);*/
507 		break;
508 	  case AF_STEREO32:
509 		/*__mix_s32d(v, out, sout, frames);*/
510 	  case AF_MIDI:	/* warning eliminator */
511 		break;
512 	}
513 }
514 
515 /*
516  * Figure out if we should use the "double output" mixers,
517  * and where to connect the output(s).
518  */
__setup_output(audio_voice_t * v)519 static inline int __setup_output(audio_voice_t *v)
520 {
521 	int prim, send;
522 /*
523 FIXME: This "automatic routing optimization" isn't needed,
524 FIXME: and causes trouble elsewhere. Simplify.
525 */
526 	v->fx1 = v->c[VC_PRIM_BUS];
527 	v->fx2 = v->c[VC_SEND_BUS];
528 	prim = (v->fx1 >= 0) && (v->fx1 < AUDIO_MAX_BUSSES);
529 	send = (v->fx2 >= 0) && (v->fx2 < AUDIO_MAX_BUSSES);
530 	if(!prim && !send)
531 		return -1;	/* No busses selected! --> */
532 
533 	if(prim && send)
534 		v->use_double = (v->fx1 != v->fx2);
535 	else
536 	{
537 		if(send)
538 			v->fx1 = v->fx2;
539 		else
540 			v->fx2 = v->fx1;
541 		v->use_double = 0;
542 	}
543 	return 0;
544 }
545 
546 
voice_process_mix(audio_voice_t * v,int * busses[],unsigned frames)547 void voice_process_mix(audio_voice_t *v, int *busses[], unsigned frames)
548 {
549 	unsigned s, frag_s;
550 
551 	if((VS_STOPPED == v->state) && (aev_next(&v->port, 0) > frames))
552 		return;	/* Stopped, and no events for this buffer --> */
553 
554 	/* Loop until buffer is full, or the voice is "dead". */
555 	s = 0;
556 	while(frames)
557 	{
558 		unsigned frag_frames;
559 		while( !(frag_frames = aev_next(&v->port, s)) )
560 		{
561 			aev_event_t *ev = aev_read(&v->port);
562 			switch(ev->type)
563 			{
564 			  case VE_START:
565 				voice_start(v, ev->arg1);
566 				if(VS_STOPPED == v->state)
567 				{
568 					aev_free(ev);
569 					return;	/* Error! --> */
570 				}
571 				/*
572 				 * NOTE:
573 				 *	This being checked here means that
574 				 *	it's not possible to change routing
575 				 *	during playback. Who would, anyway?
576 				 */
577 				if(__setup_output(v) < 0)
578 				{
579 					voice_kill(v);
580 					aev_free(ev);
581 					return;	/* No sends! --> */
582 				}
583 				break;
584 			  case VE_STOP:
585 				voice_kill(v);
586 				aev_free(ev);
587 				return;	/* Back in the voice pool! --> */
588 			  case VE_SET:
589 #ifdef	CHECKPOINTS
590 				if(ev->index >= VC_COUNT)
591 				{
592 					log_printf(ELOG, "BUG! VC index out of range!");
593 					break;
594 				}
595 #endif
596 				v->c[ev->index] = ev->arg1;
597 				if(VC_PITCH == ev->index)
598 					v->step = __calc_step(v);
599 				break;
600 			  case VE_IRAMP:
601 #ifdef	CHECKPOINTS
602 				if(ev->index >= VIC_COUNT)
603 				{
604 					log_printf(ELOG, "BUG! VIC index out of range!");
605 					break;
606 				}
607 #endif
608 				if(ev->arg2)
609 				{
610 					v->ic[ev->index].dv = ev->arg1 << RAMP_BITS;
611 					v->ic[ev->index].dv -= v->ic[ev->index].v;
612 					v->ic[ev->index].dv /= ev->arg2 + 1;
613 				}
614 				else
615 					v->ic[ev->index].v = ev->arg1 << RAMP_BITS;
616 				break;
617 			}
618 			aev_free(ev);
619 		}
620 
621 		if(frag_frames > frames)
622 			frag_frames = frames;
623 
624 		/* Handle fragmentation, end-of-waveform and looping */
625 		frag_s = (VS_PLAYING == v->state) ? 0 : frag_frames;
626 		while(frag_s < frag_frames)
627 		{
628 			unsigned offs = (s + frag_s) << 1;
629 			unsigned do_frames = __endframes(v, frag_frames - frag_s);
630 			if(do_frames)
631 			{
632 #ifdef CHECKPOINTS
633 				if(v->position >= v->section_end)
634 				{
635 					log_printf(ELOG, "BUG! position = %u while section_end = %u.",
636 							v->position, v->section_end);
637 					log_printf(ELOG, " (step = %u)\n", v->step >> FREQ_BITS);
638 					v->position = 0;
639 				}
640 #endif
641 				bustab[v->fx1].in_use = 1;
642 				if(v->use_double)
643 				{
644 					bustab[v->fx2].in_use = 1;
645 					 __fragment_double(v, busses[v->fx1] + offs,
646 							busses[v->fx2] + offs,
647 							do_frames);
648 				}
649 				else
650 					__fragment_single(v, busses[v->fx1] + offs,
651 							do_frames);
652 
653 				frag_s += do_frames;
654 
655 				// This is just for that damn oversampling...
656 				if(v->position >= v->section_end)
657 					do_frames = 0;
658 			}
659 			if(!do_frames && !__handle_looping(v))
660 			{
661 				voice_kill(v);
662 				return;
663 			}
664 		}
665 		s += frag_frames;
666 		frames -= frag_frames;
667 	}
668 }
669 
670 
voice_process_all(int * bufs[],unsigned frames)671 void voice_process_all(int *bufs[], unsigned frames)
672 {
673 	int i;
674 	for(i = 0; i < AUDIO_MAX_VOICES; ++i)
675 		voice_process_mix(voicetab + i, bufs, frames);
676 }
677 
678 
679 static int _is_open = 0;
680 
audio_voice_open(void)681 void audio_voice_open(void)
682 {
683 	int i;
684 	if(_is_open)
685 		return;
686 
687 	memset(voicetab, 0, sizeof(voicetab));
688 	for(i = 0; i < AUDIO_MAX_VOICES; ++i)
689 	{
690 		char *buf = malloc(64);
691 		snprintf(buf, 64, "Audio Voice %d", i);
692 		aev_port_init(&voicetab[i].port, buf);
693 	}
694 	_is_open = 1;
695 }
696 
697 
audio_voice_close(void)698 void audio_voice_close(void)
699 {
700 	int i;
701 	if(!_is_open)
702 		return;
703 
704 	for(i = 0; i < AUDIO_MAX_VOICES; ++i)
705 	{
706 		aev_flush(&voicetab[i].port);
707 		free((char *)voicetab[i].port.name);
708 	}
709 	memset(voicetab, 0, sizeof(voicetab));
710 	_is_open = 0;
711 }
712