Wav Header File Format
A WAV file contains a header and the raw data. This tutorial should have provided enough information to understand the WAV (RIFF) file format and to create one.
One very important thing to note is that fmt chunks are not always the same length. They can be an instance of WAVEFORMATEX which has extra bytes at the end. Use Subchunk1 size to find out what the real size of the fmt section is. You also need to be aware that the data chunk doesn't necessarily follow the fmt chunk. A WAV file can have more than just a fmt or data chunk, so it is always best to check that chunk2 Id is 'data' and if not, skip over it until you find the data chunk. – Dec 2 '12 at 7:51 •.
I've discovered it is dangerous to assume that all PCM wav audio files have 44 bytes of header data before the samples begin. Though this is common, many applications (ffmpeg for example), will generate wavs with a 46-byte header and ignoring this fact while processing will result in a corrupt and unreadable file.
But how can you detect how long the header actually is? Obviously there is a way to do this, but I searched and found little discussion about this. A LOT of audio projects out there assume 44 (or conversely, 46) depending on the authors own context. In addition to Radiodef's excellent reply, I'd like to add 3 things that aren't obvious. • The only rule for WAV files is the FMT chunk comes before the DATA chunk. Apart from that, you will find chunks you don't know about at the beginning, before the DATA chunk and after it.
You must read the header for each chunk to skip forward to find the next chunk. • The FMT chunk is commonly found in 16 byte and 18 byte variations, but the spec actually allows more than 18 bytes as well. If the FMT chunk' header size field says greater than 16, Bytes 17 and 18 also specify how many extra bytes there are, so if they are both zero, you end up with an 18 byte FMT chunk identical to the 16 byte one. It is safe to read in just the first 16 bytes of the FMT chunk and parse those, ignoring any more.
Garena Messenger Version Module.dll more. Why does this matter? - not much any more, but Windows XP's Media Player was able to play 16 bit WAV files, but 24 bit WAV files only if the FMT chunk was the Extended (18+ byte) version. There used to be a lot of complaints that 'Windows doesn't play my 24 bit WAV files', but if it had an 18 byte FMT chunk, it would. Microsoft fixed that sometime during the early days of Windows 7, so 24 bit with 16 byte FMT files work fine now. • (Newly added) Chunk sizes with odd sizes occur quite often.