Audio addon: pause sample
Neuton Mouse

There's al_play_sample and al_stop_sample... which is good but what to do if you want to pause a sample and then resume it?

al_stop_sample complete wipes the sound which means that repeated playback will start from very beginning.


The typical solution to these kinds of questions is to switch to the more advanced API. Instead of al_play_sample, you can do this:

ALLEGRO_SAMPLE* sample = al_load_sample(...);

// Start a new sample playback.

ALLEGRO_SAMPLE_INSTANCE* instance = al_create_sample_instance(sample);
al_attach_sample_instance_to_mixer(instance, al_get_default_mixer());
// Optionally set playmode, gain, pan etc.
al_set_sample_instance_playing(instance, true);

Then you can pause it using al_set_sample_instance_playing.

Neuton Mouse

Thanks a lot SiegeLord. That actually works.


Somewhat strange audio API. What's the difference between all these audio structs? ALLEGRO_SAMPLE holds the data, al_play_sample is just a shortcut wrapper (i think), what's up with audio instances and id?

I'm having over 100 sounds at the moment in the project, would that method be appropriate as initialization: load sample, create instance, attach to mixer like 100 times?

UPD: Ok, i've done some forum searches. Everything's clear now.


Just to have this in sort of one place, ALLEGRO_SAMPLE contains the actual data, ALLEGRO_SAMPLE_INSTANCE contains the current play position, pan, speed and things like that (importantly, it allows you to have several of those for each ALLEGRO_SAMPLE). ALLEGRO_SAMPLE_ID is essentially an index to an internal array of ALLEGRO_SAMPLE_INSTANCE.

In my games I typically make a vector of ALLEGRO_SAMPLE_INSTANCE and perhaps cap it at some number (only for the purposes of not washing out the sound, there shouldn't be much performance issues). Sometimes I add a priority to these sounds, so that if a new sound is played at a low priority, it won't kick out something at a higher priority. This also allows me to do positional audio (I store the physical location for each instance, and then adjust the pan and volume in relation to where the camera is).

Thread #615910. Printed from