From be3613d901fcd84f29ad6e3ccf499e95caecc842 Mon Sep 17 00:00:00 2001 From: Jonian Guveli Date: Sat, 7 Sep 2024 15:50:24 +0300 Subject: [PATCH 1/2] Prevent app crash when miniaudio samples bigger than capacity --- src/core/audio/miniaudio_device.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/audio/miniaudio_device.cpp b/src/core/audio/miniaudio_device.cpp index fa36cb842..4e2adf83e 100644 --- a/src/core/audio/miniaudio_device.cpp +++ b/src/core/audio/miniaudio_device.cpp @@ -90,16 +90,17 @@ void MiniAudioDevice::init(Samples& samples, bool safe) { deviceConfig.dataCallback = [](ma_device* device, void* out, const void* input, ma_uint32 frameCount) { auto self = reinterpret_cast(device->pUserData); s16* output = reinterpret_cast(out); + usize maxSamples = std::min(self->samples->Capacity(), (usize)(frameCount * channelCount)); // Wait until there's enough samples to pop - while (self->samples->size() < frameCount * channelCount) { + while (self->samples->size() < maxSamples) { // If audio output is disabled from the emulator thread, make sure that this callback will return and not hang if (!self->running) { return; } } - self->samples->pop(output, frameCount * channelCount); + self->samples->pop(output, maxSamples); }; if (ma_device_init(&context, &deviceConfig, &device) != MA_SUCCESS) { From 98b2d10ea2b964ed6e72046eb2b35dad86a27fc1 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 7 Sep 2024 16:20:20 +0300 Subject: [PATCH 2/2] Update miniaudio_device.cpp --- src/core/audio/miniaudio_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/audio/miniaudio_device.cpp b/src/core/audio/miniaudio_device.cpp index 4e2adf83e..dd5cfa853 100644 --- a/src/core/audio/miniaudio_device.cpp +++ b/src/core/audio/miniaudio_device.cpp @@ -90,7 +90,7 @@ void MiniAudioDevice::init(Samples& samples, bool safe) { deviceConfig.dataCallback = [](ma_device* device, void* out, const void* input, ma_uint32 frameCount) { auto self = reinterpret_cast(device->pUserData); s16* output = reinterpret_cast(out); - usize maxSamples = std::min(self->samples->Capacity(), (usize)(frameCount * channelCount)); + const usize maxSamples = std::min(self->samples->Capacity(), usize(frameCount * channelCount)); // Wait until there's enough samples to pop while (self->samples->size() < maxSamples) {