Streaming a Sound (XACT)
|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.|
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.
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.
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
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
Under General, select Wave Bank in the Name box.
- Under Type, click Streaming.
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
Create the streaming wave bank.
Call SoundBank.PlayCue to begin playback.
|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.|