【问题标题】:PulseAudio cannot read buffers at consistent ratePulseAudio 无法以一致的速率读取缓冲区
【发布时间】:2019-10-06 05:12:18
【问题描述】:

我想实时记录我的麦克风和过程,例如。显示麦克风信号的示波器视图。

我尝试了 3 种方法来从 PulseAudio 中获取数据,它们的性能都相同。我希望二进制流顺畅,但有一种模式大致如下:挂起约 300 毫秒,打印大量输出,重复。
3 种不同的方法,2 台不同的机器,相同的结果。

第一种方式是parec

第二种和第三种方式是 C 和 Haskell 中的 hello world,两者都使用 pulse-simple 库。

C: https://freedesktop.org/software/pulseaudio/doxygen/parec-simple_8c-example.html#a7

Haskell:

import Sound.Pulse.Simple
import Control.Monad
import System.IO

main = do

  s <- simpleNew Nothing "example" Record Nothing
       "this is an example application"
       (SampleSpec (F32 LittleEndian) 44100 1) Nothing Nothing


  forever $ do

  let numSamples = 4410
  xs <- simpleRead s $ numSamples :: IO [Float]

  putStrLn $ "hello"

  hFlush stdout

在循环体中刷新标准输出没有任何区别。
在循环中添加延迟会改变性能,但不会达到我想要的效果。

不知何故,pavucontrol VU 表是正确的。我错过了什么?

编辑:我发现当pavucontrol 运行时,我得到了很好的结果,无论是在我的示例程序中还是在parec 中。为什么??

另外,我查看了 pavucontrolparec 的源代码,发现它们都使用 asynchronous API,而我的 2 个示例程序使用 simple API。因此,问题并不完全是由于使用了 1 个 API 或其他,因为 parec 的行为类似于示例程序。

【问题讨论】:

标签: linux audio pulseaudio


【解决方案1】:

调用pa_simple_new 时,将pa_buffer_attr 参数的fragsize 字段设置为所需的延迟(以字节为单位)。 PulseAudio 将尝试实现该延迟,但可能无法达到,具体取决于硬件功能。

(pavucontrol 可能要求低延迟。PulseAudio 尝试达到所有当前正在运行的程序所要求的最低延迟,这就是为什么 pavucontrol 运行时录制延迟低的原因。

在 C 中:

    pa_buffer_attr attr;
    attr.maxlength = (uint32_t) -1;
    attr.tlength = (uint32_t) -1;
    attr.prebuf = (uint32_t) -1;
    attr.minreq = (uint32_t) -1;
    attr.fragsize = 1600;

    pa = pa_simple_new(NULL,
                       argv[0],
                       PA_STREAM_RECORD,
                       NULL,
                       "record",
                       &ss,
                       NULL,
                       &attr,
                       &error);

请参阅LatencyControl 了解更多信息。 (请注意,使用简单 API 时会自动设置 PA_STREAM_ADJUST_LATENCY 标志)。

这应该会提供更流畅的样本流,但因为 pa_simple_read 是阻塞的(等待整个缓冲区可以被填满),即使您使用一个小缓冲区进行读取,您仍然会增加延迟。要摆脱这种额外的延迟,您需要使用异步 API 进行非阻塞读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2018-03-12
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多