Reputation: 9035
I'm trying to write software that reads a WAV file and I want to validate whether the size provided in the "RIFF" header actually matches the size of the file. Currently my software will loop through each chunk and stop once it reads the "data" chunk.
In every file I have encountered, the "data" chunk is the last chunk in the file, but I haven't found any specification that explicitly states it MUST be the last chunk in the file.
Is it technically valid for a WAV file to have additional chunks after the "data" chunk? Or can I safely assume that any file that does this is not conforming to the standard format?
Upvotes: 2
Views: 1210
Reputation: 2359
It is technically valid for a WAV file to have additional chunks after the data
chunk. And you will find WAV files like this out in the wild.
The only chunk ordering requirement within the "WAVE" form is that the fmt
chunk must occur before the data
chunk. From p56 of the 1.0 Microsoft/IBM spec:
The WAVE form is defined as follows. Programs must expect (and ignore) any unknown chunks encountered, as with all RIFF forms. However,
fmt-ck
must always occur beforewave-data
, and both of these chunks are mandatory in a WAVE file.
Note also the "expect (and ignore) any unknown chunks" requirement. If you're writing code to read in WAV files that are generated by all kinds of other software, (e.g. Audacity, Reaper, ProTools, etc.) then you don't want to fail reading the WAV when it has some extra chunks like list
and cue
.
For compatibility, the best way to write your WAV decoder is to read chunk headers after the RIFF/WAVE header and use that header info to either skip over a chunk or process it:
read RIFF/WAVE header
while read position is not at EOF
read next 4 bytes (chunk ID)
read next 4 bytes (chunk data size)
if chunk ID matches 'fmt ', 'data' or an ID your app cares about...
read the chunk data and parse what you need
else
advance read position by chunk data size
If you are encoding a WAVE file, it's an unfortunate defacto standard to first write header
+ fmt
+ data
and then follow the data
chunk with any extra chunks beyond that. This is to accommodate all the people in the World that wrote off-spec readers by assuming all WAV files would only ever follow the header
+ fmt
+ data
chunk sequence. (angry nerd fist shaking)
Upvotes: 2
Reputation: 6151
RIFF files may contain several types of information. WAV format is a private case of RIFF. According to this link there may be some other chunks in a WAV file, like FACT chunk and Cue-Points chunk, altough - " must always occur before , and both of these chunks are mandatory in a WAVE file."
Upvotes: 0