Microsoft DirectX 9.0 |
This topic provides a brief overview of the steps you must take to play a sound by using the DirectSound API. For details, see Using DirectSound.
Playing a short sound requires the following steps:
1. Create a device object
Call DirectSoundCreate8 to create an object that supports the IDirectSound8 interface. This object usually represents the default playback device. Optionally, you can first enumerate available devices to obtain the GUID of the device you want to use, and pass this to DirectSoundCreate8.
Note DirectSound is based on the Component Object Model (COM). However, you do not have to initialize COM explicitly unless you are using effect
2. Create a secondary buffer
Use IDirectSound8::CreateSoundBuffer to create a buffer object that will contain sound data. Such buffers are called secondary buffers, to distinguish them from the primary buffer, which is the object that mixes all sounds.
3. Obtain PCM data
Read data from a WAV file or resource into a private buffer. The DirectX SDK contains sample code that you can use to do this.
4. Put data in the buffer
Prepare the secondary buffer for a write operation by calling IDirectSoundBuffer8::Lock. In most cases, this method returns a single memory address. Copy data from your private buffer to that address, and then call IDirectSoundBuffer8::Unlock.
5. Play the buffer
Play the sound by calling IDirectSoundBuffer8::Play. You can instruct the buffer either to stop when it reaches the end, or to continue playing over and over again until you call IDirectSoundBuffer8::Stop. You can start and stop the buffer repeatedly; however, only one instance of the sound can play at one time, unless you create other buffers containing the same data. Any number of buffers can be played at a given time, and mixing is done automatically.
The preceding steps apply to the simplest possible scenario, that of a short sound that fits into a buffer of a reasonable size. Typically, sounds that last more than a few seconds are streamed: that is, data that has already been played is periodically replaced by new data.
Sample code distributed with the SDK shows how to perform many common tasks using DirectSound. For more information, see DirectSound C++ Samples.