al_create_audio_stream(3alleg5)
π al_create_audio_stream β the Allegro 5 API glow-up π
okay bestie, this is a page from the Allegro 5 API β the Allegro reference manual, to be exact β and today we're building an audio stream from scratch. let's go π
β¨ SYNOPSIS β¨
#include <allegro5/allegro_audio.h>
ALLEGRO_AUDIO_STREAM *al_create_audio_stream(size_t fragment_count,
unsigned int frag_samples, unsigned int freq, ALLEGRO_AUDIO_DEPTH depth,
ALLEGRO_CHANNEL_CONF chan_conf)
πΈ DESCRIPTION πΈ
She makes you an ALLEGRO_AUDIO_STREAM(3alleg5) β¨ and by default that stream is already set to play β like, it comes out the box ready, no extra steps. It feeds audio data out of a buffer, and that buffer gets chopped up into a bunch of fragments. it's giving "pizza sliced for the group chat" π
The parameters, bestie, one by one:
fragment_countβ how many fragments you want your audio stream to use. Usually you only need two β just split the audio buffer in half. But lowkey that means the only moment you're allowed to hand over new data is when one of the halves is done playing. So when you're out here using a lot of fragments, you'll usually make each one hold fewer samples, so there's always a (tiny) fragment sitting free and ready to be filled with fresh data. πfrag_samplesβ the size of a fragment, measured in samples. there's a whole note and explanation for this below, don't worry πfreqβ the frequency, in Hertz. yes queen, actual Hz.depthβ has to be one of the values listed over atALLEGRO_AUDIO_DEPTH(3alleg5). no off-menu orders πchan_confβ has to be one of the values listed over atALLEGRO_CHANNEL_CONF(3alleg5). same rules, bestie.
π¦ so what even IS a "sample" here
when frag_samples says "sample", it means a sequence of channel intensities. like, picture a stereo stream where frag_samples is set to 4 β then the data inside that fragment lays out like this:
LRLRLRLR
where L and R are the intensities for the left and right channels respectively. so a single sample is the LR pair in this example. iconic, honestly. π
π the delay math (iykyk)
the way fragment_count, frag_samples and freq are chosen directly influences the audio delay. the delay in seconds goes like:
delay = fragment_count * frag_samples / freq
and this is only the delay coming from Allegro's streaming, okay? there may be additional delay caused by sound drivers and/or hardware. not our fault, not our problem, but definitely our vibe check. π
Note: if you already know the fragment size in bytes, you can get the size in samples like this:
sample_size = al_get_channel_count(chan_conf) * al_get_audio_depth_size(depth);
samples = bytes_per_fragment / sample_size;
and the size of the complete buffer is:
buffer_size = bytes_per_fragment * fragment_count
Note: unlike a lot of Allegro objects, audio streams are not implicitly destroyed when Allegro shuts down. π not a drill. you must destroy them manually with al_destroy_audio_stream(3alleg5) before the audio system is shut down. do not skip this step, bestie, fr fr.
and that's the whole page, mwah π β this was al_create_audio_stream(3alleg5) straight out of the Allegro reference manual. no date on this one, just the manual name and the page name riding along the bottom, so consider this your sign-off: go make something that slays, and destroy your streams manually. periodt. π«Άβ¨
π¦ where this page comes from
- debian trixie: allegro5-doc 2:5.2.10.1+dfsg-1 Β· Zlib
- ubuntu questing: allegro5-doc 2:5.2.10.1+dfsg-1build1
- ubuntu resolute: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- ubuntu stonking: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms