DirectX SDK

Obtaining the DirectSound3DBuffer Object

[C++]

To obtain a pointer to an IDirectSound3DBuffer interface, you must first create a secondary 3-D sound buffer. Do this by using the IDirectSound::CreateSoundBuffer method, specifying the DSBCAPS_CTRL3D flag in the dwFlags member of the DSBUFFERDESC structure. Then, use the IDirectSoundBuffer::QueryInterface method on the resulting buffer to obtain a pointer to an IDirectSound3DBuffer interface for that buffer.

The following example calls the QueryInterface method with the C++ syntax:

// LPDIRECTSOUNDBUFFER lpDsbSecondary;
// The buffer has been created with DSBCAPS_CTRL3D. 
LPDIRECTSOUND3DBUFFER lpDs3dBuffer;
 
HRESULT hr = lpDsbSecondary->QueryInterface(IID_IDirectSound3DBuffer, 
                                            (LPVOID *)&lpDs3dBuffer); 
if SUCCEEDED(hr) 
{ 
    // Set 3-D parameters of this sound. 
    . 
    . 
    . 
} 
 
[Visual Basic]

To obtain a DirectSound3DBuffer object, you must first create a secondary 3-D sound buffer. Do this by using the DirectSound.CreateSoundBuffer method, specifying the DSBCAPS_CTRL3D flag in the lFlags member of the DSBUFFERDESC type. Then, use the DirectSoundBuffer.GetDirectSound3DBuffer method on the resulting buffer to obtain a DirectSound3DBuffer object for that buffer.

The following example shows how to create a DirectSound3DBuffer object.

' In this example, it is assumed that ds is a valid 
' DirectSound object 
 
Dim dsbd As DSBUFFERDESC
Dim dsbPrimary As DIRECTSOUNDBUFFER
Dim waveFormat As WAVEFORMATEX
Dim ds3dBuffer As DirectSound3DBuffer
 
' Set up the wave format
waveFormat.nSize = LenB(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
 
' Create the primary buffer
dsbd.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_PRIMARYBUFFER
dsbPrimary = ds.CreateSoundBuffer(dsbd, waveFormat)
 
' Now create the DirectSound3DListener object
ds3DBuffer = dsbPrimary.GetDirectSound3DBuffer
 

Note  Pan control conflicts with 3-D processing. If both DSBCAPS_CTRL3D and DSBCAPS_CTRLPAN are specified when the buffer is created, DirectSound returns an error.