How To: Apply Basic 3D Positional Effects to a SoundEffect

Demonstrates how to apply 3D positioning effects to SoundEffects.

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.

Note that speaker mix is the only effect that will be applied automatically using this method.

The 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. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Basic3DAudioWithoutXact
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        AudioEmitter emitter = new AudioEmitter();
        AudioListener listener = new AudioListener();
        SoundEffect soundEffect;
        SoundEffectInstance soundEffectInstance;

        protected override void LoadContent()
        {
            soundEffect = Content.Load<SoundEffect>("buzz");
            soundEffectInstance = soundEffect.CreateInstance();

            soundEffectInstance.IsLooped = true;
            soundEffectInstance.Apply3D(listener, emitter);
            soundEffectInstance.Play();
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
                ButtonState.Pressed)
                this.Exit();

            // Move the object around in a circle.
            Vector3 objectPos = new Vector3(
                (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds) / 2,
                0,
                (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds));

            // Apply 3D settings to the cue.
            emitter.Position = objectPos;
            soundEffectInstance.Apply3D(listener, emitter);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
        }
    }
}

Applying Basic 3D Positional Effects to a SoundEffect

To retrieve and play a SoundEffect in 3D

  1. Add a WAV file to a new XNA Game Studio project as described in How To: Play a Sound.

    The project should contain at least one SoundEffect.

  2. In code, create a SoundEffectInstance for the SoundEffect when it is initially loaded.

  3. Create an AudioEmitter, an AudioListener, and a Vector3 to store the 3D position of the sound entity.

  4. Load the SoundEffect, and create a SoundEffectInstance to play the sound.

  5. Call Apply3D on the SoundEffectInstance, passing the emitter and listener.

  6. Call Play to play the sound. Set the IsLooped property to true before calling Play if you want the sound to repeat.

To process audio data

  1. Set the Vector3 to the position from which you want the sound to come.

  2. Set the AudioEmitter.Position property to this vector.

  3. As an option, set the Vector3 to the position where you want the listener of the 3D sound to be, and then set the AudioListener.Position property to this vector.

  4. Call SoundEffectInstance.Apply3D on the cue object you retrieved previously, passing in the AudioEmitter, which is an AudioListener.

Bb447687.note(en-us,XNAGameStudio.31).gifNote
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.

Community Additions

ADD
Show: