From e6ea49d10bb149e5e87581f58d803749a5002894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 14 Oct 2022 12:44:26 +0200 Subject: [PATCH] LibAudio: Fix 24-bit PCM rescaling This code was so totally wrong I can't even explain it. --- Userland/Libraries/LibAudio/WavLoader.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp index 2747615386..f7b89a9636 100644 --- a/Userland/Libraries/LibAudio/WavLoader.cpp +++ b/Userland/Libraries/LibAudio/WavLoader.cpp @@ -72,10 +72,13 @@ static ErrorOr read_sample_int24(Core::Stream::Stream& stream) i32 sample3 = byte; i32 value = 0; - value = sample1 << 8; - value |= sample2 << 16; - value |= sample3 << 24; - return static_cast(value) / static_cast((1 << 24) - 1); + value = sample1; + value |= sample2 << 8; + value |= sample3 << 16; + // Sign extend the value, as it can currently not have the correct sign. + value = (value << 8) >> 8; + // Range of value is now -2^23 to 2^23-1 and we can rescale normally. + return static_cast(value) / static_cast((1 << 23) - 1); } template