From 0f17ad23f9b1496173c4246e2b702ce3b1fafdb2 Mon Sep 17 00:00:00 2001 From: Dario Date: Thu, 12 Dec 2024 00:27:50 -0300 Subject: [PATCH] Rework embedded sound player to support simultaneous playback. --- UnleashedRecomp/apu/audio.cpp | 26 +++++ UnleashedRecomp/apu/embedded_player.cpp | 148 ++++++++++++++---------- UnleashedRecompResources | 2 +- thirdparty/miniaudio | 1 + 4 files changed, 118 insertions(+), 59 deletions(-) create mode 160000 thirdparty/miniaudio diff --git a/UnleashedRecomp/apu/audio.cpp b/UnleashedRecomp/apu/audio.cpp index c234c5a0..7bb0aed3 100644 --- a/UnleashedRecomp/apu/audio.cpp +++ b/UnleashedRecomp/apu/audio.cpp @@ -1,11 +1,24 @@ #include + +#include + #include "audio.h" #include "cpu/code_cache.h" #define AUDIO_DRIVER_KEY (uint32_t)('DAUD') +//#define AUDIO_DUMP_SAMPLES_PATH "audio.pcm" + +#ifdef AUDIO_DUMP_SAMPLES_PATH +std::ofstream g_audioDumpStream; +#endif + uint32_t XAudioRegisterRenderDriverClient(XLPDWORD callback, XLPDWORD driver) { +#ifdef AUDIO_DUMP_SAMPLES_PATH + g_audioDumpStream.open(AUDIO_DUMP_SAMPLES_PATH, std::ios::binary); +#endif + *driver = AUDIO_DRIVER_KEY; XAudioRegisterClient(KeFindHostFunction(*callback), callback[1]); return 0; @@ -18,6 +31,19 @@ uint32_t XAudioUnregisterRenderDriverClient(DWORD driver) uint32_t XAudioSubmitRenderDriverFrame(uint32_t driver, void* samples) { +#ifdef AUDIO_DUMP_SAMPLES_PATH + static uint32_t xaudioSamplesBuffer[XAUDIO_NUM_SAMPLES * XAUDIO_NUM_CHANNELS]; + for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++) + { + for (size_t j = 0; j < XAUDIO_NUM_CHANNELS; j++) + { + xaudioSamplesBuffer[i * XAUDIO_NUM_CHANNELS + j] = std::byteswap(((uint32_t *)samples)[j * XAUDIO_NUM_SAMPLES + i]); + } + } + + g_audioDumpStream.write((const char *)(xaudioSamplesBuffer), sizeof(xaudioSamplesBuffer)); +#endif + XAudioSubmitFrame(samples); return 0; } diff --git a/UnleashedRecomp/apu/embedded_player.cpp b/UnleashedRecomp/apu/embedded_player.cpp index d9f38a5f..80398c3d 100644 --- a/UnleashedRecomp/apu/embedded_player.cpp +++ b/UnleashedRecomp/apu/embedded_player.cpp @@ -13,58 +13,87 @@ enum EmbeddedSound EmbeddedSoundCount, }; +struct EmbeddedSoundData +{ + static const int SimultaneousLimit = 4; + std::array, SimultaneousLimit> sounds; + std::array, SimultaneousLimit> decoders; + int oldestIndex = 0; +}; + static ma_engine g_audioEngine = {}; static bool g_audioEngineInitialized = false; -static std::array g_embeddedSoundCache = {}; -static std::array g_embeddedDecoderCache = {}; +static std::array g_embeddedSoundData = {}; static const std::unordered_map g_embeddedSoundMap = { { "sys_worldmap_cursor", EmbeddedSoundSysWorldMapCursor }, { "sys_worldmap_finaldecide", EmbeddedSoundSysWorldMapFinalDecide }, }; -void InitEmbeddedSound(EmbeddedSound s) +static void PlayEmbeddedSound(EmbeddedSound s) { - ma_sound &sound = g_embeddedSoundCache[s]; - ma_decoder &decoder = g_embeddedDecoderCache[s]; - const void *soundData = nullptr; - size_t soundDataSize = 0; - float volume = Config::EffectsVolume; - switch (s) + EmbeddedSoundData &data = g_embeddedSoundData[s]; + int pickedIndex = -1; + for (int i = 0; (i < EmbeddedSoundData::SimultaneousLimit) && (pickedIndex < 0); i++) { - case EmbeddedSoundSysWorldMapCursor: - soundData = g_sys_worldmap_cursor; - soundDataSize = sizeof(g_sys_worldmap_cursor); - volume *= 0.259f; - break; - case EmbeddedSoundSysWorldMapFinalDecide: - soundData = g_sys_worldmap_finaldecide; - soundDataSize = sizeof(g_sys_worldmap_finaldecide); - volume *= 0.61f; - break; - default: - assert(false && "Unknown embedded sound."); - return; + if (data.sounds[i] == nullptr) + { + // The sound hasn't been created yet, create it and pick it. + const void *soundData = nullptr; + size_t soundDataSize = 0; + switch (s) + { + case EmbeddedSoundSysWorldMapCursor: + soundData = g_sys_worldmap_cursor; + soundDataSize = sizeof(g_sys_worldmap_cursor); + break; + case EmbeddedSoundSysWorldMapFinalDecide: + soundData = g_sys_worldmap_finaldecide; + soundDataSize = sizeof(g_sys_worldmap_finaldecide); + break; + default: + assert(false && "Unknown embedded sound."); + return; + } + + ma_result res; + data.decoders[i] = std::make_unique(); + res = ma_decoder_init_memory(soundData, soundDataSize, nullptr, data.decoders[i].get()); + if (res != MA_SUCCESS) + { + fprintf(stderr, "ma_decoder_init_memory failed with error code %d on embedded sound %d.\n", res, s); + return; + } + + data.sounds[i] = std::make_unique(); + res = ma_sound_init_from_data_source(&g_audioEngine, data.decoders[i].get(), MA_SOUND_FLAG_DECODE, nullptr, data.sounds[i].get()); + if (res != MA_SUCCESS) + { + fprintf(stderr, "ma_sound_init_from_data_source failed with error code %d on embedded sound %d.\n", res, s); + return; + } + + pickedIndex = i; + } + else if (ma_sound_at_end(data.sounds[i].get())) + { + // A sound has reached the end, pick it. + pickedIndex = i; + } } - if (soundData != nullptr) + if (pickedIndex < 0) { - ma_result res; - res = ma_decoder_init_memory(soundData, soundDataSize, nullptr, &decoder); - if (res != MA_SUCCESS) - { - fprintf(stderr, "ma_decoder_init_memory failed with error code %d on embedded sound %d.\n", res, s); - return; - } + // No free slots are available, pick the oldest one. + pickedIndex = data.oldestIndex; + data.oldestIndex = (data.oldestIndex + 1) % EmbeddedSoundData::SimultaneousLimit; + } - res = ma_sound_init_from_data_source(&g_audioEngine, &decoder, MA_SOUND_FLAG_DECODE, nullptr, &sound); - if (res != MA_SUCCESS) - { - fprintf(stderr, "ma_sound_init_from_data_source failed with error code %d on embedded sound %d.\n", res, s); - return; - } - - ma_sound_set_volume(&sound, volume); + if (data.sounds[pickedIndex] != nullptr) + { + ma_sound_set_volume(data.sounds[pickedIndex].get(), Config::EffectsVolume); + ma_sound_seek_to_pcm_frame(data.sounds[pickedIndex].get(), 0); + ma_sound_start(data.sounds[pickedIndex].get()); } } @@ -101,34 +130,37 @@ void EmbeddedPlayer::Play(const char *name) return; } - ma_sound &sound = g_embeddedSoundCache[it->second]; - if (sound.pDataSource == nullptr) - { - InitEmbeddedSound(it->second); - } - - if (sound.pDataSource != nullptr) - { - ma_sound_seek_to_pcm_frame(&sound, 0); - ma_sound_start(&sound); - } + PlayEmbeddedSound(it->second); } void EmbeddedPlayer::Shutdown() { - for (ma_sound &sound : g_embeddedSoundCache) + for (EmbeddedSoundData &data : g_embeddedSoundData) { - if (sound.pDataSource != nullptr) + for (auto &sound : data.sounds) { - ma_sound_uninit(&sound); - } - } + if (sound != nullptr) + { + if (sound->pDataSource != nullptr) + { + ma_sound_uninit(sound.get()); + } - for (ma_decoder &decoder : g_embeddedDecoderCache) - { - if (decoder.pBackend != nullptr) + sound.reset(); + } + } + + for (auto &decoder : data.decoders) { - ma_decoder_uninit(&decoder); + if (decoder != nullptr) + { + if (decoder->pBackend != nullptr) + { + ma_decoder_uninit(decoder.get()); + } + + decoder.reset(); + } } } diff --git a/UnleashedRecompResources b/UnleashedRecompResources index f5352d15..b01a45b4 160000 --- a/UnleashedRecompResources +++ b/UnleashedRecompResources @@ -1 +1 @@ -Subproject commit f5352d15813026a1cdf44f67723f3c07567c9229 +Subproject commit b01a45b4b67128dba8d9e10503543d29465eea47 diff --git a/thirdparty/miniaudio b/thirdparty/miniaudio new file mode 160000 index 00000000..12a8d4e4 --- /dev/null +++ b/thirdparty/miniaudio @@ -0,0 +1 @@ +Subproject commit 12a8d4e4911c5ab4f4c089b4d039433975ed8a66