Skip to content

Commit

Permalink
WAV reader: tolerate odd-length "data" chunk
Browse files Browse the repository at this point in the history
RIFF chunks are supposed to be padded to even length, but some WAV files feature an odd-length "data" chunk. These changes make the WAV reader code tolerant to this.
  • Loading branch information
getdunne committed Nov 7, 2023
1 parent d62fdf6 commit a1265e1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions modules/juce_audio_formats/codecs/juce_WavAudioFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,8 @@ class WavAudioFormatReader : public AudioFormatReader
int cueNoteIndex = 0;
int cueLabelIndex = 0;
int cueRegionIndex = 0;
// SD move chunkType declaration here, add new state variable prevChunkType
int chunkType = 0, prevChunkType = 0;

StringMap dict;

Expand Down Expand Up @@ -1255,9 +1257,14 @@ class WavAudioFormatReader : public AudioFormatReader

while ((uint64) input->getPosition() < end && ! input->isExhausted())
{
auto chunkType = input->readInt();
// SD keep track of previous chunk type for later comparison
prevChunkType = chunkType;
chunkType = input->readInt();
auto length = (uint32) input->readInt();
auto chunkEnd = input->getPosition() + length + (length & 1);
// SD after any chunk except "data", advance by chunk length rounded up to next multiple of 2
// (some WAV files don't round up length of the data chunk)
auto chunkEnd = input->getPosition() + length;
if (chunkType != chunkName("data")) chunkEnd += (length & 1);

if (chunkType == chunkName ("fmt "))
{
Expand Down Expand Up @@ -1458,6 +1465,11 @@ class WavAudioFormatReader : public AudioFormatReader
input->readIntoMemoryBlock (tracktion, (ssize_t) length);
dict[WavAudioFormat::tracktionLoopInfo] = tracktion.toString();
}
// SD If unknown chunk type seen after "data", we might have had to advance by 1 byte after all
else if (prevChunkType == chunkName("data"))
{
chunkEnd = input->getPosition() + 1;
}
else if (chunkEnd <= input->getPosition())
{
break;
Expand Down

0 comments on commit a1265e1

Please sign in to comment.