|
[Allegro 5] Multiple Audio Questions |
RPG Hacker
Member #12,492
January 2011
|
I have multiple questions regarding Audio output with Allegro 5.
That's it for now.
|
Matthew Leverton
Supreme Loser
January 1999
|
A sample instance is a single instance of a sample. So no, you cannot play a sample instance multiple times in parallel. You create one sample and many instances. al_play_sample() does this for you automatically, and works fine for simple sound effects. I'm not sure if there is a loop or finished event. Try listening to the stream's events to see. Otherwise, you could poll it to see if it is still playing, assuming you don't have it set to loop. |
RPG Hacker
Member #12,492
January 2011
|
Matthew Leverton said: A sample instance is a single instance of a sample. So no, you cannot play a sample instance multiple times in parallel. You create one sample and many instances. al_play_sample() does this for you automatically, and works fine for simple sound effects. But can I use al_play_sample() in combination with a mixer? I do need one for the sound effects. What about creating sample instances on the fly whenver I need them? Can I do that or is that impossible due to being too time consuming or whatever? Or wait... Can I just use al_set_default_mixer() in combination with my Sound Mixer? After all I don't think I'll be using any samples on my Music Mixer. Also what's a good number for al_reserve_samples()? Do I have to prevent using a too high number? Quote: Otherwise, you could poll it to see if it is still playing, assuming you don't have it set to loop. So does al_get_audio_stream_playing() return false if a stream has ended?
|
Matthew Leverton
Supreme Loser
January 1999
|
You need to reserve enough with al_reserve_samples() to cover the number of samples you will be playing simultaneously. There's little overhead involved, other than the tiny bit of memory used by an instance and the linear search that is done when using an ALLEGRO_SAMPLE_ID. If you run out of reserved instances, then al_play_sample() will return false. So play around to figure out what you need in your application. RPG Hacker said: So does al_get_audio_stream_playing() return false if a stream has ended? I don't know. Try it. If the stream's play mode is set to ONCE, then probably. |
RPG Hacker
Member #12,492
January 2011
|
Matthew Leverton said: You need to reserve enough with al_reserve_samples() to cover the number of samples you will be playing simultaneously. So I pretty much have to guess? For example: I have to assume that not more than X sound effects will ever be played at the same time? And is there some way to set sample priority like in Allegro 4 to prevent a sample from not getting played when all slots are used up?
|
Matthew Leverton
Supreme Loser
January 1999
|
There is no built-in concept of priority. Anything you play with al_play_sample_instance() will get played. So you can use that for top priority samples. Or you can just reserve a sufficiently high number of samples. Are you really going to have (for example) 100 samples playing at the same time? Would it even sound good? |
RPG Hacker
Member #12,492
January 2011
|
Probably not, but at least up to 10 sound effects at the same time doesn't seem that unlikely to me. Well, I guess I'll just go with the sample instance method than and create them on the fly, storing each new instance in a vector or something. After they've played through I'll then just destroy them again. Seems to best solution for my needs.
|
Elias
Member #358
May 2000
|
I'd also go with the sample instance method. My method uses fixed ones though. E.g. if I say I want at most 8 bullet sounds at the same time and at most 4 explosion sounds at the same time, I create 8 bullet instances and 4 explosion instances. Then when I play a bullet sound and there is an unused instance, I use that. Otherwise I stop the oldest of the 8 playing bullet sounds and replace it with the new one. Releated to priority, A5 also makes it very easy to use different mixers. For example, use one mixer for sound effects, one mixer for user-interface sounds, one for voice overlays, one for ambient sounds, one for music. Then attach all of those mixers to the default mixer. That way you can control the volume of all your sound channels independently (e.g. to allow the user setting the volumes in the configuration). Or also do things like lower the effects volume while a user interface sound is playing. -- |
RPG Hacker
Member #12,492
January 2011
|
True. I could just create additional mixers for all important samples like voices (if I had any). So that's one way to handle priority. EDIT: 1 audioDevice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
2 mixerMaster = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
3 mixerMusic = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
4 mixerSounds = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
5
6 [...]
7
8 if (audioDevice == NULL || mixerMaster == NULL || mixerMusic == NULL || mixerSounds == NULL ||
9 !al_attach_mixer_to_mixer(mixerMusic, mixerMaster) ||
10 !al_attach_mixer_to_mixer(mixerSounds, mixerMaster) ||
11 !al_attach_mixer_to_voice(mixerMaster, audioDevice))
12 {
13 errorCode = audioDeviceError;
14 }
And to play a sound I use: 1void CAudio::PlaySound(ALLEGRO_SAMPLE* soundSample)
2{
3 if (soundSample != NULL)
4 {
5 ALLEGRO_SAMPLE_INSTANCE* newInstance = al_create_sample_instance(soundSample);
6 al_set_sample_instance_playmode(newInstance, ALLEGRO_PLAYMODE_ONCE);
7 al_attach_sample_instance_to_mixer(newInstance, mixerSounds);
8 al_play_sample_instance(newInstance);
9
10 [...]
11 }
12}
|
Matthew Leverton
Supreme Loser
January 1999
|
Does ALLEGRO_AUDIO_DEPTH_FLOAT32 for the mixers help? |
RPG Hacker
Member #12,492
January 2011
|
Matthew Leverton said: Does ALLEGRO_AUDIO_DEPTH_FLOAT32 for the mixers help? It does indeed. The buzzing is completely gone now. Thank you! Now I only get stuttering when a lot of sound effects are playing, but that's OK and not too annoying actually. Also this reminds me: When using al_load_sample() or al_load_stream() to load music files from my disk: Do I have to care about their format? Like, do they have to match the mixers' format in some kind or can they be in any format?
|
Matthew Leverton
Supreme Loser
January 1999
|
The stuttering can be overcome by increasing the number or size of buffers via the Allegro config options. (Each driver has its own settings, unfortunately...) It will increase the latency of course, but that's usually not very noticeable when simply playing sound effects. You can load any format of audio file when it is attached to a mixer. |
RPG Hacker
Member #12,492
January 2011
|
Matthew Leverton said: The stuttering can be overcome by increasing the number or size of buffers via the Allegro config options. Well, as I said the stuttering (I think stuttering is not even the right word for this, but I can't think of a better one) isn't that bad plus I was testing under extreme conditions (probably around 30 sound effects at the same time). That number is way more than I'll ever need, so I'll just stay with the current settings. Even if I had that many sound effects it's barely noticable (and probably is only the overlapping of all sound effects, anyways). And thanks for the help again!
|
|