Demonstrates how to create and use a streaming wave bank.
|This example assumes you already built an XACT sound bank and wave bank. To learn how to do this, see How To: Add a Sound File to Your Game Using XACT.|
The Complete Sample
The code in this tutorial illustrates the technique described in the text. 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 amount of time that a device takes to retrieve waves from memory and subsequently play the sound. If latency is substantial, it can cause delays in audio playback, or possibly it can interrupt game sounds altogether.
A wave bank may be designated as in-memory, which means all of the 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, the 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
Most often 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 the 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 the streaming wave bank in XACT is the same as the process described in How To: Add a Sound File to Your Game Using XACT, with the exception of one step. When creating the new wave bank, select thestreaming option in the dialog box.
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 will start 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 will result 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 will specify a packet size that uses the least amount of memory, while at the same time allows for uninterrupted sounds during game play.
|Before you execute code to play any sounds, you must call the Update method of the AudioEngine that was used to create the streaming wave bank. Calling the Update method prepares the streaming wave bank for use. If you try to use the wave bank before you call the Update method, an InvaidOperation exception will be thrown.|