mirror of
https://github.com/hedge-dev/UnleashedRecomp.git
synced 2026-04-27 04:41:39 +00:00
Rework embedded sound player to support simultaneous playback.
This commit is contained in:
parent
e714c69f1d
commit
0f17ad23f9
4 changed files with 118 additions and 59 deletions
|
|
@ -1,11 +1,24 @@
|
||||||
#include <stdafx.h>
|
#include <stdafx.h>
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "cpu/code_cache.h"
|
#include "cpu/code_cache.h"
|
||||||
|
|
||||||
#define AUDIO_DRIVER_KEY (uint32_t)('DAUD')
|
#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)
|
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;
|
*driver = AUDIO_DRIVER_KEY;
|
||||||
XAudioRegisterClient(KeFindHostFunction(*callback), callback[1]);
|
XAudioRegisterClient(KeFindHostFunction(*callback), callback[1]);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -18,6 +31,19 @@ uint32_t XAudioUnregisterRenderDriverClient(DWORD driver)
|
||||||
|
|
||||||
uint32_t XAudioSubmitRenderDriverFrame(uint32_t driver, void* samples)
|
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);
|
XAudioSubmitFrame(samples);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,58 +13,87 @@ enum EmbeddedSound
|
||||||
EmbeddedSoundCount,
|
EmbeddedSoundCount,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct EmbeddedSoundData
|
||||||
|
{
|
||||||
|
static const int SimultaneousLimit = 4;
|
||||||
|
std::array<std::unique_ptr<ma_sound>, SimultaneousLimit> sounds;
|
||||||
|
std::array<std::unique_ptr<ma_decoder>, SimultaneousLimit> decoders;
|
||||||
|
int oldestIndex = 0;
|
||||||
|
};
|
||||||
|
|
||||||
static ma_engine g_audioEngine = {};
|
static ma_engine g_audioEngine = {};
|
||||||
static bool g_audioEngineInitialized = false;
|
static bool g_audioEngineInitialized = false;
|
||||||
static std::array<ma_sound, EmbeddedSoundCount> g_embeddedSoundCache = {};
|
static std::array<EmbeddedSoundData, EmbeddedSoundCount> g_embeddedSoundData = {};
|
||||||
static std::array<ma_decoder, EmbeddedSoundCount> g_embeddedDecoderCache = {};
|
|
||||||
static const std::unordered_map<std::string, EmbeddedSound> g_embeddedSoundMap =
|
static const std::unordered_map<std::string, EmbeddedSound> g_embeddedSoundMap =
|
||||||
{
|
{
|
||||||
{ "sys_worldmap_cursor", EmbeddedSoundSysWorldMapCursor },
|
{ "sys_worldmap_cursor", EmbeddedSoundSysWorldMapCursor },
|
||||||
{ "sys_worldmap_finaldecide", EmbeddedSoundSysWorldMapFinalDecide },
|
{ "sys_worldmap_finaldecide", EmbeddedSoundSysWorldMapFinalDecide },
|
||||||
};
|
};
|
||||||
|
|
||||||
void InitEmbeddedSound(EmbeddedSound s)
|
static void PlayEmbeddedSound(EmbeddedSound s)
|
||||||
{
|
{
|
||||||
ma_sound &sound = g_embeddedSoundCache[s];
|
EmbeddedSoundData &data = g_embeddedSoundData[s];
|
||||||
ma_decoder &decoder = g_embeddedDecoderCache[s];
|
int pickedIndex = -1;
|
||||||
const void *soundData = nullptr;
|
for (int i = 0; (i < EmbeddedSoundData::SimultaneousLimit) && (pickedIndex < 0); i++)
|
||||||
size_t soundDataSize = 0;
|
|
||||||
float volume = Config::EffectsVolume;
|
|
||||||
switch (s)
|
|
||||||
{
|
{
|
||||||
case EmbeddedSoundSysWorldMapCursor:
|
if (data.sounds[i] == nullptr)
|
||||||
soundData = g_sys_worldmap_cursor;
|
{
|
||||||
soundDataSize = sizeof(g_sys_worldmap_cursor);
|
// The sound hasn't been created yet, create it and pick it.
|
||||||
volume *= 0.259f;
|
const void *soundData = nullptr;
|
||||||
break;
|
size_t soundDataSize = 0;
|
||||||
case EmbeddedSoundSysWorldMapFinalDecide:
|
switch (s)
|
||||||
soundData = g_sys_worldmap_finaldecide;
|
{
|
||||||
soundDataSize = sizeof(g_sys_worldmap_finaldecide);
|
case EmbeddedSoundSysWorldMapCursor:
|
||||||
volume *= 0.61f;
|
soundData = g_sys_worldmap_cursor;
|
||||||
break;
|
soundDataSize = sizeof(g_sys_worldmap_cursor);
|
||||||
default:
|
break;
|
||||||
assert(false && "Unknown embedded sound.");
|
case EmbeddedSoundSysWorldMapFinalDecide:
|
||||||
return;
|
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<ma_decoder>();
|
||||||
|
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<ma_sound>();
|
||||||
|
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;
|
// No free slots are available, pick the oldest one.
|
||||||
res = ma_decoder_init_memory(soundData, soundDataSize, nullptr, &decoder);
|
pickedIndex = data.oldestIndex;
|
||||||
if (res != MA_SUCCESS)
|
data.oldestIndex = (data.oldestIndex + 1) % EmbeddedSoundData::SimultaneousLimit;
|
||||||
{
|
}
|
||||||
fprintf(stderr, "ma_decoder_init_memory failed with error code %d on embedded sound %d.\n", res, s);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = ma_sound_init_from_data_source(&g_audioEngine, &decoder, MA_SOUND_FLAG_DECODE, nullptr, &sound);
|
if (data.sounds[pickedIndex] != nullptr)
|
||||||
if (res != MA_SUCCESS)
|
{
|
||||||
{
|
ma_sound_set_volume(data.sounds[pickedIndex].get(), Config::EffectsVolume);
|
||||||
fprintf(stderr, "ma_sound_init_from_data_source failed with error code %d on embedded sound %d.\n", res, s);
|
ma_sound_seek_to_pcm_frame(data.sounds[pickedIndex].get(), 0);
|
||||||
return;
|
ma_sound_start(data.sounds[pickedIndex].get());
|
||||||
}
|
|
||||||
|
|
||||||
ma_sound_set_volume(&sound, volume);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,34 +130,37 @@ void EmbeddedPlayer::Play(const char *name)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ma_sound &sound = g_embeddedSoundCache[it->second];
|
PlayEmbeddedSound(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EmbeddedPlayer::Shutdown()
|
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)
|
sound.reset();
|
||||||
{
|
}
|
||||||
if (decoder.pBackend != nullptr)
|
}
|
||||||
|
|
||||||
|
for (auto &decoder : data.decoders)
|
||||||
{
|
{
|
||||||
ma_decoder_uninit(&decoder);
|
if (decoder != nullptr)
|
||||||
|
{
|
||||||
|
if (decoder->pBackend != nullptr)
|
||||||
|
{
|
||||||
|
ma_decoder_uninit(decoder.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit f5352d15813026a1cdf44f67723f3c07567c9229
|
Subproject commit b01a45b4b67128dba8d9e10503543d29465eea47
|
||||||
1
thirdparty/miniaudio
vendored
Submodule
1
thirdparty/miniaudio
vendored
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 12a8d4e4911c5ab4f4c089b4d039433975ed8a66
|
||||||
Loading…
Add table
Reference in a new issue