ALSA意外缓冲区欠载。谁能解释一下?

时间:2012-12-14 14:07:18

标签: alsa

我有一个使用ALSA播放音频样本的音频应用程序。 “hw:0”设备已设置为: 样品:48kHz,16位LE 缓冲:1920帧(= 20 ms) 周期大小:960帧(= 10毫秒) 这是伪代码:

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

发生了两件奇怪的事情: 虽然我写了960帧的所有东西,但snd_pcm_avail_update并不总是反映这一点 突然出现了xrun。例如,在只有18个帧可用的情况下,下一行在尝试将新周期写入缓冲区时给出了一个xrun。

有人可以解释一下这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

写入960帧时,可用帧数减少960。 同时,正在播放的任何样本都会增加可用帧数。

缓冲区变空时会出现欠载。 根据您的日志,您的程序运行时间大约为234毫秒。