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

Demonstrates how to apply 3D positioning effects to cues.

The XNA Framework audio system contains support for 3D audio positioning effects. It uses the AudioEmitter and AudioListener classes, and the Cue.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 automatically applied using this method. Attenuation and Doppler-shift pitch modification effects must be applied via creation of runtime parameter controls (RPC) in the Microsoft Cross-Platform Audio Creation Tool (XACT). See How To: Apply Attenuation and Doppler 3D Audio Effects and Apply3D for more information.

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 System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Basic3DAudio
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;

        // Audio objects
        AudioEngine engine;
        SoundBank soundBank;
        WaveBank waveBank;

        public Game1()
            graphics = new GraphicsDeviceManager(this);

        // 3D audio objects
        AudioEmitter emitter = new AudioEmitter();
        AudioListener listener = new AudioListener();
        Cue cue;

        protected override void Initialize()

            // Initialize audio objects
            engine = new AudioEngine("Content\\Audio\\3DAudio.xgs");
            soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb");
            waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb");

            // Get the cue and play it.
            // For 3D cues, you must call Apply3D before calling Play.
            cue = soundBank.GetCue("buzz");
            cue.Apply3D(listener, emitter);

        protected override void LoadContent()

        protected override void UnloadContent()

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

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

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

            // Update the audio engine


        protected override void Draw(GameTime gameTime)

Applying Basic 3D Positional Effects to a Cue

To retrieve and play a cue in 3D

  1. Create an XACT project and add it to a new XNA Game Studio project as described in How To: Add a Sound File to Your Game Using XACT.

    The project should contain at least one cue.

  2. In code, create an AudioEngine, WaveBank, and SoundBank at game start.

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

  4. In the Game.Initialize method, load the AudioEngine, WaveBank, and SoundBank.

  5. Call SoundBank.GetCue to retrieve the Cue you want to play in 3D.

  6. Call Cue.Apply3D on the cue you retrieved in the previous step.

  7. Call Cue.Play to begin playback of the cue.

    Note that you must have called Apply3D on the cue first. Otherwise, the next call to Apply3D will throw an exception.

To process audio data

  1. During game update, call the Update method of the AudioEngine to enable the audio engine to process audio data.

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

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

  4. 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.

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

Calling the Cue.Apply3D method automatically sets the speaker mix for any sound played by this cue 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