DirectX SDK

Step 3: Play the 3-D Sound

[C++]

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

[Visual Basic]

When the user clicks the Play button in the Tutorial 2 sample there are several initialization steps that occur before the sound is actually played. The volume, pan and loop values are set like in Tutorial 1, but there are several additional values that are initialized because of 3-D sound processing.

The orientation of the sound projection cone for the sound buffer must be set using DirectSound3DBuffer.SetConeOrientation. In the Tutorial 2 sample, this is done in the scrlAngle_Change procedure:

Private Sub scrlAngle_Change(i As Integer)

'first we must calculate a vector of what direction
'the sound is traveling in

Dim x As Single
Dim z As Single
'we take the current angle in degrees convert to radians
'and get the cos or sin to find the direction from an angle
x = 5 * Cos(3.141 * scrlAngle(0).Value / 180)
z = 5 * Sin(3.141 * scrlAngle(0).Value / 180)
    
'Update the UI
DrawPositions

If m_dsBuffer(i) Is Nothing Then Exit Sub

'the zero at the end indicates we want the postion updated immediately
m_ds3dBuffer(i).SetConeOrientation x, 0, z, DS3D_IMMEDIATE

End Sub

Next we set the inside and outside angles of the sound projection cone and current cone outside volume for this sound buffer using the following calls:

m_ds3dBuffer(i).SetConeAngles DS3D_MINCONEANGLE, 100, DS3D_IMMEDIATE
m_ds3dBuffer(i).SetConeOutsideVolume -400, DS3D_IMMEDIATE

Then we sets the sound buffer's current position, in distance units:

m_ds3dBuffer(i).SetPosition m_pos(i).x / 50, 0, m_pos(i).z / 50, DS3D_IMMEDIATE

Finally the sound is played with the call:

m_dsBuffer(i).Play flag

In Tutorial 1, the user can move the position of the two sounds relative to the listener position by clicking either the left or right-mouse button and moving the triangle representing the sound. This updates the position of the sound using the DirectSound3DBuffer.SetPosition method.