From 0c002cd0147f3c7105550b3eb62293428d4be935 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 10 Jan 2018 08:04:39 -0800 Subject: [PATCH] Obey data size field, avoid WAV clicks w/metadata The DATA chunk has a size field that was ignored. When there was additional metadata after the chunk, however, it would try and play it leading to clicks. Store and only read that amount of data now. --- src/AudioGeneratorWAV.cpp | 7 +++++-- src/AudioGeneratorWAV.h | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/AudioGeneratorWAV.cpp b/src/AudioGeneratorWAV.cpp index 1dcf4a6..aff91da 100644 --- a/src/AudioGeneratorWAV.cpp +++ b/src/AudioGeneratorWAV.cpp @@ -62,7 +62,9 @@ bool AudioGeneratorWAV::GetBufferedData(int bytes, void *dest) // Potentially load next batch of data... if (buffPtr >= buffLen) { buffPtr = 0; - buffLen = file->read( buff, buffSize ); + uint32_t toRead = availBytes > buffSize ? buffSize : availBytes; + buffLen = file->read( buff, toRead ); + availBytes -= buffLen; } if (buffPtr >= buffLen) return false; // No data left! @@ -159,7 +161,8 @@ bool AudioGeneratorWAV::ReadWAVInfo() // Skip size, read until end of file... if (!ReadU32(&u32)) return false; - + availBytes = u32; + // Now set up the buffer or fail buff = reinterpret_cast(malloc(buffSize)); if (!buff) return false; diff --git a/src/AudioGeneratorWAV.h b/src/AudioGeneratorWAV.h index 9fe62d0..185454f 100644 --- a/src/AudioGeneratorWAV.h +++ b/src/AudioGeneratorWAV.h @@ -48,6 +48,8 @@ class AudioGeneratorWAV : public AudioGenerator uint32_t sampleRate; uint16_t bitsPerSample; + uint32_t availBytes; + // We need to buffer some data in-RAM to avoid doing 1000s of small reads uint32_t buffSize; uint8_t *buff;