PulseAudio

From LinuxReviews
Jump to navigationJump to search

PulseAudio is a standard audio stack used by as good as all Linux distributions. It places itself between end-user software and the kernels ALSA audio stack. It can be used for mixing, per-application volume control and network audio. It has a history of criticism for it's high CPU use and many, many bugs. Most of this was valid from it's creation in 2004 to about 2014. Modern versions are fine and quite useful.

History

PulseAudio was in 2012 described as:

"Pulse is the single worst Linux move I've ever seen. In the interest of removing audio from the kernel space (necessary for low-latency), it simply eliminated what used to be advanced capabilities. Lennart Poettering, author of Pulse simply disregarded these concerns, waved his hands and said "that's not the concerns Pulse was designed to address!"

Configuration example for (placebo) better audio

PulseAudio is system-wide configured by /etc/pulse/daemon.conf and this has some settings which may affect audio quality.

These settings may be of interest:

;; PRIORITY
high-priority = yes
nice-level = -11
realtime-scheduling = yes
realtime-priority = 9
rlimit-rtprio = 9

;; AUDIO QUALITY
default-sample-channels = 6
default-sample-rate = 48000

; pacmd list-sinks | grep sample   will show what you can/should (ab)use
; lscpu | grep Byte Order        will tell if your CPU likes le or be
;default-sample-format = s24le
default-sample-format = float32le

; pulseaudio --dump-resample-methods for all options
; speex-float-10 is best fast method, soxr-vhq is better but slower
; avoid soxr-vhq if you have an old slow CPU
; resample-method = speex-float-10
resample-method = soxr-vhq

; resampling is fine
avoid-resampling = false
enable-remixing = yes
remixing-use-all-sink-channels = no

See HOWTO choose default-sample-rate below if you think you want something other than 48 kHz. You likely don't.

You will also want this if you are using a 5.1 surround setup:

default-sample-channels = 6
; use enable-lfe-remixing if you have old PulseAudio version
;enable-lfe-remixing = no
; does the same as enable-lfe-remixing = no in latest PA versions
remixing-produce-lfe = no
remixing-consume-lfe = no

Or you may want this instead if you never use more than stereo.

default-sample-channels = 2
;enable-lfe-remixing = no
remixing-produce-lfe = no
remixing-consume-lfe = yes

Do note that PulseAudio will detect if the output sink lacks the number of channels required and downmix to stereo if enable-remixing is set to yes (which is the default).

The above settings provide the following benefits:

  • default-sample-rate sets the default sample-rate used by PulseAudio. Modern digital audio equipment can usually handle 48000 just fine. Using 96000 would also be fine if you only use a external DAC/AMP connected by USB or motherboard audio. HDMI outputs on modern GPUs do not support anything higher than 48000. You can look at less /proc/asound/card?/codec#0 to see what your audio outputs support.
  • default-sample-format = float32le sends higher quality samples to ALSA which in turn sends it to the actual audio card. You may not be able to use the better float32le option. Run the command pacmd list-sinks | grep sample to see what your card(s) actually support.
  • avoid-resampling will, if true, make PulseAudio switch between 48000 and 44100 audio output to the sound device depending on what is being played. This has historically been buggy and problematic when playing say 44100 music in the background and opening a video file that's 48000. Setting this to false makes PulseAudio use the default-sample-rate even if it means re-sampling 441000 audio. This is the better choice you will be playing audio from different sources - and you will end up doing that from time to time.
  • resample-method sets the type of technology used for resampling. speex-float is a good option for weaker processors. It has many quality/speed options you can use by adding a -number to it. speex-float-10 provides better quality than say speex-float-2 - higher values produce better quality and consume more CPU, lower values have lower quality and consume less CPU. soxr-vhq produces better audio quality than speex-float-10 but it is also more CPU-intensive. The difference in CPU power required to re-sample audio mattered in 2005. Perhaps it meant something in 2010. It's mostly irrelevant on modern desktop machines. Use soxr-vhq if you have something like a Ryzen; use speex-float-10 or speex-float-6 if your machine is a very old trainwreck.
  • enable-remixing refers to re-mixing audio channels. It is useful for remixing mono to stereo. It should be combined with remixing-use-all-sink-channels
  • remixing-use-all-sink-channels set to true or false decides if stereo should be up-mixed to all channels on a surround system. Setting this to no makes it possible to have enable-remixing set to yes and still have mono up-mixed to stereo.
  • enable-lfe-remixing refers to remixing of the sub-woofer channel. Modern surround receivers will typically have a built-in speaker setup where you can configure how many speakers you have. A surround receiver which knows there's 5 speakers and no subwoofer in a 5.1 setup (actually 5.0) will send the LFE sub-woofer channel to the front-speakers. enable-lfe-remixing=no may be the better option even if you have no sub-woofer. However, if you are connecting two stereo speakers by RCA then you may want to have pulseaudio convert LFE channel into a part of the stereo signal.
    • PulseAudio 13.99 replaces enable-lfe-remixing with remixing-produce-lfe, which ensures that a LFE channel is enabled if the output has one but the input doesn't, and remixing-consume-lfe which will remix the LFE signal into other channels if the source audio has a LFE channel and the output does not. Both default to no

