Platform SDK: DirectX

Step 1: Create a DirectSound Object

[C++]

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

[Visual Basic]

The central object to a DirectSound application is an instance of the DirectSound class. Applications use the methods of a DirectSound object to set up the sound environment and to create buffers that hold audio data.

This object is obtained by creating the global DirectX7 object, which is at the core of all DirectX for Visual Basic applications, and then calling the DirectX7.DirectSoundCreate method.

The following global declarations create the DirectX7 object and variables for the DirectSound object and a couple of buffers.

Dim m_dx As New DirectX7
Dim m_ds As DirectSound
Dim m_dsBuffer(2) As DirectSoundBuffer

In the startup code, create the DirectSound object as follows:

On Local Error Resume Next
 
Set m_ds = m_dx.DirectSoundCreate("")
 
If Err.Number <> 0 Then
    MsgBox "Unable to start DirectSound. Check to see" & _
           " that your sound card is properly installed."
    End
End If

Passing an empty string to the DirectSoundCreate method indicates that you want the default sound device on the system.

Now you need to set the cooperative level for the sound device. In the Tutorial 1 sample, this is done with the following statement.

m_ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY

The first argument is the window handle to the application. Note that the hWnd property for a form is not set until the form becomes visible, which normally happens after the form's Load procedure has been fully executed. In the sample, hWnd is available because Show was called for the form window earlier in the procedure.

The second argument sets the priority cooperative level, which is the best one for most applications.

Next for Tutorial 1: Step 2: Load a Wave File into a Buffer

Next for Tutorial 2: Step 2: Obtain the Listener