DirectX SDK

Step 2: Load a Wave File into a Buffer

[C++]

This section pertains only to DirectX for Visual Basic. See DirectSound C/C++ Tutorials.

[Visual Basic]

After creating the main DirectSound object, the next step is to create and load a wave file into a sound buffer. You describe the characteristics of a DirectSound buffer through a DSBUFFERDESC type.

The first member of a DSBUFFERDESC type is lBufferBytes which is the size of the buffer. Since we are creating a primary sound buffer, this value must be 0. The second member is one or more constants of the CONST_DSBCAPSFLAGS enumeration identifying the capabilities of the buffer.

In the Tutorial 1 sample we create a buffer which has volume changes, frequency changes, and pan changes enabled. Additionally, the DDSBCAPS_STATIC flag indicates that we want the entire file loaded into memory.

Dim bufferDesc As DSBUFFERDESC

bufferDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC

The next step in creating a sound buffer is to specify the format of the wave-form audio data. This is done by setting the members of a WAVEFORMATEX type.

Dim waveFormat As WAVEFORMATEX
 
waveFormat.nFormatTag = WAVE_FORMAT_PCM
waveFormat.nChannels = 2
waveFormat.lSamplesPerSec = 22050
waveFormat.nBitsPerSample = 16
waveFormat.nBlockAlign = waveFormat.nBitsPerSample / 8 * waveFormat.nChannels
waveFormat.lAvgBytesPerSec = waveFormat.lSamplesPerSec * waveFormat.nBlockAlign
 

The final step is to create the buffer with the preceding descriptions and load a wave file into the buffer. This is done with a single call using the DirectSound.CreateSoundBufferFromFile method.

Set m_dsBuffer(i) = m_ds.CreateSoundBufferFromFile(sfile, bufferDesc, waveFormat)

Next: Step 3: Play the Sound