DirectX SDK |
This section pertains only to DirectX for Visual Basic. See DirectSound C/C++ Tutorials.
After creating the main DirectSound object, the next step is to create and load a wave file into a 3-D enabled sound buffer. You describe the characteristics of a DirectSound buffer through a parameter of 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.
The first step is to create a primary buffer that is capable of processing 3-D sounds. This is done with the describing the wave format and specifying the DSBCAPS_CTRL3D and DSBCAPS_PRIMARYBUFFER flags:
Dim primDesc As DSBUFFERDESC, format As WAVEFORMATEX format.nSize = LenB(WAVEFORMATEX) format.nFormatTag = WAVE_FORMAT_PCM format.nChannels = 2 format.lSamplesPerSec = 22050 format.nBitsPerSample = 16 format.nBlockAlign = format.nBitsPerSample / 8 * format.nChannels format.lAvgBytesPerSec = format.lSamplesPerSec * format.nBlockAlign primDesc.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_PRIMARYBUFFER Set m_dsPrimaryBuffer = m_ds.CreateSoundBuffer(primDesc, format)
Next we use one of the methods of the DirectSoundBuffer object, DirectSoundBuffer.GetDirectSound3DListener, to create a DirectSound3DListener object. This call would fail if we had not specified the DSBCAPS_CTRL3D flag when creating the primary sound buffer.
The DirectSound3DListener object describes a listener's position, orientation, and listening environment in 3-D space.
The next step is to load the wave form audio data into a sound buffer. Since we want to control the 3-D position of the sounds, we specify the DSBCAPS_CTRL3D flag in the buffer description:
Dim bufferDesc1 As DSBUFFERDESC Dim waveFormat1 As WAVEFORMATEX bufferDesc1.lFlags = (DSBCAPS_CTRL3D Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME) Or DSBCAPS_STATIC Set m_dsBuffer(i) = m_ds.CreateSoundBufferFromFile(file, bufferDesc1, waveFormat1)
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
Finally, we create a DirectSound3DBuffer object to describe the position, orientation, and environment of a sound buffer in 3-D space.
Dim m_ds3dBuffer(2) As DirectSound3DBuffer Set m_ds3dBuffer(i) = m_dsBuffer(i).GetDirectSound3DBuffer