# Audio callback question

**URL:** <https://discourse.libsdl.org/t/audio-callback-question/9099>\
**Category:** SDL Development\
**Created:** [July 28, 2003, 6:38am UTC](https://discourse.libsdl.org/t/audio-callback-question/9099 "2003-07-28T06:38:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefano.manzella](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@stefano.manzella](https://discourse.libsdl.org/u/stefano.manzella)\
**Post date:** [July 28, 2003, 6:38am UTC](https://discourse.libsdl.org/t/audio-callback-question/9099/1 "2003-07-28T06:38:01Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [July 31, 2003, 8:54pm UTC](https://discourse.libsdl.org/t/audio-callback-question/9099/2 "2003-07-31T20:54:00Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Bill\_May](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@Bill\_May](https://discourse.libsdl.org/u/Bill_May)\
**Post date:** [May 11, 2004, 5:19pm UTC](https://discourse.libsdl.org/t/audio-callback-question/9099/3 "2004-05-11T17:19:51Z")

</div>

Hi - I’ve added an extension to SDL for our mpeg4ip video player ([www.mpeg4ip.net](http://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
