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
      Miscellaneous

DirectSound: Advanced 3D Topics
By: Jack Hoxley
Written: June 2000


There are several effects and properties you can set when using DirectSound3D to modify the way sounds are heard and created. These can be very useful when trying to interact with different environments in a Direct3D scene.

DOPPLER EFFECT
The doppler effect is how a sound is changed when it's moving. A car speeding down a road is a good example - the way in which the sound is different to the bystander from the actual driver (and the noise the Engine actually makes). You have to work out the speed of the object which the doppler effect is applied to. This effect can be used to simulate the real-world effects, or it can be used to create weird special effects. Currently, DirectX supports up to 10 different levels; with fractions between. a Doppler effect of 0 will disable it, a doppler factor of 0.5 will half it and a doppler factor of 1 is real-world normal; all those after (2-10) are amplifications; ie, factor 2 will give you twice the normal doppler effect.

DirectSound3DListener.SetDopplerFactor(dopplerFactor As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DListener.GetDopplerFactor() As Single

These two lines are all that you need. The SetDopplerFactor will allow you to set the amount; note that you can set decimal numbers, this allows you much more variation than the integers 0-10. The second argument is when the change is applied. Because changing settings can take time (relative to normal operating speeds) you may want to apply more than one at a time. You can use either DS3D_DEFERRED or DS3D_IMMEDIATE as the last flag. IMMEDIATE means that it will change at that point, and will cause it to recalculate everything. DEFERRED will mean that you can go on setting properties without it changing anything, you then have to call DirectSound3DListener.CommitDeferredSettings before DirectSound will update them. There are several constants that can be used in place of the dopplerFactor variable: DS3D_MINDOPPLERFACTOR (no doppler effect), DS3D_MAXDOPPLERFACTOR (10x normal doppler effect) and DS3D_DEFAULTDOPPLERFACTOR (Normal doppler effect: 1.0)


ROLL OFF FACTOR
This works together with Minimum and Maximum distance settings (see below). The roll off factor is simply the volume of the sound as it gets closer to or further away from the source. This is applied on a global scale; for individual buffers, you need to set the Minimum and Maximum distances. A good example of the changing Roll Off factor is environment. On a hot summers day sound will travel further than on a cold, damp winters morning - you can use the roll off factor to simulate this, therefore adding more realism to your world scene.

Directsound3DListener.SetRolloffFactor(rolloffFactor As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DListener.GetRolloffFactor() As Single

These are the two lines needed for changing the roll off factor. Currently you can set rolloffFactor to any decimal between 0 and 10.0, where 1 is normal rolloff levels; all others are multiples (0.5 = half normal, 10 = 10 times normal). You can also use the three constants provided: DS3D_MINROLLOFFFACTOR (No roll off), DS3D_DEFAULTROLLOFFFACTOR (set as 1.0 and represents normal amounts of roll off) or DS3D_MAXROLLOFFFACTOR (set as 10.0 - and is 10x the real world levels). The second parameter states when it will take effect. This is either now (IMMEDIATE) or Later (DEFFERED) see Doppler effect for more details.


3D MODE
3D processing is expensive when it comes to processor time. That is why there is an option to set which mode of 3D processing occurs. If you dont need a buffer to be 3D then you can disable it until it is needed, therefore stopping all 3D calculations until it is re-enabled. You can also set the coordinate system; although this doesn't sound like a processing issue, it actually is. Normally all sounds are positioned on a world system - no matter where the listener is the sounds are in the same place. This causes DirectSound to keep recalculating where they actually are relative to to the listeners new position. This may well slow things down. You can set it so that all sound positions are relative to where the listener is. A coordinate of 1,0,10 would no longer represent the same position all the time, instead it would be 1 unit right of the user and 10 units in front of the user - as the user moved around these coordinates would still be in this relative position to the listener. Again, apply flags appear; see doppler effect for more information on IMMEDIATE and DEFFERED settings.

1. This disables all 3D processing
DirectSound3DBuffer.SetMode DS3DMODE_DISABLE, DS3D_IMMEDIATE
2. All coordinates will be relative to the listener
DirectSound3DBuffer.SetMode DS3DMODE_HEADRELATIVE , DS3D_IMMEDIATE
3. Normal 3D processing with world coordinates. This is the default
DirectSound3DBuffer.SetMode DS3DMODE_NORMAL , DS3D_IMMEDIATE

You can also get what the current 3D Mode is usig this logic:

If DirectSound3DBuffer.GetMode = DS3DMODE_HEADRELATIVE then
'It's disabled.
End If

You may well have noted that this is no longer a DirectSound3DListener object being used; it's a DirectSound3DBuffer. This means that it only affects one buffer. Therefore you can have all sounds being on the world coordinate, and then one (or more) on HEADRELATIVE coordinates - ie, one for your weapon firing that'll always be from the same place in relation to you.


CONES
Until your program changes these values, every 3D sound source created is omni-directional and has no orientation, instead emits the same amount of sound in all directions. A sound cone is defined as two cones - an inside cone and an outside cone. The inside cone is always the buffers maximum volume (not necessarily maximum, just the current buffer volume, Buffer.SetVolume). The outside cone is set to be a certain amount less than the inside cones volume (in decibels). To specify these cones we use angles; one for the inside and one for the outside. The outside cone must be bigger (or the same size) than the inside cone.

1. Setting the angles:
We use one call to get and set the cone angles:

DirectSound3DBuffer.SetConeAngles(inCone As Long, outCone As Long, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DBuffer.GetConeAngles(inCone As Long, outCone As Long)

Both the incone and outcone values are in degrees (therefore between 0 and 360). The Apply flags are either IMMEDIATE or DEFERRED - see Doppler effect for more details on the meaning.

2. Setting the Volume
There is only one explicit property here - setting the outside volume; the inside volume is set by changing the volume of the host buffer (a normal DirectSoundBuffer object).

DirectSound3DBuffer.SetConeOutsideVolume(coneOutsideVolume As Long, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DBuffer.GetConeOutsideVolume() As Long

The volume is the same range as a normal buffers volume; 0 (max volume) to -6000 (min volume). You can also use the constants DSBVOLUME_MAX (maximum volume) or DSBVOLUME_MIN (Minimum volume). The apply flags are the same as all the other property changes; see the doppler effect for more details.

3. Cone Orientation
This goes along with the position properties. The parameters supplied are the vector from the source position, and they define where it's pointing too. This can be very important, especially if you want the sound to point in a certain direction.

DirectSound3DBuffer.SetConeOrientation( x As Single, y As Single, z As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DBuffer.GetConeOrientation(orientation As D3DVECTOR)

These two are very easy. when setting the orientation X,Y,Z are the vector that define which way it is pointing. In the second statement, orientation as D3DVECTOR requires that you have a defined D3DVECTOR, which you pass to the statement, and it fills with the correct information.

MINIMUM and MAXIMUM Distances
A sound coming from any source will get louder as it gets closer. But at a certain distance it will get no louder, this is the minimum distance of a sound. As a sound gets further away it gets quieter, the maximum distance of a sound defines at what point it becomes silent. All measurements are in meters (which is the same scale as everything in a 3D world).

DirectSound3DBuffer.SetMinDistance(minDistance As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DBuffer.SetMaxDistance(maxDistance As Single, applyFlag As CONST_DS3DAPPLYFLAGS)

-And-
DirectSound3DBuffer.GetMinDistance() As Single
DirectSound3DBuffer.GetMaxDistance() As Single

The new values are allowed to be decimal numbers; which give you a finer control over the distances, the applyflag is the same for every property so far - IMMEDIATE for changing now, or DEFERRED to be changed later. See doppler effect for more details.


VELOCITY
Velocity doesn't actually make the sound source move. It just says "If it were moving it would be going in this direction". It's up to you to actually move the sound source by using SetPosition. The velocity is used when calculating the doppler effect; so it is quite important.

DirectSound3DBuffer.SetVelocity(x As Single, y As Single, z As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
DirectSound3DBuffer.GetVelocity(velocity As D3DVECTOR)

The velocity is described as a vector in the direction of which it is travelling. The SetVelocity statement allows you to specify this in the X,Y,Z parameters and the GetVelocity statement will fill out a D3DVECTOR type describing the current velocity.


POSITION
The position is very important when creating 3D sounds. The position may well require a modifier, as any value passed to this procedure above +10 and below -10 will be below audible range. Therefore you may want to create a multiplier/divider formula so that the sound can be related properly.

DirectSound3DBuffer.SetPosition(x As Single, y As Single, z As Single, applyFlag As CONST_DS3DAPPLYFLAGS)
Directsound3DBuffer.GetPosition(position As D3DVECTOR)

The coordinates you give will be set based on what mode DS3D is in - HEADRELATIVE or NORMAL (see above). Setting the position of a sound is identical to the positioning of an object in a 3D scene. When it comes to retrieving the current position, it will fill out a D3DVECTOR that contains the X,Y,Z values for the sound.

OVERVIEW
You should now be perfectly capable of creating a real sounding 3D environment. Only practice or trial and error will allow you to progress with this field.

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