From 70defdb6c6ab0c66012537cc78dfda3a19f8b291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= <55314805+AlexanderChad@users.noreply.github.com> Date: Sat, 27 Dec 2025 09:00:46 +0700 Subject: [PATCH] Fix u8 upsampling and mono mode for AudioGeneratorWAV (#802) For AudioGeneratorWAV, the upsample from u8 to i16 bits has been fixed. Previously, playback of wav files with u8 encoding resulted in silence. The data was read, but not used in calculations. Additionally, in mono mode, I removed the unnecessary resolution increase for the second channel and implemented simple duplication. Now, when playing mono wav files, the audio will be in both the left and right channels. --- src/AudioGeneratorWAV.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/AudioGeneratorWAV.cpp b/src/AudioGeneratorWAV.cpp index b85bb9c..e0fe15d 100644 --- a/src/AudioGeneratorWAV.cpp +++ b/src/AudioGeneratorWAV.cpp @@ -87,22 +87,21 @@ bool AudioGeneratorWAV::loop() { // Try and stuff the buffer one sample at a time do { if (bitsPerSample == 8) { - uint8_t l, r; - if (!GetBufferedData(1, &l)) { + uint8_t u8s; // For read u8 sample + if (!GetBufferedData(1, &u8s)) { stop(); } + // Upsample from unsigned 8 bits to signed 16 bits + lastSample[AudioOutput::LEFTCHANNEL] = ((int16_t)u8s - 128) << 8; if (channels == 2) { - if (!GetBufferedData(1, &r)) { + if (!GetBufferedData(1, &u8s)) { stop(); } + // Upsample from unsigned 8 bits to signed 16 bits + lastSample[AudioOutput::RIGHTCHANNEL] = ((int16_t)u8s - 128) << 8; } else { - r = 0; + lastSample[AudioOutput::RIGHTCHANNEL] = lastSample[AudioOutput::LEFTCHANNEL]; } - - // Upsample from unsigned 8 bits to signed 16 bits - lastSample[AudioOutput::LEFTCHANNEL] = (((int16_t)(lastSample[AudioOutput::LEFTCHANNEL] & 0xff)) - 128) << 8; - lastSample[AudioOutput::RIGHTCHANNEL] = (((int16_t)(lastSample[AudioOutput::RIGHTCHANNEL] & 0xff)) - 128) << 8; - } else if (bitsPerSample == 16) { if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) { stop(); @@ -112,7 +111,7 @@ bool AudioGeneratorWAV::loop() { stop(); } } else { - lastSample[AudioOutput::RIGHTCHANNEL] = 0; + lastSample[AudioOutput::RIGHTCHANNEL] = lastSample[AudioOutput::LEFTCHANNEL]; } } } while (running && output->ConsumeSample(lastSample));