Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Checking if a sample is playing?

Credits go to SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] Checking if a sample is playing?
Neil Roy
Member #2,229
April 2002
avatar

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?

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...

This is basically what I understand so far...

ALLEGRO_SAMPLE *sample1;
sample = al_load_sample("Sound/sample1.ogg");
if(!sample1) {
   a5_error(AT, setting.screen, "Failed to load sample.ogg");
   shut_down();
   exit(1);
}
al_play_sample(sample1, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_stop_samples();
al_destroy_sample(sample1);

Now I just read about ALLEGRO_SAMPLE_ID's and understand them. I've had no use for them YET, but I don't see how they could be used to check if a sample is playing yet.

In Allegro 4 I used to use voice_get_position() to check if a sample was playing.

I don't need anything fancy, in my game I'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.

Is there an easy way to simply check if a sample is playing (say by using the ALLEGRO_SAMPLE_ID)? Or is there something else I need to do?

Thanks in advance, so far I have been very impressed with this version of Allegro as I get time to program with it.

---
“I love you too.” - last words of Wanda Roy

Arthur Kalliokoski
Second in Command
February 2005
avatar

They all watch too much MSNBC... they get ideas.

Neil Roy
Member #2,229
April 2002
avatar

unsigned int al_get_sample_instance_position(const ALLEGRO_SAMPLE_INSTANCE *spl)

which leads me to...

"To be played, an ALLEGRO_SAMPLE_INSTANCE object must be attached to an ALLEGRO_VOICE object, or to an ALLEGRO_MIXER object"

which then leads to pages on voices, mixers..... wow... maybe I'll figure out another way to do this before I pull all my hair out. ;)

---
“I love you too.” - last words of Wanda Roy

SiegeLord
Member #7,827
October 2006
avatar

Don't be such a drama queen.

Err... I didn't actually read OP... but you want al_get_sample_instance_playing probably...

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Matthew Leverton
Supreme Loser
January 1999
avatar

The interface you are using is a simpler, higher level version of the main API. It doesn't support all of the functionality.

An ALLEGRO_SAMPLE is just raw data (including some format information). Before you can play one, you must create an ALLEGRO_SAMPLE_INSTANCE that describes how to play it and the playback status. A single sample can be referenced by multiple sample instances.

When you call al_reserve_samples() you are really creating a default mixer and attaching that many sample instances to it. When you call al_play_sample() you are just updating one of those reserved sample instances to point to your sample data and starting it.

So if you want at the more advanced features, you basically have to do that all yourself.

I wouldn't be opposed to a function like al_is_sample_playing(ALLEGRO_SAMPLE_ID id), but at some point you are just rewriting the entire lower level API.

Neil Roy
Member #2,229
April 2002
avatar

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.

I'm not being a "drama queen", 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.

I do understand why some of this is needed. I guess the more flexible the library is, the greater need for documentation and examples.

Perhaps I can write a function to simplify it myself for future projects.

SiegeLord's example REALLY helped clarify what I needed to do though, thanks.

---
“I love you too.” - last words of Wanda Roy

Go to: