From 9f31af87f83e3c2d469f58e6dd8ceaf1f4012471 Mon Sep 17 00:00:00 2001 From: Thomas Rohloff Date: Wed, 6 May 2020 18:40:24 +0200 Subject: [PATCH] Fix stereo downmixing. Without this fix that happens: ======================== ASAN: out-of-bounds load Source: 0x0D0254D8: homebrew_launcher|_ZN12SoundDecoder6DecodeEv Bad load was on address 0x10CEE778. Guessing possible issues: Guess Chunk ChunkSz Dist ChunkOwner Overflow 0x10CEA760 0x00004000 0x00000018 homebrew_launcher|_ZN12BufferCircle6ResizeEi Underflow 0x10CEE780 0x00004000 0xFFFFBFF8 homebrew_launcher|_ZN12BufferCircle6ResizeEi With this fix playing a mono stream still produces clipping but without the fix it's completely garbled. Signed-off-by: Thomas Rohloff --- source/sounds/SoundDecoder.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/sounds/SoundDecoder.cpp b/source/sounds/SoundDecoder.cpp index 80f4c43..72c5ac7 100644 --- a/source/sounds/SoundDecoder.cpp +++ b/source/sounds/SoundDecoder.cpp @@ -186,12 +186,15 @@ void SoundDecoder::Decode() { } //! TODO: remove this later and add STEREO support with two voices, for now we convert to MONO + done >>= 1; if(IsStereo()) { int16_t* monoBuf = (int16_t*)write_buf; - done = done >> 1; + done >>= 1; for(int32_t i = 0; i < done; i++) monoBuf[i] = monoBuf[i << 1]; + + done <<= 1; } DCFlushRange(write_buf, done);