October 1998

RIFF update from Part 1

by Kent Reisdorph

Part 1 of this article series contained some misleading information. That article said, in part:

Notice the line that writes the wave format header. It looks like this:


mmioWrite(handle, (char*)&waveFmt, 
  sizeof(WAVEFORMATEX) - 2);

Notice that we subtract 2 from the size of a WAVEFORMATEX structure when we write the structure. If we don't do this then the wave file we created won't be able to be played by the Sound Recorder program that comes with Windows95.

That statement isn't correct. Wave-file corruption can occur, but the corruption doesn't stem from writing the incorrect number of bytes for the wave header--rather, it comes from not properly ascending out of the individual chunks of a RIFF file before closing the file. When you ascend out of a RIFF file chunk, Windows will automatically update the chunk info with the size of the data in the chunk. If you neglect to ascend out of the root chunk, the root chunk data size isn't updated with the total size of the file. When Windows95 encounters a file with a RIFF chunk data size of 0, it reports the file as corrupted (even though the wave data itself is intact). The SaveWaveFile() function in Listing B of the accompanying article shows the correct way to ascend out of the root chunk before closing the wave file.