Reputation: 1
I am trying to make a game in C++ and have multiple sounds playing at the same time (player footsteps, gunshots, etc.). Using sndPlaySound() I can only play 1 sound at a time and playing a new sound cuts off the previous one. How would I go about playing multiple sounds at the same time?
void Audio::PlayGunshotSound() {
sndPlaySound(L"resources/audio/gunshot.wav", SND_ASYNC);
}
Upvotes: 0
Views: 605
Reputation: 41057
The ancient Windows Multimedia API sndPlaySound
can only play one sound at at a time.
For a game you typically use an audio API that provides a real-time 'mixer' which combines various playing sounds and then pipes the combination to the audio subsystem. For Windows APIs, you can make use of XAudio2 or legacy DirectSound for a sound API that supports this real-time mixing.
There are many third-party sound libraries that also provide their own real-time mixing (ex. Audiokinetic WWise, Firelight Technologies FMOD, RAD Game Tools MSS, etc.). These all create the combined mix and then use Windows Core Audio (WASAPI) to output the final mix.
Since you are using C++, I'd recommend starting out taking a look at XAudio2. It's built into modern versions of Windows, and to support Windows 7 SP1 or later you can make use of XAudio2Redist.
See the DirectX Tool Kit for Audio for a basic C++ audio engine built around the XAudio2 API.
Upvotes: 3