出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯,請訪問簡體中文翻譯團隊

附註: 未完成(在 Talk:專業音頻# 中討論)

該文章描述了如何將你的系統針對錄音,混音,回放或是合成音頻應用進行配置。 這類活動通常使用術語 「專業音頻」 進行描述,並相對需要較低的延遲

相對於視頻剪輯或遊戲,該類應用通常不需要高端硬體。生產於2012 年後的大多數硬體都可以針對專業音頻工作進行優化。更多信息請參考 Ardour 用戶手冊 The Right Computer System for Digital Audio 一節

注意: 如果你在找的是 GNU/Linux 作曲指南,可以參考 Sonoj wiki 或是前往 LinuxMusicians 論壇。

開始

在閱讀完 Sound system 一文後,你可能已大致了解到 ALSA 屬於 Linux 內核的一部分,並在 Arch Linux 的 驅動及接口 中作為默認聲音系統。ALSA 在完成默認的 Arch 安裝流程後應當可以開箱即用。如果出現意外,你需要在進行下一步前先排清問題。

然而,如果出現 「沒有聲音」 的情況,則很可能是需要在 ALSA 混音器配置中解除聲道的靜音。如果你在使用多個音頻設備(例如 USB 音頻接口及內置音效卡),你可能需要設置默認音效卡.

我已正確配置好聲音系統了嗎?

$ speaker-test
進一步排障可參考 ALSA

在多數情況下,默認的 Arch Linux 內核已能滿足低延遲應用。進一步的系統配置只需在你遇到了音頻丟失,或是你確實需要/想要極低延遲的情況下進行。

為了完成優化工作,你可能需要配置一個實時內核

儘管有些專業音頻軟體可以直接使用 ALSA,但該文章提到的大多數軟體都屬於 JACK Audio Connection Kit(又稱 JACK)客戶端。因此,你需要根據下方描述安裝並配置一個可用的音頻伺服器

提示:如果你剛開始使用 Arch Linux,完成了安裝指南且想要立刻開始作曲,可以安裝 pro-audio包組 軟體包組及 realtime-generic-setupAUR 並重啟。參考 #PipeWire-only 一節進行快速配置並查找適合你的音樂製作#軟體

系統配置

注意: 最佳實踐及系統配置中的信息質量差異較大(甚至有可能過時或是自相矛盾)。儘管某些系統和配置需要更高程度的優化,對大多數用戶而言這樣操作都是沒有需要的。建議先使用默認的 Arch Linux 內核及配置,只有當你確實需要低延遲及更高的穩定性時,再考慮優化的問題!

你可以參考下列常見的系統優化方式:

# 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 with sysctl 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 with sysctl 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-jackJACK 客戶端 提供支持。

僅使用 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-BusSystemd 整合,因此建議使用官方軟體源中的版本 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 參數

注意: The following guidelines should support finding best possible parameter values for running JACK without audio drop-outs (xruns) when using the #JACK-only or #PulseAudio+JACK[損壞的連結:無效的章節] sound server setup type. Due to differences depending on the hardware, whether a #Realtime kernel is used or not, and if further #System configuration is applied, there is no general preset.

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.

另外,採樣率也需要與硬體設備匹配,你可以通過以下命令查看設備支持的採樣率選項:

$ cat /proc/asound/card0/codec#0

card0codec#0 替換為你的設備對應的值;在輸出結果中,查找 Extended ID 中尋找 ratesVRA 的值。目前常見的採樣率值為 48000 Hz,另外比較常見的值有 44100 Hz,96000 Hz 和 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).

使用你找到的參數啟動 jack,如下所示:

$ /usr/bin/jackd -R -P89 -dalsa -dhw:0 -r48000 -p128 -n2

可以使用 qjackctlcadencepatchagestudio-controls-gitAUR 通過圖形界面來監控 JACK 的狀態和修改配置。

注意: 配置好 JACK 後,建議使用多個不同的音頻軟體來驗證你的配置。例如,對於 JACK 在 LMMS 上的 xrun 問題,不要當問題出在後者時花費過多時間排障。
建議閱讀 Linux Magazine article (2006) 來理解 JACK 參數調優

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:

  1. Sound propagation through the air from the mouth of the singer
  2. Analog-to-digital conversion
  3. Digital signal processing
  4. Digital-to-analog conversion
  5. 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.

注意: This latency value is comparable to a typical monitoring situation on stage using speakers with a distance to the performer's ear ranging between 2 m and 3 m according to the equation for the speed of sound in air. Performers experienced with on stage monitoring are typically used to such latencies.

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 處理線程的優先級成為可能,並有助於確保幾乎不受負載影響的流暢音頻。

注意: 目前,在實時內核上使用某些應用會有些已知限制,通常與 虛擬化(例如 XenKVM)相關。如果你同時需要使用這類功能,可以考慮在默認 Arch Linux 內核外同時安裝實時內核,並在 引導加載程序 創建另一個入口以按需啟動到對應的內核。

安裝

安裝 linux-rt 或是 linux-rt-lts 包。

編譯

如果你要使用 實時內核補丁集編譯你自己的內核,請記住,移除模塊或選項並不等同於精簡內核。雖然內核映像的體積會減小,但現代的計算機系統以不再像 1995 年那時對大小敏感了。

更多信息可參考 如何正確配置帶 PREEMPT_RT 的 Linux

不論如何,你必須要確保:

  • Timer Frequency 設為 1000Hz(CONFIG_HZ_1000=y;不使用 MIDI 可以無視該項)
  • 禁用 APMCONFIG_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]).

軟體

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.

提示: See Pacman/Tips and tricks#Listing packages for listing members of and Pacman#Installing package groups for installing package groups.

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