alsa sound output with c

how to send data to the sound card for sound generation


general process

load and prepare the device

write bits to a buffer


sample: a single number for volume. not a sound on its own. when numbers from -1 to 1 are used it is similar to the state of a speaker membrane

sampling rate: designates how many samples are to be used per second

alsa specific

frame: a group of one sample per channel

buffer: stores a series of samples specifying volume over time. the playback speed depends on the sampling rate

sample data format

interleaved: one buffer with a sample for each channel successively. "lrlrlrlr". might be more robust

non-interleaved: separate buffer for each channel. "lllll" "rrrrr". might be more efficient

transfer schemes

there are different ways when and how sample data is transferred to alsa and the sound card

significant differences seem to concern the handling of buffer over/underruns and performance in the case of the mmap interface

examples for several transfer schemes can be found here: pcm.c

i do not know the benefits of asynchronous or callback transfer

minimal example in c


the #include statement needs replacement when the code is not compiled from inside the alsa-lib source directory

all alsa features become available when the header-file is included

#include <alsa/alsalib.h>


gcc -lasound pcm_min.c


the sound data is send within the for-loop "for (i = 0; i < 16; i++)"

the same example in _


alsa library

use with guile

a guile module that uses some alsa features and provides scheme procedures can be written

creating a complete binding to the alsa library would be time expensive because the library is big

alsa and related software

alsa: communicates with the sound card using drivers

link-ehttp://en.wikipedia.org/wiki/Open_Sound_System#OSS_in_relation_to_ALSAoss: older alsa alternative. has licensing issues and received other criticisms over time so that currently gnu/linux distributions usually exclude it

jack and pulseaudio: offer higher level features, for example passing sound data between applications. they use libraries like alsa for sound output

tags: programming start document guide computer general c audio alsa sc