The WAVE (.wav file extension) format is used for storing sound data. It is a Microsoft format that is based on the Electronic Arts Interchange File Format that allows data to be stored in chunks. There are three required chunks in a WAVE file the RIFF header chunk, the format chunk and the data chunk. Note that the RIFF header chunk is the parent chunk with the other chunks as sub chunks of it. Other chunks can optionally be included e.g. cue points, application specific data, instrument. play lists, labels, samples etc. I will only cover the required chunks here (see Further Reading for info on the other chunks).
Each chunk begins with two fields:
Every WAVE has a header chunk that contains all the other chunks. It is laid out:
Make sure the file you are loading has RIFF and WAVE codes before continuing to load.
Note: the RIFF header chunk always comes first but it is not a requirement that the format chunk, data chunk and other chunks should follow in any particular order. Some software assumes that the format chunk will always come before the data chunk but while this is truy in the majority of cases it is not true in all cases.
The format chunk contains details of the data to follow, it has:
If the data is not PCM format then you may have extra data. In this case in addition to the above you have:
To load this chunk you could create a structure with the right sized members and simply load straight into the structure. If you are developing for Windows this structure already exists and is called PCMWAVEFORMAT (found in mmsystem.h). For non PCM wave files there is also a WAVEFORMATEX structure that includes the extra data size field.
The data chunk holds the actual raw sample data
You can read in the chunk length and use this to read in all the actual sound data in one go.