From e0c1d4511b677ed3297d1d6d39cd1b70389a2ee7 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Thu, 22 Feb 2024 03:37:12 +0200 Subject: [PATCH] Semi-proper audio output --- src/core/audio/miniaudio_device.cpp | 9 ++++++--- src/core/audio/teakra_core.cpp | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/audio/miniaudio_device.cpp b/src/core/audio/miniaudio_device.cpp index 8288b6e7..4753fe4e 100644 --- a/src/core/audio/miniaudio_device.cpp +++ b/src/core/audio/miniaudio_device.cpp @@ -2,6 +2,8 @@ #include "helpers.hpp" +static constexpr uint channelCount = 2; + MiniAudioDevice::MiniAudioDevice() : initialized(false), running(false), samples(nullptr) {} void MiniAudioDevice::init(Samples& samples, bool safe) { @@ -79,10 +81,10 @@ void MiniAudioDevice::init(Samples& samples, bool safe) { deviceConfig = ma_device_config_init(ma_device_type_playback); // The 3DS outputs s16 stereo audio @ 32768 Hz deviceConfig.playback.format = ma_format_s16; - deviceConfig.playback.channels = 2; + deviceConfig.playback.channels = channelCount; deviceConfig.sampleRate = 32768; //deviceConfig.periodSizeInFrames = 64; - //deviceConfig.periods = 2; + //deviceConfig.periods = 16; deviceConfig.pUserData = this; deviceConfig.aaudio.usage = ma_aaudio_usage_game; deviceConfig.wasapi.noAutoConvertSRC = true; @@ -91,7 +93,8 @@ void MiniAudioDevice::init(Samples& samples, bool safe) { auto self = reinterpret_cast(device->pUserData); s16* output = reinterpret_cast(out); - self->samples->pop(output, frameCount); + while (self->samples->size() < frameCount * channelCount) {} + self->samples->pop(output, frameCount * 2); }; if (ma_device_init(&context, &deviceConfig, &device) != MA_SUCCESS) { diff --git a/src/core/audio/teakra_core.cpp b/src/core/audio/teakra_core.cpp index 4b8750d8..432e05a0 100644 --- a/src/core/audio/teakra_core.cpp +++ b/src/core/audio/teakra_core.cpp @@ -55,7 +55,11 @@ TeakraDSP::TeakraDSP(Memory& mem, Scheduler& scheduler, DSPService& dspService) ahbm.write32 = [&](u32 addr, u32 value) { *(u32*)&mem.getFCRAM()[addr - PhysicalAddrs::FCRAM] = value; }; teakra.SetAHBMCallback(ahbm); - teakra.SetAudioCallback([=](std::array sample) { sampleBuffer.push(sample.data(), 2); }); + teakra.SetAudioCallback([=](std::array sample) { + while (sampleBuffer.size() + 2 > sampleBuffer.Capacity()) {} + + sampleBuffer.push(sample.data(), 2); + }); // Set up event handlers. These handlers forward a hardware interrupt to the DSP service, which is responsible // For triggering the appropriate DSP kernel events