我有一个使用 ALSA 播放音频样本的音频应用程序。“hw:0”设备已设置为: 样本:48kHz,16 位 LE 缓冲区大小:1920 帧 (=20 ms) 周期大小:960 帧 (=10 ms) 这是伪代码:
snd_pcm_sframes_t delayp = 0;
snd_pcm_sframes_t availp = 0;
while(true)
{
snd_pcm_delay(m_pHandle, &delayp);
availp = snd_pcm_avail(m_pHandle);
print "Delay" + delay + "Available" + availp
err = snd_pcm_writei(m_pHandle, data, periodSize);
availp = snd_pcm_avail_update(m_pHandle);
print "Wrote " + err + "samples - samples available" + avail;
}
日志如下所示:
Periodsize 960 frames for a periodtime of 20 ms
Buffersize 1920 frames for a buffertime of 40 ms
Delay: 0 frames/ 0 ms; available 1920 frames/ 40
Wrote 960 frames; available after write: 960 frames/ 20 ms
xrunRecovery: Underrun!!! (at least 0.669 ms/ 32.112 frames long
Delay: 0 frames/ 0 ms; available 1920 frames/ 40
Wrote 960 frames; available after write: 960 frames/ 20 ms
Delay: 955 frames/ 19.8958 ms; available 965 frames/ 20.1042
Wrote 960 frames; available after write: 9 frames/ 0.1875 ms
Delay: 906 frames/ 18.875 ms; available 1014 frames/ 21.125
...
Delay: 952 frames/ 19.8333 ms; available 968 frames/ 20.1667
Wrote 960 frames; available after write: 18 frames/ 0.375 ms
xrunRecovery: Underrun!!! (at least 234.825 ms/ 11271.6 frames long
Delay: 0 frames/ 0 ms; available 1920 frames/ 40
Wrote 960 frames; available after write: 960 frames/ 20 ms
xrunRecovery: Underrun!!! (at least 0.869 ms/ 41.712 frames long
Delay: 0 frames/ 0 ms; available 1920 frames/ 40
发生了两件奇怪的事情: 1. 虽然我写的所有内容都是 960 帧,但 snd_pcm_avail_update 并不总是反映这一点 2. 出乎意料,突然发生了 xrun。例如,在只有 18 帧可用的情况下,下一行在尝试将新周期写入缓冲区时给出 xrun。
有人可以解释一下这里发生了什么吗?