Implementation: A Broad Overview

This section describes the programming model for DirectSound and provides some guidelines for typical tasks.

Your application should follow these basic steps to implement DirectSound:

1Create a DirectSound object by calling the DirectSoundCreate function.

2Specify a cooperative level by calling the IDirectSound::SetCooperativeLevel method. Most applications use the lowest level, DSSCL_NORMAL.

3Create secondary buffers using the IDirectSound::CreateSoundBuffer method. Your application need not specify that they are secondary buffers in the DSBUFFERDESC structure; creating secondary buffers is the default.

4Load the secondary buffers with data. Use the IDirectSoundBuffer::Lock method to obtain a pointer to the data area and the IDirectSoundBuffer::Unlock method to set the data to the device.

5Use the IDirectSoundBuffer::Play method to play the secondary buffers.

6Stop all buffers when your application has finished playing sounds by using the IDirectSoundBuffer::Stop method of the DirectSoundBuffer object.

7Release the secondary buffers.

8Release the DirectSound object.

Your application can also perform the following optional operations:

·Set the output format of the primary buffer by creating a primary sound buffer and calling the IDirectSoundBuffer::SetFormat method. This operation requires your application to set the cooperative level to DSSCL_PRIORITY before setting the output format of the primary buffer.

·Create a primary sound buffer and play the buffer using the IDirectSoundBuffer::Play method. This guarantees that the primary buffer is always playing, even if no secondary buffers are playing. This action consumes some of the processing bandwidth, but it reduces startup time when the first secondary buffer is played.