📜 ⬆️ ⬇️

PulseAudio, Part 1: Command Line Control

One of the innovations of Ubuntu 10.10 was the transition from “bare” ALSA to PulseAudio. Previously, many tips were fastened to nail and remove him to solve problems, but now PulseAudio is stable, columns don't hiss with it;), and he is capable of something that Alsa did not dream about :)

In the article I will tell you from the very beginning what it is and how it works, as well as:

And I will present my solution, designed to simplify the management of PulseAudio;)

Briefly about PulseAudio

The review is well written in our wikipedia . I will give a summary list of the main differences from Alsa:


GUI tools

If you are reading this article to solve a couple of nuances and do not delve into - I can recommend to try the GUI-tools described on the PulseAudio page . With the help of pavucontrol you can quickly select a “default” recording device (Input Devices, button on the right “Set as fallback”) and Skype will work :) Also pavucontrol allows pavucontrol to control the volume of the overall and individual applications.

Basic PulseAudio

Card, Sink & Source

It is known that the sound card has two types of sockets: inputs (line in, microphone) and outputs (headphones, line out, S / PDIF). In a typical configuration, for each available sound card and its “nests” PulseAudio creates the following objects:

PulseAudio defines “default Sink” and “default Source”: objects used by default for newly launched applications.
I note that Sink does not have to describe the physical port of the sound card: it is an abstraction of the sound stream receiver. PulseAudio is a high-level thing and allows you to create very “tricky” objects: for example, Sink of the 'NULL' type (implemented by the PulseAudio module-null-sink module) simply absorbs the sound, and the module-pipe-sink module allows you to stream audio through the FIFO file. You can also create network Sink'i, leading multicast-broadcasting or transmitting the audio stream to a specific host. All this is implemented by PulseAudio modules.

So, the sound is played through “Sink”, and recorded through “Source”.

Sink-Input & Source-Output

Each Sink-Input defaults to contacting one of the Sinks and transmits an audio stream. PulseAudio then determines what Sink is connected with (for example, a sound card) and sends it to its destination.
There is a special type of Source: Monitor. For each Sink, a separate monitor can be created to allow recording. So, it turns out to record "sound from the speakers."

So, the media player creates Sink-Input, and Skype - and Sink-Input, and Source-Output.

module, client, sample

Within the article - purely information :)

ALSA → PulseAudio

Of course, you first need to make sure that PulseAudio is installed and running:
sudo apt-get install pulseaudio
pulseaudio --start -D

Not every application can work directly with PulseAudio: they usually know how to use Alsa. For such applications, it is necessary to inform Alsa to redirect their audio streams to PulseAudio.
To do this, ~/.asoundrc insert the following lines into the ~/.asoundrc file ( which, by the way, will cure Skype’s microphone! If you also turn off “Allow Skype to automatically adjust the volume” - suggests aim ):
pcm.pulse { type pulse }
ctl.pulse { type pulse }
pcm.!default { type pulse }
ctl.!default { type pulse }

The first two lines will create an ALSA virtual device named 'pulse' for input and output, as well as a volume control device. The following two define them as “by default”: now all applications instead of Alsa will use PulseAudio through a special plugin (and not to suspect anything).
To apply the changes, you need to restart the sound applications. Freshly launched applications will pick up the changes themselves.
UPD: yuretsz tells you that applications using OSS ( /dev/dsp ) need to be launched via the padsp [options] PROGRAM [ARGUMENTS ...] wrapper.

Control PulseAudio: pactl

PulseAudio is controlled by the pactl command, which cannot be said to be comfortable and advanced :) however, we will train with it.

Property Inspection

