[LAD] Alsa works fine the default output but distorts/stutters with hw:0/plughw:0

Muffinman news at koster.tk
Sun Jun 16 18:16:47 UTC 2013

Hello all,

I'm trying to add Alsa support to an application with mixed results.
When using the default audio output it sounds fine (my dac display 48KHz
as sampling rate, which doesn't equal the source signal of 44.1) but
when using hw:0 it is heavily distorted and with plughw:0 the sound
stutters at a steady interval of about 2Hz (in these last two cases my
dac display 44.1kHz as the sampling rate). When using libao plughw:0
sound fine (and my dac always display 44.1kHz).

Can anyone give me a direction (or answer) where I might go wrong here.
Unfortunately I'm a bit out of my league here and I do not yet
understand the data that fills buf[]. However, I do know that the data
it supplies works fine with libao, so I do suspect it is something with
the code below.

Thanks in advance, Maarten

ps. it is supposed to add Alsa to the following:

static void start(int sample_rate) {
    if (sample_rate != 44100)
        die("Unexpected sample rate!");
    int ret, dir = 0;
    snd_pcm_uframes_t frames = 32;
    ret = snd_pcm_open(&alsa_handle, alsa_out_dev,
    if (ret < 0)
        die("Alsa initialization failed: unable to open pcm device:
%s\n", snd_strerror(ret));
    snd_pcm_hw_params_any(alsa_handle, alsa_params);
    snd_pcm_hw_params_set_access(alsa_handle, alsa_params,
    snd_pcm_hw_params_set_format(alsa_handle, alsa_params,
    snd_pcm_hw_params_set_channels(alsa_handle, alsa_params, 2);
    snd_pcm_hw_params_set_rate_near(alsa_handle, alsa_params, (unsigned
int *)&sample_rate, &dir);
    snd_pcm_hw_params_set_period_size_near(alsa_handle, alsa_params,
&frames, &dir);
    ret = snd_pcm_hw_params(alsa_handle, alsa_params);
    if (ret < 0)
        die("unable to set hw parameters: %s\n", snd_strerror(ret));

static void play(short buf[], int samples) {
    int err = snd_pcm_writei(alsa_handle, (char*)buf, samples);
    if (err < 0)
        err = snd_pcm_recover(alsa_handle, err, 0);
    if (err < 0)
        die("Failed to write to PCM device: %s\n", snd_strerror(err));

More information about the Linux-audio-dev mailing list