Chapter 2: Linear Playback
Okay, let's get cracking. The first thing we want to do is be able to play sound. We add variability, dynamism, and the rest later. Let's focus on playing back a wave file.
At this point, if you have not already, you must install DirectX 9.0 as well as DirectMusic Producer, which are available on the companion CD.
Waves in Wave Tracks
To play back a wave file in DirectMusic, you'll create a Segment out of it. As already mentioned, the DirectMusic Segment file (.sgt) is the basic unit of DirectMusic production. Segments are built from one or more of DirectMusic's various track types, which can make sound (via stand-alone waves and/or DLS instrument triggering) or modify performances (tempo, chord progression, intensity level, etc.). You can create a Segment out of a wave file in DirectMusic Producer. Run DirectMusic Producer and go to File>Import File into Project>Wave File as Segment…. Open a wave file into the program. Building a Segment from a wave file, we get our first look at one of the basic track types in DirectMusic — the Wave Track. Beyond the 32 variation buttons (which we cover in Chapter 3), Wave Tracks play along the sequencer timeline with other Wave Tracks and also with MIDI sequences.
If you look at the size of the Segment file, it is much smaller than the wave file. Here is an important early lesson in content delivery: Segments will know where the wave and sample files that it needs to play are but do not store those files as part of itself. There are several reasons to do this; if several Segments use the same wave data, you do not have to worry about having two copies of it in memory. In addition, if you later want to go back and edit that wave data, you do not have to worry about copying it into several different places. That said, there are various reasons that you might instead want to embed the wave data within the Segment itself — for instance, for the convenience of only having to deliver a single file or file load time considerations.
Files with the extension ".**p" are design-time files. Design-time files are used in DirectMusic Producer for editing purposes and contain information not necessary for run-time use — for instance, a Segment used in a game does not need to include information on what size and position to open editing windows. In addition, design-time files always reference the content they use, even if you specify that content should be embedded. For these reasons, when content is meant to be integrated into a game or a special player, you should save the Segment as a run-time file (either via the per-file right-click menu Runtime Save As… option or the global Runtime Save All Files option from the File menu). When Segments are run-time saved, you will see the .sgt extension, and wave files will similarly have the more expected .wav extension. So to summarize, be sure to save your Segments as design-time (.sgp) files while you are working on them and as run-time files (.sgt) when they are finished and ready for distribution. For more details on content delivery, there is a white paper available on the Microsoft Developer Network web site (msdn.microsoft.com) called "Delivering the Goods: Microsoft DirectMusic File Management Tips."
Streaming Versus In-Memory Playback
A minute-long 44.1 kHz 16-bit stereo wave file is 10MB. 10MB isn't a big deal when you run a stand-alone DirectMusic file on a contemporary computer, but it is a very large file in the world of game audio. Do not forget that the rest of a game's resources need to reside in memory as well. If the game is written to run on a system with as little as 64MB of RAM, you are already in way over your head. Console games are even more unforgiving. Consider yourself lucky if you get 4MB for your entire sound budget! You can use audio streaming to alleviate these restrictions. Streaming is a technique that works a lot like a cassette player. In a cassette player, audio data is moved across the play head a little bit at a time. The play head reads the data as it comes and plays the appropriate sound. With streaming, a small area in memory called a buffer is created. Wave files are moved, bit by bit, through the buffer and read by DirectMusic. The CD player in your PC uses streaming for playback. If it weren't for streaming, you'd have to load the entire music file into your computer's RAM, which in most cases simply isn't an option.
DirectMusic uses the following rule for streaming: The wave streams with 500 msec readahead if it is more than five seconds long. If it is shorter than five seconds, it resides and plays from memory (RAM). Readahead determines the size of the buffer. For 500 msec, our 44.1 kHz 16-bit stereo wave file will use 88.2KB of memory (.5 sec x 44100 samples/sec x 2 bytes/sample x 2 channels), a big difference when compared to 10MB! Memory usage is reduced by a factor of more than 100! You can override this behavior, choosing to load all wave data to memory, or specify a different readahead value in the Compression/Streaming tab of a wave's property page in DirectMusic Producer. To get to any Track's property page, simply right-click on it in the Track window.