First, let's see what we have: what objects are defined in PulseAudio. I recommend to start background music to have something to watch :)
The pactl list command will show a long list of existing objects.
Each object has a set of fields describing it, as well as a special field “Properties”: a list of key-value properties (usually string).
All objects have index (Sink # 0) - a unique number of an object of a certain type. Also, many objects have names (Name:): index and Name can be used to refer to a specific object.

I recommend to look at the fields of the Sink and Source objects: by default, they describe your hardware.
I will show my example:
 Sink # 1 - Sink: reproduces sound
         State: RUNNING - Sink works: music plays through it
         Name: alsa_output.pci-0000_00_1b.0.analog-stereo - Sink's unique name
         Description: Internal Audio Analog Stereo - Sink text description
         Driver: module-alsa-card.c - Used "driver": PulseAudio module that created this Sink
         Sample Specification: s16le 2ch 44100Hz - Sampling parameters, number of channels, coding.
         Channel Map: front-left, front-right - Sound Channels
         Owner Module: 5 - index of the module that created this Sink
         Mute: no
         Volume: 0: 26% 1: 26% - Volume of both channels
                 0: -35.42 dB 1: -35.42 dB
                 balance 0.00
         Base Volume: 100% - "Base Volume": indicates some "un-amplified" volume of the output device.
                      0.00 dB
         Monitor Source: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor - The name of the "monitor" associated with this Sink
                                                         (virtual Source: allows you to write "sound from the speakers")
         Latency: 161458 usec, configured 210000 usec - Sound lag options
         Properties: ... - Any properties.  There you can see the name of the device Alsa, where this Sink gives the sound
         Ports: - Sink's ports: correspond to the physical ports of the sound card
                 analog-output: Analog Output (priority. 9900) - Headphone output
                 analog-output-speaker: Analog Speakers (priority. 10000) - Laptop speakers
         Active Port: analog-output-speaker - The current selected port: I play music from the speakers of the laptop.
 Card # 1 - Physical Sound Card
         Name: alsa_card.pci-0000_00_1b.0
         Driver: module-alsa-card.c
         Owner Module: 5
         Properties: ...
         Profiles: - Sound Card Profiles: Selected physical port mapping preset. 
                 output: analog-stereo: Analog Stereo Output (sinks: 1, sources: 0, priority. 6000)
                 output: analog-stereo + input: analog-stereo: Analog Stereo Duplex (sinks: 1, sources: 1, priority. 6060)
                 output: iec958-stereo: Digital Stereo Duplex (IEC958) (sinks: 1, sources: 0, priority. 5500)
                 output: iec958-stereo + input: analog-stereo: Digital Stereo (IEC958) Output + Analog Stereo Input (sinks: 1, sources: 1, priority. 5560)
                 input: analog-stereo: Analog Stereo Input (sinks: 0, sources: 1, priority. 60)
                 off: off (sinks: 0, sources: 0, priority. 0)
         Active Profile: output: analog-stereo + input: analog-stereo
 Source # 1 - Source: allows you to record sound
         State: IDLE - Source is inactive: I am not recording anything :)
         Name: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
         Description: Monitor of Internal Audio Analog Stereo
         Driver: module-alsa-card.c
         Sample Specification: s16le 2ch 44100Hz
         Channel Map: front-left, front-right
         Owner Module: 5
         Mute: no
         Volume: 0: 100% 1: 100%
                 0: 0.00 dB 1: 0.00 dB
                 balance 0.00
         Base Volume: 100%
                      0.00 dB
         Monitor of Sink: alsa_output.pci-0000_00_1b.0.analog-stereo - This Source was created as a monitor of the 1st Sink:
                                                  from it you can record what is heard from the speakers
         Latency: 0 usec, configured 1999818 usec
         Properties: ...

pactl & pacmd

In addition to pactl, there is another utility for managing PulseAudio: pacmd .
If a module-cli-protocol module is connected to PulseAudio, a UNIX socket is created on which the PulseAudio-shell hangs. This is a lot like telnet :)
Shell accepts text commands and executes them. pacmd help will show a list of available commands.
The problem is that pactl is not able to do everything, and pacmd not so easy to use. In the future, we will stick with pactl , but if its functionality is not enough, we use pacmd .
At the end of the article I will demonstrate my own utility that works on top of the standard ones: much more functional and convenient :)

Card Profile: sound card port mapping

The sound card has a limited number of connectors, and there can be a lot of applications for them: you can use everything for 5.1 audio output and leave one for the microphone; or leave one jack for stereo output, but get the microphone and linear inputs; or…
Alas, magic is not a ride: the sets are preinstalled by the manufacturer. Their list can be viewed in the “Profiles” field of the “Card” object.
Each profile is given priority (priority): a number that determines the “degree of attractiveness” of this particular profile. By default, PulseAudio selects the profile with the highest priority.
For each profile, it is indicated how many Sinks and Source'ov it will generate if you select it: sinks: 1, sources: 0 .

At the moment, you can not change priorities, but you can choose one or another profile.
In the pactl list output, we pactl list name of the map and profile, and select:
pactl set-card-profile 'alsa_card.pci-0000_00_1b.0' 'output:analog-stereo+input:analog-stereo'
Everything, ports of the card are reassigned.
You can generally turn off all connectors by selecting the "off" profile:
pactl set-card-profile 'alsa_card.pci-0000_00_1b.0' 'off'

Sink Port: switching sound to headphones

One Sink can have several audio ports. For example, Sink created on the basis of a sound card of a laptop, can have two ports: built-in speakers and a headphone output.
Sink's current port can be viewed in its Active Port: analog-output-speaker field Active Port: analog-output-speaker .
Looking at the Ports list of the required Sink object, assign the port:
pactl set-sink-port 'alsa_output.pci-0000_00_1b.0.analog-stereo' 'analog-output'
and the sound is instantly transferred to the headphones, turning off the speakers.
Similarly for Source. Perhaps your microphone does not work in Skype precisely because the wrong recording port is selected :)

Default Sink: default audio output

PulseAudio marks one of Sinks as “default”: new running applications by default will be played through it. In other words, if the application does not select Sink itself (for example, from user settings) when creating a Sink-Input, default-sink is selected.
The default Sink can be viewed by running the pactl stat command:
$ pactl stat
Default Sink: alsa_output.pci-0000_00_1b.0.analog-stereo
Default Source: alsa_input.usb-046d_0802_0825F490-02-U0x46d0x802.analog-mono

