Loading WAVE (.wav) files


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).


Chunks Generally

Each chunk begins with two fields:

  • A character id - 4 bytes long
  • The length of the rest of the chunk - 4 bytes long

RIFF Header Chunk

Every WAVE has a header chunk that contains all the other chunks. It is laid out:

  • Character id (4 bytes) - 'RIFF'
  • Length (4 bytes) - will be the file size minus 8
  • Format (4 bytes) - 'WAVE'

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

The format chunk contains details of the data to follow, it has:

  • Character id  (4 bytes) - 'fmt'
  • Length (4 bytes) - this chunk's length (for PCM this is 16)
  • Audio format (2 bytes) - 1 for PCM otherwise indicates that compression has been used.
  • Channels (2 bytes) - 1 for mono 2 for stereo etc.
  • Sample Rate (4 bytes) - the number of samples per second e.g. 22050, 44100
  • Average Byte Rate (4 bytes) - how many bytes per second. Can be used to estimate buffer sizes on play back.
  • Block Align (2 bytes) - number of bytes for one sample. This will be channels * bits per sample / 8.
  • Bits per sample (2 bytes) - how many bits per sample e.g. 8 or 16

If the data is not PCM format then you may have extra data. In this case in addition to the above you have:

  • Size of extra optional data (2 bytes) - this data is optional. If it exists it indicates how many extra bytes of data this chunk has (after this field). Extra parameters can exist for some cases (e.g. for a compressed format) but not for your standard PCM wave.
  • Extra Data (size of extra data bytes) - the extra data specific to the non PCM file format

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

The data chunk holds the actual raw sample data

  • Character id (4 bytes) - 'data'
  • Length (4 bytes) - number of bytes of data
  • Data (Length bytes) - the raw data

You can read in the chunk length and use this to read in all the actual sound data in one go.

Further Reading


© 2004-2016 Keith Ditchburn