Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] multiple music files / playlist

This thread is locked; no one can reply to it. rss feed Print
[A5] multiple music files / playlist
Geoff Ledak
Member #13,160
August 2011

I'm currently working on adding music to my game project and I'm wondering if a certain feature is possible with Allegro 5.

I have these 2 ogg files:
http://geoffledak.com/sidescroller/60a.ogg
http://geoffledak.com/sidescroller/60.ogg

60a.ogg is an intro to the song, and 60.ogg is the rest of it. The idea is that you play 60a.ogg once at the beginning and then right when it ends you start playing 60.ogg and loop 60.ogg repeatedly.

I've found several tutorials that explain how to add music to an allegro 5 project but haven't been able to find anything regarding what I want to do. One of the allegro playmodes is ALLEGRO_PLAYMODE_BIDIR, which so far I haven't been able to find out what it does.

Any advance would be helpful.

Raidho36
Member #14,628
October 2012
avatar

Just create an array of songs, and once first is finished, start the second one.

Todd Cope
Member #998
November 2000
avatar

Instead of having separate Oggs for the intro and looping parts, just have it all in one Ogg and set loop points with al_set_audio_stream_loop_secs().

ALLEGRO_PLAYMODE_BIDIR is for looping back and forth between the two loop points, playing forward until it hits the end point, playing in reverse until it hits the start point, and on and on. I don't think it even works with streams, though I am not certain.

If you really needed to use two separate Oggs, you will have to decode the Oggs yourself and use the audio stream API. When you reach the end of the first Ogg, open the second Ogg and continue filling the stream fragments from that until they are full.

Geoff Ledak
Member #13,160
August 2011

Raidho36, could you be more specific?

Todd Cope
Member #998
November 2000
avatar

If you do it Raidho36's way there will be gaps in the audio. The only way to get it to work without gaps is to use Allegro's looping capabilities or to decode the Ogg yourself (using the Vorbis API, of course) and use Allegro's audio stream API.

Raidho36
Member #14,628
October 2012
avatar

@Todd Cope
It can be done seamless way. One can use crossfade, for example.

@Geoff Ledak
First, you create an array of paths to your music files. It could be as well simply two variables where each stores right file name.

Create a stream using first file name. Then set up a new timer to the time slightly before playback is supposed finis. Also listen to playback end event of the stream source. When timer is ring, create another stream, from the second file name, loop it and stop it. As soon as playback end event of first stream is reached, simply start playback of another stream. By this time you can discard timer and first stream event source. You may also create crossfade mentioned, it's pretty simple: once crossfade beginning point is reached you lower first sample volume and raise second sample volume until ending point is reached. Note that by the time that crossfade is started, both samples needs to be playing. But that introduces mandatory samples syncronizaiton, which could be a hard task to accomplish.

Todd Cope
Member #998
November 2000
avatar

Without more info, I can only assume Geoff is trying to play the intro and go straight into the looping section with no gap. Crossfade will not work in this case. Unless you are filling the stream buffers yourself, you will almost always get a gap in the audio.

Audio streams work by filling up buffers and sending them off to the mixer or audio device (Allegro's audio codec add-on does this behind the scenes). When you start playback of the second stream after the first one ends, the second stream will not continue to fill up the buffer that the first one didn't get done filling up before it ended. This is where the gap comes from.

60a.ogg is an intro to the song, and 60.ogg is the rest of it. The idea is that you play 60a.ogg once at the beginning and then right when it ends you start playing 60.ogg and loop 60.ogg repeatedly.

Unless Geoff is planning on doing something more sophisticated or he is using data that he has no control over, it makes a lot more sense to just keep the two parts together in one Ogg and use Allegro's looping functionality. It works great for this type of thing.

Raidho36
Member #14,628
October 2012
avatar

Well, yes, that way is definitely simplier, though mandatory gaps presence without manually streaming the data is a subject for debate.

Personally I don't suffer from such problems because I use module music, where looping is done in a natural way with Dxx, which simply advances the cursor by xx rows, combination with wrapping around by default creates a playback start offset. It is yet to see how would it work with Allegro though.

Go to: