Debian Codegraph @

addons/audio/kcm_mixer.c

raw
1 /**
2 * Originally digi.c from allegro wiki
3 * Original authors: KC/Milan
4 *
5 * Converted to allegro5 by Ryan Dickie
6 */
7
8 /* Title: Mixer functions
9 */
10
11 #include <math.h>
12 #include <stdio.h>
13
14 #include "allegro5/allegro_audio.h"
15 #include "allegro5/internal/aintern.h"
16 #include "allegro5/internal/aintern_audio.h"
17 #include "allegro5/internal/aintern_audio_cfg.h"
18
19 ALLEGRO_DEBUG_CHANNEL("audio")
20
21
22 typedef union {
23 float f32[ALLEGRO_MAX_CHANNELS]; /* max: 7.1 */
24 int16_t s16[ALLEGRO_MAX_CHANNELS];
25 void *ptr;
26 } SAMP_BUF;
27
28
29
30 static void maybe_lock_mutex(ALLEGRO_MUTEX *mutex)
31 {
32 if (mutex) {
33 al_lock_mutex(mutex);
34 }
35 }
36
37
38 static void maybe_unlock_mutex(ALLEGRO_MUTEX *mutex)
39 {
40 if (mutex) {
41 al_unlock_mutex(mutex);
42 }
43 }
44
45
46 /* _al_rechannel_matrix:
47 * This function provides a (temporary!) matrix that can be used to convert
48 * one channel configuration into another.
49 *
50 * Returns a pointer to a statically allocated array.
51 */
52 static float *_al_rechannel_matrix(ALLEGRO_CHANNEL_CONF orig,
53 ALLEGRO_CHANNEL_CONF target, float gain, float pan)
54 {
55 /* Max 7.1 (8 channels) for input and output */
56 static float mat[ALLEGRO_MAX_CHANNELS][ALLEGRO_MAX_CHANNELS];
57
58 size_t dst_chans = al_get_channel_count(target);
59 size_t src_chans = al_get_channel_count(orig);
60 size_t i, j;
61
62 /* Start with a simple identity matrix */
63 memset(mat, 0, sizeof(mat));
64 for (i = 0; i < src_chans && i < dst_chans; i++) {
65 mat[i][i] = 1.0;
66 }
67
68 /* Multi-channel -> mono conversion (cuts rear/side channels) */
69 if (dst_chans == 1 && (orig>>4) > 1) {
70 for (i = 0; i < 2; i++) {
71 mat[0][i] = 1.0 / sqrt(2.0);
72 }
73
74 /* If the source has a center channel, make sure that's copied 1:1
75 * (perhaps it should scale the overall output?)
76 */
77 if ((orig >> 4) & 1) {
78 mat[0][(orig >> 4) - 1] = 1.0;
79 }
80 }
81 /* Center (or mono) -> front l/r conversion */
82 else if (((orig >> 4) & 1) && !((target >> 4) & 1)) {
83 mat[0][(orig >> 4) - 1] = 1.0 / sqrt(2.0);
84 mat[1][(orig >> 4) - 1] = 1.0 / sqrt(2.0);
85 }
86
87 /* Copy LFE */
88 if ((orig >> 4) != (target >> 4) &&
89 (orig & 0xF) && (target & 0xF))
90 {
91 mat[dst_chans-1][src_chans-1] = 1.0;
92 }
93
94 /* Apply panning, which is supposed to maintain a constant power level.
95 * I took that to mean we want:
96 * sqrt(rgain^2 + lgain^2) = 1.0
97 */
98 if (pan != ALLEGRO_AUDIO_PAN_NONE) {
99 float rgain = sqrt(( pan + 1.0f) / 2.0f);
100 float lgain = sqrt((-pan + 1.0f) / 2.0f);
101
102 /* I dunno what to do about >2 channels, so don't even try for now. */
103 for (j = 0; j < src_chans; j++) {
104 mat[0][j] *= lgain;
105 mat[1][j] *= rgain;
106 }
107 }
108
109 /* Apply gain */
110 if (gain != 1.0f) {
111 for (i = 0; i < dst_chans; i++) {
112 for (j = 0; j < src_chans; j++) {
113 mat[i][j] *= gain;
114 }
115 }
116 }
117
118 #ifdef DEBUGMODE
119 {
120 char debug[1024];
121 ALLEGRO_DEBUG("sample matrix:\n");
122 for (i = 0; i < dst_chans; i++) {
123 strcpy(debug, "");
124 for (j = 0; j < src_chans; j++) {
125 sprintf(debug + strlen(debug), " %f", mat[i][j]);
126 }
127 ALLEGRO_DEBUG("%s\n", debug);
128 }
129 }
130 #endif
131
132 return &mat[0][0];
133 }
134
135
136 /* _al_kcm_mixer_rejig_sample_matrix:
137 * Recompute the mixing matrix for a sample attached to a mixer.
138 * The caller must be holding the mixer mutex.
139 */
140 void _al_kcm_mixer_rejig_sample_matrix(ALLEGRO_MIXER *mixer,
141 ALLEGRO_SAMPLE_INSTANCE *spl)
142 {
143 float *mat;
144 size_t dst_chans;
145 size_t src_chans;
146 size_t i, j;
147
148 mat = _al_rechannel_matrix(spl->spl_data.chan_conf,
149 mixer->ss.spl_data.chan_conf, spl->gain, spl->pan);
150
151 dst_chans = al_get_channel_count(mixer->ss.spl_data.chan_conf);
152 src_chans = al_get_channel_count(spl->spl_data.chan_conf);
153
154 if (!spl->matrix)
155 spl->matrix = al_calloc(1, src_chans * dst_chans * sizeof(float));
156
157 for (i = 0; i < dst_chans; i++) {
158 for (j = 0; j < src_chans; j++) {
159 spl->matrix[i*src_chans + j] = mat[i*ALLEGRO_MAX_CHANNELS + j];
160 }
161 }
162 }
163
164
165 /* fix_looped_position:
166 * When a stream loops, this will fix up the position and anything else to
167 * allow it to safely continue playing as expected. Returns false if it
168 * should stop being mixed.
169 */
170 static bool fix_looped_position(ALLEGRO_SAMPLE_INSTANCE *spl)
171 {
172 bool is_empty;
173 ALLEGRO_AUDIO_STREAM *stream;
174
175 /* Looping! Should be mostly self-explanatory */
176 switch (spl->loop) {
177 case ALLEGRO_PLAYMODE_LOOP:
178 if (spl->loop_end - spl->loop_start != 0) {
179 if (spl->step > 0) {
180 while (spl->pos >= spl->loop_end) {
181 spl->pos -= (spl->loop_end - spl->loop_start);
182 }
183 }
184 else if (spl->step < 0) {
185 while (spl->pos < spl->loop_start) {
186 spl->pos += (spl->loop_end - spl->loop_start);
187 }
188 }
189 }
190 return true;
191
192 case ALLEGRO_PLAYMODE_BIDIR:
193 /* When doing bi-directional looping, you need to do a follow-up
194 * check for the opposite direction if a loop occurred, otherwise
195 * you could end up misplaced on small, high-step loops.
196 */
197 if (spl->loop_end - spl->loop_start != 0) {
198 if (spl->step >= 0) {
199 check_forward:
200 if (spl->pos >= spl->loop_end) {
201 spl->step = -spl->step;
202 spl->pos = spl->loop_end - (spl->pos - spl->loop_end) - 1;
203 goto check_backward;
204 }
205 }
206 else {
207 check_backward:
208 if (spl->pos < spl->loop_start || spl->pos >= spl->loop_end) {
209 spl->step = -spl->step;
210 spl->pos = spl->loop_start + (spl->loop_start - spl->pos);
211 goto check_forward;
212 }
213 }
214 }
215 return true;
216
217 case ALLEGRO_PLAYMODE_LOOP_ONCE:
218 if (spl->pos < spl->loop_end && spl->pos >= 0) {
219 return true;
220 }
221 if (spl->step >= 0)
222 spl->pos = 0;
223 else
224 spl->pos = spl->loop_end - 1;
225 spl->is_playing = false;
226 return false;
227
228 case ALLEGRO_PLAYMODE_ONCE:
229 if (spl->pos < spl->spl_data.len && spl->pos >= 0) {
230 return true;
231 }
232 if (spl->step >= 0)
233 spl->pos = 0;
234 else
235 spl->pos = spl->spl_data.len - 1;
236 spl->is_playing = false;
237 return false;
238
239 case _ALLEGRO_PLAYMODE_STREAM_ONCE:
240 case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
241 case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
242 stream = (ALLEGRO_AUDIO_STREAM *)spl;
243 is_empty = false;
244 while (spl->pos >= spl->spl_data.len && stream->spl.is_playing && !is_empty) {
245 is_empty = !_al_kcm_refill_stream(stream);
246 if (is_empty && stream->is_draining) {
247 stream->spl.is_playing = false;
248 }
249
250 _al_kcm_emit_stream_events(stream);
251 }
252 return !(is_empty);
253 }
254
255 ASSERT(false);
256 return false;
257 }
258
259
260 #include "kcm_mixer_helpers.inc"
261
262
263 static INLINE int32_t clamp(int32_t val, int32_t min, int32_t max)
264 {
265 /* Clamp to min */
266 val -= min;
267 val &= (~val) >> 31;
268 val += min;
269
270 /* Clamp to max */
271 val -= max;
272 val &= val >> 31;
273 val += max;
274
275 return val;
276 }
277
278
279 /* Mix as many sample values as possible from the source sample into a mixer
280 * buffer. Implements stream_reader_t.
281 *
282 * TYPE is the type of the sample values in the mixer buffer, and
283 * NEXT_SAMPLE_VALUE must return a buffer of the same type.
284 *
285 * Note: Uses Bresenham to keep the precise sample position.
286 */
287 #define BRESENHAM \
288 do { \
289 delta = spl->step > 0 ? spl->step : spl->step - spl->step_denom + 1; \
290 delta /= spl->step_denom; \
291 delta_error = spl->step - delta * spl->step_denom; \
292 } while (0)
293
294 #define MAKE_MIXER(NAME, NEXT_SAMPLE_VALUE, TYPE) \
295 static void NAME(void *source, void **vbuf, unsigned int *samples, \
296 ALLEGRO_AUDIO_DEPTH buffer_depth, size_t dest_maxc) \
297 { \
298 ALLEGRO_SAMPLE_INSTANCE *spl = (ALLEGRO_SAMPLE_INSTANCE *)source; \
299 TYPE *buf = *vbuf; \
300 size_t maxc = al_get_channel_count(spl->spl_data.chan_conf); \
301 size_t samples_l = *samples; \
302 size_t c; \
303 int delta, delta_error; \
304 SAMP_BUF samp_buf; \
305 \
306 BRESENHAM; \
307 \
308 if (!spl->is_playing) \
309 return; \
310 \
311 while (samples_l > 0) { \
312 const TYPE *s; \
313 int old_step = spl->step; \
314 \
315 if (!fix_looped_position(spl)) \
316 return; \
317 if (old_step != spl->step) { \
318 BRESENHAM; \
319 } \
320 \
321 /* It might be worth preparing multiple sample values at once. */ \
322 s = (TYPE *) NEXT_SAMPLE_VALUE(&samp_buf, spl, maxc); \
323 \
324 for (c = 0; c < dest_maxc; c++) { \
325 ALLEGRO_STATIC_ASSERT(kcm_mixer, ALLEGRO_MAX_CHANNELS == 8); \
326 switch (maxc) { \
327 case 8: *buf += s[7] * spl->matrix[c*maxc + 7]; \
328 /* fall through */ \
329 case 7: *buf += s[6] * spl->matrix[c*maxc + 6]; \
330 /* fall through */ \
331 case 6: *buf += s[5] * spl->matrix[c*maxc + 5]; \
332 /* fall through */ \
333 case 5: *buf += s[4] * spl->matrix[c*maxc + 4]; \
334 /* fall through */ \
335 case 4: *buf += s[3] * spl->matrix[c*maxc + 3]; \
336 /* fall through */ \
337 case 3: *buf += s[2] * spl->matrix[c*maxc + 2]; \
338 /* fall through */ \
339 case 2: *buf += s[1] * spl->matrix[c*maxc + 1]; \
340 /* fall through */ \
341 case 1: *buf += s[0] * spl->matrix[c*maxc + 0]; \
342 /* fall through */ \
343 default: break; \
344 } \
345 buf++; \
346 } \
347 \
348 spl->pos += delta; \
349 spl->pos_bresenham_error += delta_error; \
350 if (spl->pos_bresenham_error >= spl->step_denom) { \
351 spl->pos++; \
352 spl->pos_bresenham_error -= spl->step_denom; \
353 } \
354 samples_l--; \
355 } \
356 fix_looped_position(spl); \
357 (void)buffer_depth; \
358 }
359
360 MAKE_MIXER(read_to_mixer_point_float_32, point_spl32, float)
361 MAKE_MIXER(read_to_mixer_linear_float_32, linear_spl32, float)
362 MAKE_MIXER(read_to_mixer_cubic_float_32, cubic_spl32, float)
363 MAKE_MIXER(read_to_mixer_point_int16_t_16, point_spl16, int16_t)
364 MAKE_MIXER(read_to_mixer_linear_int16_t_16, linear_spl16, int16_t)
365
366 #undef MAKE_MIXER
367
368
369 /* _al_kcm_mixer_read:
370 * Mixes the streams attached to the mixer and writes additively to the
371 * specified buffer (or if *buf is NULL, indicating a voice, convert it and
372 * set it to the buffer pointer).
373 */
374 void _al_kcm_mixer_read(void *source, void **buf, unsigned int *samples,
375 ALLEGRO_AUDIO_DEPTH buffer_depth, size_t dest_maxc)
376 {
377 const ALLEGRO_MIXER *mixer;
378 ALLEGRO_MIXER *m = (ALLEGRO_MIXER *)source;
379 int maxc = al_get_channel_count(m->ss.spl_data.chan_conf);
380 int samples_l = *samples;
381 int i;
382
383 if (!m->ss.is_playing)
384 return;
385
386 /* Make sure the mixer buffer is big enough. */
387 if (m->ss.spl_data.len*maxc < samples_l*maxc) {
388 al_free(m->ss.spl_data.buffer.ptr);
389 m->ss.spl_data.buffer.ptr = al_malloc(samples_l*maxc*al_get_audio_depth_size(m->ss.spl_data.depth));
390 if (!m->ss.spl_data.buffer.ptr) {
391 _al_set_error(ALLEGRO_GENERIC_ERROR,
392 "Out of memory allocating mixer buffer");
393 m->ss.spl_data.len = 0;
394 return;
395 }
396 m->ss.spl_data.len = samples_l;
397 }
398
399 mixer = m;
400
401 /* Clear the buffer to silence. */
402 memset(mixer->ss.spl_data.buffer.ptr, 0, samples_l * maxc * al_get_audio_depth_size(mixer->ss.spl_data.depth));
403
404 /* Mix the streams into the mixer buffer. */
405 for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) {
406 ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i);
407 ALLEGRO_SAMPLE_INSTANCE *spl = *slot;
408 ASSERT(spl->spl_read);
409 spl->spl_read(spl, (void **) &mixer->ss.spl_data.buffer.ptr, samples,
410 m->ss.spl_data.depth, maxc);
411 }
412
413 /* Call the post-processing callback. */
414 if (mixer->postprocess_callback) {
415 mixer->postprocess_callback(mixer->ss.spl_data.buffer.ptr,
416 *samples, mixer->pp_callback_userdata);
417 }
418
419 samples_l *= maxc;
420
421 /* Apply the gain if necessary. */
422 if (mixer->ss.gain != 1.0f) {
423 float mixer_gain = mixer->ss.gain;
424 unsigned long i = samples_l;
425
426 switch (m->ss.spl_data.depth) {
427 case ALLEGRO_AUDIO_DEPTH_FLOAT32: {
428 float *p = mixer->ss.spl_data.buffer.f32;
429 while (i-- > 0) {
430 *p++ *= mixer_gain;
431 }
432 break;
433 }
434
435 case ALLEGRO_AUDIO_DEPTH_INT16: {
436 int16_t *p = mixer->ss.spl_data.buffer.s16;
437 while (i-- > 0) {
438 *p++ *= mixer_gain;
439 }
440 break;
441 }
442
443 case ALLEGRO_AUDIO_DEPTH_INT8:
444 case ALLEGRO_AUDIO_DEPTH_INT24:
445 case ALLEGRO_AUDIO_DEPTH_UINT8:
446 case ALLEGRO_AUDIO_DEPTH_UINT16:
447 case ALLEGRO_AUDIO_DEPTH_UINT24:
448 /* Unsupported mixer depths. */
449 ASSERT(false);
450 break;
451 }
452 }
453
454 /* Feeding to a non-voice.
455 * Currently we only support mixers of the same audio depth doing this.
456 */
457 if (*buf) {
458 switch (m->ss.spl_data.depth) {
459 case ALLEGRO_AUDIO_DEPTH_FLOAT32: {
460 /* We don't need to clamp in the mixer yet. */
461 float *lbuf = *buf;
462 float *src = mixer->ss.spl_data.buffer.f32;
463 while (samples_l-- > 0) {
464 *lbuf += *src;
465 lbuf++;
466 src++;
467 }
468 break;
469
470 case ALLEGRO_AUDIO_DEPTH_INT16: {
471 int16_t *lbuf = *buf;
472 int16_t *src = mixer->ss.spl_data.buffer.s16;
473 while (samples_l-- > 0) {
474 int32_t x = *lbuf + *src;
475 if (x < -32768)
476 x = -32768;
477 else if (x > 32767)
478 x = 32767;
479 *lbuf = (int16_t)x;
480 lbuf++;
481 src++;
482 }
483 break;
484 }
485
486 case ALLEGRO_AUDIO_DEPTH_INT8:
487 case ALLEGRO_AUDIO_DEPTH_INT24:
488 case ALLEGRO_AUDIO_DEPTH_UINT8:
489 case ALLEGRO_AUDIO_DEPTH_UINT16:
490 case ALLEGRO_AUDIO_DEPTH_UINT24:
491 /* Unsupported mixer depths. */
492 ASSERT(false);
493 break;
494 }
495 }
496 return;
497 }
498
499 /* We're feeding to a voice.
500 * Clamp and convert the mixed data for the voice.
501 */
502 *buf = mixer->ss.spl_data.buffer.ptr;
503 switch (buffer_depth & ~ALLEGRO_AUDIO_DEPTH_UNSIGNED) {
504
505 case ALLEGRO_AUDIO_DEPTH_FLOAT32:
506 /* Do we need to clamp? */
507 break;
508
509 case ALLEGRO_AUDIO_DEPTH_INT24:
510 switch (mixer->ss.spl_data.depth) {
511 case ALLEGRO_AUDIO_DEPTH_FLOAT32: {
512 int32_t off = ((buffer_depth & ALLEGRO_AUDIO_DEPTH_UNSIGNED)
513 ? 0x800000 : 0);
514 int32_t *lbuf = mixer->ss.spl_data.buffer.s24;
515 float *src = mixer->ss.spl_data.buffer.f32;
516
517 while (samples_l > 0) {
518 *lbuf = clamp(*(src++) * ((float)0x7FFFFF + 0.5f),
519 ~0x7FFFFF, 0x7FFFFF);
520 *lbuf += off;
521 lbuf++;
522 samples_l--;
523 }
524 break;
525 }
526
527 case ALLEGRO_AUDIO_DEPTH_INT16:
528 /* XXX not yet implemented */
529 ASSERT(false);
530 break;
531
532 case ALLEGRO_AUDIO_DEPTH_INT8:
533 case ALLEGRO_AUDIO_DEPTH_INT24:
534 case ALLEGRO_AUDIO_DEPTH_UINT8:
535 case ALLEGRO_AUDIO_DEPTH_UINT16:
536 case ALLEGRO_AUDIO_DEPTH_UINT24:
537 /* Unsupported mixer depths. */
538 ASSERT(false);
539 break;
540 }
541 break;
542
543 case ALLEGRO_AUDIO_DEPTH_INT16:
544 switch (mixer->ss.spl_data.depth) {
545 case ALLEGRO_AUDIO_DEPTH_FLOAT32: {
546 int16_t off = ((buffer_depth & ALLEGRO_AUDIO_DEPTH_UNSIGNED)
547 ? 0x8000 : 0);
548 int16_t *lbuf = mixer->ss.spl_data.buffer.s16;
549 float *src = mixer->ss.spl_data.buffer.f32;
550
551 while (samples_l > 0) {
552 *lbuf = clamp(*(src++) * ((float)0x7FFF + 0.5f), ~0x7FFF, 0x7FFF);
553 *lbuf += off;
554 lbuf++;
555 samples_l--;
556 }
557 break;
558 }
559
560 case ALLEGRO_AUDIO_DEPTH_INT16:
561 /* Handle signedness differences. */
562 if (buffer_depth != ALLEGRO_AUDIO_DEPTH_INT16) {
563 int16_t *lbuf = mixer->ss.spl_data.buffer.s16;
564 while (samples_l > 0) {
565 *lbuf++ ^= 0x8000;
566 samples_l--;
567 }
568 }
569 break;
570
571 case ALLEGRO_AUDIO_DEPTH_INT8:
572 case ALLEGRO_AUDIO_DEPTH_INT24:
573 case ALLEGRO_AUDIO_DEPTH_UINT8:
574 case ALLEGRO_AUDIO_DEPTH_UINT16:
575 case ALLEGRO_AUDIO_DEPTH_UINT24:
576 /* Unsupported mixer depths. */
577 ASSERT(false);
578 break;
579 }
580 break;
581
582 /* Ugh, do we really want to support 8-bit output? */
583 case ALLEGRO_AUDIO_DEPTH_INT8:
584 switch (mixer->ss.spl_data.depth) {
585 case ALLEGRO_AUDIO_DEPTH_FLOAT32: {
586 int8_t off = ((buffer_depth & ALLEGRO_AUDIO_DEPTH_UNSIGNED)
587 ? 0x80 : 0);
588 int8_t *lbuf = mixer->ss.spl_data.buffer.s8;
589 float *src = mixer->ss.spl_data.buffer.f32;
590
591 while (samples_l > 0) {
592 *lbuf = clamp(*(src++) * ((float)0x7F + 0.5f), ~0x7F, 0x7F);
593 *lbuf += off;
594 lbuf++;
595 samples_l--;
596 }
597 break;
598 }
599
600 case ALLEGRO_AUDIO_DEPTH_INT16:
601 /* XXX not yet implemented */
602 ASSERT(false);
603 break;
604
605 case ALLEGRO_AUDIO_DEPTH_INT8:
606 case ALLEGRO_AUDIO_DEPTH_INT24:
607 case ALLEGRO_AUDIO_DEPTH_UINT8:
608 case ALLEGRO_AUDIO_DEPTH_UINT16:
609 case ALLEGRO_AUDIO_DEPTH_UINT24:
610 /* Unsupported mixer depths. */
611 ASSERT(false);
612 break;
613 }
614 break;
615
616 case ALLEGRO_AUDIO_DEPTH_UINT8:
617 case ALLEGRO_AUDIO_DEPTH_UINT16:
618 case ALLEGRO_AUDIO_DEPTH_UINT24:
619 /* Impossible. */
620 ASSERT(false);
621 break;
622 }
623
624 (void)dest_maxc;
625 }
626
627
628 /* Function: al_create_mixer
629 */
630 ALLEGRO_MIXER *al_create_mixer(unsigned int freq,
631 ALLEGRO_AUDIO_DEPTH depth, ALLEGRO_CHANNEL_CONF chan_conf)
632 {
633 ALLEGRO_MIXER *mixer;
634 int default_mixer_quality = ALLEGRO_MIXER_QUALITY_LINEAR;
635 const char *p;
636
637 /* XXX this is in the wrong place */
638 p = al_get_config_value(al_get_system_config(), "audio",
639 "default_mixer_quality");
640 if (p && p[0] != '\0') {
641 if (!_al_stricmp(p, "point")) {
642 ALLEGRO_INFO("Point sampling\n");
643 default_mixer_quality = ALLEGRO_MIXER_QUALITY_POINT;
644 }
645 else if (!_al_stricmp(p, "linear")) {
646 ALLEGRO_INFO("Linear interpolation\n");
647 default_mixer_quality = ALLEGRO_MIXER_QUALITY_LINEAR;
648 }
649 else if (!_al_stricmp(p, "cubic")) {
650 ALLEGRO_INFO("Cubic interpolation\n");
651 default_mixer_quality = ALLEGRO_MIXER_QUALITY_CUBIC;
652 }
653 }
654
655 if (!freq) {
656 _al_set_error(ALLEGRO_INVALID_PARAM,
657 "Attempted to create mixer with no frequency");
658 return NULL;
659 }
660
661 if (depth != ALLEGRO_AUDIO_DEPTH_FLOAT32 &&
662 depth != ALLEGRO_AUDIO_DEPTH_INT16) {
663 _al_set_error(ALLEGRO_INVALID_PARAM, "Unsupported mixer depth");
664 return NULL;
665 }
666
667 mixer = al_calloc(1, sizeof(ALLEGRO_MIXER));
668 if (!mixer) {
669 _al_set_error(ALLEGRO_GENERIC_ERROR,
670 "Out of memory allocating mixer object");
671 return NULL;
672 }
673
674 mixer->ss.is_playing = true;
675 mixer->ss.spl_data.free_buf = true;
676
677 mixer->ss.loop = ALLEGRO_PLAYMODE_ONCE;
678 /* XXX should we have a specific loop mode? */
679 mixer->ss.gain = 1.0f;
680 mixer->ss.spl_data.depth = depth;
681 mixer->ss.spl_data.chan_conf = chan_conf;
682 mixer->ss.spl_data.frequency = freq;
683
684 mixer->ss.is_mixer = true;
685 mixer->ss.spl_read = NULL;
686
687 mixer->quality = default_mixer_quality;
688
689 _al_vector_init(&mixer->streams, sizeof(ALLEGRO_SAMPLE_INSTANCE *));
690
691 mixer->dtor_item = _al_kcm_register_destructor("mixer", mixer, (void (*)(void *)) al_destroy_mixer);
692
693 return mixer;
694 }
695
696
697 /* Function: al_destroy_mixer
698 */
699 void al_destroy_mixer(ALLEGRO_MIXER *mixer)
700 {
701 if (mixer) {
702 _al_kcm_unregister_destructor(mixer->dtor_item);
703 _al_kcm_destroy_sample(&mixer->ss, false);
704 }
705 }
706
707
708 /* This function is ALLEGRO_MIXER aware */
709 /* Function: al_attach_sample_instance_to_mixer
710 */
711 bool al_attach_sample_instance_to_mixer(ALLEGRO_SAMPLE_INSTANCE *spl,
712 ALLEGRO_MIXER *mixer)
713 {
714 ALLEGRO_SAMPLE_INSTANCE **slot;
715
716 ASSERT(mixer);
717 ASSERT(spl);
718
719 /* Already referenced, do not attach. */
720 if (spl->parent.u.ptr) {
721 _al_set_error(ALLEGRO_INVALID_OBJECT,
722 "Attempted to attach a sample that's already attached");
723 return false;
724 }
725
726 maybe_lock_mutex(mixer->ss.mutex);
727
728 _al_kcm_stream_set_mutex(spl, mixer->ss.mutex);
729
730 slot = _al_vector_alloc_back(&mixer->streams);
731 if (!slot) {
732 if (mixer->ss.mutex) {
733 al_unlock_mutex(mixer->ss.mutex);
734 }
735 _al_set_error(ALLEGRO_GENERIC_ERROR,
736 "Out of memory allocating attachment pointers");
737 return false;
738 }
739 (*slot) = spl;
740
741 spl->step = (spl->spl_data.frequency) * spl->speed;
742 spl->step_denom = mixer->ss.spl_data.frequency;
743 /* Don't want to be trapped with a step value of 0. */
744 if (spl->step == 0) {
745 if (spl->speed > 0.0f)
746 spl->step = 1;
747 else
748 spl->step = -1;
749 }
750
751 /* Set the proper sample stream reader. */
752 ASSERT(spl->spl_read == NULL);
753 if (spl->is_mixer) {
754 spl->spl_read = _al_kcm_mixer_read;
755 }
756 else {
757 switch (mixer->ss.spl_data.depth) {
758 case ALLEGRO_AUDIO_DEPTH_FLOAT32:
759 switch (mixer->quality) {
760 case ALLEGRO_MIXER_QUALITY_POINT:
761 spl->spl_read = read_to_mixer_point_float_32;
762 break;
763 case ALLEGRO_MIXER_QUALITY_LINEAR:
764 spl->spl_read = read_to_mixer_linear_float_32;
765 break;
766 case ALLEGRO_MIXER_QUALITY_CUBIC:
767 spl->spl_read = read_to_mixer_cubic_float_32;
768 break;
769 }
770 break;
771
772 case ALLEGRO_AUDIO_DEPTH_INT16:
773 switch (mixer->quality) {
774 case ALLEGRO_MIXER_QUALITY_POINT:
775 spl->spl_read = read_to_mixer_point_int16_t_16;
776 break;
777 case ALLEGRO_MIXER_QUALITY_CUBIC:
778 ALLEGRO_WARN("Falling back to linear interpolation\n");
779 /* fallthrough */
780 case ALLEGRO_MIXER_QUALITY_LINEAR:
781 spl->spl_read = read_to_mixer_linear_int16_t_16;
782 break;
783 }
784 break;
785
786 case ALLEGRO_AUDIO_DEPTH_INT8:
787 case ALLEGRO_AUDIO_DEPTH_INT24:
788 case ALLEGRO_AUDIO_DEPTH_UINT8:
789 case ALLEGRO_AUDIO_DEPTH_UINT16:
790 case ALLEGRO_AUDIO_DEPTH_UINT24:
791 /* Unsupported mixer depths. */
792 ASSERT(false);
793 break;
794 }
795
796 _al_kcm_mixer_rejig_sample_matrix(mixer, spl);
797 }
798
799 spl->parent.u.mixer = mixer;
800 spl->parent.is_voice = false;
801
802 maybe_unlock_mutex(mixer->ss.mutex);
803
804 return true;
805 }
806
807
808 /* Function: al_attach_audio_stream_to_mixer
809 */
810 bool al_attach_audio_stream_to_mixer(ALLEGRO_AUDIO_STREAM *stream, ALLEGRO_MIXER *mixer)
811 {
812 ASSERT(mixer);
813 ASSERT(stream);
814
815 return al_attach_sample_instance_to_mixer(&stream->spl, mixer);
816 }
817
818
819 /* Function: al_attach_mixer_to_mixer
820 */
821 bool al_attach_mixer_to_mixer(ALLEGRO_MIXER *stream, ALLEGRO_MIXER *mixer)
822 {
823 ASSERT(mixer);
824 ASSERT(stream);
825 ASSERT(mixer != stream);
826
827 if (mixer->ss.spl_data.frequency != stream->ss.spl_data.frequency) {
828 _al_set_error(ALLEGRO_INVALID_OBJECT,
829 "Attempted to attach a mixer with different frequencies");
830 return false;
831 }
832
833 if (mixer->ss.spl_data.depth != stream->ss.spl_data.depth) {
834 _al_set_error(ALLEGRO_INVALID_OBJECT,
835 "Mixers of different audio depths cannot be attached to one another");
836 return false;
837 }
838
839 if (mixer->ss.spl_data.chan_conf != stream->ss.spl_data.chan_conf) {
840 _al_set_error(ALLEGRO_INVALID_OBJECT,
841 "Mixers of different channel configurations cannot be attached to one another");
842 return false;
843 }
844
845 return al_attach_sample_instance_to_mixer(&stream->ss, mixer);
846 }
847
848
849 /* Function: al_set_mixer_postprocess_callback
850 */
851 bool al_set_mixer_postprocess_callback(ALLEGRO_MIXER *mixer,
852 void (*pp_callback)(void *buf, unsigned int samples, void *data),
853 void *pp_callback_userdata)
854 {
855 ASSERT(mixer);
856
857 maybe_lock_mutex(mixer->ss.mutex);
858
859 mixer->postprocess_callback = pp_callback;
860 mixer->pp_callback_userdata = pp_callback_userdata;
861
862 maybe_unlock_mutex(mixer->ss.mutex);
863
864 return true;
865 }
866
867
868 /* Function: al_get_mixer_frequency
869 */
870 unsigned int al_get_mixer_frequency(const ALLEGRO_MIXER *mixer)
871 {
872 ASSERT(mixer);
873
874 return mixer->ss.spl_data.frequency;
875 }
876
877
878 /* Function: al_get_mixer_channels
879 */
880 ALLEGRO_CHANNEL_CONF al_get_mixer_channels(const ALLEGRO_MIXER *mixer)
881 {
882 ASSERT(mixer);
883
884 return mixer->ss.spl_data.chan_conf;
885 }
886
887
888 /* Function: al_get_mixer_depth
889 */
890 ALLEGRO_AUDIO_DEPTH al_get_mixer_depth(const ALLEGRO_MIXER *mixer)
891 {
892 ASSERT(mixer);
893
894 return mixer->ss.spl_data.depth;
895 }
896
897
898 /* Function: al_get_mixer_quality
899 */
900 ALLEGRO_MIXER_QUALITY al_get_mixer_quality(const ALLEGRO_MIXER *mixer)
901 {
902 ASSERT(mixer);
903
904 return mixer->quality;
905 }
906
907
908 /* Function: al_get_mixer_gain
909 */
910 float al_get_mixer_gain(const ALLEGRO_MIXER *mixer)
911 {
912 ASSERT(mixer);
913
914 return mixer->ss.gain;
915 }
916
917
918 /* Function: al_get_mixer_playing
919 */
920 bool al_get_mixer_playing(const ALLEGRO_MIXER *mixer)
921 {
922 ASSERT(mixer);
923
924 return mixer->ss.is_playing;
925 }
926
927
928 /* Function: al_get_mixer_attached
929 */
930 bool al_get_mixer_attached(const ALLEGRO_MIXER *mixer)
931 {
932 ASSERT(mixer);
933
934 return mixer->ss.parent.u.ptr;
935 }
936
937 /* Function: al_mixer_has_attachments
938 */
939 bool al_mixer_has_attachments(const ALLEGRO_MIXER* mixer)
940 {
941 ASSERT(mixer);
942
943 return _al_vector_is_nonempty(&mixer->streams);
944 }
945
946
947 /* Function: al_set_mixer_frequency
948 */
949 bool al_set_mixer_frequency(ALLEGRO_MIXER *mixer, unsigned int val)
950 {
951 ASSERT(mixer);
952
953 /* You can change the frequency of a mixer as long as it's not attached
954 * to anything.
955 */
956 if (mixer->ss.parent.u.ptr) {
957 _al_set_error(ALLEGRO_INVALID_OBJECT,
958 "Attempted to change the frequency of an attached mixer");
959 return false;
960 }
961
962 mixer->ss.spl_data.frequency = val;
963 return true;
964 }
965
966
967 /* Function: al_set_mixer_quality
968 */
969 bool al_set_mixer_quality(ALLEGRO_MIXER *mixer, ALLEGRO_MIXER_QUALITY new_quality)
970 {
971 bool ret;
972 ASSERT(mixer);
973
974 maybe_lock_mutex(mixer->ss.mutex);
975
976 if (mixer->quality == new_quality) {
977 ret = true;
978 }
979 else if (_al_vector_size(&mixer->streams) == 0) {
980 mixer->quality = new_quality;
981 ret = true;
982 }
983 else {
984 _al_set_error(ALLEGRO_INVALID_OBJECT,
985 "Attempted to change the quality of a mixer with attachments");
986 ret = false;
987 }
988
989 maybe_unlock_mutex(mixer->ss.mutex);
990
991 return ret;
992 }
993
994
995 /* Function: al_set_mixer_gain
996 */
997 bool al_set_mixer_gain(ALLEGRO_MIXER *mixer, float new_gain)
998 {
999 int i;
1000 ASSERT(mixer);
1001
1002 maybe_lock_mutex(mixer->ss.mutex);
1003
1004 if (mixer->ss.gain != new_gain) {
1005 mixer->ss.gain = new_gain;
1006
1007 for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) {
1008 ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i);
1009 _al_kcm_mixer_rejig_sample_matrix(mixer, *slot);
1010 }
1011 }
1012
1013 maybe_unlock_mutex(mixer->ss.mutex);
1014
1015 return true;
1016 }
1017
1018
1019 /* Function: al_set_mixer_playing
1020 */
1021 bool al_set_mixer_playing(ALLEGRO_MIXER *mixer, bool val)
1022 {
1023 ASSERT(mixer);
1024
1025 mixer->ss.is_playing = val;
1026 return true;
1027 }
1028
1029
1030 /* Function: al_detach_mixer
1031 */
1032 bool al_detach_mixer(ALLEGRO_MIXER *mixer)
1033 {
1034 ASSERT(mixer);
1035
1036 _al_kcm_detach_from_parent(&mixer->ss);
1037 ASSERT(mixer->ss.spl_read == NULL);
1038 return true;
1039 }
1040
1041
1042 /* vim: set sts=3 sw=3 et: */