[alsa-devel] Playback underrun recovery
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. My top level code consists of a loop that generates one period at a time:
int buf[512];
while (true) { generate_samples(buf, 256); // generate 256 stereo frames if (snd_pcm_writei(h, buf, 256) < 0) snd_pcm_prepare(h); }
The funny thing is that if I break to the debugger and then restart, I get exactly one underrun as you would expect, and then it recovers. What could cause it to get into a state where it fails every other time around the loop, and then sucks up milliseconds of CPU time before it gets going again, thus guaranteeing another underrun?
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.
Regards, Clemens
participants (2)
-
Clemens Ladisch
-
Paul D. DeRocco