DirectSound

Squirm
10-25-2001, 08:02 AM
Finally I get round to writing the DS tutorial. I wont bother with the DX basics, such as referencing. For that, you just need to check out the first bit in my DM tutorial (http://www.visualbasicforum.com/showthread.php?s=&threadid=11945). Now thats out the way, let us begin...

I'm not very well versed in the ways of DS yet, I am gradually learning it myself as I incorporate it into my applications, so the credit for most of this code goes to DirectX4VB (http://www.directx4vb.com) and their great site..

First up, we need some objects, just as we did with DD and DM:

Option Explicit

Dim DS As DirectSound
Dim DSBuffer As DirectSoundBuffer
Dim DSDesc As DSBUFFERDESC
Dim DSWave As WAVEFORMATEX

Okay, here is a simple description:

DirectSound - The main DS object which acts as the stem of all soundy things
DirectSoundBuffer - A memory buffer which takes data from the sound file and then feeds it to the device at the right rate.
DSBUFFERDESC - A description of the buffer. This will hold various flags about how we want to play our sound
WAVEFORMATEX - Holds information about the wave sound we are playing, including number of channels, sample bit depth, and the format of the sound file.

Right, thats all our dims for now. We can now initialise DS:

Set DS = DX.DirectSoundCreate("")
DS.SetCooperativeLevel Form1.hWnd, DSSCL_EXCLUSIVE

This creates our sound device, and "" means the default driver. In a real application you would want to let the user decide which device to use, but for now the default will suffice. The second line makes it so that our form has complete (exclusive) control over the sound, all other Windows applications are shut out from the sound driver.

Now, here's the tricky part:

DSDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
DSWave.nFormatTag = WAVE_FORMAT_PCM
DSWave.nChannels = 2
DSWave.lSamplesPerSec = 22050
DSWave.nBitsPerSample = 16
DSWave.nBlockAlign = DSWave.nBitsPerSample / 8 * DSWave.nChannels
DSWave.lAvgBytesPerSec = DSWave.lSamplesPerSec * DSWave.nBlockAlign

This basically sets up everything for our sound device. Most of it is straightforward. Thoe only things you might want to change are DSWave.nChannels or DSWave.nBitsPerSample. One thing you MUST NOT change is DSWave.nFormatTag = WAVE_FORMAT_PCM otherwise we get nasty errors.

Now that we know whats going on, we are ready to load the buffer with sound...

Set DSBuffer = DS.CreateSoundBufferFromFile("C:\mysound.wav", DSDesc, DSWave)

Of course, you will want to change the filename to match your file location.

Thats it, all we need to do now is play the sound:

DSBuffer.Play DSBPLAY_DEFAULT

And then, the sound has been played once. To get the sound to loop we use:

DSBuffer.Play DSBPLAY_LOOPING

and in this case, to stop we use:

DSBuffer.Stop

And to rewind we can use:

DSBuffer.SetCurrentPosition 0

Although you can change the value above to jump around the sound file, which may or may not be useful to you, if you can find out the right position to jump to. All we need to do when we clear up is:

DS.SetCooperativeLevel Form1.hWnd, DSSCL_NORMAL
Set DS = Nothing
Set DSBuffer = Nothing

This hands control of the sound driver back to windows and mops up. Job done, all finished.

Now, you've got it. You can load and play sounds now, all asynchronously (more than one at a time). You can loop them, start them, stop them, jump around in them. Thats pretty much all you need to know.

For more information and advanced DS, including using DS to record sound, streaming straight from files, and using 3D sound, I suggest you visit DirectX4VB (http://www.directx4vb.com) and their great selection of tutorials, for both DX7 and DX8.

Next up, advanced DD.....

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum