Microsoft DirectX 8.0 (Visual Basic)

Obtaining the 3-D Listener

Global sound parameters are set and retrieved by using the DirectSound3DListener8 object, which is obtained from the primary sound buffer. There is only one primary buffer in an application, and only one listener.

If you are using the DirectMusic performance and audiopaths to play your 3-D sounds, you can obtain the listener from the audiopath by using DirectMusicAudioPath8.GetObjectInPath, setting the lStage parameter to DMUS_PATH_PRIMARY_BUFFER. In the following example, AudioPath is a DirectMusicAudioPath8 object:

Dim dx As New DirectX8

Dim perf As DirectMusicPerformance8
Dim audparams As DMUS_AUDIOPARAMS
Dim audiopath As DirectMusicAudioPath8
Dim listener As DirectSound3dListener8

Private Sub Form_Load()
  Set perf = dx.DirectMusicPerformanceCreate
  perf.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, audparams, _
      Nothing, Nothing, DMUS_APATH_DYNAMIC_3D, 16
  Set audiopath = perf.GetDefaultAudioPath
  Set listener = audiopath.GetObjectinPath(DMUS_PCHANNEL_ALL, _
      DMUS_PATH_PRIMARY_BUFFER, 0, vbNullString, 0, _
      IID_DirectSound3DListener)
End Sub

If your application is creating and managing its own sound buffers using only the DirectSound API, you must create a primary buffer object and then obtain the listener interface from that.

Create the primary buffer object by using the DirectSound8.CreatePrimarySoundBuffer method, specifying the DSBCAPS_CTRL3D and DSBCAPS_PRIMARYBUFFER flags in the lFlags member of the accompanying DSBUFFERDESC type. Call the DirectSoundPrimaryBuffer8.GetDirectSound3DListener method on the resulting buffer to obtain a DirectSound3DListener8 object. These steps are shown in the following example, where ds is a global DirectSound8 object:

Dim dsbd As DSBUFFERDESC
Dim dsbPrimary As DirectSoundPrimaryBuffer8
Dim waveFormat As WAVEFORMATEX
Dim ds3dListener As DirectSound3DListener8
 
' 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.CreatePrimarySoundBuffer(dsbd, waveFormat)
 
' Create the DirectSound3DListener8 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.