专业音频
该文章描述了如何将你的系统针对录音,混音,回放或是合成音频应用进行配置。 这类活动通常使用术语 “专业音频” 进行描述,并相对需要较低的延迟。
相对于视频剪辑或游戏,该类应用通常不需要高端硬件。生产于2012 年后的大多数硬件都可以针对专业音频工作进行优化。更多信息请参考 Ardour 用户手册 The Right Computer System for Digital Audio 一节。
开始[编辑 | 编辑源代码]
在阅读完 Sound system 一文后,你可能已大致了解到 ALSA 属于 Linux 内核的一部分,并在 Arch Linux 的 驱动及接口 中作为默认声音系统。ALSA 在完成默认的 Arch 安装流程后应当可以开箱即用。如果出现意外,你需要在进行下一步前先排清问题。
然而,如果出现 “没有声音” 的情况,则很可能是需要在 ALSA 混音器配置中解除声道的静音。如果你在使用多个音频设备(例如 USB 音频接口及内置声卡),你可能需要设置默认声卡.
我已正确配置好声音系统了吗?
$ speaker-test
- 进一步排障可参考 ALSA 。
在多数情况下,默认的 Arch Linux 内核已能满足低延迟应用。进一步的系统配置只需在你遇到了音频丢失,或是你确实需要/想要极低延迟的情况下进行。
为了完成优化工作,你可能需要配置一个实时内核。
尽管有些专业音频软件可以直接使用 ALSA,但该文章提到的大多数软件都属于 JACK Audio Connection Kit(又称 JACK)客户端。因此,你需要根据下方描述安装并配置一个可用的音频服务器。
系统配置[编辑 | 编辑源代码]
你可以参考下列常见的系统优化方式:
- 配置 CPU 调频 调速器至 performance。
- 配置 pam_limits(例如安装 realtime-privileges包 并将用户添加至
realtime
组)。 - 使用
threadirqs
内核参数(参考 [1])- realtime kernel patchset 已默认启用该参数。 - 使用 realtime kernel patchset。
- 添加
noatime
至 fstab(参考 性能优化#挂载选项)。 - 通过在启动阶段执行下列命令来提高最大 requested RTC interrupt frequency(默认为 64 Hz):
# echo 2048 > /sys/class/rtc/rtc0/max_user_freq # echo 2048 > /proc/sys/dev/hpet/max-user-freq
- Reducing swappiness (aka swap frequency, set to
60
by default) to e.g.10
will make the system wait much longer before trying to swap to disk (see wikipedia:Paging#Swappiness). This can be done on the fly withsysctl vm.swappiness=10
(see sysctl(8)) or setup permanently, using a configuration file (see sysctl.d(5)) such as:
/etc/sysctl.d/90-swappiness.conf
vm.swappiness = 10
- Increasing the maximum watches on files (defaults to
524288
) to e.g.600000
, that inotify keeps track of for your user, can help with applications, that require many file handles (such as DAWs). This again can be done on the fly withsysctl fs.inotify.max_user_watches=600000
or in a dedicated configuration file:
/etc/sysctl.d/90-max_user_watches.conf
fs.inotify.max_user_watches = 600000
You may also want to maximize the PCI latency timer of the PCI sound card and raise the latency timer of all other PCI peripherals (default is 64).
$ setpci -v -d *:* latency_timer=b0 # setpci -v -s $SOUND_CARD_PCI_ID latency_timer=ff
E.g. SOUND_CARD_PCI_ID=03:00.0
.
The SOUND_CARD_PCI_ID can be obtained like so:
$ lspci | grep -i audio
03:00.0 Multimedia audio controller: Creative Labs SB Audigy (rev 03) 03:01.0 Multimedia audio controller: VIA Technologies Inc. VT1720/24 [Envy24PT/HT] PCI Multi-Channel Audio Controller (rev 01)
选择音频服务器[编辑 | 编辑源代码]
音频硬件无法同时播放来自多个应用的声音。虽然理论上 ALSA 可以被配置为混合来自多个应用的声音,但这方面的工作通常会交由音频服务器来处理。单独使用 ALSA 时难以实现低延迟,无法协调多个音频应用的实时播放、同时播放以及维持同一速率等,并且无法简单通过连接客户端的方式在多个应用间传输音频流,因此在这一场景下,你需要一个专业音频级的音频服务器。
有三个音频服务器可以选择,其中两个专为专业音频应用而设计:
- PulseAudio 在音频回放和多媒体领域是事实标准,例如视频、浏览器、音乐及游戏。它并没有针对专业音频进行构思,并缺乏低延迟及应用间同步的能力。
- JACK 专门针对专业音频的需要进行开发,并已在全球范围内获得长期应用,因此其非常成熟和稳定。多数专业音频应用针对 JACK API 进行开发。
- PipeWire 正处于活跃开发中,在音频方面目标为取代 JACK 及 PulseAudio。除本文内容外,它同时可以处理视频路由。目前为 beta 开发阶段。
音频服务器的配置很大程度上由使用场景、工作流及应用交互限制所决定。JACK Audio Connection Kit 设计为在不同应用间传输音频,并在维持低延迟下通过客户端的同步执行来同时访问同一音频硬件。
以下为将要介绍的两种音频服务器配置的模型:
#仅使用 PipeWire #仅使用 JACK ┌──────────────┐ ┌──────────────┐ │ Applications │ │ Applications │ ├──────────────┤ ├──────────────┤ │ PipeWire │ │ JACK │ ├──────────────┤ ├──────────────┤ │ ALSA │ │ ALSA │ └──────────────┘ └──────────────┘
仅使用 PipeWire[编辑 | 编辑源代码]
新的 PipeWire 框架目标通过更优秀的简洁性取代 JACK 及其它音频服务器。因此,建议先尝试在默认 Arch Linux 内核下使用仅 PipeWire 配置,并通过安装 pipewire-jack包 为 JACK 客户端 提供支持。
仅使用 JACK[编辑 | 编辑源代码]
在通用性的原则下,你可以将 JACK 与实时内核搭配进一步的系统配置以达到进阶应用所需的低延迟。使用 JACK 作为唯一的音频服务器时,需要所有音频交互应用都作为 JACK 客户端运行。
不幸的是,一些流行的桌面应用(例如火狐)以及大多数游戏都取消了或是根本不支持 JACK。因此,该配置仅适合作为专业音频系统的独立设备,此时不支持 JACK 的软件影响可以忽略不计。如果你仍需使用到不支持 JACK 的软件,可以在完成下列设置后参考 Professional audio/Examples#Advanced sound server setups。在安装和运行 JACK 前,请确保它可以访问到你的音频设备。
PulseAudio 或者其它软件在占用我的设备吗?
$ lsof +c 0 /dev/snd/pcm* /dev/dsp*
或者
$ fuser -fv /dev/snd/pcm* /dev/dsp*}}
如果你的音频设备没有显示出来,它可能是在被 PulseAudio 占用(可能是由其它软件作为需求包安装)。如果你不打算使用 Professional audio/Examples#PulseAudio+JACK,可以将 PulseAudio 连带其它软件一起移除,以使其释放音频设备。
JACK 版本 1 已处于 “逐渐淘汰”[2] 阶段中,其不支持 对称多处理(SMP),缺乏 D-Bus 及 Systemd 整合,因此建议使用官方软件源中的版本 2 jack2包。If you're going to use a JACK control GUI or a Systemd user service for Starting the audio graph, also install jack2-dbus包.
- More details in JACK Audio Connection Kit#Comparison of JACK implementations
The article on JACK describes a GUI-based and a shell-based example setup as a point of reference for your own scenario. Parameter values of JACK are discussed in detail in the #JACK parameters section and may depend on other system factors covered by the #System configuration section above.
JACK parameters[编辑 | 编辑源代码]
The aim here is to find the best possible combination of buffer size and periods, given the hardware you have. Frames/Period = 256 is a sane starter. For onboard and USB devices, try Periods/Buffer = 3 before lowering both values. Commonly used values are: 256/3, 256/2, 128/3, 128/2.
Also, the sample rate must match the hardware sample rate. To check what sample and bit rates your device supports:
$ cat /proc/asound/card0/codec#0
Replace card0 and codec#0 depending on what you have. You will be looking for rates or VRA in Extended ID. A common sample rate across many of today's devices is 48000 Hz. Others common rates include 44100 Hz, 96000 Hz and 192000 Hz.
Almost always, when recording or sequencing with external gear is concerned, realtime is a must. Also, you may like to set maximum priority (at least 10 lower than system limits defined in /etc/security/limits.d/99-realtime-privileges.conf
; the highest is for the device itself).
Start jack with the options you just found out:
$ /usr/bin/jackd -R -P89 -dalsa -dhw:0 -r48000 -p128 -n2
qjackctl包, cadence包, patchage包 and studio-controls-gitAUR can all be used to as GUIs to monitor JACK's status and simplify its configuration.
- Further reading: Linux Magazine article (2006) for basic understanding on JACK parameter finding
Latency verification[编辑 | 编辑源代码]
JACK parameters are most significant for controlling the round-trip delay (RTD). In the context of this article that is the overall time it takes for an audio signal to be recorded, processed and played back. The next subsection deals with theoretical background on the sources of latency adding up to the RTD. If you are already familiar with that, you can go to #Measuring latency to verify your RTD or skip this section completely.
Latency sources[编辑 | 编辑源代码]
Consider a typical recording situation of a singer performance. The voice is being captured with a microphone as it propagates trough the air with the speed of sound. An analog-to-digital-conversion enables the electrical signal to be recorded by a computer for digital signal processing. Finally, a digital-to-analog conversion returns the signal to be played back at the singer's headphones for monitoring similar to stage monitor system usage.
In that voice recording situation there are five significant latency sources constructing the RTD and occuring in the following order:
- Sound propagation through the air from the mouth of the singer
- Analog-to-digital conversion
- Digital signal processing
- Digital-to-analog conversion
- Sound propagation through the air to the ear of the singer
The first and last latency source is hard to change as a particular distance is technically necessary to create an intended sound during recording or playback, respectively. Additionally, when using closer miking for capturing and headphones for monitoring both sound propagation latencies are typically within the range of a few microseconds which is not noticeable by humans. Thus, an objective for RTD minimization is to reduce the other sources of latency.
Conversion latency[编辑 | 编辑源代码]
In theory JACK maintains a constant low latency by using fixed values (frames, periods, sample rate) for sampling and buffering of audio to be converted analog-to-digital and vice versa. The latency occurring in the capturing process is described by the following equation:
- Lc = n / f
Lc: Capture latency in milliseconds (ms), n: Frames or buffer (multiples of 2, starting at 16), f: Sample rate in Hertz (Hz).
The playback latency is also employing the periods value:
- Lp = n * p / f
Lp: Playback latency in milliseconds (ms), n: Frames or buffer (multiples of 2, starting at 16), p: Periods, f: Sample rate in Hertz (Hz).
As already stated before the capabilities of the audio interface define working combinations. You have to trial and error to find a setup. Sure, it's a trade-off between xrun prevention and achieving low latency, but recent audio interfaces can be used at high sample rates (up to 192 kHz) to deal with that requirement. The audio flux of JACK clients in the digital domain is about zero and thus, negligible for latency measurements [3].
- See FramesPeriods in the ALSA wiki for more information.
Measuring latency[编辑 | 编辑源代码]
Once you have set up #JACK parameters you might want to verify the RTD described above. For example, using a frames or buffer size of n = 128, a periods value of p = 2, and a sample rate of f = 48000 results in a capture latency of about Lc = 2,666... ms and a playback latency of about Lp = 5,333... ms summing up to a total round-trip delay of RTD = 8 ms.
The jack_delay包 utility by Fons Adriaensen measures RTD by emitting test tones out a playback channel and capturing them again at a capture channel for measuring the phase differences to estimate the round-trip time the signal has taken through the whole chain. Use an appropriate cable to connect an input and output channel of your audio device or put a speaker close to a microphone as described by JACK Latency tests.
For example, running jack_delay
for a JACK-only setup using a cable connection between the ports playback_1 and capture_1 (the description may differ depending on your hardware) to close the loop, as well as the values discussed before yields the following insights:
$ jack_delay -O system:playback_1 -I system:capture_1
capture latency = 128 playback_latency = 256 Signal below threshold... Signal below threshold... Signal below threshold... 422.507 frames 8.802 ms total roundtrip latency extra loopback latency: 38 frames use 19 for the backend arguments -I and -O 422.507 frames 8.802 ms total roundtrip latency extra loopback latency: 38 frames use 19 for the backend arguments -I and -O 422.506 frames 8.802 ms total roundtrip latency extra loopback latency: 38 frames use 19 for the backend arguments -I and -O 422.507 frames 8.802 ms total roundtrip latency extra loopback latency: 38 frames use 19 for the backend arguments -I and -O <output omitted>
As the output indicates further optimization of JACK can be done by using the parameters -I 19
and -O 19
to compensate for the reported extra loopback latency in the chain:
$ /usr/bin/jackd -R -P89 -dalsa -dhw:0 -r48000 -p128 -n2 -I19 -O19
- More details can be found in the Ardour Manual page about latency and latency compensation.
实时内核[编辑 | 编辑源代码]
"实时" 这一概念在操作系统中的 定义 为:操作的结果可以在固定时间内得出,只有在更广的意义上才是“与现实时间同步”,例如:用户在输入后可以立即产生相应的声音。这个案例称为“低延迟”,而达成它的操作这篇文章的主要目标之一。
从一段时间前开始,默认的 Linux 内核(Arch Linux 的初始内核默认带 CONFIG_PREEMPT=y
选项)已被证明适用于低延迟操作。操作系统中对延迟的定义为:从硬件中断的开始,到对应的中断线程开始运行的时间。不幸的是,某些硬件的驱动会导致更高的延迟。因此,根据你的硬件、驱动和需求,你可能需要一个满足 实时计算标准 的内核。
优势与劣势[编辑 | 编辑源代码]
Ingo Molnar 和 Thomas Gleixner 创造的 RT_PREEMPT 补丁集是对于软/硬实时应用而言非常有意思的选项,涵盖了从专业音频到工业控制等范围。大多数专注于音频的 Linux 发行版都启用了该补丁集。一个可实时抢占的内核也将使调整 IRQ 处理线程的优先级成为可能,并有助于确保几乎不受负载影响的流畅音频。
安装[编辑 | 编辑源代码]
安装 linux-rt包 或是 linux-rt-lts包 包。
编译[编辑 | 编辑源代码]
如果你要使用 实时内核补丁集 来 编译你自己的内核,请记住,移除模块或选项并不等同于精简内核。虽然内核映像的体积会减小,但现代的计算机系统以不再像 1995 年那时对大小敏感了。
- 更多信息可参考 如何正确配置带 PREEMPT_RT 的 Linux 。
不论如何,你必须要确保:
- Timer Frequency 设为 1000Hz(
CONFIG_HZ_1000=y
;不使用 MIDI 可以无视该项) - 禁用 APM(
CONFIG_APM=n
;在某些硬件上会产生问题 - 在 x86_64 中为默认配置)
If you truly want a slim system, we suggest you go your own way and deploy one with static /devs. You should, however, set your CPU architecture. Selecting "Core 2 Duo" for appropriate hardware will allow for a good deal of optimisation, but not so much as you go down the scale.
(实时)内核的常见问题:
- 超线程(如果怀疑是它导致的问题,可以在 UEFI 设置中将其禁用)
Tips and tricks[编辑 | 编辑源代码]
- Disable WiFi and close any programs that do not need to be open when recording such as browsers. Many have reported disabling WiFi has led to more reliable JACK performance.
- Some USB audio hardware is known not to work properly when plugged into USB 3 ports so try USB 2/1 ports instead.
- IRQ issues can occur and cause problems. An example is video hardware reserving the bus, causing needless interrupts in the system I/O path. See discussion at FFADO IRQ Priorities How-To. If you have a realtime or a recent kernel, you can use rtirq包 to adjust priorities of IRQ handling threads.
- Do not use the irqbalance daemon, or do so carefully [4].
- If you need to use multiple audio devices with JACK2, the alsa_in and alsa_out utilities can be used to have extra devices wrapped and show up as outputs in the JACK patchbay.
- Some daemons/processes can unexpectedly cause xruns. If you do not need it - kill it. No questions asked.
$ ls /var/run/daemons $ top # or htop, ps aux, whatever you are comfortable with $ killall -9 $processname # systemctl stop $daemonname
- If you are facing a lot of xruns especially with nvidia包, disable your GPU throttling. This can be done via the card's control applet and for NVIDIA it is "prefer maximum performance" (thanks to a post in LAU by Frank Kober[5]).
- You may like to read more on ALSA: https://www.volkerschatz.com/noise/alsa.html
软件[编辑 | 编辑源代码]
Arch Linux provides the package group pro-audio包组 holding all relevant (semi-) professional applications. All applications in the pro-audio package group are JACK clients. Also lv2-plugins包组, ladspa-plugins包组, dssi-plugins包组 and vst-plugins包组 are subgroups of the pro-audio group.
An overview and brief information on some applications is found in List of applications/Multimedia#Audio. Especially the categories Digital audio workstations, Audio effects and Music trackers, as well as Audio synthesis environments and Sound generators, provide examples of pro audio software for e.g. recording, mixing, mastering, and sound design. Other categories include Scorewriters, Audio editors, Audio converters, and DJ software.
Packages not (yet) in the official repositories can be found in proaudio. Browse the list of packages to find the application you need or request packaging of your desired applications via GitHub.
Hardware[编辑 | 编辑源代码]
The majority of sound cards and audio devices will work with no extra configuration or packages, simply set JACK to use the desired one.
This is not true for all devices, have a look at the Category:Sound as well as Envy24control#Supported cards for those special cases.
Get help[编辑 | 编辑源代码]
Mailing lists[编辑 | 编辑源代码]
- Arch Linux Pro-audio Discussion about real-time multimedia, including (semi-)pro audio and video.
- Linux Audio User The Linux pro-audio related mailing list with much traffic and a huge subscriber community of users and developers.
IRC[编辑 | 编辑源代码]
- #archlinux-proaudio - Arch Linux pro-audio channel
- #lau - General Linux Audio channel for users
- #jack - Development and support related to JACK audio system
- #lv2 - Development and support related to the LV2 plugin format
- #ardour - Discussion and support relating to the Ardour DAW
- #opensourcemusicians - Large general OSS musician discussion channel
See also[编辑 | 编辑源代码]
- Audio setup Comprehensive guide for a pro-audio setup based on Arch Linux
- User repositories: proaudio, realtime
- AUR meta packages: proaudio-metaAUR, soundfonts-aur-metaAUR, lv2-plugins-aur-metaAUR
- awesome-linuxaudio A list of software and resources for professional audio/video/live events production on the Linux platform
- Multimedia and Games / Arch Linux Forums
- Realtime The Linux Foundation wiki on the PREEMPT_RT patches