Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple sound playback using ALSA API and libasound
* Simple sound playback using ALSA API and libasound.
* Compile:
* $ cc -o play sound_playback.c -lasound
* Usage:
* $ ./play <sample_rate> <channels> <seconds> < <file>
* Examples:
* $ ./play 44100 2 5 < /dev/urandom
* $ ./play 22050 1 8 < /path/to/file.wav
* Copyright (C) 2009 Alessandro Ghedini <>
* --------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Alessandro Ghedini wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can
* buy me a beer in return.
* --------------------------------------------------------------
#include <alsa/asoundlib.h>
#include <stdio.h>
#define PCM_DEVICE "default"
int main(int argc, char **argv) {
unsigned int pcm, tmp, dir;
int rate, channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff;
int buff_size, loops;
if (argc < 4) {
printf("Usage: %s <sample_rate> <channels> <seconds>\n",
return -1;
rate = atoi(argv[1]);
channels = atoi(argv[2]);
seconds = atoi(argv[3]);
/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
printf("ERROR: Can't open \"%s\" PCM device. %s\n",
PCM_DEVICE, snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
else if (tmp == 2)
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
printf("seconds: %d\n", seconds);
/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);
buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) {
if (pcm = read(0, buff, buff_size) == 0) {
printf("Early end of file.\n");
return 0;
if (pcm = snd_pcm_writei(pcm_handle, buff, frames) == -EPIPE) {
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
return 0;

mark222 commented Jan 3, 2015

Hi, just came across this as I am trying to learn ALSA programming. This all makes sense to me except the reading of the WAV file input... it does not seem to read the WAV headers, it just reads starting from the first byte and interprets them as sound samples. But a WAV file has metadata before the sample data starts (e.g. "RIFF...WAVE..., etc).

Yeah, WAV-file has a header which contains metadata, the player should skip this section by fseek(fp,44, SEEK_SET);

You can add reflexive structure for a wav to handle and cast the 44 bytes to it so you`ll fill it with sample rate, fmt, RIFF, data, etc.

Sorebit commented Aug 26, 2016

Is this actually the same as aplay?

tvlooy commented Sep 13, 2016

having a small simple example when you start learning something is priceless. Thanks for sharing this

aherlas commented Sep 3, 2017 edited

according to the link below:

WAVE files often have information chunks that precede or follow the sound data (data chunk). Some programs (naively) assume that for PCM data, the preamble in the file header is exactly 44 bytes long (as in the table above) and that the rest of the file contains sound data.
This is not a safe assumption.

and taking into consideration that there are a few format (like ~128) code for the RIFF file and each can have a different fmt chunk, with different sizes, then making the assumption that the data chunk will start from 44 it is not safe.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment