kraflab / dsda-doom

This is a successor of prboom+ with extra tooling for demo recording and playback, with a focus on speedrunning and quality of life.
320 stars 81 forks source link

Stutter on first playback of sound effect #405

Open bkoropoff opened 1 year ago

bkoropoff commented 1 year ago

dsda-doom is prone to stuttering when a lump is first accessed, especially if the wad file is on a spinning HDD. This is likely because it memory maps wad files, so data is read from disk just in time as the program accesses the mapped region. This can cause the main thread to hang while the disk spins up/seeks on first access of a lump. Options:

bkoropoff commented 1 year ago

OK, there is existing sound precache code. Not sure why I'm getting stutter on first sound access, then.

kraflab commented 1 year ago

When I added that precache it fixed the problem for some people but not for others. No clue what causes it

fabiangreffrath commented 11 months ago

Well, while indeed the sound lumps are cached, the SFX that they contain are still not in the right samplerate/channels/bits configuration for SDL2_Mixer to feed to your hardware. What you need, though, is to cache the sound lumps converted to your sound hardware spec.

kraflab commented 1 month ago

@fabiangreffrath any idea how to go about doing that? 😸 Does it work already like that in woof?

fabiangreffrath commented 1 month ago

We had this in Woof before we did the switch from SDL2_Mixer to OpenAL Soft. What you will need to do, in a nut shell, is to load the sound lumps, convert them to your current format/channels/samplerate configuration, store this data in a buffer and feed this into the SDL Mixer.

A complete implementation of this solution can be found in our CacheSound() function at this point of the code history, i.e. right before we switched over to OpenAL Soft. Please note that we use libsndfile to load non-Doom format sound samples, but I think there is already some solution in the PRBoom+ code base to load at least 16-bit WAV samples.

https://github.com/fabiangreffrath/woof/blob/49abe0d28a671ba9c841446ac957dd22efca6635/src/i_sound.c#L209

fabiangreffrath commented 1 month ago

Second part, call CacheSound() for every sound effect once at engine start:

https://github.com/fabiangreffrath/woof/blob/49abe0d28a671ba9c841446ac957dd22efca6635/src/i_sound.c#L706