Let's change it: look at the name of the desired Sink and write:
pacmd set-default-sink '1'
Note that pacmd in some cases allows the use of numbers instead of long names.
Similarly for Source. By the way, an incorrectly chosen default-Source can also prevent you from talking to Skype :)

Move Sink-Input: move audio stream

With this action you can transfer Sink-Input from one Sink to another. In this way, you can transfer music to a newly connected USB speaker.
We look at the names of two Sinks, find the required Sink-Input and write:
pactl move-sink-input 16 '1'
and the Sink-Input # 16 sound instantly switches to Sink # 1. This may be a USB speaker.
Alas, it is impossible to transfer all Sink-Input'y at pactl using pactl or pacmd . At the end of the article will be a solution.
Source-Output can also be switched between Source'ami.

Volume control

The volume in PulseAudio is set numerically: 0..65535 (corresponds to 0% and 100%), and also - a surprise! - Values ​​greater than 100% cause signal amplification, which is indispensable when watching movies with unnormalized volume.
Here you can use Sink's index:
pactl set-sink-volume '1' 100 # ! 100/65535 = 0,15%
pactl set-sink-volume '1' 65535 # 100%
pactl set-sink-volume '1' 78642 # 120% —
pactl set-sink-mute '1' true #

Gain can cause clipping: the well-known "bounce".
Similarly, you can control the volume of the Source.
UPD : since version 0.9.15, the “FLAT_VOLUME” feature has appeared (it can be seen in Sink 'Flags'). With this flag, Sink's volume is automatically set as the maximum volume of all Sink-Input. You can disable it like this:
echo "flat-volumes = no" >> /etc/pulse/daemon.conf

Application volume

With the advent of Windows7, Linux users, not familiar with PulseAudio, were envious of the ability to adjust the volume of individual applications :)
Now it is possible: we look at the index of the required Sink-Input and strengthen the separate application:
pactl set-sink-input-volume '16' 80000


Sink and Source can “pause”:
pactl suspend-sink '1'


When the thought comes to your mind to add one of these commands to autoload, immediately throw it away! In such cases, use the PulseAudio configuration file.
An example can be found in the /etc/pulse/default.pa file. Pay attention to shebang #!/usr/bin/pulseaudio -nF : this is a script whose syntax is very similar to commands that pacmd understands.
For self-education:


Having been tortured by the inconvenience of official utilities, your faithful servant wrote his utility to manage the PulseAudio daemon :)


You can eat on GitHub: ootync / Patricks
(put the script files somewhere and make a symlink on patricks.php)

Work example:
See the list of Sinks. The current Sink is highlighted with a>:
  ootync @ rtfm ~ $ patricks ls sinks
 > Sink # 0 <alsa_output.pci-0000_00_1b.0.analog-stereo>
  Sink # 1 <alsa_output.pci-0000_00_2b.0.hdmi-stereo> 

Take a closer look at Sink # 0:
  ootync @ rtfm ~ $ patricks ls sink 0
 Sink # 0 <alsa_output.pci-0000_00_1b.0.analog-stereo>
 Is Default: yes
 Description: Internal Audio Analog Stereo
 Driver: module-alsa-card.c
 Owner Module: Module # 4 <module-alsa-card>
 Sample Spec: s16le 44100ch 2Hz
 Channel Map: front-left, front-right
 Mute: no
 Volume: 25%
 Base Volume: 100
 Latency: 199703
 Configured Latency: 210000
 Ports: analog-output, analog-output-headphones
 Active Port: analog-output
 Monitor Source: Source # 0 <alsa_output.pci-0000_00_1b.0.analog-stereo.monitor>
 Id: 0
 Name: alsa_output.pci-0000_00_1b.0.analog-stereo
 Proplist: 33 items 

Take a closer look at the list of ports Sink # 0:
  ootync @ rtfm ~ $ patricks ls sink 0 ports
 > # 0 analog-output: "Analog Output" prio = 9900
  # 1 analog-output-headphones: "Analog Headphones" prio = 9000 

Switch Sink # 0 to the next port
  ootync @ rtfm ~ $ patricks set sink 0 port next
 Sink # 0 <alsa_output.pci-0000_00_1b.0.analog-stereo>: analog-output-headphones 

Transfer all Sink-Input'y to the next Sink (for example, a newly connected USB-column)
  ootync @ rtfm ~ $ patricks mv sink next all
 Moving 1 inputs {65} to sink "alsa_output.pci-0000_00_1b.0.analog-stereo" ...
 Sink # 0 <alsa_output.pci-0000_00_1b.0.analog-stereo>: default sink 


Setting "from the author"
Examples of console commands
Module Description

In the next series

I will immediately say that the article will not be soon ... I myself have not understood everything :))

Source: https://habr.com/ru/post/106806/

All Articles