al_create_sample(3alleg5)

πŸ“¦
version 5.2.11.3 Β· 4 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

al_create_sample πŸ’–βœ¨

okay bestie, this is from the Allegro 5 API manual β€” yes, the audio one, and yes, it's giving main character energy because this is the function that literally makes samples exist. πŸŽ€


NAME πŸ¦‹

al_create_sample β€” Allegro 5 API


SYNOPSIS ✨

#include <allegro5/allegro_audio.h>

ALLEGRO_SAMPLE *al_create_sample(void *buf, unsigned int samples,
   unsigned int freq, ALLEGRO_AUDIO_DEPTH depth,
   ALLEGRO_CHANNEL_CONF chan_conf, bool free_buf)

DESCRIPTION πŸ’…

So basically, this function builds a sample data structure out of the buffer you hand it. That's it, that's the vibe. 🌸

Now listen β€” free_buf is the one you have to pay attention to, fr fr. If free_buf is true, then the buffer gets freed with al_free(3alleg5) when the sample data structure gets destroyed. But for portability (and especially on Windows, no cap πŸ‘€), the buffer should have been allocated with al_malloc(3alleg5). If you don't do that, then you're on your own β€” you have to free the sample data yourself. Not me forgetting to free it and leaking memory πŸ˜­πŸ’€


okay so what even IS a "sample" here 🧚

A sample that's referred to by the samples parameter refers to a sequence of channel intensities. Like, if you're making a stereo sample with samples set to 4, then the layout of the data in buf is gonna be:

LRLRLRLR

Where L and R are the intensities for the left and right channels respectively. So one single sample, in this example, refers to the LR pair. Iconic little pair, honestly. 🫢


how to allocate a buffer of the right size 🌈

To allocate a buffer of the correct size, you can do something like this:

int sample_size = al_get_channel_count(chan_conf)
                  * al_get_audio_depth_size(depth);
int bytes = samples * sample_size;
void *buffer = al_malloc(bytes);

Slay. Now you got your bytes, now you got your buffer, now you go make a sample. πŸ’–


SEE ALSO ⭐


and that's the whole page bestie, every flag, every arg, every byte, untouched, periodt πŸ’…

mwah πŸ’‹ β€” this was the <b>Allegro reference manual</b> page for al_create_sample (that's the 3alleg5 fam, Allegro 5 era), and honestly? ate. bye bestie, go make some noise 🎧πŸ”₯

πŸ“¦ where this page comes from