Intigrading Audio: XNA (tutorial)

Source:
Professional XNA Game Programming for Xbox 360 and Windows
By Benjamin Nitschke

Section: Part III:
Improving Your Game Engine

Chapter 9:
Adding Sound with XACT

Integrating Audio into XNA
A tutorial by Ashraf Dawod

Integrating audio in the XNA frame work is done in five (5) stages according to the book, one thing they mention is the sound files have to be in .WAV format. So in actuality it should be six (6) stages, because your first step will be converting your audio files into WAV format. Here is a quick outline of this tutorial:
What is Xact Xactly?
Step 1: Converting your files to .WAV.
Step 2: Creating your wave bank.
Step 3: Compressing music data.
Step 4: Creating a sound bank.
Step 5: Cue Variables.
Step 6: Creating a sound class.


What is XACT Xactly?
XACT is the name for Microsoft’s Cross-Platform Audio Creation Tool, which allows you to create audio projects for the Windows and Xbox 360 platforms. The reason you can’t sue anything but XACT is the fact that you cannot use any unmanaged code in XNA. The reason why it was implemented into XNA is to make sure it is possible to use the same sound content and sound playback code for both the Windows and Xbox 360 platforms.
Note: Xbox 360 supports only XMA compression.
For the PC only ADPCM is supported in XACT.


Step1 : Converting your files to .WAV
There are several ways to do this, if you use iTunes, you could configure it to do the conversion for you. You can also go to download.com, and use programs like “Mp3Decoder”.
Here is how you convert you files using iTunes:
1- Start up "iTunes"
2- Go to "Preferences"
MAC: click 'iTunes' \ then preferences
PC: click 'File' \ then preferences
3- Go to the "Advanced" tab
4- Click the "Importing" tab
5- In the "Import Using:", use the drop down menu to choose "Encode WAV"
6- Click "OK" until you exit out of the preferences
7- Now simple choose a song, "Right Click" it, choose the option to convert it to a WAV

Where to find your converted files:
Navigate to it
You will need to navigate to where the original song is located.
For example:
I converted the song "Sunday Bloody Sunday", which is from the album "The Joshua Tree", from my favorite band "U2".
My Path would be:
iTunes \ music \ U2 \ The Joshua Tree \ [the wav file will be found here]


Step 2: Creating your wave bank
To get any wav files into your new XACT project you have to create a wave bank first, which stores all the wav files into a wave bank, and is then loaded and used by your game.
There are two (2) ways to do this:
Way #1: You can either right-click Wave Banks and select “New Wave Bank”.
Way #2: You can use the toolbar icon for creating a wave bank (eighth entry, the orange-white rectangle with the wave effect)
Note: you can have multiple Wave Banks, this is especially good if you have multiple levels in your game.

After you have created your Wave Bank you can drag and drop wav files into it or alternatively use the “Insert Wave Files” entry from the context menu.
Note: the italic red font indicates the files are still unused. As soon as you add these wav files to a sound bank they become green.

By default no compression is chosen and most of the sound files are pretty small. If you want to compress the music sound file, click it and select your desired compression preset in the wave properties panel, before you can choose a compression you will have to create a compression preset, which leads us into Step 3.


Step 3: Compressing music data
To create a new compression preset click Compression Presets and select “New Compression Preset”. Now you can enter a name for your compression; in this case you just want to create a music compression schema.
Note: XACT does not show the resulting file size of XMA right away, you will have to compile project first. It is recommended to use XMA files with 50% or higher file size.

After setting the new compression for the music file you should now get file sizes of around 5-6 MB for the Xbox directory and 10MB for the Win Directory.

Step 4: Creating a sound bank
In order to playback the music, you need sound cues, which have to be created in a sound bank, which we will create.
You can do this by clicking the Sound Banks entry or clicking the second orange orange icon in the toolbar (the one with the orange wave going over the orange-white box on the right side).
Note: If you just have one wave bank and one sound bank, then leave the default name “sound bank”, but if you have multiple wave banks then feel free to give them different names to be easily accessible for you when needed.

Try to position your windows in a way that will allow you to see both windows clearly, then drag all wave files from the wave bank over to your sound bank.
The sound bank window consists of two parts:
Sound names (on the top) & Cue names (at the bottom)
Note: Each sound here can get additional settings like changing the volume, frequency, or applying other variations or effects.

Here are some adjustments you might want to do as well:
-In the Sound Properties Panel (on the left) make sure that the Category is “Music” instead of “Default”
-Set instances limiting and fade in/out durations for your music files, either each single one, OR by simply setting it once in the “Music” category.
-“LimitInstances” is by default set to “false”, once you set it ot “true” the properties below it become active.
-“BehaviorAtMax” should be set to “Replace” instead of “FailToPlay”


Step5: Cue Variables
XNA allows you to access only sound cures, which have to be created first.
Select all sound names and drag them down to the cue section of the Sound Bank windows. F7 to compile.
Note: Names are automatically taken from the filenames, but you can rename any entry if you like.


Step 6: Creating a sound class
When creating the Sound Class we will define a function with all the sound cue names in it, this is the most valuable thing you can do for sound playback in XNA. Playing a sound cue is easy now with help of the “play” method in the Sound class.

I couldn't actually display the actual "code" here, because it interfered with Blogger's website script.
So I've added a screen shot of what the code looks like:




That concludes the end of the XNA integrating Audio tutorial.
I hope it has been helpful to you.
Have a great day.

-Ash

Comments

Popular posts from this blog

No more controllers for playing games

Look out iPhone, the G1 is coming. sw33t.