Chapter 6. Sound Effects and Audio
CS 583 11Feb2013

Section 6.1. Using XACT
Section 6.2. Implementing XACT Audio Files in Code
Ch6-1-AnimatedSprites.zip
Section 6.3. Using the Simplified API for Sound and Audio
Ch6-2-SimpleSounds.zip
Section 6.4. Adding More Sound to Your Game
Section 6.5. What You Just Did
Section 6.6. Summary
Section 6.7. Test Your Knowledge: Quiz
Section 6.8. Test Your Knowledge: Exercise
The key sounds provided for exploration:
start.wav
track.wav
skullcollision.wav

Chapter 6. Sound Effects and Audio

From Chapter 5 you learned to design your 2D game code to better handle sprites, allowing for user-controlled and automated sprites. Now we complete the game process, adding in sound. This chapter introduces the XACT engine or the simplied sound API which XNA can use.

Section 6.1. Using XACT

en.wikipedia.org/wiki/Cross-platform_Audio_Creation_Tool

The Cross-platform Audio Creation Tool (XACT) provides many of the resources of a sound studio for you to create just the right sounds for your game using .wav sound files. Our author provided several sounds files to use - linked above.

If you use XACT you should find it to be more robust but only runs with the HiDef platform. The simplied sounds API runs with both the HiDef and Reach platforms. If you are happy with simple sounds and do not want to use the high resolution graphics of the Windows or Xbox360, you might focus on Simplified Sound API in Section 6.3.

Handling sound it different from the 2D sprites you have manipulated before. You will use Windows Explorer to copy the .wav files to the project's Content\Audio directory. But you will not use Visual Studio to add the items to the project in Solution Explorer. We do not want the content pipeline to process sound, instead we rely on XACT. We will build an audio project with the sounds for the game using XACT and that XACT audio project will be processed by the content pipeline.

We start with the Animated Sprites from Chapter 5 code. Ch5AnimatedSprites.zip Copy the start.wav and track.wav files to project's AnimatedSpritesContent\Audio directory in Windows Explorer.

Then open that solution in Solution Explorer (VisStudio) and add a new folder where you’ll put all the audio files that you work with in this chapter. Do this by right-clicking the AnimatedSpritesContent project in Solution Explorer and selecting Add->New Folder. Name the new folder Audio.

To create the XACT audio project, begin by launching XACT by selecting

Start -> All Programs -> Microsoft XNA Game Studio 4.0 -> Tools -> Microsoft Cross-Platform Audio Creation Tool3 (XACT3)
but note:
On Windows Vista and Windows 7, you’ll want to launch XACT with
administrator privileges. To do that, right-click on the previously mentioned
Start menu item and select “Run as Administrator.”

When the XACT window opens, select File->New Project. Save your project in your XNA game project's AnimatedSpritesContent\Audio directory, and call it GameAudio.xap

