Advertisement

How do you handle multiple WAVs in SDL?

Started by January 11, 2022 11:16 PM
3 comments, last by Shaarigan 2 years, 11 months ago

Hi Folks!

I'm a n00b but trying to work my way through a “should be trivial” project, and have been able to find everything that I need around. But I'm stuck with one thing. My collection of assets includes about 50 wavs. Any of these could be played at any time, so I need to have them “at the ready”. I can find PLENTY of examples of playing a single wav, and I have that working. I only ever play 1 sound at a time, so I don't need the mixer, or callbacks, or anything like that. So, how is it done?

I'd (ideally) like to load them all at launch, and then have them ready so that I could just call an API to play them when the time comes.

I'm porting an old game from an embedded platform where it did everything on the bare metal. It's all in C, so I'd like to keep it that way.

THANKS!

-Frank

The Audio device can only handle a single stream at a time, this is a limitation of the hardware and how Sound is processed. I'm not an expert Audio Programmer but I've worked with it on some experimental project for our Game Engine SDK.

You shouldn't load all files at once into memory but instead use some kind of package format and memory mapped I/O. This has the advantage tthat you can quickly access the audio data but don't have to manage everything in application space memory but have the OS handle everything for you.

The way to get it working is to either implement your own Audio Mixer or use the OS provided or a third party API. Audio Mixer is responsible to play multiple Audio Sources at once and also to have some sources be louder than others, for example when you have a blacksmith working in certain distance and someone talks next to you.

Windows for example offers 2 different APIs, the legace WAVEAPI where you have to do everything on your own and since Vista they also have the WASAPI which also provides access to the Windows internal Audio Mixer. However, there isn't a standard way neither in C nor C++ to do this in a general fashion and always depends on the platform used.

I can't tell you how SDL handles that, you should look for another Audio library or take a look at OpenAL. It is deprecated but still in use. There are however more advanced libraries which are used in professional game development but those have their price. Wwise to name one we use at work as an example

Advertisement

I only need to ever play a single sound at a time. Thus the reason I don't think I need the mixer. Just a quick way to “play sound x”. Since the sounds a small, I'll see if I can just load them on demand, and play them that way. I was trying to opt for “already loaded” as I figured that would be faster in all situations.

You question sounded like playing multiple sounds at once but if you just want to play a single sound and have others ready to do so, you can for sure run the memory mapped I/O approach and let the OS handle all the loading/unloading of memory pages for you, so you don't have to worry about if a file is already loaded from disk.

It would be best to have all your WAVs in a single (package) file for this approach to be beneficial anyways, so you'd just have to shift a binary pointer in order to access one of the files

This topic is closed to new replies.

Advertisement