No DirectSound


I’m assuming that most of you have worked with DirectSound before and when you write your first game in XNA you will notice that the sound namespace is very different from DirectSound of DirectX. Take a quick look how DirectSound was used in Managed DirectX. First, you had to initialize the DirectSound device:

  soundDevice = new DirectSound.Device(); soundDevice.SetCooperativeLevel(form, CooperativeLevel.Priority); 

Now you can load all the sounds, but it can be a little bit more complicated to make sure you have multiple sound buffers and to catch the case when the file does not exist, which would cause DirectSound just to throw an InvalidOperationException with no additional error message. In the Managed DirectX Rocket Commander code, the following code was used to make sure all sound files exist and to make sure you can play back sounds multiple times:

  if (File.Exists(Directories.SoundsDirectory + "\\" + filename) ==   false) {   Log.Write("Sample " + filename + " was not found!");   loaded = false;   return; } // if (File.Exists) BufferDescription bufferDesc = new BufferDescription(); bufferDesc.StaticBuffer = true; bufferDesc.ControlPan = true; bufferDesc.ControlVolume = true; bufferDesc.ControlFrequency = true; buffer = new SecondaryBuffer[setNumberOfSimultaneousSounds]; channelVolume = new float[setNumberOfSimultaneousSounds]; channelPanning = new float[setNumberOfSimultaneousSounds]; buffer[0] = new SecondaryBuffer(   Directories.SoundsDirectory + "\\" + filename,   bufferDesc,   Sound.Device); defaultFrequency = buffer[0].Frequency; for (int i = 1; i < buffer.Length; i++)   buffer[i] = buffer[0]; try {   for (int i = 1; i < buffer.Length; i++)     buffer[i] = buffer[0].Clone(Sound.Device); } // try catch { } // catch 

And finally, the following is the code required to play the sound effect. Additionally, you can set the volume and panning of each sound buffer channel thanks to the code in the constructor of the Sample class (ControlPan, ControlVolume, ControlFrequency, and so on). The extra code to manage all the sound buffers and the properties for each sound (volume, panning, frequency) are not shown here because there is a lot of code in the Sample class of Rocket Commander. Take a look if you are interested further.

  // Make sure to start at position 0 buffer[currentBufferNum].SetCurrentPosition(0); // And play the sound buffer[currentBufferNum].Play(0, BufferPlayFlags.Default); 

In XNA you no longer need a Sample class and you can extend the Sound class to support everything you need, including the MusicManager of Rocket Commander, the Sample class, and everything else in the Sound namespace of Rocket Commander.

Because of the structure of XACT, which is discussed in a short while, you have to load the XACT project and the used wave and sound banks yourself. But don’t worry; the code for that is very simple. You already used XACT in the first chapters for all the arcade games there.

  audioEngine = new AudioEngine("YourSoundProject.xgs"); waveBank = new WaveBank(audioEngine, "Wave Bank.xwb"); soundBank = new SoundBank(audioEngine, "Sound Bank.xsb"); 

And that’s it; all you have to do to play a sound now is to call:

  soundBank.PlayCue(soundName); 

Looks much easier, doesn’t it? You might ask about all the extra functionality you had to implement for DirectSound. No, it is not missing, but you have to think differently about sound in XNA. Sound files are no longer managed completely by the custom code of your game. Instead the XACT sound project contains all the predefined sound parameters for you. Instead of modifying each sound volume yourself or changing the pitching to reuse existing sounds yourself, you could set all this inside of the XACT tool. This is especially useful to make sure all sound effects are at the same volume level and to make sure the music is not too loud. You can even specify that certain sounds are only allowed to be played once (like the background music) or if it is allowed to play multiple instances of the same sound for firing sounds or explosions. You learn more about this later in this chapter.

Handling .wav Files

For both DirectSound and XACT all your sound files must be in the .wav file format, especially for all sound effects. If you have different sound files in other formats, you have to convert them first. For example, MP3 files are not supported and you cannot play them in DirectSound or just drop them into your XACT project. You have to convert them to .wav files first. Please also note that certain .wav files are not supported in XACT and you will get a warning message that the format is not supported when you add the sound files into a wave bank in XACT.

It is not easy to make sure a music file is in the correct format and that it can be compressed the best way possible in XACT for both the Xbox 360 and the Windows platform (see Figure 9-1).

image from book
Figure 9-1

Just convert all your sounds to 16-bit 44 KHz Stereo PCM wav files to make sure you can compress them. Even if your music is only mono, you still have to save it in stereo and if you have 8-bit PCM files, don’t try to use them. Convert to 16-bit and stereo or else XACT will not allow you to compress the sound files correctly. For 8-bit files you will get the error message shown in Figure 9-2, and mono files compress badly. You will also lose quality for your 16-bit stereo files, but choosing the right XMA and ADPCM compression level for you might tweak the sound quality a little bit.

image from book
Figure 9-2

Please note that music files are not really supported by XACT and it is a much better tool for sound effect files than for music files. For the PC platform using ADPCM files for your music might not make much sense if you have MP3 files and already have some code to play them back. On the Xbox 360 platform you currently have no choice but to use XMA compression and XACT for your music files.

You can use compression for your sound effects too, but I recommend that you try to use high compression ratios. For a PC game it is also very uncommon to use heavily compressed sound files; instead you will just use files of lower quality (22KHz mono) because loading them is much faster, and using MP3 files for sounds is impractical because the CPU would have to decompress them every time you play them or you would have to decompress all files at the start of your game, which could take a while. In most cases adding a few extra MB to your game will not affect much, but if you are an optimization freak like I am and you want to make sure the game is as small as possible without losing any feature or noticing any quality drop, you will still try to compress all the files with various options until everything is small and still sounds right.

I also compressed all texture files of all games in this book several times and used different formats to find out which one was the optimal format for each texture. For example, menu and interface graphics are compressed very well with the PNG format because it stores the bitmap in a lossless manner, but PNG files are just too big for model textures or landscape textures. In these cases DDS files are used, which also have the advantage of saving video memory, which is another important point for the Xbox 360. Don’t waste your main memory, disk space, or video memory - all of them are very rare on a console.

As an example, the Rocket Commander game from the previous chapter uses about 15 sound files (see Figure 9-3) and with compression enabled the game is nearly 1 MB smaller on the Xbox 360. Not much, but every bit counts.

image from book
Figure 9-3

Enough talk about the basics and file formats; next you create your first sound project in XACT.




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

Similar book on Amazon
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net