Add the wave files to your wave bank by Right-click anywhere in wave bank window and select Insert Wave Files(s). Browser to start.wav and track.wav (in your game project's AnimatedSpritesContent\Audio directory and select them to add to wave bank. The result should be Fig 6-2.

Now look at the sound bank window. It is divided into two sections: the top section contains a list of sound names, and the bottom section contains a list of cue names. When dealing with audio using XACT in XNA, you use something called a cue to access audio and sound effects. In the XACT project file, cues are made up of one or more sounds, and sounds are made up of one or more wave bank entries. Cues and sounds are listed in the two sections of the Sound Bank window, and wave bank entries are listed in the Wave Bank window.

You need to create a cue and a sound entry for your start.wav file and your track.wav file. To do this, drag both items from the Wave Bank window (where you just added them) and drop them into the Sound Bank window, in the Cue Name section.
NOTE: If you drop the wave entries into the Sound Bank window in the Sound Name section, XACT will only create a sound name for your wave bank entries. You need a sound entry and a cue entry for each wave bank item, and dropping the wave bank items into the Cue Name section of the Sound Bank window will create both of these for you.

We can proceed with including the XACT project file into your XNA game. We skip the testing of the sounds in XACT for now. If you wish to having looping in your game you will want to work through all of 6.1 carefully.

With the Auditioning Utility running, you can right-click on any sound, cue, or wave within XACT and select Play to hear the sound.

In addition to playing sounds within XACT, you can modify some properties of the sounds themselves. When you select a sound name in the sound bank, XACT will display properties for that sound in the lower-left pane of the XACT project window. You’ll notice some options here that allow you to modify the volume, pitch, priority, and looping for that sound. In regard to this current project, the start.wav file may be a bit quiet for some people’s tastes, so feel free to modify the volume and play the sound until you find a volume that sounds good to you.

You want your track.wav file to loop indefinitely in your application, and you can change a setting within XACT to accomplish that without any need for extra coding in XNA. To set the looping property of the track sound, select the item named track in the Sound Name section of the Sound Bank window. In the properties pane located in the lower-left corner of the project window, click the Infinite checkbox under the Looping section (see Figure 6-5). This setting will cause the sound to loop indefinitely when you start the associated cue from your XNA code.

Finally, if you click on a cue name rather than a sound name in the Sound Bank window, you’ll see some different options in the lower-left pane of the XACT window. This pane allows you to modify how the cue will play different sounds associated with that cue. Currently, you only have one sound associated with this cue, but you can add as many as you like. If you have multiple sounds for a cue, XACT will select a different one of those sounds to play each time you call that particular cue from your XNA code. This is helpful for sounds such as explosions or crashes that are similar yet slightly different from each other. For example, explosions.

Section 6.2. Implementing XACT Audio Files in Code

The only file that you actually need to include in the XNA game project is the project file you created in XACT. Hopefully, you named the XACT project file GameAudio.xap and saved it in the project's AnimatedSpritesContent\Audio directory. If so, add it to your project now by right-clicking the AnimatedSpritesContent\Audio folder in Solution Explorer, selecting Add->Existing Item, and browsing to your GameAudio.xap file.

To load the data from your XACT project file into objects that you can use to play the sounds in XNA, you need to add the following class-level variables at the top of your Game1 class:

 
AudioEngine audioEngine;
WaveBank waveBank;
SoundBank soundBank;
Cue trackCue;

Please carefully read section 6.2 for the details on how the XNA content pipeline incorpoates the sound files.

Section 6.3. Using the Simplified API for Sound and Audio

When developing with the HiDef game profile, it can save time and add flexibility to your game audio if you take advantage of the benefits that XACT offers. However, XACT isn’t supported on the Reach game profile, so the XNA Framework 4.0 provides a simplified sound API that allows developers to play audio when using Reach. You can also use the simplified API in projects developed with the HiDef profile if you don’t require the additional features provided by XACT.

Close your current XNA game project and create a new XNA Windows Game project in Visual Studio called SimpleSounds.

To play a sound using the simplified sound API, the first step is to add a sound file to your project. Remember that when dealing with XACT, the actual sound files themselves are not added to the project in Visual Studio. That is not the case, however, when dealing with the simplified sound API. In this case, audio files are treated like other resources in the content pipeline and have to be added to the Visual Studio project just as you’ve done with your 2D images thus far in this book.

The sound API in XNA 4.0 supports the .wav, .wma, and .mp3 file types. In this example, you’ll be using the start.wav file from the previous example in this chapter.

Section 6.4. Adding More Sound to Your Game

In the game that you’re building, a user-controlled sprite will be moving around the screen, with the objective of avoiding the automated sprites that are flying in from all directions. (That’s right, plunk your money down now; this is going to be one amazing game.) You’re moving along in that direction, and you’ll get there soon enough. Even though the automated sprites in the game currently don’t move, you can still add some code to play a sound effect whenever your user-controlled sprite collides with an automated sprite.

You’ll be passing the name of a cue to be played in the event of a collision into each Sprite object, so you’ll first need to open your Sprite.cs file and add to the Sprite class a class-level variable that will hold the name of the cue to be used. In addition, you’ll need to use the auto-implemented properties feature of C# 3.0 to create a public get accessor and a private set accessor for this variable:

public string collisionCueName { get; private set; }
If you’re new to C# 3.0 and are unfamiliar with this feature, auto-implemented prop- erties allow developers to create accessors for a given variable at the point in code where the variable is declared. This streamlines the code, making it easier to implement and read. (Feel free to read up on auto-implemented properties further on the Internet if you’d like to find out more about this or other features added in C# 3.0.) Finally, add a parameter of type string to the end of the parameter list in both con- structors. In the first constructor, pass the new parameter on to the call to the second constructor. In the body of the second constructor, assign the new parameter’s value to the collisionCueName variable. Your new Sprite class constructors should look like this:
Sprite.cs


Section 6.5. What You Just Did

•You added sound effects to your game using XACT.
•You added a looping background soundtrack to your game using XACT.
•You learned how to add sound using the simplified sound API.
•You fine-tuned some sound code to add sound effects for colliding sprites.

Section 6.6. Summary