Volume Control and Output Selection with PulseAudio Command Line Tools

This article shows you commands to control volume and move playback streams to different outputs. For example, you can move VLC sound from laptop headphones to external HDMI monitor speakers while the stream is playing.
You can do all these things with graphical user interface, too. Just use “Audio mixer” (pavucontrol) from the menus and move sliders around. Command line just allow you to automate these tasks or create new keyboard shortcuts.

Fluency with Linux command line interface is required to follow this short article.

PulseAudio the Default Sound System

PulseAudio is the default sound system in many modern Linux desktop distributions, such as Ubuntu.
In PulseAudio, programs that play sound create playback streams (also known as input streams). For example, you might have VLC playing a video, and it’s sound would be a playback stream.
To actually hear the sound, an sink (also known as output device) such as a speaker or headphones are needed. For example, you might hear the sound of your video from your laptop speakers.

Install Command Line Tools

PulseAudio itself provides the best command line tools for managing it. In Ubuntu, they are in package pulseaudio-utils. Many articles in the web recommend older, more cumbersome amixer.

$ sudo apt-get -y install pulseaudio-utils

Control Volume

To make volume louder or quieter

$ pactl set-sink-volume 1 -- +5%
$ pactl set-sink-volume 1 -- -5%
$ pactl set-sink-volume 1 -- 30%

The syntax is

  • pactl
  • set-sink-volume
  • sink numberĀ  – seems to be sequential and zero based (0, 1, 2…)
  • two dashes “–” so that the next minus is not mistakenly considered a flag
  • plus “+” louder, minus “-” quieter, no prefix for absolute volume. Percentage and the percent sign.

A sink can be unmuted with

$ pactl set-sink-mute 1 0

Where the first number 1 is the number of sink and the second number 0 is False (mute: false).
You can combine volume up with unmuting to get the typical behavior of volume control

$ pactl set-sink-mute 1 0; pactl set-sink-volume 1 -- +5%

You can list sinks

$ pacmd list-sinks

From Headphones to Speakers – Redirect to Sink

Find your playback stream

$ pacmd list-sink-inputs

Then you can move it to another sink (output device)

$ pacmd move-sink-input 42 1

The first number is playback stream (input stream) index, the second number is the output device (sink).
You can set a default sink. Maybe it’s used as a default for new playback streams.

$ pacmd set-default-sink 1

Move All Playback Streams to a Sink

Here is my one-liner to move all playback streams to a new sink. There seem to be a lot of longer scripts to do the same thing in the web. The last number in pacmd is the target sink, typically 0 or 1.

pacmd list-sink-inputs|awk '/ +index: /{ print $2}'|xargs -n1 --replace="PLAYBACK" pacmd move-sink-input PLAYBACK 1

What Next?

Now that you have the control in the command line, you can do anything with these commands. For example, create a shell script to move the sound to speakers, start a video, disable email alerts and dim lights.
If you want to do complicated things, you can easily manipulate PulseAudio streams from any language by making a system call. For example, you can run these commands from a Python program. You cound even create a program to toggle between speakers and headphones, pa-toggle-sink.py.


Fixed some typing mistakkes. “What’s Next?”

Posted in Uncategorized | Tagged , , , , , , | Comments Off on Volume Control and Output Selection with PulseAudio Command Line Tools

Comments are closed.