Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Problem with streaming sound

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] Problem with streaming sound
TeaRDoWN
Member #8,518
April 2007
avatar

I've tried to load and stream the music in my game and the file is loaded but it never starts to play. I've checked the "ex_stream_seek example" and I don't see any other functions being used that should relate to this issue.

As far as I've read in the manual only the al_load_audio_stream is needed to load and start streaming the file.

ALLEGRO_AUDIO_STREAM *music;
...
music = al_load_audio_stream(path, 4, 2048);
al_set_audio_stream_playmode(music, ALLEGRO_PLAYMODE_LOOP);
al_set_audio_stream_playing(music, true);

I've tested to load the entire ogg file into memory with al_load_sample and play it with al_play_sample and that work as intended.

Another question, how big sound files are reasonable to load into memory and when is it time to start streaming? Currently the ogg files in my game are ranging from 1.5Mb to 4Mb. Stream or load whole file into memory?

AMCerasoli
Member #11,955
May 2010
avatar

TeaRDoWN said:

Currently the ogg files in my game are ranging from 1.5Mb to 4Mb. Stream or load whole file into memory?

You should definitely load them into memory. I think streaming is good for files > 50 Mb.

TeaRDoWN
Member #8,518
April 2007
avatar

Oh, hehe. Roger. ;)

Matthew Leverton
Supreme Loser
January 1999
avatar

Loading a sample will uncompress the file. The source size is not directly relevant. It's the length of the audio and its quality that affects how much RAM is used.

I would always stream background music.

Edgar Reynaldo
Member #8,592
May 2007
avatar

TeaRDoWN
Member #8,518
April 2007
avatar

Edgar: Tried this, changed my music from ALLEGRO_SAMPLE to ALLEGRO_AUDIO_STREAM and after loading the stream I used:

voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
music = al_load_audio_stream("music.ogg", 4, 2048);
al_attach_audio_stream_to_voice(music, voice);

As far as I can see in the ex_stream_file example this is all you need to do...or?

EDIT: Noticed the text "don't recommend attaching audio streams directly to voices" and that helped:

mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
music = al_load_audio_stream("music.ogg", 4, 2048);
al_attach_audio_stream_to_mixer(music, mixer);
al_attach_mixer_to_voice(mixer, voice);

When I want to start to stream a new music file what do I need to do? Just run al_detach_audio_stream or?

Edgar Reynaldo
Member #8,592
May 2007
avatar

TeaRDoWN said:

When I want to start to stream a new music file what do I need to do? Just run al_detach_audio_stream or?

Looks like it, and then attach your new audio stream to the same mixer. I haven't tried any of this stuff myself yet.

TeaRDoWN
Member #8,518
April 2007
avatar

Ok, I'll go with this for now and keep an eye out for memory leaks.

Matthew Leverton
Supreme Loser
January 1999
avatar

You need to destroy the stream at some point.

TeaRDoWN
Member #8,518
April 2007
avatar

Of course, when I start to stream a new file this is the first I do:

if (music != NULL)
{
  al_detach_audio_stream(music);
  al_destroy_audio_stream(music);
}

Go to: