Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Successful but silent audio playback

Credits go to Matias Persson and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Successful but silent audio playback
Edgar Reynaldo
Member #8,592
May 2007
avatar

This code by itself works, and is directly copy pasted out of my program. But it doesn't work within my program. al_play_sample and al_play_sample_instance both return true, but there is no sound in my actual program where there is in this simple test program. It's kind of mysterious. Any ideas why this might be?

#SelectExpand
1 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_audio.h> 4#include <allegro5/allegro_acodec.h> 5 6 7#include <cstdio> 8 9 10int main(int argc , char** argv) { 11 12 if (!al_init()) {return 1;} 13 if (!al_install_audio()) {return 2;} 14 if (!al_init_acodec_addon()) {return 3;} 15 if (!al_reserve_samples(1)) {return 4;} 16 17 ALLEGRO_SAMPLE* idle_sound = al_load_sample("Annoy1.ogg"); 18 19 ALLEGRO_SAMPLE_INSTANCE* sample_instance = al_create_sample_instance(idle_sound); 20 21 if (!idle_sound || !sample_instance) { 22 printf("Setup error.\n"); 23 return 5; 24 } 25 26 ALLEGRO_SAMPLE_ID sample_id; 27 28 if (!al_play_sample(idle_sound , 1.0 , 0.0 , 1.0 , ALLEGRO_PLAYMODE_ONCE , &sample_id)) { 29 printf("Failed to play sample.\n"); 30 } 31 32 if (!al_play_sample_instance(sample_instance)) { 33 printf("Failed to play sample instance.\n"); 34 } 35 36 al_rest(5.0); 37 38 al_destroy_sample_instance(sample_instance); 39 al_destroy_sample(idle_sound); 40 41 return 0; 42}

Matias Persson
Member #15,093
May 2013

Just checked it out.

Failed to play sample instance.

But I can hear the sound file playing.

Just thought I'd let you know.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Matias Persson
Member #15,093
May 2013

I tested the given code with Allegro 5.0.10.

May I ask, not to go off topic or anything, but how did you compile 5.1.11 for MinGW, I tried before and failed and I would like to try out some Android Programming with Allegro.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Compiling A5.1.11 with MinGW worked fine for me. I just used CMake, and make like normal. If you need help, post a new thread with your errors, and I'll help you as best I can. The hardest part is getting the dependencies compiled.

Btw, MinGW is for windows, but to compile on Android you use gcc IIRC, which shouldn't be much different.

Still don't know why there is no sound. Ideas?

Matias Persson
Member #15,093
May 2013

Nope I have no idea why this could happen, everything seems to appear fine to me, except for the debugging output.

Edgar Reynaldo
Member #8,592
May 2007
avatar

SiegeLord
Member #7,827
October 2006
avatar

It's on my TODO list to look into this, sorry it's been a busy week :-[

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

Hey, no problem. I appreciate all the help I can get. You can download a 7z of my project where the audio is silent from here (includes win32 binary and src) :

http://members.allegro.cc/EdgarReynaldo/ks/StarProject.7z

Press space to start the experiment and then move the mouse around the inside of the star. Audio is supposed to play when the mouse is idle for more than about 3-5 seconds, but it is quiet. I don't believe I have any memory corruption or overruns, so I don't really know what would explain it.

Thanks for looking into this. ;)

SiegeLord
Member #7,827
October 2006
avatar

Alright, so the issue was that in your game you called al_restore_default_mixer(). This actually returns a bool, which if you checked it, you'd have seen it returned false. It turns out that there was a bug in Allegro when al_restore_default_mixer() was called twice (it's called the first time when you call al_reserve_samples) which resulted in the default mixer getting destroyed, but not cleared (which let the al_play_sample calls succeed).

It's all fixed now in 5.1 HEAD.

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

SiegeLord,

That fixed one issue, and now al_play_sample works correctly, but al_play_sample_instance is still silent, even though it returns true. (Note: I am still using 5.1.11, as I haven't rebuilt A5 from Git yet)

I've uploaded an updated 7z of the source and exe to :
http://members.allegro.cc/EdgarReynaldo/ks/StarProject.7z

SiegeLord
Member #7,827
October 2006
avatar

That's just because you haven't attached it to a mixer. I do wonder if maybe it should return false in that case...

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

SiegeLord
Member #7,827
October 2006
avatar

Yep, that's right. This whole attaching things to the mixer is why the simpler api ( al_play_sample ) exists. This is in principle documented in the ALLEGRO_SAMPLE_INSTANCE entry, but perhaps there should note of this in al_play_sample_instance 's documentation as well?

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

Yeah, that's pretty confusing. It should definitely be noted in al_play_sample_instance that you have to attach a sample instance to a voice or mixer. I didn't think to look at the docs for ALLEGRO_SAMPLE_INSTANCE. It just didn't occur to me, as I figured everything I needed to know to play a sample instance would have been documented in the al_play_sample_instance docs. In that section of the docs, it should reference al_attach_sample_instance_to_voice as well as al_attach_sample_instance_to_mixer.

Go to: