Chapter 7: DirectX Audio Scripting
DirectX Audio Scripting is another tool provided to composers and programmers working with DirectMusic. Rather than needing to write low-level code in order to control such things as Segment playback, the composer can build sets of instructions using a more basic language. While this language does not cover the full gamut of behaviors available to the programmer using the C/C++ DirectMusic programming interfaces, it provides the most common behaviors in an easier-to-understand format. The application then triggers the composer's sets of instructions at the appropriate time. This allows more of the music playback behavior to remain in the hands of the composer and in a form where the behavior can be adjusted more easily without having to recompile the application. The script can be updated in DirectMusic Producer, run-time saved, and dropped back into the application, and the new behavior will be used without any additional changes.
Defining the Audio Implementation Problem
While DirectMusic allows a composer or sound designer to create flexible, nonrepetitive audio content, the audio producer must still deal with the process of integrating that audio into the interactive application. Such integration is generally placed in the hands of the programmer. While the programmer no longer needs to be concerned with what exact content a piece of music (a DirectMusic Segment) plays, he must still manage quite a bit of information:
Dynamic volume and groove level changes for Segments
Managing AudioPaths that Segments are played onto (unless each Segment contains its own embedded AudioPath)
Playing Segments at musically appropriate boundaries (generally decided on by the audio producer)
Playing Segments as primary or secondary Segments
DLS Collection download/unload management
This can set up a significant workflow challenge — if the composer wants a piece of music to transition at a different boundary, he needs to communicate this to the programmer, wait for the application to be changed and its code rebuilt, and then he can proceed. This often leads to the same kind of slow turnaround process that we were trying to avoid, where very few changes will be able to be made simply because of the time involved with each iteration of the application.
Enter DirectX Audio Scripting. Using a basic, human-readable language, an audio producer can now author scripts, instructions for what behaviors should occur when a certain trigger is reached. Each "trigger" is a script routine, a list of instructions that this trigger should cause to occur — play a piece of music, stop another piece of music, adjust the musical intensity, change the volume, and so on. The programmer, rather than needing to implement these instructions, simply calls these script routines at appropriate points while the application runs.
DirectX Audio Scripting provides a subset of the features of the lower-level C/C++ DirectMusic programming interfaces. That is, anything you can do with scripting can be done in application code if desired. Scripting allows the composer to abstract more of the behavior away from the programmer. For instance, rather than the programmer needing to know that when a player enters a room, the ambience for this room should play, the intensity should be adjusted based on the number of enemies, and the previous ambience should fade out, he can just send a trigger called EnteredRoom, and the composer's script can give the instructions for what that means in terms of the audio.
The other powerful feature that DirectX Audio Scripting supplies is the ability to pass information back and forth between the application and the music. For instance, the programmer could tell the script what the current score is, and when the programmer triggers the routine called AnnounceScore, the sound designer could stitch together the proper dialog necessary to report the score. Or perhaps the crowd would react with more energy if the script was made aware of the time remaining and how far apart the two teams' scores are. All of this is accomplished by using scripting variables, where data can be passed in both directions; the script can tell the application when an event has occurred in the music, and the application can tell the script what the user is doing and the current state of the application.
But wait — isn't all of this scripting a form of programming? Are we turning the composer into a programmer? In some respects we are, but hopefully it is with a straightforward language that allows the composer to do more of what he wants without having to depend on the programmer. As you'll see below, many of the most powerful functions (namely, playing or stopping a piece of music) are very straightforward. Feel free to cut and paste from both of these examples, those in the demo project supplied with DirectMusic Producer, and sample scripts found in the DirectX Software Development Kit (SDK).