Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Streaming a Sound (XACT)

Demonstrates how to create and use a streaming wave bank. This topic builds on concepts in Sounds Overview and Adding a Sound File (XACT).

Dd231917.note(en-us,XNAGameStudio.41).gifNote
This example assumes you already built a Microsoft Cross-Platform Audio Creation Tool (XACT) sound bank and wave bank. If not, see Adding a Sound File (XACT) to learn how to do this.

Complete Sample

The code in this tutorial illustrates the technique for creating and streaming a wave (.wav) file. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

Latency

In this context, latency is the length of time it takes a device to retrieve waves from memory, and, subsequently, to play the sound. If latency is substantial, it can cause delays in audio playback, or possibly interrupt game sounds altogether.

Why Stream?

A wave bank may be designated as in-memory, which means all waves are loaded from the storage medium into game memory. Otherwise, a wave bank may be designated as streaming, which means that waves remain stored on disk. When a wave bank is designated as streaming, sound information is delivered to the game through in-memory buffers. This is useful for sounds that are too large or too long to completely load into working memory. Streaming allows a game developer to effectively use large amounts of audio data without using too much memory.

When to Stream

Usually, it is better to store wave banks in-memory. When waves are in-memory, the playback of the sound always occurs without latency. However, in cases when sounds are too large to load into memory, a streaming wave bank may be a better choice. For example, this could be the case with background music that is much longer than other game sounds, such as short sound effects.

Creating a Streaming Wave Bank

With XACT

The process for creating a streaming wave bank in XACT, with the exception of one step, is the same as the process described in Adding a Sound File (XACT).

To create a streaming wave bank

  1. Under General, select Wave Bank in the Name box.

    Dd231917.PG_Audio_StreamingWaveBankXACTDialog(en-us,XNAGameStudio.41).png

  2. Under Type, click Streaming.

In Code

You can create a streaming wave bank by using the WaveBank (AudioEngine, String, Int32, Int16) constructor that requires two streaming-related parameters: offset and packet size. An offset of zero starts the stream at the beginning of the wave file. Packet size determines how much memory a streaming wave bank uses for buffering. A smaller packet size results in smaller amounts of data streaming through the buffer at any given time. A larger packet size indicates a larger amount of data moving through the buffer. Ideally, a game developer specifies a packet size that uses the least amount of memory, and at the same time allows for uninterrupted sounds during gameplay. The minimum value for packet size is 2, which results in a packet size of 4,096 bytes. The optimal packet size is a multiple of 16 (1 DVD block = 16 DVD sectors).

To create a streaming wave bank

  1. Allocate an AudioEngine, a SoundBank, and a WaveBank.

    // Audio objects
    AudioEngine engine;
    SoundBank soundBank;
    WaveBank waveBank;
    
  2. In the Game.Initialize method, load the AudioEngine and SoundBank.

    engine = new AudioEngine("Content\\PlaySound.xgs");
    soundBank = new SoundBank(engine, "Content\\Sound Bank.xsb");
    
  3. Create the streaming wave bank.

    waveBank =
        new WaveBank(engine, "Content\\Wave Bank.xwb", 0, 4);
    
  4. Call the AudioEngine.Update method of the AudioEngine during initialization to allow the audio engine to process audio data.

    engine.Update();
    
  5. Call SoundBank.PlayCue to begin playback.

    // Play the sound.
    soundBank.PlayCue("kaboom");
    
Dd231917.note(en-us,XNAGameStudio.41).gifNote
Before you execute code to play any sound, you must call the Update method of the AudioEngine that was used to create the streaming wave bank. Calling the AudioEngine method prepares the streaming wave bank for use. If you try to use the wave bank before you call the Update method, an InvalidOperation exception is thrown.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.