DirectX SDK

Obtaining the DirectSound3DListener Object

[C++]

To obtain a pointer to an IDirectSound3DListener interface, you must first create a primary 3-D sound buffer. Do this by using the IDirectSound::CreateSoundBuffer method, specifying the DSBCAPS_CTRL3D and DSBCAPS_PRIMARYBUFFER flags in the dwFlags member of the accompanying DSBUFFERDESC structure. Then, use the IDirectSoundBuffer::QueryInterface method on the resulting buffer to obtain a pointer to an IDirectSound3DListener interface for that buffer, as shown in the following example with C++ syntax:

/* In this example, it is assumed that lpds is a valid 
   pointer to a DirectSound object */
 
DSBUFFERDESC        dsbd;
LPDIRECTSOUNDBUFFER lpdsbPrimary;
 
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;
if SUCCEEDED(lpds->CreateSoundBuffer(&dsbd, &lpdsbPrimary, NULL))
{
    // Get listener interface
    if FAILED(lpdsbPrimary->QueryInterface(IID_IDirectSound3DListener,
            (LPVOID *)&lp3DListener))
    {
    lpdsbPrimary->Release();
    }
} 
 
[Visual Basic]

To obtain a DirectSound3DListener object, you must first create a primary 3-D sound buffer. Do this by using the DirectSound.CreateSoundBuffer method, specifying the DSBCAPS_CTRL3D and DSBCAPS_PRIMARYBUFFER flags in the lFlags member of the accompanying DSBUFFERDESC type. Then, use the DirectSoundBuffer.GetDirectSound3DListener method on the resulting buffer to obtain a DirectSound3DListener object for that buffer, as shown in the following example with Visual Basic syntax:

' 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 ds3dListener As DirectSound3DListener
 
' 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
ds3DListener = dsbPrimary.GetDirectSound3DListener