Applying a 3D Positional Effect to a Sound
This topic demonstrates how to apply 3D positioning effects to a SoundEffect. The XNA Framework audio system contains support for 3D audio positioning effects. It uses the AudioEmitter and AudioListener classes, and the SoundEffectInstance.Apply3D method. The effects simulate 3D positioning for sound by adjusting speaker mix for cues that use the 3D values. Speaker mix is the only effect that is applied automatically using this method.
Complete Sample
The following example uses a circular rotation around a stationary AudioListener to emphasize the 3D effect.
The code in this topic shows you the technique for applying 3D positional effects. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Download Basic3DAudio_Sample.zip
Applying Basic 3D Positional Effects to a SoundEffect
To retrieve and play a SoundEffect in 3D
Add a wave (.wmv) file to a new XNA Game Studio project as described in Playing a Sound.
Note
The project should contain at least one SoundEffect.
Declare SoundEffect and Stream by using the method shown in Playing a Sound. In addition to the above method, declare SoundEffectInstance.
SoundEffectInstance soundEffectInstance;
Create an AudioEmitter, an AudioListener, and a Vector3 to store the 3D position of the sound entity.
AudioEmitter emitter = new AudioEmitter(); AudioListener listener = new AudioListener(); Vector3 objectPos;
In the the LoadContent method, set the SoundEffectInstance object to the return value of SoundEffect.CreateInstance.
soundfile = TitleContainer.OpenStream(@"Content\buzz.wav"); soundEffect = SoundEffect.FromStream(soundfile); soundEffectInstance = soundEffect.CreateInstance();
Call Apply3D on the SoundEffectInstance, passing the emitter and listener.
soundEffectInstance.Apply3D(listener, emitter);
Set the IsLooped property to true before calling Play if you want the sound to repeat.
Call SoundEffectInstance.Play to play the sound.
soundEffectInstance.Play();
To process audio data
Set the Vector3 structure to the position from which you want the sound to come.
// Move the object around in a circle. objectPos = new Vector3( (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds) / 2, 0, (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds));
Set the AudioEmitter.Position property to this vector.
emitter.Position = objectPos;
As an option, set the Vector3 structure to the position where you want the listener of the 3D sound to be, and then set the AudioListener.Position property to this vector.
Call SoundEffectInstance.Apply3D on the cue object you retrieved previously, passing in the AudioEmitter, which is an AudioListener.
soundEffectInstance.Apply3D(listener, emitter);
Note
Calling the SoundEffectInstance.Apply3D method automatically sets the speaker mix for any sound played by this sound effect to a value calculated by the difference in Position values between listener and emitter. In preparation for the mix, the sound is converted to monoaural. Any stereo information in the sound is discarded.
Concepts
- Applying a 3D Positional Effect to a Cue (XACT)
Demonstrates how to apply a 3D positioning effect to a cue. - Playing a Sound
Demonstrates how to play a simple sound by using SoundEffect. - Looping a Sound
Demonstrates how to loop a sound. - Creating and Playing Sounds
Provides overviews about audio technology, and presents predefined scenarios to demonstrate how to use audio.
Reference
- SoundEffect Class
Provides a loaded sound resource. - SoundEffectInstance Class
Provides a single playing, paused, or stopped instance of a SoundEffect sound. - AudioListener Class
Represents a 3D audio listener. - AudioEmitter Class
Represents a 3D audio emitter.