[alsa-devel] Playback underrun recovery

Clemens Ladisch clemens at ladisch.de
Tue Apr 24 16:04:51 CEST 2012

Paul D. DeRocco wrote:
> I've got a synth program outputting directly to the main hw PCM device, with
> no intervening plugins. It uses about 75-80% of the time on one core, so it
> normally doesn't underrun. Once in a while, other burdens on the system
> trigger an underrun, and my program never recovers. My buffer contains two
> periods of 256 stereo frames (with 32-bit samples), which at 96KHz comes out
> to a little over 5.3ms total. When it fails, its CPU usage goes up to 100%
> on that core, and it spits out bursts of 5.3ms of audio (two full periods,
> not one), with about 4ms of silence between them.

When a PCM device is (re)started, the buffer is initially empty, so you
need more CPU to fill it.

What is your start threshold?

> My top level code consists of a loop that generates one period at a time:
>     while (true) {
>         generate_samples(buf, 256); // generate 256 stereo frames
>         if (snd_pcm_writei(h, buf, 256) < 0)
>             snd_pcm_prepare(h);
>     }

Is this the actual code?  You should check if snd_pcm_writei() returns
that less than 256 frames have been written, and you should use
snd_pcm_recover(), and you should that one's return value, too.


More information about the Alsa-devel mailing list