Chase's Techno Rants and Raves

Umm.. did I just hear that guy call it "C-Pound?!"

  Home  |   Contact  |   Syndication    |   Login
  19 Posts | 0 Stories | 27 Comments | 5 Trackbacks

News

Me

Archives

Image Galleries

As I promised in my last post I am going to demonstrate how much better it is to use Audio with the latest XNA bits from Microsoft. 

The XNA 3.0 CTP can be downloaded here...

http://www.microsoft.com/downloads/details.aspx?FamilyId=DF4AF56A-58A7-474C-BFD0-7CF8ED3036A3&displaylang=en

 

The audio support with XNA prior to this release was limited to say the least.  You had to use a tool called XACT to compile audio resources and use them in your game using the Audio Engine, Wave Bank and Sound Bank classes.  The API was easy enough - the XACT tool however has challenges of its own.  I have found the tool to be buggy and intermittent in expected behavior despite its ease of use.  It's really is a crap shoot when you start using this tool.  In that tool's defense however, it did what it was set out to do when it worked.  But when a bad relationship comes to an end we must move on. 

Now for the good stuff.

One of the touted features of XNA 3.0 Audio is the ability to play MP3 files.  This is a great advancement in XNA development as it now allows developers to use a more compact format when introducing Audio into their games.  Since most music files these days are MP3 (or at least that is the accepted format) this is a very nice feature to have.  Here is an example of how to load and play an MP3 file.

 

Playing MP3 Audio.

The first thing you need to do in your XNA 3.0 project is add an MP3 resource for audio.  It is consistent with the behavior of adding textures and other resources outside of what XNA provides in the Add New Dialog.

Right Click on the Content Area and select "Existing Item...".
Browse to your music file and click ok.

You will see your solution explorer should look similar to the image in the left below.  The image on the right is the all to familiar property explorer and is showing what we can configure when we select our MP3 file - you can see that it intelligently selects the appropriate content processor for "Song".  You cannot make MP3 files sound effects unfortunately - this would be nice since you could make the size of your SFX files even smaller, but it doesn't matter.  My rule of thumb on sound effect files is if the file is larger than a couple of K then you might want to rethink how it is composed anyway - so this approach works fine.

Solution Explorer (AudioMP3) Properties (AudioMP3)

As you can see you can set the compression quality and other attributes of your MP3 file - another nice feature to have.

To use this file in code is simple and very intuitive, just load it using the content manager the same way you load textures or anything else.  Once you have a handle to it all loaded up and ready to go, you can play the file.

Declaration...

private static Song theMirrorsTruth;

Initialization...

Code Snippet

0:   public static void InitializeAudio(ContentManager content)
1:   {

2:   theMirrorsTruth = content.Load<
Song>("01 The Mirror's Truth"
);
/font>

 

 

Playing the MP3...

 

Code Snippet

0:   public static void PlayInFlamesTheMirrorsTruth()
1:   {
2:  
MediaPlayer.Play(theMirrorsTruth);
3:   }
/pre>

 

 

Simple enough - there is a static method called "Play" that takes instances of song objects and plays them.  You can get more creative with this using the MediaQueue objects, but I will not be talking about that now, on to sound effects...

Playing WAV sound effects.

Adding the sound effect resources is very similar to adding the other audio resources.  One of the things I should mention is that when you want to use the sound effect class you need to keep in mind that this class will only allow you to load content processed as a wav file.   Your property designer for wav sound effect files will look like this...

Properties WAV

 

You can see that it is now using the Sound Effect content processor and WAV Audio File content importer.  To load and use it you have to use the SoundEffect class.

Declaring our sound effect...

private static SoundEffect plasmaMissleSFX;

Loading our sound effect...

plasmaMissleSFX = content.Load<SoundEffect>(@"RaptorOneSFX\PlasmaMissleExplosion");

Playing our sound effect...

 

 

Code Snippet

0:   public static void PlayPlasmaMissleExplosion()
1:   {
2:   plasmaMissleSFX.Play(1.0f, 0.0f, 0.0f,
false);
3:   }
/pre>

 

 

Easy Easy Easy!  I should mention what the different arguments are for a Sound Effect's Play method.

The prototype looks like this.

public SoundEffectInstance Play(float volume, float pitch, float pan, bool loop)
Volume is a float value of 0.0 to 1.0 - setting it to 1.0 will play the sound at the volume you originally recorded the WAV file at.
Pitch is a float value of -1.0 to 1.0 - this set's the octave range of the sound effect - setting this value to 0.0f will give you "unity" which is the pitch the sound was recorded at
-0.1 is down an octave and 1.0 is up an octave.
Pan is a float value of -1.0 to 1.0 - this set's the sound's left or right channel, you can use something like this to control which speaker the sound will play louder (nice if you want to 
set a sort of 3D effect with bombs going off - negative values move the balance to the left, positive values move it to the right. Setting to zero centers the sound on both speakers
as recorded.
Loop is a bool value that tells the sound effect to either play the sound once and stop or keep playing it over and over again.


The return value is something called a SoundEffectInstance.  This is sort of like the old Cue object.  Something I use this for is to keep the sound around while it is playing
and check it during updates to see if it is still playing - if I want to play the sound again in response to user input but only want to restrict it to a single play at a time regardless
of what the user is doing - this this is the object it use.

What I have done in my own project is create something called a SoundEffectHelper class that keeps a reference to the instance playing and a reference to the actual wav file.  
Then I create "Manager" classes to manage my sound effects and songs - this keeps everything in once place for me so if I want to change a sound effect that get's played in a bunch
of different places - I only need to change it once (it's just good design practice).

There are so many more features here that I could go on for quite a while,  you can do lots of neat things like doppler effects, etc...  And I only scratched the surface on the API, 
but this should be enough of an example to get you started.

 
 
 
posted on Sunday, June 22, 2008 12:20 PM

Feedback

# re: Using Audio in XNA 3.0 6/23/2008 9:47 AM Ziggyware XNA News and Tutorials
Great article! I pimped you on my site :)


# re: Using Audio in XNA 3.0 8/26/2008 5:06 PM safasf
and how do you stop playing the sound?

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 2 and type the answer here: