<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] Checking if a sample is playing?</title>
		<link>http://www.allegro.cc/forums/view/611901</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 24 Jan 2013 10:48:30 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have just started working with Allegro 5 sounds.  I am used to working with Allegro 4, but I am wondering how I would check to see if a sound is currently playing?</p><p>I can load and play samples with no problems.  I looked over the documentation and it is a little confusing with voices, mixers, sample instances etc...</p><p>This is basically what I understand so far...</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE"><span class="a">ALLEGRO_SAMPLE</span></a> <span class="k3">*</span>sample1<span class="k2">;</span>
sample <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_sample"><span class="a">al_load_sample</span></a><span class="k2">(</span><span class="s">"Sound/sample1.ogg"</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>sample1<span class="k2">)</span> <span class="k2">{</span>
   a5_error<span class="k2">(</span>AT, setting.screen, <span class="s">"Failed to load sample.ogg"</span><span class="k2">)</span><span class="k2">;</span>
   shut_down<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
<a href="http://www.allegro.cc/manual/al_play_sample"><span class="a">al_play_sample</span></a><span class="k2">(</span>sample1, <span class="n">1</span>.<span class="n">0</span>, <span class="n">0</span>.<span class="n">0</span>, <span class="n">1</span>.<span class="n">0</span>, ALLEGRO_PLAYMODE_ONCE, NULL<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_stop_samples"><span class="a">al_stop_samples</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_destroy_sample"><span class="a">al_destroy_sample</span></a><span class="k2">(</span>sample1<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Now I just read about <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_ID"><span class="a">ALLEGRO_SAMPLE_ID</span></a></span>&#39;s and understand them.  I&#39;ve had no use for them YET, but I don&#39;t see how they could be used to check if a sample is playing yet.</p><p>In Allegro 4 I used to use <span class="source-code"><a href="http://www.allegro.cc/manual/voice_get_position"><span class="a">voice_get_position</span></a><span class="k2">(</span><span class="k2">)</span></span> to check if a sample was playing.</p><p>I don&#39;t need anything fancy, in my game I&#39;ll be playing basic sound samples once, as well as one of two background sample that will be looping.  I want to check each background sample (back1 and back2 for example) to see which is playing (only one of the two will be) and stop it from playing if it is then switch to the other.</p><p>Is there an easy way to simply check if a sample is playing (say by using the <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_ID"><span class="a">ALLEGRO_SAMPLE_ID</span></a></span>)?  Or is there something else I need to do?</p><p>Thanks in advance, so far I have been very impressed with this version of Allegro as I get time to program with it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 24 Jan 2013 08:57:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="https://www.allegro.cc/manual/5/al_get_sample_instance_position">https://www.allegro.cc/manual/5/al_get_sample_instance_position</a> ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 24 Jan 2013 09:00:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="source-code"><span class="k1">unsigned</span> <span class="k1">int</span> <a href="http://www.allegro.cc/manual/al_get_sample_instance_position"><span class="a">al_get_sample_instance_position</span></a><span class="k2">(</span><span class="k1">const</span> <a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_INSTANCE"><span class="a">ALLEGRO_SAMPLE_INSTANCE</span></a> <span class="k3">*</span>spl<span class="k2">)</span></span></p><p>which leads me to...</p><p>&quot;To be played, an ALLEGRO_SAMPLE_INSTANCE object must be attached to an ALLEGRO_VOICE object, or to an ALLEGRO_MIXER object&quot;</p><p>which then leads to pages on voices, mixers..... wow... maybe I&#39;ll figure out another way to do this before I pull all my hair out. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 24 Jan 2013 09:24:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Don&#39;t be such a drama queen.</p><div class="source-code snippet"><div class="inner"><pre><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>

<a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE"><span class="a">ALLEGRO_SAMPLE</span></a><span class="k3">*</span> s <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_sample"><span class="a">al_load_sample</span></a><span class="k2">(</span><span class="s">"Sound/sample1.ogg"</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_INSTANCE"><span class="a">ALLEGRO_SAMPLE_INSTANCE</span></a><span class="k3">*</span> i <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_sample_instance"><span class="a">al_create_sample_instance</span></a><span class="k2">(</span>s<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_attach_sample_instance_to_mixer"><span class="a">al_attach_sample_instance_to_mixer</span></a><span class="k2">(</span>i, <a href="http://www.allegro.cc/manual/al_get_default_mixer"><span class="a">al_get_default_mixer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_set_sample_instance_playing"><span class="a">al_set_sample_instance_playing</span></a><span class="k2">(</span>i, <span class="k1">true</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">auto</span> pos <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_sample_instance_position"><span class="a">al_get_sample_instance_position</span></a><span class="k2">(</span>i<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Err... I didn&#39;t actually read OP... but you want <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_sample_instance_playing"><span class="a">al_get_sample_instance_playing</span></a></span> probably...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Thu, 24 Jan 2013 09:40:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The interface you are using is a simpler, higher level version of the main API. It doesn&#39;t support all of the functionality. </p><p>An <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE"><span class="a">ALLEGRO_SAMPLE</span></a></span> is just raw data (including some format information). Before you can play one, you must create an <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_INSTANCE"><span class="a">ALLEGRO_SAMPLE_INSTANCE</span></a></span> that describes how to play it and the playback status. A single sample can be referenced by multiple sample instances.</p><p>When you call <span class="source-code"><a href="http://www.allegro.cc/manual/al_reserve_samples"><span class="a">al_reserve_samples</span></a><span class="k2">(</span><span class="k2">)</span></span> you are really creating a default mixer and attaching that many sample instances to it. When you call <span class="source-code"><a href="http://www.allegro.cc/manual/al_play_sample"><span class="a">al_play_sample</span></a><span class="k2">(</span><span class="k2">)</span></span> you are just updating one of those reserved sample instances to point to your sample data and starting it.</p><p>So if you want at the more advanced features, you basically have to do that all yourself.</p><p>I wouldn&#39;t be opposed to a function like <span class="source-code">al_is_sample_playing<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_SAMPLE_ID"><span class="a">ALLEGRO_SAMPLE_ID</span></a> id<span class="k2">)</span></span>, but at some point you are just rewriting the entire lower level API.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Thu, 24 Jan 2013 09:49:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you, that is more of what I was looking for.  When I looked at the function to check if a sample was playing I was hoping there was an example showing how to implement it.  </p><p>I&#39;m not being a &quot;drama queen&quot;, the documentation seems to have me jumping all over the place without many examples so visualizing how to do this all gets a little confusing at first glance.  </p><p>I do understand why some of this is needed.  I guess the more flexible the library is, the greater need for documentation and examples.</p><p>Perhaps I can write a function to simplify it myself for future projects.</p><p>SiegeLord&#39;s example REALLY helped clarify what I needed to do though, thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 24 Jan 2013 10:48:30 +0000</pubDate>
	</item>
</rss>
