Platform SDK: DirectX

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 DSBUFFERDESC type. Call the DirectSoundBuffer.GetDirectSound3DListener method on the resulting buffer to obtain a DirectSound3DListener object. These steps are shown in the following example, where ds is a global DirectSound object.

Dim dsbd As DSBUFFERDESC
Dim dsbPrimary As DirectSoundBuffer
Dim waveFormat As WAVEFORMATEX
Dim ds3dListener As DirectSound3DListener
 
' Create the primary buffer. The waveFormat parameter is ignored
' for primary buffers, so it doesn't have to be initialized.
 
dsbd.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_PRIMARYBUFFER
Set dsbPrimary = ds.CreateSoundBuffer(dsbd, waveFormat)
 
' Create the DirectSound3DListener object.
 
Set ds3DListener = dsbPrimary.GetDirectSound3DListener
 

Once the listener has been obtained, the primary buffer object is not needed and can be allowed to go out of scope.