Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_load_sample's loading many data

This thread is locked; no one can reply to it. rss feed Print
al_load_sample's loading many data
JurekGP
Member #15,891
February 2015

Few days ago, I tried to include music files for my program. I wrote right function which read my file with headers of files music and same files.
I have not used music file yet. And now when I want load this 19 files (33 MB), I'm loading this samples and I get over 700 MB data in memory too.

I'm reading these files from my file:

#SelectExpand
1for(unsigned i = 0; i < songCount; i++) 2{ 3 buffer = new char[songs[i]->fileLength]; 4 file.read(buffer, songs[i]->fileLength); 5 alFile = al_open_memfile((void*)buffer, songs[i]->fileLength, "rb"); 6 ALLEGRO_SAMPLE *sample = al_load_sample_f(alFile, songs[i]->fileType.c_str()); 7 songs[i]->addSongFile(sample); 8 std::cout << &buffer << " " << alFile << " " << sample << std::endl; 9 al_fclose(alFile); 10 alFile = NULL; 11 delete[] buffer; 12 buffer = NULL; 13}

I thought that I do something wrong, and get memory leak. So I tried load these files "manually":

#SelectExpand
2ALLEGRO_SAMPLE *sample = NULL; 3sample = al_load_sample("music.ogg"); 4songs[0]->addSongFile(sample); 5sample = al_load_sample("music.ogg"); 6songs[1]->addSongFile(sample); 7sample = al_load_sample("music.ogg"); 8songs[2]->addSongFile(sample); 9sample = al_load_sample("music.ogg"); 10songs[3]->addSongFile(sample); 11sample = al_load_sample("music.ogg"); 12songs[4]->addSongFile(sample); 13sample = al_load_sample("music.ogg"); 14songs[5]->addSongFile(sample); 15sample = al_load_sample("music.ogg"); 16songs[6]->addSongFile(sample); 17sample = al_load_sample("music.ogg"); 18songs[7]->addSongFile(sample); 19sample = al_load_sample("music.ogg"); 20songs[8]->addSongFile(sample); 21sample = al_load_sample("music.ogg"); 22songs[9]->addSongFile(sample); 23sample = al_load_sample("music.ogg"); 24songs[10]->addSongFile(sample); 25sample = al_load_sample("music.ogg"); 26songs[11]->addSongFile(sample); 27sample = al_load_sample("music.ogg"); 28songs[12]->addSongFile(sample); 29sample = al_load_sample("music.ogg"); 30songs[13]->addSongFile(sample); 31sample = al_load_sample("music.ogg"); 32songs[14]->addSongFile(sample); 33sample = al_load_sample("music.ogg"); 34songs[15]->addSongFile(sample); 35sample = al_load_sample("music.ogg"); 36songs[16]->addSongFile(sample); 37sample = al_load_sample("music.ogg"); 38songs[17]->addSongFile(sample); 39sample = al_load_sample("music.ogg"); 40songs[18]->addSongFile(sample);

For simplification I loading one file 19 times.

Two this ways doesn't load sample file correctly. After loading I should get about 33 MB data, but I get over than 700 MB.

Did you heard about problem like this? I use Allegro 5.0.10. I tried load this way in two projects to be sure.

If you understand, I send link for my topic about this on a Polish forum PC Format: http://forum.pcformat.pl/C-Allegro-5-Dealokacja-pliku-ALLEGRO-FILE-t

Hi! I'm Pole and my english is not very well. So, please understanding to me. :)

Thomas Fjellstrom
Member #476
June 2000
avatar

Ogg is compressed. al_load_sample loads the audio and decompresses it into raw PCM data.

If you want to use music, you're best off using streaming[1]

--
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

JurekGP
Member #15,891
February 2015

Ok, thanks! :)
Now I know why I get so many megabytes of data.
But I'd ask if I understand everything correctly and I'm doing everything what I should.

There is cool example which I base: ex_stream_file, but I'm not sure that I'm understanding everything right.

So, now I would like to summary what I centrainly do. This is test project, but there I include every step, which I want implement in my game. Only difference that I read music files from one file by al_load_audio_stream_f.

1. When I'm initializing Allegro, I turn on audio streaming. I'm saving a frequency, mixerDepth, voiceDepth and channel conf. I'm creating mixer and voice.
2. In my player I will have pointer on ALLEGRO_AUDIO_STREAM - *musicStream. On begin set on NULL.
3. Now when I want to handle stream:
a) If I want play stream, I call my function playStream:

#SelectExpand
1void TestGame::playStream(std::string filePath) 2{ 3 if(musicStream) 4 al_destroy_audio_stream(musicStream); 5 musicStream = al_load_audio_stream(filePath.c_str(), 4, 2048); 6 al_register_event_source(gameCore->alManager->getAllegroEventQueue(), al_get_audio_stream_event_source(musicStream)); 7 al_attach_audio_stream_to_mixer(musicStream, gameCore->alManager->getAllegroMixer()); 8}

b) If I want change volume, call this function: al_set_audio_stream_gain(musicStream, 'float value');
c) If I want stop stream, call this function: al_detach_audio_stream(musicStream);

Two asks:
1. How is difference between function above and al_drain_audio_stream? What should I use?
2. I'm not sure what means second and third argument of al_load_audio_stream.
- Second is probably size (on kilobytes?) of stream, which will be loading to play like one piece of stream.
- Third is for information how many streams can play at the same time, through my pointer musicStream? Can I change to 1? At the same time I will playing only one sample. Maybe is nonsense give 2048?

So, this is everything what I should do for correctly handling audio stream in Allegro? Of course, in the end, I'm destroing audio stream, mixer and voice.

Hi! I'm Pole and my english is not very well. So, please understanding to me. :)

Go to: