Microsoft DirectX 8.1 (Visual Basic) |
To create a secondary sound buffer, call the DirectSound8.CreateSoundBuffer, DirectSound8.CreateSoundBufferFromFile, or DirectSound8.CreateSoundBufferFromResource method. These methods create a DirectSoundSecondaryBuffer8 object, which can then be used to manipulate and play the buffer.
CreateSoundBufferFromFile and CreateSoundBufferFromResource are good methods to use when the sound is not very long. DirectSound creates a buffer just big enough to hold the entire sound, matches its wave format to the format of the data, and fills it with the data. CreateSoundBuffer is suitable for
Always specify the DSBCAPS_GETCURRENTPOSITION2 flag in the DSBUFFERDESC type when creating a streaming buffer.
The following example shows how to create a streaming buffer able to hold 2 seconds of data. Assume that the m_ds variable has been set to a DirectSound8 object.
Dim m_dsb As DirectSoundSecondaryBuffer8
Public Sub CreateSoundBuffer()
Dim buffdesc As DSBUFFERDESC
' Describe the sound buffer to be created.
With buffdesc
lFlags = DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME _
Or DSBCAPS_CTRLFREQUENCY
With .fxFormat
.nFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.lSamplesPerSec = 22050
.nBitsPerSample = 16
.nBlockAlign = (.nChannels * .nBitsPerSample) / 8
.lAvgBytesPerSec = .lSamplesPerSec * .nBlockAlign
.nSize = 0 ' Ignored for WAVE_FORMAT_PCM
End With
lBufferBytes = .fxFormat.lAvgBytesPerSec * 2
End With
Set m_dsb = ds.CreateSoundBuffer(buffdesc)
End Sub
Creating a buffer from a file or resource is much simpler, as the following example shows:
Dim m_dsb As DirectSoundSecondaryBuffer8
Dim buffdesc As DSBUFFERDESC
buffdesc.lFlags = DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME _
Or DSBCAPS_CTRLFREQUENCY
Set dsbuffer = ds.CreateSoundBufferFromFile( _
"c:\media\ding.wav", dsbdesc)
In this case, the format of the buffer is based on that of the data, and the method places this information in dsbdesc.fxFormat.
DirectSound allocates hardware resources to the first buffer that can take advantage of them. Because hardware buffers are mixed by the sound card processor, they have much less impact on application performance.
If you wish to specify the location of a buffer rather than letting DirectSound decide where it belongs, set either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flag in the DSBUFFERDESC type. If the DSBCAPS_LOCHARDWARE flag is set and there are insufficient hardware resources, the buffer creation request fails.
To take advantage of the voice management features of DirectSound, specify the DSBCAPS_LOCDEFER flag when creating the buffer. This flag defers the allocation of resources for the buffer until it is played. For more information, see Dynamic Voice Management.
You can ascertain the location of an existing buffer by using the DirectSoundSecondaryBuffer8.GetCaps method and checking the lFlags member of the DSBCAPS type for either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flags. One or the other is always specified.
Setting the DSBCAPS_STATIC flag lets DirectSound know that the buffer should be created in on-board hardware memory if possible. The buffer creation method does not fail if a hardware buffer is not available. This flag has no effect on most modern sound cards, which use system memory for their buffers. Hardware static buffers should be used only for short sounds that are to be played repeatedly. For more information, see Voice Management on ISA and PCI Cards.