1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (audio_mixer.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "../../config.h"
27 #include <audio/audio_mixer.h>
28 #include <audio/audio_resampler.h>
31 #include <formats/rwav.h>
40 #ifdef HAVE_STB_VORBIS
41 #define STB_VORBIS_NO_PUSHDATA_API
42 #define STB_VORBIS_NO_STDIO
43 #define STB_VORBIS_NO_CRT
45 #include <stb/stb_vorbis.h>
49 #define DR_FLAC_IMPLEMENTATION
50 #include <dr/dr_flac.h>
54 #define DR_MP3_IMPLEMENTATION
55 #include <retro_assert.h>
56 #define DRMP3_ASSERT(expression) retro_assert(expression)
57 #include <dr/dr_mp3.h>
61 #include <ibxm/ibxm.h>
65 #include <rthreads/rthreads.h>
66 #define AUDIO_MIXER_LOCK(voice) slock_lock(voice->lock)
67 #define AUDIO_MIXER_UNLOCK(voice) slock_unlock(voice->lock)
69 #define AUDIO_MIXER_LOCK(voice) do {} while(0)
70 #define AUDIO_MIXER_UNLOCK(voice) do {} while(0)
73 #define AUDIO_MIXER_MAX_VOICES 8
74 #define AUDIO_MIXER_TEMP_BUFFER 8192
76 struct audio_mixer_sound
78 enum audio_mixer_type type;
89 #ifdef HAVE_STB_VORBIS
127 struct audio_mixer_voice
136 #ifdef HAVE_STB_VORBIS
140 void *resampler_data;
141 const retro_resampler_t *resampler;
145 unsigned buf_samples;
155 void *resampler_data;
156 const retro_resampler_t *resampler;
159 unsigned buf_samples;
168 void *resampler_data;
169 const retro_resampler_t *resampler;
173 unsigned buf_samples;
182 struct replay* stream;
183 struct module* module;
186 unsigned buf_samples;
190 audio_mixer_sound_t *sound;
191 audio_mixer_stop_cb_t stop_cb;
200 /* TODO/FIXME - static globals */
201 static struct audio_mixer_voice s_voices[AUDIO_MIXER_MAX_VOICES] = {0};
202 static unsigned s_rate = 0;
204 static void audio_mixer_release(audio_mixer_voice_t* voice);
207 static bool wav_to_float(const rwav_t* wav, float** pcm, size_t samples_out)
210 /* Allocate on a 16-byte boundary, and pad to a multiple of 16 bytes */
211 float *f = (float*)memalign_alloc(16,
212 ((samples_out + 15) & ~15) * sizeof(float));
219 if (wav->bitspersample == 8)
222 const uint8_t *u8 = (const uint8_t*)wav->samples;
224 if (wav->numchannels == 1)
226 for (i = wav->numsamples; i != 0; i--)
228 sample = (float)*u8++ / 255.0f;
229 sample = sample * 2.0f - 1.0f;
234 else if (wav->numchannels == 2)
236 for (i = wav->numsamples; i != 0; i--)
238 sample = (float)*u8++ / 255.0f;
239 sample = sample * 2.0f - 1.0f;
241 sample = (float)*u8++ / 255.0f;
242 sample = sample * 2.0f - 1.0f;
249 /* TODO/FIXME note to leiradel - can we use audio/conversion/s16_to_float
253 const int16_t *s16 = (const int16_t*)wav->samples;
255 if (wav->numchannels == 1)
257 for (i = wav->numsamples; i != 0; i--)
259 sample = (float)((int)*s16++ + 32768) / 65535.0f;
260 sample = sample * 2.0f - 1.0f;
265 else if (wav->numchannels == 2)
267 for (i = wav->numsamples; i != 0; i--)
269 sample = (float)((int)*s16++ + 32768) / 65535.0f;
270 sample = sample * 2.0f - 1.0f;
272 sample = (float)((int)*s16++ + 32768) / 65535.0f;
273 sample = sample * 2.0f - 1.0f;
282 static bool one_shot_resample(const float* in, size_t samples_in,
283 unsigned rate, const char *resampler_ident, enum resampler_quality quality,
284 float** out, size_t* samples_out)
286 struct resampler_data info;
288 const retro_resampler_t* resampler = NULL;
289 float ratio = (double)s_rate / (double)rate;
291 if (!retro_resampler_realloc(&data, &resampler,
292 resampler_ident, quality, ratio))
295 /* Allocate on a 16-byte boundary, and pad to a multiple of 16 bytes. We
296 * add 16 more samples in the formula below just as safeguard, because
297 * resampler->process sometimes reports more output samples than the
298 * formula below calculates. Ideally, audio resamplers should have a
299 * function to return the number of samples they will output given a
300 * count of input samples. */
301 *samples_out = (size_t)(samples_in * ratio);
302 *out = (float*)memalign_alloc(16,
303 (((*samples_out + 16) + 15) & ~15) * sizeof(float));
309 info.data_out = *out;
310 info.input_frames = samples_in / 2;
311 info.output_frames = 0;
314 resampler->process(data, &info);
315 resampler->free(data);
320 void audio_mixer_init(unsigned rate)
326 for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++)
328 audio_mixer_voice_t *voice = &s_voices[i];
330 voice->type = AUDIO_MIXER_TYPE_NONE;
333 voice->lock = slock_new();
338 void audio_mixer_done(void)
342 for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++)
344 audio_mixer_voice_t *voice = &s_voices[i];
346 AUDIO_MIXER_LOCK(voice);
347 audio_mixer_release(voice);
348 AUDIO_MIXER_UNLOCK(voice);
350 slock_free(voice->lock);
356 audio_mixer_sound_t* audio_mixer_load_wav(void *buffer, int32_t size,
357 const char *resampler_ident, enum resampler_quality quality)
362 /* WAV samples converted to float */
366 audio_mixer_sound_t* sound = NULL;
368 wav.bitspersample = 0;
372 wav.subchunk2size = 0;
375 if ((rwav_load(&wav, buffer, size)) != RWAV_ITERATE_DONE)
378 samples = wav.numsamples * 2;
380 if (!wav_to_float(&wav, &pcm, samples))
383 if (wav.samplerate != s_rate)
385 float* resampled = NULL;
387 if (!one_shot_resample(pcm, samples, wav.samplerate,
388 resampler_ident, quality,
389 &resampled, &samples))
392 memalign_free((void*)pcm);
396 sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
400 memalign_free((void*)pcm);
404 sound->type = AUDIO_MIXER_TYPE_WAV;
405 sound->types.wav.frames = (unsigned)(samples / 2);
406 sound->types.wav.pcm = pcm;
416 audio_mixer_sound_t* audio_mixer_load_ogg(void *buffer, int32_t size)
418 #ifdef HAVE_STB_VORBIS
419 audio_mixer_sound_t* sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
424 sound->type = AUDIO_MIXER_TYPE_OGG;
425 sound->types.ogg.size = size;
426 sound->types.ogg.data = buffer;
434 audio_mixer_sound_t* audio_mixer_load_flac(void *buffer, int32_t size)
437 audio_mixer_sound_t* sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
442 sound->type = AUDIO_MIXER_TYPE_FLAC;
443 sound->types.flac.size = size;
444 sound->types.flac.data = buffer;
452 audio_mixer_sound_t* audio_mixer_load_mp3(void *buffer, int32_t size)
455 audio_mixer_sound_t* sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
460 sound->type = AUDIO_MIXER_TYPE_MP3;
461 sound->types.mp3.size = size;
462 sound->types.mp3.data = buffer;
470 audio_mixer_sound_t* audio_mixer_load_mod(void *buffer, int32_t size)
473 audio_mixer_sound_t* sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
478 sound->type = AUDIO_MIXER_TYPE_MOD;
479 sound->types.mod.size = size;
480 sound->types.mod.data = buffer;
488 void audio_mixer_destroy(audio_mixer_sound_t* sound)
496 case AUDIO_MIXER_TYPE_WAV:
497 handle = (void*)sound->types.wav.pcm;
499 memalign_free(handle);
501 case AUDIO_MIXER_TYPE_OGG:
502 #ifdef HAVE_STB_VORBIS
503 handle = (void*)sound->types.ogg.data;
508 case AUDIO_MIXER_TYPE_MOD:
510 handle = (void*)sound->types.mod.data;
515 case AUDIO_MIXER_TYPE_FLAC:
517 handle = (void*)sound->types.flac.data;
522 case AUDIO_MIXER_TYPE_MP3:
524 handle = (void*)sound->types.mp3.data;
529 case AUDIO_MIXER_TYPE_NONE:
536 static bool audio_mixer_play_wav(audio_mixer_sound_t* sound,
537 audio_mixer_voice_t* voice, bool repeat, float volume,
538 audio_mixer_stop_cb_t stop_cb)
540 voice->types.wav.position = 0;
544 #ifdef HAVE_STB_VORBIS
545 static bool audio_mixer_play_ogg(
546 audio_mixer_sound_t* sound,
547 audio_mixer_voice_t* voice,
548 bool repeat, float volume,
549 const char *resampler_ident,
550 enum resampler_quality quality,
551 audio_mixer_stop_cb_t stop_cb)
553 stb_vorbis_info info;
556 unsigned samples = 0;
557 void *ogg_buffer = NULL;
558 void *resampler_data = NULL;
559 const retro_resampler_t* resamp = NULL;
560 stb_vorbis *stb_vorbis = stb_vorbis_open_memory(
561 (const unsigned char*)sound->types.ogg.data,
562 sound->types.ogg.size, &res, NULL);
567 info = stb_vorbis_get_info(stb_vorbis);
569 if (info.sample_rate != s_rate)
571 ratio = (double)s_rate / (double)info.sample_rate;
573 if (!retro_resampler_realloc(&resampler_data,
574 &resamp, resampler_ident, quality,
579 /* Allocate on a 16-byte boundary, and pad to a multiple of 16 bytes. We
580 * add 16 more samples in the formula below just as safeguard, because
581 * resampler->process sometimes reports more output samples than the
582 * formula below calculates. Ideally, audio resamplers should have a
583 * function to return the number of samples they will output given a
584 * count of input samples. */
585 samples = (unsigned)(AUDIO_MIXER_TEMP_BUFFER * ratio);
586 ogg_buffer = (float*)memalign_alloc(16,
587 (((samples + 16) + 15) & ~15) * sizeof(float));
591 if (resamp && resampler_data)
592 resamp->free(resampler_data);
596 voice->types.ogg.resampler = resamp;
597 voice->types.ogg.resampler_data = resampler_data;
598 voice->types.ogg.buffer = (float*)ogg_buffer;
599 voice->types.ogg.buf_samples = samples;
600 voice->types.ogg.ratio = ratio;
601 voice->types.ogg.stream = stb_vorbis;
602 voice->types.ogg.position = 0;
603 voice->types.ogg.samples = 0;
608 stb_vorbis_close(stb_vorbis);
612 static void audio_mixer_release_ogg(audio_mixer_voice_t* voice)
614 if (voice->types.ogg.stream)
615 stb_vorbis_close(voice->types.ogg.stream);
616 if (voice->types.ogg.resampler && voice->types.ogg.resampler_data)
617 voice->types.ogg.resampler->free(voice->types.ogg.resampler_data);
618 if (voice->types.ogg.buffer)
619 memalign_free(voice->types.ogg.buffer);
625 static bool audio_mixer_play_mod(
626 audio_mixer_sound_t* sound,
627 audio_mixer_voice_t* voice,
628 bool repeat, float volume,
629 audio_mixer_stop_cb_t stop_cb)
635 void *mod_buffer = NULL;
636 struct module* module = NULL;
637 struct replay* replay = NULL;
639 data.buffer = (char*)sound->types.mod.data;
640 data.length = sound->types.mod.size;
641 module = module_load(&data, message);
645 printf("audio_mixer_play_mod module_load() failed with error: %s\n", message);
649 if (voice->types.mod.module)
650 dispose_module(voice->types.mod.module);
652 voice->types.mod.module = module;
654 replay = new_replay(module, s_rate, 1);
658 printf("audio_mixer_play_mod new_replay() failed\n");
662 buf_samples = calculate_mix_buf_len(s_rate);
663 mod_buffer = memalign_alloc(16, ((buf_samples + 15) & ~15) * sizeof(int));
667 printf("audio_mixer_play_mod cannot allocate mod_buffer !\n");
671 samples = replay_calculate_duration(replay);
675 printf("audio_mixer_play_mod cannot retrieve duration !\n");
679 voice->types.mod.buffer = (int*)mod_buffer;
680 voice->types.mod.buf_samples = buf_samples;
681 voice->types.mod.stream = replay;
682 voice->types.mod.position = 0;
683 voice->types.mod.samples = 0; /* samples; */
689 memalign_free(mod_buffer);
691 dispose_module(module);
696 static void audio_mixer_release_mod(audio_mixer_voice_t* voice)
698 if (voice->types.mod.stream)
699 dispose_replay(voice->types.mod.stream);
700 if (voice->types.mod.buffer)
701 memalign_free(voice->types.mod.buffer);
706 static bool audio_mixer_play_flac(
707 audio_mixer_sound_t* sound,
708 audio_mixer_voice_t* voice,
709 bool repeat, float volume,
710 const char *resampler_ident,
711 enum resampler_quality quality,
712 audio_mixer_stop_cb_t stop_cb)
715 unsigned samples = 0;
716 void *flac_buffer = NULL;
717 void *resampler_data = NULL;
718 const retro_resampler_t* resamp = NULL;
719 drflac *dr_flac = drflac_open_memory((const unsigned char*)sound->types.flac.data,sound->types.flac.size);
723 if (dr_flac->sampleRate != s_rate)
725 ratio = (double)s_rate / (double)(dr_flac->sampleRate);
727 if (!retro_resampler_realloc(&resampler_data,
728 &resamp, resampler_ident, quality,
733 /* Allocate on a 16-byte boundary, and pad to a multiple of 16 bytes. We
734 * add 16 more samples in the formula below just as safeguard, because
735 * resampler->process sometimes reports more output samples than the
736 * formula below calculates. Ideally, audio resamplers should have a
737 * function to return the number of samples they will output given a
738 * count of input samples. */
739 samples = (unsigned)(AUDIO_MIXER_TEMP_BUFFER * ratio);
740 flac_buffer = (float*)memalign_alloc(16,
741 (((samples + 16) + 15) & ~15) * sizeof(float));
745 if (resamp && resamp->free)
746 resamp->free(resampler_data);
750 voice->types.flac.resampler = resamp;
751 voice->types.flac.resampler_data = resampler_data;
752 voice->types.flac.buffer = (float*)flac_buffer;
753 voice->types.flac.buf_samples = samples;
754 voice->types.flac.ratio = ratio;
755 voice->types.flac.stream = dr_flac;
756 voice->types.flac.position = 0;
757 voice->types.flac.samples = 0;
762 drflac_close(dr_flac);
766 static void audio_mixer_release_flac(audio_mixer_voice_t* voice)
768 if (voice->types.flac.stream)
769 drflac_close(voice->types.flac.stream);
770 if (voice->types.flac.resampler && voice->types.flac.resampler_data)
771 voice->types.flac.resampler->free(voice->types.flac.resampler_data);
772 if (voice->types.flac.buffer)
773 memalign_free(voice->types.flac.buffer);
778 static bool audio_mixer_play_mp3(
779 audio_mixer_sound_t* sound,
780 audio_mixer_voice_t* voice,
781 bool repeat, float volume,
782 const char *resampler_ident,
783 enum resampler_quality quality,
784 audio_mixer_stop_cb_t stop_cb)
787 unsigned samples = 0;
788 void *mp3_buffer = NULL;
789 void *resampler_data = NULL;
790 const retro_resampler_t* resamp = NULL;
793 res = drmp3_init_memory(&voice->types.mp3.stream, (const unsigned char*)sound->types.mp3.data, sound->types.mp3.size, NULL);
798 if (voice->types.mp3.stream.sampleRate != s_rate)
800 ratio = (double)s_rate / (double)(voice->types.mp3.stream.sampleRate);
802 if (!retro_resampler_realloc(&resampler_data,
803 &resamp, resampler_ident, quality,
808 /* Allocate on a 16-byte boundary, and pad to a multiple of 16 bytes. We
809 * add 16 more samples in the formula below just as safeguard, because
810 * resampler->process sometimes reports more output samples than the
811 * formula below calculates. Ideally, audio resamplers should have a
812 * function to return the number of samples they will output given a
813 * count of input samples. */
814 samples = (unsigned)(AUDIO_MIXER_TEMP_BUFFER * ratio);
815 mp3_buffer = (float*)memalign_alloc(16,
816 (((samples + 16) + 15) & ~15) * sizeof(float));
820 if (resamp && resampler_data)
821 resamp->free(resampler_data);
825 voice->types.mp3.resampler = resamp;
826 voice->types.mp3.resampler_data = resampler_data;
827 voice->types.mp3.buffer = (float*)mp3_buffer;
828 voice->types.mp3.buf_samples = samples;
829 voice->types.mp3.ratio = ratio;
830 voice->types.mp3.position = 0;
831 voice->types.mp3.samples = 0;
836 drmp3_uninit(&voice->types.mp3.stream);
840 static void audio_mixer_release_mp3(audio_mixer_voice_t* voice)
842 if (voice->types.mp3.resampler && voice->types.mp3.resampler_data)
843 voice->types.mp3.resampler->free(voice->types.mp3.resampler_data);
844 if (voice->types.mp3.buffer)
845 memalign_free(voice->types.mp3.buffer);
846 if (voice->types.mp3.stream.pData)
847 drmp3_uninit(&voice->types.mp3.stream);
852 audio_mixer_voice_t* audio_mixer_play(audio_mixer_sound_t* sound,
853 bool repeat, float volume,
854 const char *resampler_ident,
855 enum resampler_quality quality,
856 audio_mixer_stop_cb_t stop_cb)
860 audio_mixer_voice_t* voice = s_voices;
865 for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++, voice++)
867 if (voice->type != AUDIO_MIXER_TYPE_NONE)
870 AUDIO_MIXER_LOCK(voice);
872 if (voice->type != AUDIO_MIXER_TYPE_NONE)
874 AUDIO_MIXER_UNLOCK(voice);
878 /* claim the voice, also helps with cleanup on error */
879 voice->type = sound->type;
883 case AUDIO_MIXER_TYPE_WAV:
884 res = audio_mixer_play_wav(sound, voice, repeat, volume, stop_cb);
886 case AUDIO_MIXER_TYPE_OGG:
887 #ifdef HAVE_STB_VORBIS
888 res = audio_mixer_play_ogg(sound, voice, repeat, volume,
889 resampler_ident, quality, stop_cb);
892 case AUDIO_MIXER_TYPE_MOD:
894 res = audio_mixer_play_mod(sound, voice, repeat, volume, stop_cb);
897 case AUDIO_MIXER_TYPE_FLAC:
899 res = audio_mixer_play_flac(sound, voice, repeat, volume,
900 resampler_ident, quality, stop_cb);
903 case AUDIO_MIXER_TYPE_MP3:
905 res = audio_mixer_play_mp3(sound, voice, repeat, volume,
906 resampler_ident, quality, stop_cb);
909 case AUDIO_MIXER_TYPE_NONE:
918 voice->repeat = repeat;
919 voice->volume = volume;
920 voice->sound = sound;
921 voice->stop_cb = stop_cb;
922 AUDIO_MIXER_UNLOCK(voice);
926 if (i < AUDIO_MIXER_MAX_VOICES)
928 audio_mixer_release(voice);
929 AUDIO_MIXER_UNLOCK(voice);
937 /* Need to hold lock for voice. */
938 static void audio_mixer_release(audio_mixer_voice_t* voice)
945 #ifdef HAVE_STB_VORBIS
946 case AUDIO_MIXER_TYPE_OGG:
947 audio_mixer_release_ogg(voice);
951 case AUDIO_MIXER_TYPE_MOD:
952 audio_mixer_release_mod(voice);
956 case AUDIO_MIXER_TYPE_FLAC:
957 audio_mixer_release_flac(voice);
961 case AUDIO_MIXER_TYPE_MP3:
962 audio_mixer_release_mp3(voice);
969 memset(&voice->types, 0, sizeof(voice->types));
970 voice->type = AUDIO_MIXER_TYPE_NONE;
973 void audio_mixer_stop(audio_mixer_voice_t* voice)
975 audio_mixer_stop_cb_t stop_cb = NULL;
976 audio_mixer_sound_t* sound = NULL;
980 AUDIO_MIXER_LOCK(voice);
981 stop_cb = voice->stop_cb;
982 sound = voice->sound;
984 audio_mixer_release(voice);
986 AUDIO_MIXER_UNLOCK(voice);
989 stop_cb(sound, AUDIO_MIXER_SOUND_STOPPED);
993 static void audio_mixer_mix_wav(float* buffer, size_t num_frames,
994 audio_mixer_voice_t* voice,
998 unsigned buf_free = (unsigned)(num_frames * 2);
999 const audio_mixer_sound_t* sound = voice->sound;
1000 unsigned pcm_available = sound->types.wav.frames
1001 * 2 - voice->types.wav.position;
1002 const float* pcm = sound->types.wav.pcm +
1003 voice->types.wav.position;
1006 if (pcm_available < buf_free)
1008 for (i = pcm_available; i != 0; i--)
1009 *buffer++ += *pcm++ * volume;
1014 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_REPEATED);
1016 buf_free -= pcm_available;
1017 pcm_available = sound->types.wav.frames * 2;
1018 pcm = sound->types.wav.pcm;
1019 voice->types.wav.position = 0;
1024 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_FINISHED);
1026 audio_mixer_release(voice);
1030 for (i = buf_free; i != 0; i--)
1031 *buffer++ += *pcm++ * volume;
1033 voice->types.wav.position += buf_free;
1037 #ifdef HAVE_STB_VORBIS
1038 static void audio_mixer_mix_ogg(float* buffer, size_t num_frames,
1039 audio_mixer_voice_t* voice,
1043 float* temp_buffer = NULL;
1044 unsigned buf_free = (unsigned)(num_frames * 2);
1045 unsigned temp_samples = 0;
1048 if (!voice->types.ogg.stream)
1051 if (voice->types.ogg.position == voice->types.ogg.samples)
1054 if (temp_buffer == NULL)
1055 temp_buffer = (float*)malloc(AUDIO_MIXER_TEMP_BUFFER * sizeof(float));
1057 temp_samples = stb_vorbis_get_samples_float_interleaved(
1058 voice->types.ogg.stream, 2, temp_buffer,
1059 AUDIO_MIXER_TEMP_BUFFER) * 2;
1061 if (temp_samples == 0)
1066 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_REPEATED);
1068 stb_vorbis_seek_start(voice->types.ogg.stream);
1073 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_FINISHED);
1075 audio_mixer_release(voice);
1079 if (voice->types.ogg.resampler)
1081 struct resampler_data info;
1082 info.data_in = temp_buffer;
1083 info.data_out = voice->types.ogg.buffer;
1084 info.input_frames = temp_samples / 2;
1085 info.output_frames = 0;
1086 info.ratio = voice->types.ogg.ratio;
1088 voice->types.ogg.resampler->process(
1089 voice->types.ogg.resampler_data, &info);
1092 memcpy(voice->types.ogg.buffer, temp_buffer,
1093 temp_samples * sizeof(float));
1095 voice->types.ogg.position = 0;
1096 voice->types.ogg.samples = voice->types.ogg.buf_samples;
1099 pcm = voice->types.ogg.buffer + voice->types.ogg.position;
1101 if (voice->types.ogg.samples < buf_free)
1103 for (i = voice->types.ogg.samples; i != 0; i--)
1104 *buffer++ += *pcm++ * volume;
1106 buf_free -= voice->types.ogg.samples;
1110 for (i = buf_free; i != 0; --i )
1111 *buffer++ += *pcm++ * volume;
1113 voice->types.ogg.position += buf_free;
1114 voice->types.ogg.samples -= buf_free;
1117 if (temp_buffer != NULL)
1123 static void audio_mixer_mix_mod(float* buffer, size_t num_frames,
1124 audio_mixer_voice_t* voice,
1128 float samplef = 0.0f;
1129 unsigned temp_samples = 0;
1130 unsigned buf_free = (unsigned)(num_frames * 2);
1133 if (voice->types.mod.samples == 0)
1136 temp_samples = replay_get_audio(
1137 voice->types.mod.stream, voice->types.mod.buffer, 0 ) * 2;
1139 if (temp_samples == 0)
1144 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_REPEATED);
1146 replay_seek( voice->types.mod.stream, 0);
1151 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_FINISHED);
1153 audio_mixer_release(voice);
1157 voice->types.mod.position = 0;
1158 voice->types.mod.samples = temp_samples;
1160 pcm = voice->types.mod.buffer + voice->types.mod.position;
1162 if (voice->types.mod.samples < buf_free)
1164 for (i = voice->types.mod.samples; i != 0; i--)
1166 samplef = ((float)(*pcm++) + 32768.0f) / 65535.0f;
1167 samplef = samplef * 2.0f - 1.0f;
1168 *buffer++ += samplef * volume;
1171 buf_free -= voice->types.mod.samples;
1175 for (i = buf_free; i != 0; --i )
1177 samplef = ((float)(*pcm++) + 32768.0f) / 65535.0f;
1178 samplef = samplef * 2.0f - 1.0f;
1179 *buffer++ += samplef * volume;
1182 voice->types.mod.position += buf_free;
1183 voice->types.mod.samples -= buf_free;
1188 static void audio_mixer_mix_flac(float* buffer, size_t num_frames,
1189 audio_mixer_voice_t* voice,
1193 struct resampler_data info;
1194 float temp_buffer[AUDIO_MIXER_TEMP_BUFFER] = { 0 };
1195 unsigned buf_free = (unsigned)(num_frames * 2);
1196 unsigned temp_samples = 0;
1199 if (voice->types.flac.position == voice->types.flac.samples)
1202 temp_samples = (unsigned)drflac_read_f32( voice->types.flac.stream, AUDIO_MIXER_TEMP_BUFFER, temp_buffer);
1203 if (temp_samples == 0)
1208 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_REPEATED);
1210 drflac_seek_to_sample(voice->types.flac.stream,0);
1215 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_FINISHED);
1217 audio_mixer_release(voice);
1221 info.data_in = temp_buffer;
1222 info.data_out = voice->types.flac.buffer;
1223 info.input_frames = temp_samples / 2;
1224 info.output_frames = 0;
1225 info.ratio = voice->types.flac.ratio;
1227 if (voice->types.flac.resampler)
1228 voice->types.flac.resampler->process(
1229 voice->types.flac.resampler_data, &info);
1231 memcpy(voice->types.flac.buffer, temp_buffer, temp_samples * sizeof(float));
1232 voice->types.flac.position = 0;
1233 voice->types.flac.samples = voice->types.flac.buf_samples;
1236 pcm = voice->types.flac.buffer + voice->types.flac.position;
1238 if (voice->types.flac.samples < buf_free)
1240 for (i = voice->types.flac.samples; i != 0; i--)
1241 *buffer++ += *pcm++ * volume;
1243 buf_free -= voice->types.flac.samples;
1247 for (i = buf_free; i != 0; --i )
1248 *buffer++ += *pcm++ * volume;
1250 voice->types.flac.position += buf_free;
1251 voice->types.flac.samples -= buf_free;
1256 static void audio_mixer_mix_mp3(float* buffer, size_t num_frames,
1257 audio_mixer_voice_t* voice,
1261 struct resampler_data info;
1262 float temp_buffer[AUDIO_MIXER_TEMP_BUFFER] = { 0 };
1263 unsigned buf_free = (unsigned)(num_frames * 2);
1264 unsigned temp_samples = 0;
1267 if (voice->types.mp3.position == voice->types.mp3.samples)
1270 temp_samples = (unsigned)drmp3_read_f32(
1271 &voice->types.mp3.stream,
1272 AUDIO_MIXER_TEMP_BUFFER / 2, temp_buffer) * 2;
1274 if (temp_samples == 0)
1279 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_REPEATED);
1281 drmp3_seek_to_frame(&voice->types.mp3.stream,0);
1286 voice->stop_cb(voice->sound, AUDIO_MIXER_SOUND_FINISHED);
1288 audio_mixer_release(voice);
1292 info.data_in = temp_buffer;
1293 info.data_out = voice->types.mp3.buffer;
1294 info.input_frames = temp_samples / 2;
1295 info.output_frames = 0;
1296 info.ratio = voice->types.mp3.ratio;
1298 if (voice->types.mp3.resampler)
1299 voice->types.mp3.resampler->process(
1300 voice->types.mp3.resampler_data, &info);
1302 memcpy(voice->types.mp3.buffer, temp_buffer,
1303 temp_samples * sizeof(float));
1304 voice->types.mp3.position = 0;
1305 voice->types.mp3.samples = voice->types.mp3.buf_samples;
1308 pcm = voice->types.mp3.buffer + voice->types.mp3.position;
1310 if (voice->types.mp3.samples < buf_free)
1312 for (i = voice->types.mp3.samples; i != 0; i--)
1313 *buffer++ += *pcm++ * volume;
1315 buf_free -= voice->types.mp3.samples;
1319 for (i = buf_free; i != 0; --i )
1320 *buffer++ += *pcm++ * volume;
1322 voice->types.mp3.position += buf_free;
1323 voice->types.mp3.samples -= buf_free;
1327 void audio_mixer_mix(float* buffer, size_t num_frames,
1328 float volume_override, bool override)
1332 float* sample = NULL;
1333 audio_mixer_voice_t* voice = s_voices;
1335 for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++, voice++)
1339 AUDIO_MIXER_LOCK(voice);
1341 volume = (override) ? volume_override : voice->volume;
1343 switch (voice->type)
1345 case AUDIO_MIXER_TYPE_WAV:
1346 audio_mixer_mix_wav(buffer, num_frames, voice, volume);
1348 case AUDIO_MIXER_TYPE_OGG:
1349 #ifdef HAVE_STB_VORBIS
1350 audio_mixer_mix_ogg(buffer, num_frames, voice, volume);
1353 case AUDIO_MIXER_TYPE_MOD:
1355 audio_mixer_mix_mod(buffer, num_frames, voice, volume);
1358 case AUDIO_MIXER_TYPE_FLAC:
1360 audio_mixer_mix_flac(buffer, num_frames, voice, volume);
1363 case AUDIO_MIXER_TYPE_MP3:
1365 audio_mixer_mix_mp3(buffer, num_frames, voice, volume);
1368 case AUDIO_MIXER_TYPE_NONE:
1372 AUDIO_MIXER_UNLOCK(voice);
1375 for (j = 0, sample = buffer; j < num_frames * 2; j++, sample++)
1377 if (*sample < -1.0f)
1379 else if (*sample > 1.0f)
1384 float audio_mixer_voice_get_volume(audio_mixer_voice_t *voice)
1389 return voice->volume;
1392 void audio_mixer_voice_set_volume(audio_mixer_voice_t *voice, float val)
1397 AUDIO_MIXER_LOCK(voice);
1398 voice->volume = val;
1399 AUDIO_MIXER_UNLOCK(voice);