DirectX SDK

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 any 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 which hold audio data.

This object is created by first creating the global DirectX7 object, which is at the core of all DirectX for Visual Basic applications, and calling the DirectX7.DirectSoundCreate method. This method returns a DirectSound object.

The following global declarations create the DirectX7 object and variables for the other necessary objects:

Dim m_dx As New DirectX7
Dim m_ds As DirectSound
 

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 we want the default sound device installed on the system. This method could fail if there are no installed sound devices or if DirectX is not installed. We can check the success of this method by trapping the error value generated by a failed method call. You should have error handling routines to trap and handle all errors in your application.

After creating a DirectSound object, you need to set the cooperative level of the application for the sound device. In the Tutorial 1 sample, this is done with the statement:

m_ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
 

The first argument is the window handle to the application. The second argument sets the priority level. For information on priority levels, see the DirectSound.SetCooperativeLevel topic.

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

Next for Tutorial 2: Step 2: Load a Wave File into a 3-D Buffer