Applying 3D Audio Effects (XACT)

This topic builds upon Applying a 3D Positional Effect to a Cue (XACT), which provides procedures to retrieve and play a cue in 3D and to process audio data. It also introduces Microsoft Cross-Platform Audio Creation Tool (XACT) projects, the audio engine, cues, and emitter and listener positions.

Three-dimensional game audio typically implements at least three effects: speaker positioning, volume attenuation over distance, and Doppler pitch shifting. The basic 3D audio how-to discusses speaker positioning. This topic explains how to add attenuation and Doppler effects to 3D audio.

At design time, the sound designer defines the attenuation and Doppler effects and applies them to specific sounds. The designer uses XACT runtime parameter controls (RPC) to specify the relationships between game information—exposed as XACT variables—and resulting sound effects. To determine final attentuation and Doppler effects at run time, the XACT engine combines the designer's effects with calculations based on position and velocity of the audio source and listener.

Sound designers can add attenuation without requiring any additional code changes by the developer (assuming that the game already sets emitter and listener positions). However, Doppler effects require that XACT know the velocity of the emitter and listener, so the game must set the AudioEmitter.Velocity and AudioListener.Velocity properties.

Complete Sample

The code in the topic shows you the technique for applying audio 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 Advanced3DAudio_Sample.zip

To add a velocity effect to audio

The following steps add some minor code modifications to the Applying a 3D Positional Effect to a Cue (XACT):

  1. Follow all the steps outlined in the Applying a 3D Positional Effect to a Cue (XACT) topic.

  2. In the Game.Initialize method, initialize the AudioEmitter and AudioListener positions.

    // Set emitter and listener position.
    emitter.Position = Vector3.Backward;
    listener.Position = Vector3.Zero;
    
  3. In the Game.Update method, add the following code to move the sound from left to right. This example uses the left and right triggers on the Xbox 360 Controller to move the sound.

    // Move the sound left and right out to maximum distance.
    emitter.Position = new Vector3(
        (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds / 5.0f) *
        maxEmitterDistance, 0.0f, 1.0f);
    
    // Add velocity with left or right triggers.
    emitter.Velocity = new Vector3(maxVelocity *
        GamePad.GetState(PlayerIndex.One).Triggers.Left, 0.0f, 0.0f);
    listener.Velocity = new Vector3(-maxVelocity *
        GamePad.GetState(PlayerIndex.One).Triggers.Right, 0.0f, 0.0f);
    

Concepts

Reference

Online Resources