<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Audio synchronization in Allegro 5</title>
		<link>http://www.allegro.cc/forums/view/613194</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 29 Aug 2013 23:13:59 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey all,</p><p>    I&#39;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&#39;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.</p><p>    Here&#39;s a quick snippet of the code I&#39;m using right now to synchronize:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> SoundManager::syncStream<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_AUDIO_STREAM"><span class="a">ALLEGRO_AUDIO_STREAM</span></a><span class="k3">*</span> stream<span class="k2">)</span>
<span class="k2">{</span>
        <span class="k1">const</span> <span class="k1">double</span> duration <span class="k3">=</span> <span class="n">8</span>.<span class="n">0</span><span class="k2">;</span> <span class="c">// a magic number for now; the loop duration of the files i'm playing</span>
        <span class="k1">double</span> timeElapsed <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a><span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> m_startTime<span class="k2">;</span> <span class="c">// m_startTime is a value taken from al_get_time() in the game initialization</span>
        <span class="k1">double</span> seekTime <span class="k3">=</span> timeElapsed <span class="k3">-</span> <span class="k2">(</span><span class="k2">(</span><span class="k1">int</span><span class="k2">)</span><span class="k2">(</span>timeElapsed<span class="k3">/</span>duration<span class="k2">)</span><span class="k2">)</span><span class="k3">*</span>duration<span class="k2">;</span> <span class="c">// wrap the seek time modulo duration</span>
        <a href="http://www.allegro.cc/manual/al_seek_audio_stream_secs"><span class="a">al_seek_audio_stream_secs</span></a><span class="k2">(</span>stream, seekTime<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Note that the problem doesn&#39;t appear to be the loop duration calculation; the problem will occur even on the first playthrough.  My guess is that this isn&#39;t working because the seek doesn&#39;t occur instantaneously; instead it is queued and later picked up by the audio thread.</p><p>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?</p><p>Thanks in advance,<br />- aureus</p><p>Note: I&#39;m using Allegro 5.0.9 with a patch from 5.0.10 to fix the loop duration of ogg streams.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (aureus)</author>
		<pubDate>Thu, 29 Aug 2013 23:13:59 +0000</pubDate>
	</item>
</rss>
