C++ – Using ALSA’s function snd_pcm_writei can I free the sample buffer right away

alsaclinux

Using ALSA to play audio, after calling snd__pcm__writei, can I free the sound sample buffer right away or do I need to wait until the sound is finished playing before I can free the sample buffer?

For example:

unsigned short *buffer;

buffer = malloc(size of sample to play);
...load data into buffer...
snd_pcm_writei (playback_handle, buffer, size of sample)
free(buffer)

Best Answer

Easiest way to find out would be to start writing backwards from the end of the buffer and see if you affect the audio playback. If you do then you definitely can't free the buffer. If it makes no difference then you can safely free the buffer as the sound card is not reading from that particular block of memory.