The DirectSoundBuffer Object

Each sound or audio stream is represented by a DirectSoundBuffer object that your application can access through the IDirectSoundBuffer interface. An application can create a DirectSoundBuffer object by calling the IDirectSound::CreateSoundBuffer method, which returns an IDirectSoundBuffer interface.

Applications can also create primary sound buffers or secondary sound buffers. In the current implementation, each DirectSound object has only one primary buffer.

Your application can write data into sound buffers by locking the buffer, writing data to the buffer, and then unlocking the buffer. A buffer can be locked by calling the IDirectSoundBuffer::Lock method. This method returns a pointer to the locked portion of the buffer. After the buffer is locked, your application can copy audio data to the buffer. After writing data to the buffer, you must unlock the buffer and complete the write operation by calling the IDirectSoundBuffer::Unlock method.

The primary sound buffer contains the data that is heard. You can play audio data from a secondary sound buffer by using the IDirectSoundBuffer::Play method. This method causes DirectSound to begin mixing the secondary buffer into the primary buffer. By default, IDirectSoundBuffer::Play plays the buffer once and stops at the end. You can also play a sound repeatedly in a continuous loop by specifying the DSBPLAY_LOOPING flag when calling this method. The application can stop a buffer that is playing by using the IDirectSoundBuffer::Stop method.

Typically, the duration of a sound determines how your application uses the associated sound buffer. If the sound data is only a few seconds long, you can use a static sound buffer to store the sound. If the sound is longer than that, you should use a streaming sound buffer.

Your application can create a DirectSoundBuffer object that has a static buffer by using the IDirectSound::CreateSoundBuffer method and specifying the DSBCAPS_STATIC flag. (If your application does not specify this flag, a streaming sound buffer is created.) DirectSound attempts to store static buffers by using sound memory located on the sound hardware, if that memory is available. Buffers stored on sound hardware do not consume system processing time when they are played because the mixing is done in the hardware. Reusable sounds, such as engine roars, cheers, and jeers, are perfect candidates for static buffers.

Streaming buffers can also use hardware mixing if it is supported by the sound device; however, this is efficient only when your application runs on computers with fast data buses, such as the peripheral component interconnect (PCI) bus. If the computer does not have a fast bus, the data-transfer overhead outweighs the benefits of hardware mixing. DirectSound locates streaming buffers in hardware only if the sound device is located on a fast bus.