Bagikan melalui


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

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.

Download Basic3DAudio_Sample.zip

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.

    cue.Play();
    

    Note

    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,
        0,
        (float)Math.Sin(gameTime.TotalGameTime.Seconds));
    
  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
    engine.Update();
    

Note

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.

Concepts

Reference