Applying a 3D Positional Effect to a Cue (XACT)

XNA Game Studio 4.0

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.

The speaker mix is the only effect that is 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 Applying 3D Audio Effects (XACT) and Apply3D for more information.

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

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 Adding a Sound File (XACT).

    The project should contain at least one cue.

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

    // Audio objects
    AudioEngine engine;
    SoundBank soundBank;
    WaveBank waveBank;
  3. Create an AudioEmitter, an AudioListener, and a Cue to store the 3D position of the sound entity.

    // 3D audio objects
    AudioEmitter emitter = new AudioEmitter();
    AudioListener listener = new AudioListener();
    Cue cue;
  4. In the Game.Initialize method, load the AudioEngine, SoundBank, and WaveBank.

    engine = new AudioEngine("Content\\3DAudio.xgs");
    soundBank = new SoundBank(engine, "Content\\Sound Bank.xsb");
    waveBank = new WaveBank(engine, "Content\\Wave Bank.xwb");
  5. Call SoundBank.GetCue to retrieve the Cue you want to play in 3D.

    cue = soundBank.GetCue("buzz");
  6. Call Cue.Apply3D on the cue you retrieved in the prior step.

    cue.Apply3D(listener, emitter);
  7. Call Cue.Play to begin play back of the cue.

    You must have called Apply3D on the cue first. Otherwise, the next call to Apply3D throws an exception.

To process audio data

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

    // Move the object around in a circle.
    Vector3 objectPos = new Vector3(
        (float)Math.Cos(gameTime.TotalGameTime.Seconds) / 2,
  2. 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.

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

    cue.Apply3D(listener, emitter);
  4. During game update, call the Update method of the AudioEngine to enable the audio engine to process audio data.

    // Update the audio engine
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.


Applying a 3D Positional Effect to a Sound
Demonstrates how to apply 3D positioning effects to a SoundEffect.
Adding a Sound File (XACT)
Demonstrates how to add wave (.wav) files to an XACT project that can be built and interpreted by an XNA Game Studio game to play audio.
Applying 3D Audio Effects (XACT)
Demonstrates how to apply attenuation and Doppler 3D audio effects in code.
Audio Attenuation and Doppler Pitch Shifting Overview (XACT)
Provides an overview of attenuation and Doppler pitch shifting.
Getting Started with XACT
Introduces the Microsoft Cross-Platform Audio Creation Tool (XACT).


AudioListener Class
Represents a 3D audio listener.
AudioEmitter Class
Represents a 3D audio emitter.
Cue Class
Defines methods for managing the playback of sounds.
AudioEngine Class
Represents the audio engine. Applications use the methods of the audio engine to instantiate and manipulate core audio objects.

Community Additions