I write a MFC project . I use IXAudio2 to play wav file. my code is like this :
pSourceVoice->SubmitSourceBuffer( &buffer );
hr = pSourceVoice->Start( 0 );
but in this way I only can play one sound by a time. It must wait this .wav
file play over. I can play the second file. How can I play the file I need and not wait for the first one is over, likes mixing sounds?
How can I achieve this function?
You create one
IXAudio2SourceVoice
for each 'playing, active' sound you want at a time. Typically games will create a few hundred source voices to manage--beyond that the sound mix is typically too muddy to hear anyhow.Game audio engines have a mix of 'one-shot' voices that just play-on-demand until they complete--at which point that frees up that source voice for use by another one-shot sound--, 'ambients' or 'music' voices that are manipulated as they play, and 3D audio positioned sounds which have to be updated every rendering frame to track the 3D rendering object they are emitted from.
If you submit the same audio data to two distinct
IXAudio2SourceVoice
instances, then you'll hear it being played twice, mixed together in real-time. Typically you won't start them at precisely the same instant because the result is just a louder version of the one sound. Normally they are started at different times so you get overlapped playback.See XAudio2 Programming Guide, GitHub, and DirectX Tool Kit for Audio.