The preferred method for doing audio in XNA is Microsoft's tool called XACT (Cross-platform Audio Creation Tool). XACT is free and ships with XNA Game Studio, so you should already have it on your computer. In this tutorial, we will look at how to use XACT. In the next tutorial, we will see how to use an XACT project to play audio in a game.
XACT was designed as a way of separating the audio engineer's job from the programmer's job. It is pretty nice, because your sound effect people, and your composers can deal with this application, and not have to worry about any coding. Meanwhile, your programmers can worry about how to implement the audio engine without having to worry about how the sound is made.
XACT has a lot of built in capabilities, like repeated playbacks, variations in a sound effect (so it doesn't sound the exact same every time you play it), and 3D sound effects (sound that is farther away is quieter, sound that is on the left side comes out of the left speaker, etc.). We will look at these in other tutorials, but for now, we're just going to concentrate on the basics of building an XACT project.
Since XACT comes with XNA Game Studio, you already have it on your computer. You should be able to find it in the Start menu, in the same directory as XNA Game Studio 4.0 (or whatever version you're using) under Tools. (So look for XNA Game Studio 4.0 > Tools > Microsoft Cross-Platform Audio Creation Tool 3 (XACT3)).
This should open up XACT.
Loading .wav Files
The first step of our project is to acquire a .wav file for use. If you need audio for your game, see my page on suggestions for audio in XNA. I've downloaded an explosion sound effect from www.flashkit.com called powerup.wav, which I will be using in this tutorial.
As you are getting set up to start your new XACT project, there is one issue that we want to deal with. Sometimes the path for an audio file can cause problems. To avoid any problems like this, I would recommend you create your XNA Game project before you start working with XACT. Place your audio files in the directories that they are going to be in at the end. This way, you don't have to worry about any sort of problems that may arise from the audio files not being in the location that your project is looking for them at. If you must change the path of your audio files, you will most likely need to go back and edit the XACT project so that it is looking for them in the new spot.
The next step is to load the .wav file into XACT. The first thing we will need to to is create a new project. Go to the File menu and select New Project. This will open up a window to indicate where to save the project to. Browse to your game's project folder and find the Content folder there. If you have created a subdirectory for your audio, you will probably want to save the project there, otherwise, create the new project in the Content folder. At this point, you will also need to give the project a name. I've called mine SoundDemoAudio.xap. (My XNA Game project is called SoundDemo, and the .xap extension is the standard extension for XACT audio projects.) Once you've done that, select Save and the new project will open up.
In the main XACT window, on the left hand side, you should see a window that looks like the image below:
We are going to be working on a new project, which is at the root of the tree structure. You can see from the tree structure that projects consist of wave banks and sound banks. There are a lot of other options available in an XACT project, but we will discuss these in another tutorial.
A wave bank consists of the actual raw audio data. We will need to create a new wave bank for our project. You can do this by right clicking on the Wave Banks node and selecting New Wave Bank. We will also need to create a new sound bank, so make a new one of those by right clicking on the Sound Banks node and selecting New Sound Bank from the menu that pops up.
Notice that your new wave bank is called "Wave Bank" and your new sound bank is called "Sound Bank". We will need to remember these names for later. (Of course, you can name them whatever you want, but you'll just have to keep track of the name you chose.)
Two new windows should be open now. One for the wave bank, and one for the sound bank. In the wave bank window, right click anywhere in the middle of the window. A popup-menu will appear. Choose **Insert Wave File…" from this popup menu. A dialog box will appear that will allow you to browse for your .wav file. Find the file you want and select it. The file will now appear in the list in the wave bank window.
We now need to create a sound and a cue for our .wav file. We can accomplish both of these in one step, by dragging the .wav file from the wave bank window to the lower part of the sound bank file, where it is labeled Cue Name. This will create both a cue, and a sound to go with it.
You can add multiple .wav files in the same way.
Save your project again, with the new changes.
We have now created a basic XACT project that we can use in our game. The next step is to use the project in our game to play back sound. The Playing Sound Tutorial will cover how to do that.
|Having problems with this tutorial? Try the troubleshooting page!|