Platform SDK: DirectX

Step 4: Play and Change the 3-D Sound

[C++]

This section pertains only to DirectX for Visual Basic. See DirectSound C/C++ Tutorials.

[Visual Basic]

The 3-D sound buffer is now represented by two objects: a DirectSoundBuffer object that has the standard functionality of buffers, and a DirectSound3DBuffer object that controls the 3-D properties of the buffer.

Play the buffer just as you would a 2-D buffer. See Step 3: Play the Sound in Tutorial 1.

Changes in the 3-D properties of the buffer can be made at any time, whether it is playing or not. In the sample application, properties are set when the buffers are created and also in response to user input, as the Direction scrollbars and the triangles representing the buffers are moved.

The direction of the sound cone for the buffer is set in the Change procedure of the Direction scrollbar.

Private Sub scrlAngle_Change(i As Integer)
 
  Dim x As Single
  Dim z As Single
 
  'Calculate a vector for the direction.
  'Convert degrees to radians and get the cos or sin.
 
  x = 5 * Cos(3.141 * scrlAngle(0).Value / 180)
  z = 5 * Sin(3.141 * scrlAngle(0).Value / 180)
    
  'Update the graphics.
 
  DrawPositions
 
  If m_dsBuffer(i) Is Nothing Then Exit Sub
  m_ds3dBuffer(i).SetConeOrientation x, 0, z, DS3D_IMMEDIATE

End Sub

In the sample, the user can move the position of the two sounds relative to the listener by clicking the left or right mouse button and moving the triangle representing the sound. The position of the sound is changed by using the DirectSound3DBuffer.SetPosition method. See the UpdatePosition procedure in the sample.