Microsoft DirectX 9.0

Reading WAV Data

WAV files are in the Resource Interchange File Format (RIFF), which consists of a variable number of named chunks containing either header information (for example, the format of sound samples) or data (the samples themselves). The Win32 API supplies functions for opening and closing RIFF files, seeking to chunks, and so on. The names of these functions all start with "mmio".

To store WAV sounds in an executable, import your WAV files as resources and give them string names. Note that the CWaveFile Sample Class expects these resources to be of type "WAVE" or "WAV", and expects to find them in the executable module rather than a DLL.

The DirectSound API does not include methods for loading sound data. However, the Dsutil.cpp file used by many of the SDK sample applications implements several classes that can be used to create sound buffers from files, resources, or a memory location.

The basic steps involved in using the sample classes to initialize DirectSound and to create and load buffers are as follows:

  1. Create an object of the CSoundManager Sample Class.
  2. Call CSoundManager::Initialize to create the device object.
  3. Pass the name of a file or resource to CSoundManager::Create or a memory location to CSoundManager::CreateFromMemory. These methods return an object of the CSound Sample Class representing one or more static buffers just large enough to contain the data. (You can create multiple buffers to play several instances of the sound at the same time.) Alternatively, pass the name of a file or resource to CSoundManager::CreateStreaming. This method returns an object of the CStreamingSound Sample Class representing a single streaming buffer .
  4. Call the FillBufferWithSound method on the object retrieved in the preceding step. This reads the data from the file, resource, or memory location into the buffer. For a streaming buffer, it fills the buffer only with as much data as it can hold; to refresh the data as the buffer plays, use CStreamingSound::HandleWaveStreamNotification.

Note that the actual reading of data is done by a CWaveFile object that is a protected member of the CSound or CStreamingSound object. You do not normally need to use the CWaveFile class directly; however, you can refer to the implementation of this class for information on how to parse WAV data.

See Also