HOWTO choose default-sample-rate

The short answer: 48000 Hz is likely the best option on most modern hardware.

PulseAudio can operate using two different sample-rates by configuring

default-sample-rate = 44100
alternate-sample-rate = 48000

Some documentation indicate that this is supposedly good if you play some CD audio with a 44.1kHz sample-rate and some movies/musicvideos with a 48kHz sample-rate. It is not. Sound cards and HIFI equipment (when using HDMI) can only be locked to one sample-rate. You will get down-mixed if you have an audio player open and you start playing a video. "CD audio" is a non-issue anyway, all the good k-pop songs are released as .flac with a 48 kHz sample-rate.

PulseAudio will up or down-mix the source audio to the set sample-rate and then up or down-mix to the sound cards highest supported sample-rate. This means that if you set default-sample-rate to 96000 and you play 44.1 kHz on a sound card with a 48 kHz limit the audio gets up-mixed to 96 kHz and then down-mixed to 48 kHz.

You can check what your sound cards (and other audio hardware) actually supports with:

cat /proc/asound/card?/codec#0

The kernel-provided file /proc/asound/card0/codec#0 holds all kinds of incriminating evidence about your systems first sound card, including rates. There will be individual files for each cardX you have. Both the cheap Realtek ALC887-VD and the more "high-end" Realtek ALC1220 allow you to choose between 32000 44100 48000 88200 96000 192000. The codec#0 file will not show actually accepted rates for HDMI outputs, see below. HDMI outputs provided by computer graphics cards (and APUs) will show 32000 44100 48000 even if connected devices support higher rates.

default-sample-rate = 96000 could be a good idea if your motherboards audio chip supports it or you use a USB DAC/AMP which supports it and you only use that to play audio. 48000 is a better choice if you mix between Bluetooth headphones, a surround receiver connected through HDMI and headphones connected to your motherboards sound card.

Users of an ancient audio connector called "SPDIF" (encoded with IEC958) may be interested to know that "SPDIF" can carry 96 kHz stereo but it can't carry surround sound at 96 kHz. A SPDIF connection can only do 5.1 surround if it is encoded using AC3 at 48 kHz.

What formats and rates HDMI connected audio hardware supports can be shown with cat /proc/asound/card?/eld\#*|grep rates

Special files caleld eld#0.0, eld#0.1 and so on are present in /proc/asound/card?/ if one or more HDMI devices are connected. These files will either say eld_valid 0 if the HDMI connection does not support audio OR list a lot of information about the device, specially if it is a AV receiver where the file will indicate what sample rates are accepted, what encoding formats (PCM, LPCM, AC-3, DTS, E-AC-3/DD+, etc) and so on are supported.

PulseAudio can do Audio over Network

A dedicated machine next to a nice HI-FI surround receiver can run PulseAudio with the following in /etc/pulse/system.pa if PulseAudio is started as a system user - or /etc/pulse/default.pa if you start it as a user for some reason - and allow other machines to use it to output audio via this content of /etc/pulse/system.pa:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;10.66.78.0/24;192.168.0.0/24 auth-anonymous=1
load-module module-zeroconf-publish

Make sure the local network subnet you are using is allowed in the auth-ip-alc= parameter. That parameter can be skipped, load-module module-native-protocol-tcp auth-anonymous=true would allow everyone access (note that everyone includes remote Internet users if the firewall permits it).

The client(s) on the network would just need the following in /etc/pulse/default.pa to make a local pulseaudio use a remote pulseaudios outputs - if the client has a working zeroconf setup.

load-module module-zeroconf-discover

Avahi and systemds resolved are the most commonly used zeroconf-service for automatic discovery.

It is, alternatively, possible to have a line in /etc/pulse/default.pa which specifies a remote PulseAudio server to use by IP:[1]

load-module module-tunnel-sink server=xxx.xxx.xxx.xxx

(replace xxx.xxx.xxx.xxx with an IP like 192.168.0.2)

Both of the above methods of using a remote PulseAudio instance makes local programs connect to a locally running Pulseaudio instance which has the remote PulseAudio server as one of many possible outputs. Local microphones and local audio outputs are available in addition to the remote servers audio outputs.

Client computers can, alternatively, use a remove pulseaudio server directly by adding a line which says[2]

default-server = ip

to /etc/pulse/client.conf.

That line makes applications connect directly to the remote pulseaudio server. One clear downside with this approach is that local inputs and outputs on the client machine will not be available. Connecting directly to a remote PulseAudio server may be preferable if the local machine/device has no audio devices connected to it.

pactl

in bash, pactl info and stat and list will not tell you much if your PA has crashed.

Footnotes

Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.