Discussion:
[Alsa-user] buffer underrun issue
Ferenc Engárd
2017-03-08 23:03:59 UTC
Permalink
Hi all,

Welcome everybody, as this is my first mail to the list.

I am trying to write my first ALSA audio program, and I have intermittent
issues I can't explain.

I am playing ~68ms of sound from memory, in 1-channel U8 setup, and quite
frequently I get the following errors:

ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred

The test project can be found here:
https://github.com/circum1/alsa-underrun-demo
Compile simply with g++ -o alsa-test alsa-test.cpp -lasound

The program writes 3*1000 samples, then drains, sleeps 1 second, and loops.
My example output running this program:

***@fery-E6540:~/tmp/alsa-demo/alsa-underrun-demo$ g++ -o alsa-test
alsa-test.cpp -lasound && ./alsa-test
SOUND: setupWithFreq sampling rate: 44100
SOUND: number of periods: 3
SOUND: period size: 1000 frames
0 {0}
1 {1}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
1 {2}
1 before drain
48 after drain

1049 {0}
1050 {1}
1050 {2}
1050 before drain
1119 after drain

2120 {0}
2121 {1}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
2121 {2}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
2122 before drain
2122 after drain

3123 {0}
3124 {1}
3124 {2}
3124 before drain
3194 after drain

4195 {0}
4195 {1}
4195 {2}
4195 before drain
4265 after drain

5266 {0}
5266 {1}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
5267 {2}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
5268 before drain
5268 after drain

6269 {0}
6270 {1}
6270 {2}
6270 before drain
6339 after drain

^C

The numbers at the beginning of the lines are elapsed milliseconds. It is
visible that the program writes 2000 samples in 1-2ms, but nevertheless the
underrun occurs.

I don't know if the error is in my code, or is it some bug or maybe hw
issue?

Possibly relevant env info:

Ubuntu 16.04, Linux version 4.4.0-59-generic
libasound2: Version: 1.1.0-0ubuntu1
pulseaudio: Version: 1:8.0-0ubuntu3.2

00:03.0 Audio device: Intel Corporation Xeon E3-1200 v3/4th Gen Core
Processor HD Audio Controller (rev 06)
(Dell E6540 notebook)

Independent of this issue, I also have another question: is it safe to
write fewer samples than period size, even if it is not the last pcm_writei
in the stream?

Thanks in advance,
Ferenc
Clemens Ladisch
2017-03-09 09:15:50 UTC
Permalink
Post by Ferenc Engárd
SOUND: setupWithFreq sampling rate: 44100
SOUND: number of periods: 3
SOUND: period size: 1000 frames
0 {0}
1 {1}
ALSA error: Broken pipe
ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
1 {2}
Are you using PulseAudio? Does the same happen if you use the device name "plughw"?
Post by Ferenc Engárd
is it safe to write fewer samples than period size,
Yes.
Post by Ferenc Engárd
even if it is not the last pcm_writei in the stream?
snd_pcm_drain() detects the end only on a period boundary, so the last
write should not leave a period partially unfilled.


Regards,
Clemens

Loading...