From 0b9bcc7e08335cf072d70291724ea8788c738121 Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Mon, 16 Mar 2026 14:27:27 +0100 Subject: [PATCH] Only mark channel as finished when resample buffer is empty Does not fix the audio corruption but necessary either way --- src/dusk/audio/DuskDsp.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dusk/audio/DuskDsp.cpp b/src/dusk/audio/DuskDsp.cpp index 25bc822e9f..947b414bc3 100644 --- a/src/dusk/audio/DuskDsp.cpp +++ b/src/dusk/audio/DuskDsp.cpp @@ -125,6 +125,12 @@ static void SDLCALL ReadChannelSamples( auto& channel = JASDsp::CH_BUF[index]; auto& aux = ChannelAux[index]; + if (channel.mSamplesLeft == 0 && !channel.mLoopFlag) { + // May get called when we're out of data to read. + // This is expected, as we need to drain the resampler channel before we mark the channel as finished. + return; + } + additional_amount = ALIGN_NEXT(additional_amount, channel.mSamplesPerBlock); int requestedSize = static_cast(sizeof(s16) * additional_amount); @@ -154,8 +160,6 @@ static void SDLCALL ReadChannelSamples( if (channel.mSamplesLeft == 0) { // Reached end of buffer. if (!channel.mLoopFlag) { - // Finish. - channel.mIsFinished = true; return; } @@ -190,10 +194,15 @@ static void RenderChannel( ResetChannel(channel, channelAux); } - SDL_GetAudioStreamData( + int wantRead = static_cast(subframe.size() * sizeof(s16)); + auto read = SDL_GetAudioStreamData( channelAux.resampleStream, subframe.data(), - static_cast(subframe.size() * sizeof(s16))); + wantRead); + + if (read < wantRead) { + channel.mIsFinished = true; + } for (auto& sample : subframe) { u16 volume;