Audio callback question

I am writing a video player with SDL. To synchronize audio and video I use the audio as master clock. To get the audio clock I ask an audio buffer size that plays for 1/4 second. Now each time the callback function is called I add 1/4 s to the reference clock. This is the smartest solution I was able to implement. To let it work I need that the callback function is called when the audio buffer is consumed by audio card and needs more data to play. I found that when my program starts to play, the audio callback is called twice, almost in the same millisecond, and then it is called as expected, about each 1/4 sec.
I suppose that at the beginning the audio callback is called twice because the internal SDL buffers. My question is: Is this behavior the same on all machines, or it depends on the operative system or the audio drivers or the sound card ?
If it is machine dependant, how can I understand if at the beginning the audio callback will be called once, twice or three times ?

I am writing a video player with SDL. To synchronize audio and video I use the audio as master clock. To get the audio clock I ask an audio buffer size that plays for 1/4 second. Now each time the callback function is called I add 1/4 s to the reference clock. This is the smartest solution I was able to implement. To let it work I need that the callback function is called when the audio buffer is consumed by audio card and needs more data to play. I found that when my program starts to play, the audio callback is called twice, almost in the same millisecond, and then it is called as expected, about each 1/4 sec.
I suppose that at the beginning the audio callback is called twice because the internal SDL buffers. My question is: Is this behavior the same on all machines, or it depends on the operative system or the audio drivers or the sound card ?
If it is machine dependant, how can I understand if at the beginning the audio callback will be called once, twice or three times ?

SDL typically will fill twice to double-buffer the audio. It’s not really
defined, but on most platforms that’s what happens. You could time the first
few callbacks to see how much pre-filling is happening, if you want.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Hi - I’ve added an extension to SDL for our mpeg4ip video player (www.mpeg4ip.net)
that returns a millisecond value for output latency. This will help you
get a fairly reliable reference clock.

We’ve been using it in our project for about a year and a half with pretty
good success - it works on Linux, windows and solaris.

Bill May

Sam Lantinga wrote:>>I am writing a video player with SDL. To synchronize audio and video I use the audio as master clock. To get the audio clock I ask an audio buffer size that plays for 1/4 second. Now each time the callback function is called I add 1/4 s to the reference clock. This is the smartest solution I was able to implement. To let it work I need that the callback function is called when the audio buffer is consumed by audio card and needs more data to play. I found that when my program starts to play, the audio callback is called twice, almost in the same millisecond, and then it is called as expected, about each 1/4 sec.

I suppose that at the beginning the audio callback is called twice because the internal SDL buffers. My question is: Is this behavior the same on all machines, or it depends on the operative system or the audio drivers or the sound card ?
If it is machine dependant, how can I understand if at the beginning the audio callback will be called once, twice or three times ?

SDL typically will fill twice to double-buffer the audio. It’s not really
defined, but on most platforms that’s what happens. You could time the first
few callbacks to see how much pre-filling is happening, if you want.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment