- Online Community Forums » Programming Questions » Audio synchronization in Allegro 5

This thread is locked; no one can reply to it. rss feed Print
Audio synchronization in Allegro 5
Member #15,288
August 2013

Hey all,

I'm doing some preproduction work for a rhythm game I intend to develop using Allegro. An important feature I want to include in this game is crossfading different music files as the player progresses. My problem, however, is that when I start a new audio stream, it doesn't quite sync up with the old one (or with the visuals, for that matter). The difference in phase is rather noticeable: often on the order of 50ms or more.

Here's a quick snippet of the code I'm using right now to synchronize:

void SoundManager::syncStream(ALLEGRO_AUDIO_STREAM* stream)
        const double duration = 8.0; // a magic number for now; the loop duration of the files i'm playing
        double timeElapsed = al_get_time() - m_startTime; // m_startTime is a value taken from al_get_time() in the game initialization
        double seekTime = timeElapsed - ((int)(timeElapsed/duration))*duration; // wrap the seek time modulo duration
        al_seek_audio_stream_secs(stream, seekTime);

Note that the problem doesn't appear to be the loop duration calculation; the problem will occur even on the first playthrough. My guess is that this isn't working because the seek doesn't occur instantaneously; instead it is queued and later picked up by the audio thread.

Is there a way I can correct for the delay before the audio thread sees it? Force the audio thread to wake up immediately after queuing the seek? Modify the audio engine itself to adjust the seek time forward by the difference between when the event is queued and when it is fired?

Thanks in advance,
- aureus

Note: I'm using Allegro 5.0.9 with a patch from 5.0.10 to fix the loop duration of ogg streams.

Go to: