DirectSound Tutorial


Introduction to DirectSound - what does it do?

DirectSound manages sound playback, streaming or buffered, and also recording from input devices such as microphones, but all this
tutorial explains is how to create a buffer and play it, the most usual way of using DirectSound and of course the easiest too.
Note: some knowledge about Visual Basic and DirectX is required to use this tutorial.



DirectSound initialization

Like all other DirectX components, DirectSound's interface is needed and assuming you've added the DirectX for VB type libraries as a reference to the project, this code creates a DirectX and DirectSound interface, a buffer description and a waveformat variable.
The waveformat variable is used to specify what kind of sound file we want to play, but needs no setup by the user in our case.
Then we need to create a DirectSound object. We're using the GUID "", i.e. Window's default device, software mode (there are enumeration possibilities to find other DirectSound devices, but that's another tutorial). Just like in DirectDraw, DirectX needs to know how we'd like to use the DirectSound object by setting a cooperative level.
There are four different cooperative levels available; normal, priority, exclusive, and write-primary (DSSCL_NORMAL, DSSCL_PRIORITY, DSSCL_EXCLUSIVE and DSSCL_WRITEPRIMARY). Normal level uses the same setting for all DirectSound buffers so that the device can switch between all applications without problems, also some device specific functions is unavailable to prevent errors when the device is being shared between different applications. Priority level gives the application full access to device specific functions, exclusive level prevents other applications to use the device when the exclusive application is in the foreground. Write-primary level is the highest cooperative level and gives the application full access to the primary sound buffer (what is sent to the speakers, similar to the DirectDraw primary buffer) and can create no secondary buffers (the ones we're gonna use now) and is not recommended in normal sound applications.

'DirectSound and DirectX interface
Dim DX As New DirectX7
Dim DS As DirectSound

'A sound buffer, a buffer description and a waveformat variable
Dim DBuffer As DirectSoundBuffer
Dim DSDesc As DSBUFFERDESC
Dim WavFormat As WAVEFORMATEX


'Create DirectSound interface and set cooperative level
Set DS = DX.DirectSoundCreate("")
DS.SetCooperativeLevel frmMain.hWnd, DSSCL_PRIORITY


Loading a wave file

Now, the wave files intend to be quite big since there's not much compression (if any at all) and playing them directly from disc wouldn't work very well,
that's why we have sound buffers. Hence the name, the buffer holds the wave file data ready to play at any time, can be replayed and written to as it pleases you. We won't fill the buffer description with anything since we just want to play the sound file as it is.
This is how we do it:

'Fill the DBuffer variable with wave data from the wave file specified
Set
DBuffer = DS.CreateSoundBufferFromFile(App.Path & "\gotohell.wav", DSDesc, WavFormat)


Playing our buffer

That's just about it folks, the buffer is ready to play 'til your computer screams, over and over again. When played, the buffer data is copied to the primary sound buffer (managed by the DirectSound interface itself) where it finds the way to your speakers through your soundcard and that's about it. There are two ways of playing a buffer, default or looped mode (I don't need to explain that, do I?)

DBuffer.Play DSBPLAY_DEFAULT 'use the DSBPLAY_LOOPING if you want to loop the buffer

'make it stop playing and rewind it to start
DBuffer.Stop
DBuffer.SetCurrentPosition 0


Conclusions - is this all?


Noo, that's far from everything, that's the very beginning and basics of using DirectSound. Further, DirectSound can be used to change frequency, panning and volume of the buffers, stream sound files from disc and record from input devices, mix sounds together and take advantage of hardware capabilities and positioning sounds in 3D space, but that has to wait for future tutorials.
Source code for this tutorial is of course downloadable too.
If there should be any problems with this tutorial or the source code or if you would like to send me a comment, I'd be glad to hear from you (e-mail at the bottom of this page) or you're welcome to discuss it at the Voodoo VB discussion forum.


© Copyright 1999 by Daniel Netz, Sentinel Design