Prevent app crash when miniaudio samples bigger than capacity

This commit is contained in:
Jonian Guveli 2024-09-07 15:50:24 +03:00
parent f1b7830952
commit be3613d901

View file

@ -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<MiniAudioDevice*>(device->pUserData);
s16* output = reinterpret_cast<ma_int16*>(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) {