Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9

DirectSound: Modifying Sounds
By: Jack Hoxley
Written: May 2000

Download: Ds_Modifier.Zip (13kb)

For this feature we will be taking the simple application that we created in the DirectSound Basics tutorial and adding several features to it. There are four main things that we can change on a sound buffer, all of which can be very useful:

  1. Frequency - the number of hertz to play the sound at, at a simple level it appears to speed up/slow down playback and/or make things high pitch/low pitch
  2. Panning - Make the sound appear left or right (or any position in between) on a stereo set of two speakers.
  3. Volume - Make the sound louder or quieter
  4. Position - Change the position in the current buffer.

There is one warning that I should give you when using these modifiers - Dont over use them. Constantly changing these values on a buffer will cause your program to slow down quite considerably; Should you need to change them in time with an animation and/or a regular basis (under a second) optimize it by making it change every third time, or every second time.

Frequency can be one of the more amusing things to change on a buffer; Find a sound file of someone talking, and keep changing the frequency randomly whilst playing it - you'll see what I mean. One of the best uses I've seen for the use of frequency is engine noises, racing cars speeding up and slowing down. Frequency can be set whilst the sound is playing, and is done like so:

DsBuffer.SetFrequency DSBFREQUENCY_MAX 'Maximum frequency
DsBuffer.SetFrequency DSBFREQUENCY_MIN 'Minimum frequency
DsBuffer.SetFrequency DSBFREQUENCY_ORIGINAL 'Useful for setting it back to normal after you messed it up!

DsBuffer.SetFrequency 3432 'Any numerical value within the range....

The valid ranges for the frequency are 100 to 100,000 although you're best advised to stay a little way in from there; An old sound card of mine wouldn't play sounds below 6000Hz - why? I have no idea...

This only effects people who have a set of stereo speakers, but you can safely assume that everyone has these days. This is not to be confused with 3D sound effects; panning in this form is simply left to right and does not include up/down or behind a person. Panning can be used to complement whats being seen on screen, should a character be standing on the right speaking to the player you could make the characters voice come form the right hand speaker. Or, you could have two characters speaking to each other, one on the left and one on the right - and use panning to seperate their voices.

DsBuffer.SetPan DSBPAN_CENTER 'No Change
DsBuffer.SetPan DSBPAN_LEFT 'Complete Left
DsBuffer.SetPan DSBPAN_RIGHT 'Complete Right

DsBuffer.SetPan 1205 'Can substitute the above constants for any value...

The valid ranges for panning are -10,000 for full left, 0 for complete center and +10,000 for full right.

Volume is fairly obvious, if you dont know what this means I really think you should learn it. Volume can be used for almost anything, from giving a certain sound empthasis or making effects such as fade-in and fade-out.

'Guess what these two do...
DsBuffer.SetVolume DSBVOLUME_MAX

DsBuffer.SetVolume 5000 '50% volume

The valid volume settings are not quite what you would expect; with 0 being maximum volume and -10,000 being silence. Remember though, the volume can still be overridden by any external controls on the speakers themselves.

Position setting can be very useful; as you may have noticed it is used when stopping a sound - to reset it back to the beginning. One clever thing that some people do is to compile all their sounds into one large Wav file, then use this function to jump around to the relevent parts; personally I dont like this method, but it is a viable option. SetPosition is one of the more complicated methods of the four featured here; try this:

Dim CurrPos as DSCURSORS
DsBuffer.GetCurrentPosition CurrPos
'Current position in bytes = CurrPos.lPlay

DsBuffer.SetCurrentPosition 1000 'Be careful not to go past the end of the file - or you'll get errors.

The new position that you give the buffer is based on the number of bytes through the buffer that you are. If the sound is not playing, next time it starts it will begin from x bytes into the buffer; if it is already playing it will jump to x bytes into the buffer.

Download my sample program from the top of this page, or from the downloads page.

DirectX 4 VB 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster