XAudio2 to load and play a .wav file on XBOX One.
The following sections describe how to use XAudio2 to load and play a sound on the XBOX One Development Kit.
- Important XAudio2 data types
- Initializing XAudio2
- Loading a .wav file
- Setting a file path to a .wav file
- Playing a .wav file
- Terminating a .wav file
XAudio2 sample, download the SimplePlaySound sample from the Microsoft Game Development Kit (GDK) samples.
Important XAudio2 data types
XAudio2 provides several data types to help you play sound effects on XBOX One. To play a .wav file, you need at least the following data types.
-
IXAudio2: This is the interface for theXAudio2object that manages all Audio Engine states, the audio processing thread, the voice graph, and more. -
IXAudio2SourceVoice: Use a source voice to submit audio data to theXAudio2processing pipeline. To be heard, voice data must be sent to a mastering voice. -
IXAudio2MasteringVoice: Use this data type to represent the audio output device. Data buffers can’t be submitted directly to mastering voices. However, to be heard, data submitted to other types of voices must be directed to a mastering voice.
-
PlaySoundVoiceContext: Use this data type to free up the audio buffer after processing. -
IXAudio2VoiceCallback: This data type contains methods that notify the client when certain events happen in a specificIXAudio2SourceVoice.
Initializing XAudio2
CoInitializeEx() initializes the Component Object Model (COM) for use by the current thread. Set the first parameter to NULL. Set the second parameter to COINIT_MULTITHREADED.
XAudio2Create() creates a new XAudio2 object and returns a pointer to its IXAudio2 interface. Ensure that XAUDIO2_PROCESSOR is set to a valid value. The value XAUDIO2_USE_DEFAULT_PROCESSOR is recommended to let the OS choose the ideal processor based on the hardware platform.
IXAudio2::CreateMasteringVoice() creates and configures a mastering voice and points to it with the user-provided pointer.
C++
Loading a .wav file
To access the audio file, use an instance of theWaveFile class.
- To open a .wav file and retrieve some information stored in the file’s header, call
WaveFile::Open(LPCWSTR strFileName). - To determine the format of the .wav file, call
WaveFile::GetFormat(). - To determine the number of bytes and samples in the .wav file, call
WaveFile::GetDuration(). - To read the sample data into memory, call
WaveFile::ReadSample().
C++
Setting a file path to a .wav file
To load and use audio files, use the following code to find the installation location of your project on the local device. Store the installation location in a string. Append the location of the audio file to the end of the installation location string.C++
Playing a .wav file
Create anIXAudio2::CreateSourceVoice() class, and then use it to submit audio data to the XAudio2 processing pipeline. For voice data to be heard, you must send it to a mastering voice directly or through intermediate submix voices. To store the details of the audio file, create an XAUDIO2_BUFFER.
After you create and initialize the XAudio2 buffer and source voice, call IXAudio2SourceVoice::SubmitSourceBuffer() to add the audio buffer to the voice input queue. Note that the audio data pointed to by XAUDIO2_BUFFER::pAudioData must remain valid until XAudio2 has finished playing the contents of the buffer.
After the voice input queue is populated, call IXAudio2SourceVoice::Start() to play the next sound in the queue.
C++
Terminating a .wav file
To determine whether a .wav file has been played to completion, you’ll need the current state of the source voice. Create anXAUDIO2_VOICE_STATE struct, and then call IXAudio2SourceVoice::GetState(). If the audio has been played to completion, call IXAudio2SourceVoice::DestroyVoice().
