Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Jumat, 04 April 2008

Sounds and Music

Stop and think for a moment what your favorite game
would be like if it did not have sound. Music sets the
atmosphere for our games and sound effects adds to the
realism of our games. In this chapter we discuss how to get
music and sounds into our demos and games.
To do this we will need to use the Microsoft Cross-Platform
Audio Creation Tool (XACT), which Microsoft provides for
us. The tool can be found in our Programs/Microsoft XNA
Game Studio Express/Tools menu. Once we have the tool
opened we will create wave banks and sound banks and
discuss global settings. We will then actually create a sound
manager that we can add to our library

Microsoft Cross-Platform Audio
Creation Tool (XACT)

Unlike textures and 3D models, we do not simply put our
raw wave files into the Content Pipeline directly. Instead
we use XACT to bundle them together and add effects to
the different sounds. The XACT tool allows us to associate
categories with our sounds. When we set a sound to have a
category of Music, it allows the Xbox 360 to ignore the
music if the gamer has a playlist playing on his or her
Xbox 360.
Before we open the actual tool we need to open up the
XACT Auditioning Utility, which is found in the same location
as XACT itself. We only need to do this if we actually
want to audition (listen to) the sounds we are making
inside of XACT. This can, of course, be beneficial, especially
when we want to add effects to the file. With that said,
XACT is not a sound editing software package. It takes
completed wave files and puts them in a format that XNA
can read and use. We can do simple effects like change the
124 CHAPTER 7 Sounds and Music
pitch and volume but the tool is not designed to be a wave file editor.
We first need to hook our XACT tool up to the Auditioning Utility we launched before.
The Auditioning Utility must be run before the XACT tool is run. To play our sounds we
need to tell XACT to connect to the Auditioning Utility by clicking the Audition menu
item and then clicking the Connect To (machine) item. After we have successfully
connected, the Auditioning Utility will say “XACT tool is now connected….”
When we open the tool we will see an empty project to which we can add .wav files.
After adding the files we can then set them up as sounds and create cue names for them
that we can kick off inside of our code. We can modify properties to get different effects
from the sounds.
Wave Banks
To get started we can create a new wave bank from inside XACT. To create a wave bank,
we can follow these steps:
1. Right-click Wave Banks and select New Wave Bank. This can be seen in Figure 7.1.
2. Inside of the Wave Bank empty pane, right-click and select Insert Wave File(s).
3. Find a wave file from the Spacewar project. We can use the Theme.wav file from
MySpacewarWin1\Content\Audio\Waves\Music.
FIGURE 7.1 The XACT tool allows us to add wave banks to utilize the sounds in our games.
4. Because we have our Auditioning Tool set up and XACT connected to it, we can
simply select the wave file and press the spacebar to hear it play. We could also
right-click and select Play, or click Play in the toolbar. As with most graphical user
interfaces (GUIs) there are many ways to accomplish the same task. This book only
lists one way in most cases.
We can go ahead and add in a few other wave files as well. Let’s follow the preceding
steps to enter all of the collision wave files from Spacewar. We can open them all up at
one time in the Open dialog box.
Sound Banks
Even though we have the wave file loaded inside our XACT project, we still could not use
it in our game. We have to create a sound bank first. To create a sound bank we can
follow these steps:
1. Right-click Sound Banks and select New Sound Bank. Just like the wave bank, we
could also accomplish this through the toolbar or the main menu items.
2. Our work pane just filled up with our Sound Bank window. We will need to work
with the Wave Bank window as well as the Sound Bank window so we will want to
position them so we can see them both. One way to do this is to click Tile
Horizontally inside of the Window menu item.
3. Now that we can see both windows we need to drag our wave file from the wave
bank into the sound bank, inside the top left pane (sounds frame).
4. A cue for the sound needs to be created in order for our code to utilize it. We can
create a cue by dragging the sound from the top left pane (sounds frame) into the
bottom left pane (cues frame).
This is the bare minimum we need to do to play sounds in our games. To hear the sound
we can select it and press the spacebar. We can press Escape to stop the sound. To get
sound effects and music into our games, these steps will work. In the next sections we
discuss more advanced ways to manipulate our sounds to get them ready for our game.
Understanding Variations
To accomplish something more than just playing sounds we need to understand variations.
With variations we can assign many waves to a track and we can create different
events for those tracks to create a sound. We can assign many sounds to a cue. We can
then set up how those sounds or waves are to be played. We will be utilizing XACT to
create different variations and then we are going to write a library component that a
demo can use to play these cues.
We can close out our existing XACT instance and open up a new one. We need to create a
wave bank and a sound bank to put our waves in. On this book’s CD, under this chapter’s
folder there is a subfolder entitled Sounds. We need to add all of these waves into our
Understanding Variations 125
7
wave bank. After adding in the waves we need to create some sounds. We are going to
add the different sounds and create different variations so we can learn how to use some
of the XACT features by example.
1. We can drag the Attention and Explosion sounds directly into our cues frame
(bottom left pane) because we are going to play these sounds as is.
2. We can drag the Axe_throw sound directly into our cues frame and rename the cue
to Bullet. We can do this by right-clicking the name in the cues frame and selecting
Rename. At this point, the screen should resemble Figure 7.2.
126 CHAPTER 7 Sounds and Music
FIGURE 7.2 Wave files can be dragged directly into the cues frame.
3. To start our more complicated tasks let’s drag our CoolLoop wave into our cues
frame.
4. In the top right pane (tracks frame) we can see that XACT created a track with a
play event that include our wave name. We want to make sure this sound loops so
we click the Play Wave tree node of the Track 1 root to select it.
5. In the properties window we can see LoopEvent under the PlayWave Properties. The
default value is No but we could either loop it a certain number of times by selecting
Finite and then entering the number of times to loop in the LoopCount property
or we can have it loop forever by selecting Infinite. We are going to let this
sound loop forever so we select Infinite. We can see this in Figure 7.3.
FIGURE 7.3 We can set our play event on a track to loop indefinitely.
6. Because this loop is going to be music we can click the CoolLoop’s sound icon and
drag it on top of the Music category on the left side of the window. When this is
successful we will see the Category change from Default to Music.
7. Next up we are going to add multiple sounds into one cue. We will use
Synth_beep_1 to start us off. Let’s drag that wave into our cues frame.
8. Now we want to make sure that the sound name is selected (in our sounds frame) so
the track frame is showing the play event for track 1.
9. We need to drag Synth_beep_2 and Synth_beep_3 waves and drop them on our play
event inside of track 1. We need to be careful to not drag over the cues frame as it
will make the tracks frame empty (it deselects the sound from the sound pane). We
drag the files by clicking on the icon (XACT does not allow us to drag by the name
of the wave). We can see where we need to release the cursor in Figure 7.4. The final
result after releasing the cursor and completing our drop operation can be seen in
Figure 7.5.
10. Rename this cue from Synth_beep_1 to Hit.
11. With Hit as our active cue, we can press the spacebar to hear it play (assuming we
are connected to the Auditioning Utility). Let’s hit the spacebar several times in a
row very fast. We can hear one sound being played every time we hit the spacebar
regardless if the previous sound was played. We see they are not played at the same
time by “pressing play” once. By putting our different waves directly into a play
event we are giving XACT a list of waves to play from when we call play. It does not
play them all at once (but we will see how to do that in a moment).
Understanding Variations 127
7
12. Because we do not want this particular cue to play more than one sound at a time
we can change the LimitInstances property in the Instance Limiting Properties
section of our properties window to True. We will need to have our cue name
selected to do this. Now when we press the spacebar multiple times it will not start
playing until the last sound has finished.
13. We can hear the sounds are random but we want them to play in the order we specified
in the play event of our track. To do this we need to select Ordered in the
PlayListType property in the Variation Playlist Properties section of the property
frame. We need to do this when we have the cue selected. Changing this value to
Ordered causes XACT to play the sounds in the order we specified. If we wanted it
to start with a random entry and then do them in order, we could have selected
Ordered From Random.
14. Now when we play the sounds, they play in order and only play one at a time.
However, we want the sounds to be queued up so that if we press the spacebar five
times, it will play all of the waves in the sound with each one starting as soon as the
previous one finishes. To do this we need to change the BehaviorAtMax property to
Queue instead of FailToPlay. This can be useful for queuing up things like voiceover
audio that we would never want to play simultaneously. This is shown in Figure 7.6.
Understanding Variations 129
7
15. Now we are going to make a crash cue. To start, let’s drag Ambulance_siren from our
waves into our cues and rename the cue to Crash.
16. Inside of our tracks frame, with our Ambulance_siren sound selected from the sound
frame, right-click and add a new track.
17. Now we can add a play event to the track we just added by right-clicking the track
and selecting Add Event > Play Wave.
18. We need to drag the Car_brake wave into the play event of the track we just created.
Remember not to drag across the cue frame, as the contents of the track frame will
disappear.
19. We need to repeat steps 16 through 18 with the wave files Bus_horn and Explosion.
When this step is completed we should have a total of four tracks, each with its
own play event that has a wave file associated with it. We have expanded the Sound
Banks window in Figure 7.7 so we can see the end result.
130 CHAPTER 7 Sounds and Music
FIGURE 7.7 We can create multiple tracks for a sound, each with one or more events in our
tracks frame.
20. Now if we play our Crash cue we will hear all four sounds at the same time. We
want them to be spaced out as to simulate an accident. We can delay the time at
which different tracks start to play by setting the TimeStamp property in the Timing
Properties section of our property frame when we have the play wave event selected.
Our Car_brake wave can be left alone as it is a little longer of a sound file. We
should modify the rest of the values as follows:
Bus_horn: 0.300
Explosion: 1.000
Ambulance_siren: 1.600
Now when we play the cue we can hear something that resembles a car colliding
with a bus, creating a large explosion and the fastest EMT response time ever!
21. Now we want to make a variant of our explosion sound. We are going to make a
gunshot sound without requiring an additional wave file to be used. To do this we
are going to drag our Explosion sound (top right pane) into the empty white space
inside of the same sound pane so it will create a copy of itself. It should have called
itself Explosion 2. Let’s drag that Explosion 2 from the sounds frame into our cues
frame and rename the cue to Gunshot.
22. When we play Gunshot, it does not sound any different than Explosion. Let’s
change that by adding a Pitch event to our only track for the Explosion 2 sound in
our tracks frame. We add this event just like we added the play event earlier, by
right-clicking the track and selecting Add Event > Pitch.
23. Now we need to actually modify the pitch to get the desired sound effect. To do this
we need to make sure our Pitch event is selected and then change the Constant
value. We can find this value in our properties frame under Pitch Properties/Setting
Type (Equation)/EquationType (Constant). Let’s change the pitch to 50.00 as shown
in Figure 7.8. Now if we play the cue we will hear something that resembles a
gunshot instead of an explosion. We did not have to add another large wave file to
accomplish this effect.
Understanding Variations 131
7
24. Next, we are going to create a cue with multiple sounds. We need to right-click
inside of our cues frame and select New Cue. We can name this cue Complex.
25. Drag the Explosion sound down on top of the Complex cue. Let’s do the same thing
with the Synth_beep_1 sound. Remember, this Synth_beep_1 sound has its own
sound variations already as it plays three different waves in order.
26. We need to change the PlayList type for this Complex cue to Random. This way it
will just play a sound randomly even if the one it picks was just played.
27. The final thing we will do is add a music playlist. Unfortunately, XACT does not
have a way to give us typical playlist functionality so we will have to put that into
our code. For now, though, we can at least add our songs to this playlist. We will
start by dragging our Song1, Song2, and Song3 waves into our sounds frame.
28. We will also drag those sounds to our music category.
29. Now we need to drag our Song1, Song2, and Song3 sounds from our sounds frame
into the cues frame.
30. We can now save our project, calling it Chapter7.
That was a lot of examples, but it was worth going through because at this point we have
good idea of how to do a lot of sound manipulation to prepare cues for our games. Our
games will always reference the cue value.
There are other actions XACT allows us to do, such as setting local and global variables,
setting up transitions through interactive audio settings, and setting up runtime parameter
controls (RPCs).
We can create RPCs when a simple pitch or volume change across the board will not do.
Perhaps we would like to add some reverb, or maybe we would like to modify the volume
as a cue plays up and down. Doing any of these things requires setting up an RPC, which
can be done by right-clicking the RPC tree node and selecting New RPC Preset as shown
in Figure 7.9.
Then we can drag that preset over to one of our sounds in the sounds frame. To test with,
we can make a copy of our CoolLoop sound and then add our preset to this new sound
by dragging the RPC on top of the new sound we just created. We can open the RPC
preset by double-clicking it. Because there is only one sound it is associated with, it will
play that one. At the top of the RPC dialog box we can select other sounds (if we have
added the RPC to multiple sounds). A default volume curve has been added for us. We
can move the points around and then the vertical horizontal bar that is the same color as
our curve can be moved left and right. The bar can be moved or we can modify the variable
above beside the curve declaration. This is the variable that we can change in our
code to cause the sound to react exactly how we want. We might want to do this, for
example, to dim the background music when dialogue is happening between characters.
We can pass a value to the global variable that will produce the sound identical to what
we are hearing as we audition the sound with our RPC added. We can add more curves
(pitch and reverb) by right-clicking the Volume item at the top of the dialog box and
selecting Add New Curve. We can add nodes to our curve by right-clicking inside of the
main white area and selecting Add. A screen shot of this dialog box is shown in Figure
7.10. Have fun coming up with a totally different CoolLoop 2 sound!
7
We can pause and resume all sounds in a category. This means that we could organize our
sounds in such a way that sounds that are used in our playing state can all be assigned to
a category we can define. Then through the code we can pause that category and it will
pause all sounds that are playing. This way we do not need to worry ourselves with
making sure all the sounds stop when someone pauses the game. When we associate a
sound with the Music category the Xbox 360 can mute that and replace it with the
playlist the gamer has his or her console playing at the time. This is a really nice feature
because no matter how good our soundtrack is, at some point gamers will most likely
want to play our games with their own music in the background.

0 komentar: