Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » music in allegro5

This thread is locked; no one can reply to it. rss feed Print
music in allegro5
adamk kromm
Member #5,432
January 2005

So i've never done anything with music before... samples/voices/mixers... mean nothing to me I'm dumb!...

so by alot of reading the api and other things i came up with this

al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT);
ALLEGRO_SAMPLE *test = al_load_sample_wav("music/simple.wav");
if(!test)
    cout<<"couldnt load music"<<endl;
else
    al_play_sample(test,255, 128, 1000, 0,NULL);

but it doesnt work...
1. it says undefined reference to al_install_audio. (i've got allegro5/acodec.h included and liba5_acodec-4.9.12.dll.a linked... am i mission something?
2. al_play_sample gives me the same error... undefined reference...

i looked for other files to link to and include in the project but didnt find anything that looked like it would help.

----------
-Adam Kromm

My Website

Thomas Fjellstrom
Member #476
June 2000
avatar

You'll want to link to kcm_audio as well. acodec just provides audio decoders for popular formats. At some point I'll probably be renamed to better fit the current naming scheme.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

adamk kromm
Member #5,432
January 2005

perfect, now it compiles and i dont get any errors, but i dont hear anything :(

EDIT:

after more reading i've changed it to this;

al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT);
    if(!al_reserve_samples(5))
        cout<<"couldnt reserve samples"<<endl;

    ALLEGRO_SAMPLE *test = al_load_sample("music/simple.wav");
    ALLEGRO_SAMPLE_ID testid;
    if(!test)
        cout<<"couldnt load music"<<endl;

    al_play_sample(test,1, ALLEGRO_AUDIO_PAN_NONE, 1, 1, &testid);

but still no sound...

----------
-Adam Kromm

My Website

weapon_S
Member #7,859
October 2006
avatar

if(!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT))
  cout<<"Apparently can't find a driver"<<endl;

Maybe? I don't know about a5 error messages...

Trent Gamblin
Member #261
April 2000
avatar

Post a test case. What you have should work unless there's something else wrong in your code.

Secondly, you're better of streaming your music than loading it all into memory. A 1 minute sample is going to take huge amounts of memory.

Matthew Leverton
Supreme Loser
January 1999
avatar

Use ALLEGRO_PLAYMODE_ONCE or ALLEGRO_PLAYMODE_LOOP for the fifth argument. I don't think either of them are defined to 1.

The last parameter can be NULL if you never need to explicitly stop it.

Also, some wav files won't load in 4.9.12 if they have extended information embedded in them. But most should work.

adamk kromm
Member #5,432
January 2005

ok here is what i got now.

if(!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT))
        cout<<"couldnt install audio"<<endl;
    if(!al_reserve_samples(2048))
        cout<<"couldnt reserve samples"<<endl;

    ALLEGRO_SAMPLE *test = al_load_sample("music/simple.wav");
    ALLEGRO_SAMPLE_ID testid;
    if(!test)
        cout<<"couldnt load music"<<endl;

    al_play_sample(test,1.0, ALLEGRO_AUDIO_PAN_NONE, 1.0, ALLEGRO_PLAYMODE_ONCE , &testid);

and it works now!... kinda. after adding ALLEGRO_PLAYMODE_ONCE... but it sounds really jacked up... then crashes haha.

how do i stream it instead of loading it all?

----------
-Adam Kromm

My Website

Matthew Leverton
Supreme Loser
January 1999
avatar

al_stream_from_file

adamk kromm
Member #5,432
January 2005

did that and now i dont get any noise, or any errors... bad .wav? it works in itunes...

btw: what do i put in al_reserve_samples() (or do i even need that anymore?)
and what should i put for the first two args to al_stream_from_file()?

----------
-Adam Kromm

My Website

Matthew Leverton
Supreme Loser
January 1999
avatar

Did you attach the stream to a mixer? Check out ex_stream_file.c.

al_reserve_samples() sets up a default voice and mixer and is required for al_play_sample().

If you set up your own voice and mixer and don't use al_play_sample(), then you don't need it.

adamk kromm
Member #5,432
January 2005

nope didnt attach the stream to a mixer... (stream, mixer, sample, voice, they all still mean very little to me, but they're starting to come together.)

EDIT: I now have this

#SelectExpand
1if(!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) 2 cout<<"couldnt install audio"<<endl; 3 if(!al_reserve_samples(2048)) 4 cout<<"couldnt reserve samples"<<endl; 5 6 ALLEGRO_VOICE* voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); 7 if(!voice) 8 cout<<"couldnt create voice"<<endl; 9 ALLEGRO_MIXER* mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 10 if(!mixer) 11 cout<<"couldnt create mixer"<<endl; 12 if(!al_attach_mixer_to_voice(voice, mixer)) 13 cout<<"couldnt attach mixer to voice"<<endl; 14 ALLEGRO_STREAM *stream = al_stream_from_file(4,2048,"music/simple.wav"); 15 if(!stream) 16 cout<<"couldnt start the stream"<<endl; 17 if(!al_attach_stream_to_mixer(mixer, stream)) 18 cout<<"Couldnt attach stream to mixer"<<endl;

but still no sound or errors... i copied ex_stream_file.c as best i could.

EDIT: EDIT: is there someway to change the volume or something?? and for al_stream_from_file, how do i know what to put for the first two arguments?

EDIT: EDIT: EDIT: Just found out the wav file was junk :P

----------
-Adam Kromm

My Website

Matthew Leverton
Supreme Loser
January 1999
avatar

  • voice => a direct line to hardware


  • mixer => takes multiple audio sources, mixes them together, and sends it to a voice


  • stream => produces audio via a small memory buffer that must be filled in real time


  • sample => an audio clip that can be played directly via al_play_sample()


  • sample instance => a "copy" of a sample that can be controlled and played back with more flexibility


  • http://alleg.sourceforge.net/a5docs/refman/kcm_audio.html#al_set_stream_gain, etc


  • When you call al_reserve_samples(), a stereo (2 channel) voice and mixer are created for you. You can use al_get_default_mixer() to retrieve it.


  • 4.9.13 has better support for loading wav files.

adamk kromm
Member #5,432
January 2005

perfect thanks! Just what i needed to know!

----------
-Adam Kromm

My Website

Thomas Fjellstrom
Member #476
June 2000
avatar

4.9.13 has better support for loading wav files.

Technically its worse. While Elias added support for skipping unknown data in the wav, Peter decided it was ok to remove support for libsndfile, which is what supported encoded wav files (ADPCM etc). So now all we're going to get is plain old un encoded wav support (back to how it was in A4).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

I won't be including libsndfile in any Allegro.cc binary distributions, so I don't care either way.

Peter Wang
Member #23
April 2000

I don't think we need an official sndfile addon. ADPCM wave files would be the strongest reason, but in a game you can pick which file formats you want to use, and I can't see why you'd use ADPCM wave files over FLAC or Ogg Vorbis, if you are concerned about the size of uncompressed PCM. The sizes of the FLAC and sndfile libraries are nearly the same, and Ogg Vorbis is smaller than either:

Quote:

-rwxr-xr-x 1 root root 328704 2008-09-11 09:28 libFLAC.so.8.2.0
-rwxr-xr-x 1 root root 16404 2007-02-16 17:19 libogg.so.0.5.3
-rwxr-xr-x 1 root root 369228 2009-03-19 14:17 libsndfile.so.1.0.18
-rwxr-xr-x 1 root root 165380 2007-09-24 05:04 libvorbis.so.0.4.0
-rwxr-xr-x 1 root root 30600 2007-09-24 05:04 libvorbisfile.so.3.2.0

Not to mention ADPCM sounds crap.
(edit: in my experience, which might be due to the implementations, plus it's been a long time)

Thomas Fjellstrom
Member #476
June 2000
avatar

Yeah, I can't say why someone would pick ADPCM or other encoded wav formats over plain old Ogg, flac, or mp3 for that matter. And heck, one of these days someone should add a Speex loader as well.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: