Game Development Reference
In-Depth Information
wavFile.Length;
// Setup our audio buffer for looping.
m_AudioBuffer.LoopCount =
XAudio2.LoopInfinite;
// Create the source voice object. This
is used to submit our audio data to the
// mastering voice object so we can
play it.
m_SourceVoice = new
SourceVoice(m_XAudio2, wavFile.Format);
m_SourceVoice.SubmitSourceBuffer(m_AudioBuffer);
m_SourceVoice.Start();
}
}
This method can be a bit confusing, so I left the comments in the previous code list-
ing. As you can see, the first thing we do is create our XAudio2 object. Then, we
have an if statement that checks to see if we have any valid sound devices to use.
If not, then the XAudio2 object's DeviceCount property will return 0 . In this case,
if no sound device is available, we simply return and don't try to continue initializing
XAudio2, since this would probably cause a crash. This goes back to the tip in the
DirectSound section of this chapter; error handling is very important in a real game,
so don't forget it or put if off! Next, we create the MasteringVoice object. I know
you're probably thinking something along the lines of "what the heck is a mastering
voice?". Well, it is simply a class that represents the audio output device; however,
you cannot submit an audio buffer directly to the mastering voice object. We will use
our SourceVoice object to do that in a minute.
Now, we have a using block similar to the one we have in our InitDirec-
tSound() method. It opens our sound file so we can get the audio data and put it
into our buffer.
Search WWH ::




Custom Search