目录

Linux 虚拟机中音频卡顿的解决方案

故障排除

一般来说这种情况不应该出现,但多半是由驱动程序不稳定造成的。在虚拟机环境中最常见,因为音频设备是被虚拟化出来的。

通常可以通过在 ALSA 设备环形缓冲区中提供更多的缓冲空间来解决此问题。

你需要按如下方式编辑 WirePlumber 配置(自 0.5 版起,旧的 0.4 版本使用 Lua 脚本进行配置):

1
2
mkdir -p ~/.config/wireplumber/wireplumber.conf.d/
cd ~/.config/wireplumber/wireplumber.conf.d

然后在编辑器中创建~/.config/wireplumber/wireplumber.conf.d/50-alsa-config.conf,并添加:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
monitor.alsa.rules = [
  {
    matches = [
      # This matches the value of the 'node.name' property of the node.
      {
        node.name = "~alsa_output.*"
      }
    ]
    actions = {
      # Apply all the desired node specific settings here.
      update-props = {
        api.alsa.period-size   = 1024
        api.alsa.headroom      = 8192
      }
    }
  }
]

之后,通过systemctl --user restart wireplumber pipewire pipewire-pulse重启所有服务。


性能调优

在虚拟机中运行时,还需禁用 Firefox 语音分派功能。

Firefox 自带启用的阅读器,该功能启用后可用于语音合成(朗读文本)。然而,仅启用此功能时,Firefox 会使 PipeWire 占用 CPU 资源,特别是在虚拟机中。

你可以在 pw-top 中注意到,当进程 speech-dispatcher-dummy 和 speech-dispatcher-espeak-ng 占用大量 CPU 时,就会出现这种现象。

为了解决这个问题,请在 Firefox 中关闭相关设置:在 Firefox 中打开 about:config 页面,根据下面提供的设置进行配置,然后重启 Firefox。

1
2
reader.parse-on-load.enabled false
media.webspeech.synth.enabled false

自 0.3.44 版本以来,pulse-server 对语音调度器有一个特殊处理,会强制使用更高的量子值,这应该能缓解该问题。

自 0.3.61 版本以来,pulse-server 会在语音调度器 5 分钟未活动时挂起它,这应该可以彻底解决该问题。

你可以用pipewire --version查看 pipewire 的版本。


参考文献