Microsoft DirectX 8.1 (Visual Basic)

Filling and Playing Static Buffers

A secondary buffer that contains an entire self-contained sound is called a static buffer. Although it is possible to reuse the same buffer for different sounds, typically data is written to a static buffer only once.

Static buffers are created and managed just like streaming buffers. The only difference is in the way they are used: static buffers are filled once and then played, but streaming buffers are constantly refreshed with data as they are playing.

Note   A static buffer is not necessarily one created by setting the DSBCAPS_STATIC flag in the buffer description. This flag requests allocation of memory on the sound card, which not available on most modern hardware. A static buffer can exist in system memory and can be created with either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flag. For more information, see Voice Management on ISA and PCI Cards.

A static buffer can be created and filled with data by using a single method call, either DirectSound8.CreateSoundBufferFromFile or DirectSound8.CreateSoundBufferFromResource.

The following sample code creates a static buffer from the wave identified as "bounce" in the resource file. Assume that m_ds is a DirectSound8 object.

Dim dsbd As DSBUFFERDESC
Dim dsbBounce As DirectSoundSecondaryBuffer8
 
dsbd.lFlags = DSBCAPS_CTRLVOLUME
Set dsbBounce = m_ds.CreateSoundBufferFromResource( _
    vbNullString, "bounce", dsbd)

The resource file has been compiled into the executable file, so no module name needs to be provided. Nor does the value of the lBufferBytes member of the DSBUFFERDESC type need to be set, because the method determines the size of the buffer from the size of the data. The WAVEFORMATEX type receives information about the wave format from the header stored in the resource.

To play the buffer, call DirectSoundSecondaryBuffer8.Play, as in the following example:

dsbResource.SetCurrentPosition 0
dsbResource.Play 0

Because the DSBPLAY_LOOPING flag is not set in the example, the buffer automatically stops when it reaches the end. You can also stop it prematurely by using DirectSoundSecondaryBuffer8.Stop. When you stop a buffer prematurely, the play cursor remains where it is. Hence the call to DirectSoundSecondaryBuffer8.SetCurrentPosition in the example, which ensures that the buffer starts from the beginning.

You can also create an empty static buffer by using DirectSound8.CreateSoundBuffer and then fill it with data from another location in memory by using the DirectSoundSecondaryBuffer8.WriteBuffer method.

More information on writing to secondary buffers is given in the next topic, Using Streaming Buffers.