Using Effects with Your Sounds

Aside from 3D manipulation of sounds, another technique used to bring out the realism of the games you play would be the effects added to already existing sounds. For example, walking across a wood floor would sound totally different in a small room from the way it would sound if you were in a large empty concert hall. Yelling in a cavern may cause your voice to echo among the cavern walls. These effects are an integral part of the immersion you are trying to create when developing a game, or any good interactive multimedia application.

BUFFERS THAT SUPPORT EFFECTS

Effects can only be used on a secondary buffer. The required method to apply the effects to the buffer only resides on this object.

Luckily, the act of using these special effects is pretty straightforward and there are quite a few of these effects already built into the DirectSound API. Revisiting the old example you created that was controlling the pan, update that to control some effects. Remove the timer since you won't be using that anymore. Then change the InitializeSound method as follows:

 public void InitializeSound() {     device = new Device();     device.SetCooperativeLevel(this, CooperativeLevel.Normal);     BufferDescription desc = new BufferDescription();     desc.ControlEffects = true;     desc.GlobalFocus = true;     sound = new SecondaryBuffer(@"..\..\drumpad-crash.wav",desc, device);     EffectDescription[] effects = new EffectDescription[1];     effects[0].GuidEffectClass = DSoundHelper.StandardEchoGuid;     sound.SetEffects(effects);     sound.Play(0, BufferPlayFlags.Looping); } 

The big changes here are the ability to control the pan being removed, and change it to controlling the effects. Then you create an array of effect descriptions (currently with just one member), and assign that member to be the standard echo effect. You then pass in the effects that you have just created and start playing the buffer.

SETTING EFFECTS

Effects can only be set on a buffer that is stopped. If the buffer is playing when SetEffects is called, an exception will be thrown. You can use the Status property on the buffer to detect if it is playing. If it is, you can call the Stop method to ensure that it has stopped before applying the effects.

That's really all there is to setting the basic effects. If you run the application now you will be able to hear the echo that wasn't there before. It's also possible to add "stacks" of these effects on top of each other. If we wanted to have the sound played with an echo effect along with a flanger effect, followed by a distortion effect, you could update the method as follows:

 EffectDescription[] effects = new EffectDescription[3]; effects[0].GuidEffectClass = DSoundHelper.StandardEchoGuid; effects[1].GuidEffectClass = DSoundHelper.StandardFlangerGuid; effects[2].GuidEffectClass = DSoundHelper.StandardDistortionGuid; sound.SetEffects(effects); 

Well, you've managed to take the drum cymbal sound and make it sound like an airplane engine; at least that's what this sounds like to me. The effects also do not have to be different; you could just as easily pass in three flanger effects for the example above. As you can already tell, there are a few built-in effect types already, but what are they? You can find the effects listed in Table 14.4.

Table 14.4. Built-in Effect Types

EFFECT

DESCRIPTION

Chorus

The chorus effect doubles the voices by echoing the original sound with a slight delay and slightly modulating this delay.

Compression

The compression effect is essentially a reduction in the signal above certain amplitudes.

Distortion

The distortion effect is implemented by adding harmonics to the signal, so that as the level increases, the top of the waveform becomes either squared off, or clipped.

Echo

The echo effect will cause the entire sound to be played again (at a lower volume) after a set delay.

Environment Reverberation

The environment reverberation effect is implemented with the Interactive 3D Audio Level 2 (I3DL2) specification. It only controls the listener portions of this specification.

Flange

The flange effect is like the chorus effect in that it also is implemented with an echo; however, the delay is very short, and varies over time.

Gargle

This effect simply modulates the amplitude of the signal.

Parametric Equalizer

This effect is much like the equalizer in your car or stereo. It amplifies or attenuates signals of a certain frequency.

Waves Reverberation

This effect is intended to be used with music. It causes a nice reverb effect to be applied to your sound.

It is also possible to take any of the effects you've applied to the buffer and change any or all of the settings currently applied. You can do this with the GetEffects method on the secondary buffer. In order to better hear the changes you're making, get rid of all the effects you're currently playing except for the echo effect. Now, add the following section of code immediately after the call to SetEffects:

 EchoEffect echo = (EchoEffect)sound.GetEffects(0); EffectsEcho param = echo.AllParameters; param.Feedback = 1.0f; param.LeftDelay = 1060.2f; param.RightDelay = 1595.3f; param.PanDelay = 1; echo.AllParameters = param; 

Here, you use the GetEffects method to create an instance of the EchoEffect object, which holds all of the data for the echo effect that is currently being used. You then change a few properties on the echo effect, and you're done. Listen to the audio with and without this code running, the changes to the effect should be immediately obvious. Any effect that you load can have its options manipulated in this way.

UPDATING EFFECT PROPERTIES

Even though SetEffects may only be called when the buffer is stopped, you can manipulate any of the effects that are currently loaded in real time as the buffer is playing.



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