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;