Manipulating the Listener

The Listener3D object is created and used much like the Buffer3D object is. However, instead of manipulating the actual sound source (of which there can be many), you are dealing directly with the listener, of which there is only one per device. For this reason, you cannot use a SecondaryBuffer when creating a listener object; you can only use a Buffer object, and then only if it is a primary buffer.

Before you write the code using the listener object, you should take a look at the properties and settings you can modify on it, much like you did with the 3D buffer object. You will find these properties in Table 14.3.

Table 14.3. Listener3D Properties

PROPERTY

DESCRIPTION

CommitDeferredSettings

If any of the settings were deferred on any 3D sound buffers, or the listener, this method will commit all new values at the same time. This method will have no effect if there were no deferred changes made.

DistanceFactor

A read-write property that controls the number of meters in a vector unit.

DopplerFactor

A read-write property that controls the multiplier for the Doppler effect. Any buffer that has velocity will automatically get Doppler shift effects, and these effects are cumulative.

Deferred

A read-write Boolean property that determines if property changes will happen instantly or if they will be deferred until the listener updates settings. The default is false, and the properties are updated immediately.

Orientation

A read-write property that controls the orientation of the listener. DirectSound will automatically normalize the vector.

RolloffFactor

A read-write property that determines the rolloff factor. This factor determines the rate of attenuation over distance.

Position

A read-write property that determines the current position of the listener in world space.

Velocity

A read-write property that determines the velocity of the listener, in meters per second by default.

You should take the existing example where the sound buffer is moving, and update it to move the listener instead. You can get rid of the 3D buffer object you were using, and replace that variable with the following two:

 private Listener3D listener = null; private Microsoft.DirectX.DirectSound.Buffer primary = null; 

You need to have a primary buffer in order to create the listener object. You'll also notice that since the class name "Buffer" is also in the system namespace, you will need to fully qualify the buffer variable name. Since you don't have the "buffer" variable anymore, you'll need to update the InitializeSound method as well. Replace the code that is used to create the 3D sound buffer with this:

 BufferDescription primaryBufferDesc = new BufferDescription(); primaryBufferDesc.Control3D = true; primaryBufferDesc.PrimaryBuffer = true; primary = new Microsoft.DirectX.DirectSound.Buffer(primaryBufferDesc, device); listener = new Listener3D(primary); listener.Position = new Vector3(0.1f, 0.0f, 0.0f); 

Here similar things are done. You create a primary buffer, and use that to get the listener object, then set the listener's position to a slightly left value. Finally, you just need to update the timer code to move the listener rather than the buffer that's no longer in the code:

 private void timer1_Tick(object sender, System.EventArgs e) {     // Adjust the position     listener.Position *= mover;     if ((Math.Abs(listener.Position.X) > MoverMax) && (mover == MoverUp))     {         mover = MoverDown;     }     if ((Math.Abs(listener.Position.X) < MoverMin) && (mover == MoverDown))     {         mover = MoverUp;     } } 

As you can see, you did nothing more than replace buffer with listener. This example should sound identical to the last one, since the same basic principle is being applied.

SOUND LISTENERS EFFECT

It's also important to note that the listener object only affects buffers created with the control 3D flag. If you create buffers without this flag they will be played at equal volume from all speakers.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net