<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Delay when playing a sound effect using &quot;al_play_sample&quot;</title>
		<link>http://www.allegro.cc/forums/view/616209</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 19 Apr 2016 08:17:56 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There is a delay / latency when I use &quot;al_play_sample&quot; to play a WAV file. It&#39;s just a split second, but the fact that the sound doesn&#39;t match up with what is occurring on screen is VERY noticeable. <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p><p>Should I expect latency when using &quot;al_play_sample&quot;? Is there a better or faster way to play sound effects?</p><p>There was a similar thread a couple years ago here: <a href="https://www.allegro.cc/forums/thread/614392">https://www.allegro.cc/forums/thread/614392</a> but I&#39;m afraid I don&#39;t understand what the resolution was, if any.</p><p>About my setup:<br />Arch Linux with allegro 5.0.11<br />WAV (PCM) file, constant bit rate of 705 Kbps, 44.1 KHz, 16 bits, NO silent &quot;gap&quot; at the beginning of the file.<br />Preloading the sound effect early in an ALLEGRO_SAMPLE doesn&#39;t make a difference.</p><p>Thank you for any help! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Couzelis)</author>
		<pubDate>Sun, 17 Apr 2016 19:14:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_VOICE"><span class="a">ALLEGRO_VOICE</span></a></span> has an internal buffer for some audio backends that can cause some audio delay. In many cases it&#39;s hard coded, but in a few cases you can modify it using the system configuration. You&#39;re probably using PulseAudio as your backend (it typically has lagging issues), but perhaps not.</p><p>Try one of these before you initialize your audio and see if it makes a difference:</p><p><span class="source-code"><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"audio"</span>, <span class="s">"driver"</span>, <span class="s">"oss"</span><span class="k2">)</span></span><br /><span class="source-code"><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"audio"</span>, <span class="s">"driver"</span>, <span class="s">"alsa"</span><span class="k2">)</span></span><br /><span class="source-code"><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"audio"</span>, <span class="s">"driver"</span>, <span class="s">"pulseaudio"</span><span class="k2">)</span></span></p><p>For <span class="source-code">pulseaudio</span> also try this (change the 512 to lower if you want, default is 1024):</p><p><span class="source-code"><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"pulseaudio"</span>, <span class="s">"buffer_size"</span>, <span class="s">"512"</span><span class="k2">)</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sun, 17 Apr 2016 21:56:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you for the suggestion. I added this code shortly after calling &quot;al_install_audio&quot; (since I am using ALSA):</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"audio"</span>, <span class="s">"driver"</span>, <span class="s">"alsa"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 2</span><a href="http://www.allegro.cc/manual/al_set_config_value"><span class="a">al_set_config_value</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_system_config"><span class="a">al_get_system_config</span></a><span class="k2">(</span><span class="k2">)</span>, <span class="s">"alsa"</span>, <span class="s">"buffer_size"</span>, <span class="s">"512"</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

It had no discernible effect. I also tried &quot;oss&quot;, &quot;pulseaudio&quot;, and many many variations of buffer sizes (down to 32, up to 2048...) with no discernible effect as well. <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p><p>...I&#39;ve been studying the Allegro examples, namely &quot;ex_acodec.c&quot; (which can be seen by selecting it <a href="https://www.allegro.cc/manual/5/al_play_sample_instance">here</a>), and had good results. By creating an ALLEGRO_VOICE, ALLEGRO_MIXER, and ALLEGRO_SAMPLE_INSTANCE I&#39;m able to play my pre-loaded ALLEGRO_SAMPLE instantly.</p><p>Unfortunately, I haven&#39;t quite got the hang of all those parts yet. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /> Does every instance of a sound effect need its own instance of an ALLEGRO_SAMPLE_INSTANCE? But can they all share the same ALLEGRO_VOICE and ALLEGRO_MIXER?</p><p>And why is this different from just using the default mixer and the default voice (with &quot;al_play_sample&quot;)?</p><p>EDIT: If I create a mixer and set it as the default:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>voice <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_voice"><span class="a">al_create_voice</span></a><span class="k2">(</span><span class="n">44100</span>, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 2</span>mixer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_mixer"><span class="a">al_create_mixer</span></a><span class="k2">(</span><span class="n">44100</span>, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 3</span><a href="http://www.allegro.cc/manual/al_attach_mixer_to_voice"><span class="a">al_attach_mixer_to_voice</span></a><span class="k2">(</span>mixer, voice<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 4</span><a href="http://www.allegro.cc/manual/al_set_default_mixer"><span class="a">al_set_default_mixer</span></a><span class="k2">(</span>mixer<span class="k2">)</span><span class="k2">;</span>
</div></div><p>

And THEN call &quot;al_play_sample&quot; the sound effect plays INSTANTLY! ...but only ONCE...? Any time I play a sound effect after that it&#39;s laggy again. <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Couzelis)</author>
		<pubDate>Mon, 18 Apr 2016 00:13:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616209/1021917#target">David Couzelis</a> said:</div><div class="quote"><p> Thank you for the suggestion. I added this code shortly after calling &quot;al_install_audio&quot; (since I am using ALSA):</p></div></div><p>It needs to come before <span class="source-code"><a href="http://www.allegro.cc/manual/al_install_audio"><span class="a">al_install_audio</span></a></span>. For ALSA, there is no configurable buffer size, so that setting won&#39;t really help.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Does every instance of a sound effect need its own instance of an ALLEGRO_SAMPLE_INSTANCE? But can they all share the same ALLEGRO_VOICE and ALLEGRO_MIXER?</p></div></div><p>Yes. There&#39;s a bit of a writeup here which explains the relationship between these concepts: <a href="http://liballeg.org/a5docs/trunk/audio.html">http://liballeg.org/a5docs/trunk/audio.html</a></p><p>Still thinking about why you&#39;d get lag-free playback only once and why creating a new voice helps...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 18 Apr 2016 01:26:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616209/1021922#target">SiegeLord</a> said:</div><div class="quote"><p>There&#39;s a bit of a writeup here which explains the relationship between these concepts: <a href="http://liballeg.org/a5docs/trunk/audio.html">http://liballeg.org/a5docs/trunk/audio.html</a></p></div></div><p>
