I am building an application that generates its own sound stream at 8 kHz. Once I built the sound sample, I pass it to SDL_QueueAudio(). The problem is that my application and SDL appear to be slightly out-of-sync. I mean - I open the SDL audio device with a 8 kHz rate, and I emit samples at 8 kHz, but there must be some small rate difference because the sound is building up a delay (after one minute, audio is lagging 2s behind video).
I understand the reason of the problem, but I don’t know how to solve it. Currently, I use this:
if (SDL_GetQueuedAudioSize() > 4096) SDL_ClearQueuedAudio(devid);
This kinda works, but it’s ugly and generates sound glitches every time it executes.
What would be the best way to solve this problem?
I was thinking along the lines of skipping a frame from time to time on my side, but for this I would need to know how much audio is lagging behind video (and here the problem is that SDL_GetQueuedAudioSize() is not very precise, to say the least). Any idea? I suppose this is a common problem as soon as anyone uses the SDL_QueueAudio() API, but sadly I fail to see any obvious solution.