[SDL] Audio callback function is not called in time sometimes

David Olofson david at olofson.net
Tue Dec 4 12:20:45 PST 2007


On Tuesday 04 December 2007, Jukka-Pekka Manninen wrote:
> Just guessing here, and assuming you have a busy main loop running…
> 
>  
> 
> Do you have any SDL_Delay calls in your main loop? If not, and your
> app is using up all the CPU time it can get, Windows might tag your
> program as a resource hog and give it less CPU time especially when
> other processes are busy. Like minimizing/maximizing etc.


This *should* happen on the thread level rather than the process 
level, and I think even Windows gets that pretty much right. ;-)

(What I'm saying is, as a non-vsync main loop burns all CPU it can 
get, the OS will consider it a CPU hog and lower it's priority, to 
allow somewhat fair system wide time sharing. Meanwhile, the audio 
thread blocks once per buffer, and usually doesn't burn more than a 
few % of the CPU time, so it keeps it's "nominal" priority, or gets a 
higher dynamic priority, depending on how the OS scheduler works.)


I think the problem is simply that this particular system is 
misbehaving slightly. For Windows, not being able to go below 50 ms 
audio latency is pretty common, and I wouldn't expect it to work 
reliably on your average system. Don't go that low, at least not if 
hardwiring it into the application...


If you *really* want to go that low (without resorting to pro 
audio/studio system requirements), you should talk directly to the 
shared output buffer, doing "mix-ahead" as new sounds are started, 
while using more buffering (say, 200 ms) for "old" sounds. This gives 
you close to zero latency for sound triggering with some risk of 
glitches, but reliable, glitch free playback for anything that's been 
playing for more than those 200 ms or whatever.

Bad news (apart from this being a bit tricky to get right) is that 
you'll need to do an SDL deep hack, or bypass SDL audio entirely to 
do this. The good news is that most SDL backends already use shared 
memory I/O, so you might be able to piggy-back on that. (I've been 
meaning to demonstrate how to do this some time, but well... Five 
million projects...)


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'


More information about the SDL mailing list