Oh, excellent! That documentation seems to have more information than the docs at allegro.cc. I&#39;ll study it it detail.</p><div class="quote_container"><div class="title">SiegeLord said:</div><div class="quote"><p>Still thinking about why you&#39;d get lag-free playback only once and why creating a new voice helps...</p></div></div><p>
I did another test:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><a href="http://www.allegro.cc/manual/ALLEGRO_VOICE"><span class="a">ALLEGRO_VOICE</span></a> <span class="k3">*</span>voice<span class="k2">;</span>
<span class="number">  2</span><a href="http://www.allegro.cc/manual/ALLEGRO_MIXER"><span class="a">ALLEGRO_MIXER</span></a> <span class="k3">*</span>mixer<span class="k2">;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="k1">if</span> <span class="k2">(</span>voice <span class="k3">!</span><span class="k3">=</span> NULL <span class="k3">&amp;</span><span class="k3">&amp;</span> mixer <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  5</span>    <a href="http://www.allegro.cc/manual/al_detach_voice"><span class="a">al_detach_voice</span></a><span class="k2">(</span>voice<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>    <a href="http://www.allegro.cc/manual/al_destroy_voice"><span class="a">al_destroy_voice</span></a><span class="k2">(</span>voice<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>    <a href="http://www.allegro.cc/manual/al_destroy_mixer"><span class="a">al_destroy_mixer</span></a><span class="k2">(</span>mixer<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span><span class="k2">}</span>
<span class="number">  9</span>
<span class="number"> 10</span>voice <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_voice"><span class="a">al_create_voice</span></a><span class="k2">(</span><span class="n">44100</span>, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>mixer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_mixer"><span class="a">al_create_mixer</span></a><span class="k2">(</span><span class="n">44100</span>, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span><a href="http://www.allegro.cc/manual/al_attach_mixer_to_voice"><span class="a">al_attach_mixer_to_voice</span></a><span class="k2">(</span>mixer, voice<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span><a href="http://www.allegro.cc/manual/al_set_default_mixer"><span class="a">al_set_default_mixer</span></a><span class="k2">(</span>mixer<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span><a href="http://www.allegro.cc/manual/al_reserve_samples"><span class="a">al_reserve_samples</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>
If I run this code before EVERY TIME I play a sound effect using &quot;al_play_sample&quot; it plays absolutely perfectly. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> But, of course, needing to do all this before I play any sound effect seems pretty overkill...</p><p>Is there anything else I can do get to the bottom of this? Post more code? Do another test? Collect any logs? Wait patiently? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Couzelis)</author>
		<pubDate>Tue, 19 Apr 2016 00:51:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you sure you are using the Alsa and not the PulseAudio driver? The Alsa driver is very old and a bit untested at this point.</p><p>There&#39;s for example this comment <span class="ref"><sup>[<a href="#">1</a>]</sup></span>:</p><div class="source-code snippet"><div class="inner"><pre>   <span class="c">// TODO: Setting this to 256 causes (extreme, about than 10 seconds)</span>
   <span class="c">// lag if the alsa device is really pulseaudio.</span>
   <span class="c">//</span>
   <span class="c">// pw: But there are calls later which expect this variable to be set an on</span>
   <span class="c">// my machine (without PulseAudio) the driver doesn't work properly with</span>
   <span class="c">// anything lower than 32.</span>
   ex_data-&gt;frag_len <span class="k3">=</span> <span class="n">32</span><span class="k2">;</span>
</pre></div></div><p>

So someone just randomly set some setting called &quot;frag_len&quot; to 32. I couldn&#39;t figure out what buffer size we instruct ALSA to use on a quick glance - but chances are it is too large. And maybe, just as a blind guess, by creating a new voice you somehow force the buffer to be flushed and so the sound plays immediately?
</p><div class="ref-block"><h2>References</h2><ol><li><a href="https://github.com/liballeg/allegro5/blob/master/addons/audio/alsa.c#L567">https://github.com/liballeg/allegro5/blob/master/addons/audio/alsa.c#L567</a></li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 19 Apr 2016 03:48:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The issue has been resolved (to my satisfaction, at least). Thank you both! <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p>I spent the last 15 minutes switching my Arch Linux setup from ALSA to PulseAudio and SURPRISE! the most simple Allegro setup and call to &quot;al_play_sample&quot; now works flawlessly.</p><p>...Now, for my next step, is to decide if I want to keep PulseAudio (there&#39;s still some stuff I&#39;d need to configure if I wanted to stick with it), try OSS, or simply leave ALSA the way it is and ignore the Allegro output issue. But, that&#39;s for me to worry about.</p><p>Thank you again! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Couzelis)</author>
		<pubDate>Tue, 19 Apr 2016 06:30:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I actually wrote an entire sound handler using Allegro for my current project as the A5 approach to sound is... infuriatingly complicated. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>I have experienced no delays, but then, my code uses al_play_sample_instance() to do its thing. *shrugs*
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Tue, 19 Apr 2016 08:17:56 +0000</pubDate>
	</item>
</rss>
