# Linux Plays Sound
by Seth Kenlon

It happens to everyone, and usually only when it matters the most. You
might be gearing up for a family Christmas video chat, or settling in
for a movie night on your big screen TV, or getting ready to record a
tune that popped into your head and wants out. At some point, if you
use a computer, sound is going to need to be routed.


## Linux Audio

Without going into technical detail, here's a map of how Linux audio works.

First of all, there's a source and there's a target: something is
making sound, and something else is supposed to receive and process
that sound.

For most everyday tasks, this translates to an application (like VLC,
for instance) generating sound and some device (like your
speakers or headphones) receiving that sound and delivering it to your
ears.

The other way round is basically the same; some device (like a
microphone) generates sound and sends it to some application (like
[Jitsi video chat](https://jitsi.org) or the
[Qtractor](https://qtractor.sourceforge.io) DAW) for processing.

No matter what, the model is always the same. Sound is generated by
one thing and sent to another.

Between those two end points, there's the Linux sound system(s),
because, after all, something's gotta route the sound.

![A frontiersman's map of Linux audio](soundmap.png)

Without going too far back in history, it can be said that Linux audio
was traditionally managed by ALSA. In fact, Linux audio is *still*
managed by ALSA. The difference is that on modern Linux, users don't
generally need to deal directly with ALSA to route sound. Instead,
they can use tools sitting on top of ALSA, like [Pulse
Audio](https://www.freedesktop.org/wiki/Software/PulseAudio).

I'm assuming in this article that you have sound working on your Linux
machine on a daily basis, but that you get thrown off balance when you
suddenly need to get specific about sound inputs and outputs. This is
not an article about how to install drivers or set sound defaults. If
you want to know more about that level of sound configuration, there
are [support forums](http://linuxquestions.org) and [documentation
sites](http://slackermedia.info/handbook/doku.php?id=linuxaudio) to
help you. This article is about getting comfortable with the sound
controls of a modern Linux system.


## Why Pulse?

Why is Pulse necessary?

Strictly speaking, it isn't. ALSA works so well that some
distributions only just integrated Pulse this past year.

However, dealing directly with ALSA can require a lot of manual
hacking. Sometimes this could lead to some pretty convoluted configs
and wrapper-scripts and still never get one configuration that would serve
your every use-case. The problem wasn't always with ALSA. Sometimes it
was [the fault of the
application](https://bugzilla.mozilla.org/show_bug.cgi?id=812900#c24)
itself, but that doesn't change the end result. Your box is still
"broken".

The thing is, we're demanding a lot more of our computers now than
ever before. Audio output used to be either a speaker or
headphones. Now we want our computer to beam audio across the room to
the screen we use as a TV, and to pick up audio from a bluetooth mic
in a phone.

Pulse sits passively between the thing generating sound and the thing meant to
receive that sound, and makes sure everyone plays nicely with one
another. It adds several bonus features, too, such as the ability to
send audio to a different computer, and invisibly changing the sample
format or channel count.


## Learning Pulse

To get comfortable with Pulse, you need to remember three things:

1. Check your cables (virtual and physical)
2. Set sound input or output from source of sound
3. Manage your targets from the Pulse Audio Control (`pavucontrol`)


### Checking Cables

Check your cables. Check volume knobs. Check mute buttons and power
buttons. It's the "turn it off, and then on again" of audio
engineering.

[Admit it. You've done this once or twice yourself, too.](mute.png)

If you left your headphones plugged in, or you forgot to power on your
speakers, or turned the volume down on your speaker or the application
playing sound, then it's pointless to spend time and effort
configuring your system. Do the "dummy check" first.


### Application Preferences

Similar to checking cables and knobs, check the settings of the sound
application you're using on your computer. Not all applications give
you much of a choice, but there's usually some kind of menu somewhere
governing what the application does with its sound. VLC, for example,
gives you lots of choices:

[VLC audio preferences](vlc.png)

While an application like Google Hangouts gives you a simplified view.

[Google video chat preferences](google.png)

It's up to you to decide where your sound is headed once it leaves its
parent application. Make sure it's set sanely.

If you're confused by all the choices, then it's usually safe to send
sound to Pulse.

* Send sound to Pulse to benefit from Pulse's simplified
 world-view. You can send stuff to Pulse and manage it from Pulse's
 control panel. That's Pulse's job: to dynamically manage sound.

* Send sound to ALSA, and you have more direct control. This is
 especially important if you're using pro apps, like a soft synth and
 an effects rack and a DAW, and you need absolute control over
 channel routing and process flow.

Pulse has an ALSA plug-in, so even if your first choice as a
destination is ALSA, you'll still have some ability to manage that
sound from Pulse. Pulse doesn't "steal" your audio, though, so you
don't have to worry about Pulse intercepting your signal and
re-routing it some place else. Pulse always respects the choices made
at lower levels (and ALSA is about as low as you can get in the sound
system, drivers notwithstanding).


### Pulse Audio Volume Control (pavucontrol)

The nerve center of Pulse Audio is `pavucontrol`, more often known as
"the sound control panel", because its default home is in Gnome's
System Settings. It's also available as `pavucontrol-qt` for KDE
System Settings. Finally, it can be installed and invoked as a stand-alone
application, so it pays to remember its official title.

`pavucontrol` is a dynamic panel consisting of five tabs:

* Configuration: activates sound cards and defines the usage
 profile. On my desktop machine, for instance, I generally have HDMI
 de-activated and my built-in analogue card on and set to Stereo
 Duplex.

* Input Devices: currently available input devices (anything capable of making sound). These
 usually consist of a microphone (very common on laptops, which
 usually have a built-in mic for the webcam), a line-in, and a
 monitor device for whatever is currently playing on your system
 (more on that later).

* Output Devices: currently available output targets, such as
 desktop speakers and headphones (plugged into Line Out ports), and
 USB headsets.

* Recording: currently active recording sessions. This might
 be a web browser looking for sound input for a video chat session,
 or it might be a recording application like Audacity. If it's got a
 socket open for sound, it's here.

* Playback: currently active sounds streams being played. If it's
 meant to be heard, then it's here.

The important thing to remember about `pavucontrol` is that it is
dynamic. If Audacity isn't recording, then it won't show up in the
**Recording** tab. If XMMS isn't playing, then it won't show up in the
**Playback** tab. If your USB headset isn't plugged in, then it won't
show up in the Input or Output tabs.

### Routing Sound with pavucontrol

Routing sound in `pavucontrol` is done entirely through drop-down
menus. Try something simple, first, by launching your favourite music
player and and playing some music. Then open `pavucontrol` (remember,
it may be located in the GNOME or KDE System Setting > Sound panel on
your distro) and click the **Configuration** tab.

In the **Configuration** tab, take note of what device is the active
one, and what profile it is using. Mine is **Built-in Audio** set to
Analog Stereo Duplex, but yours may be different.

Once you've got that jotted down somewhere, change it to **Off**. Sure
enough, the music stops. Well, it doesn't stop, it's just not being
heard by you because you un-set your default active output. Change the
setting from **Off** to whatever it was before, and your music
returns.

[pavucontrol](soundconfig.png)

That's the principle behind Pulse. Seems pretty obvious now, doesn't
it? Let's try something something more complex: let's hijack the sound
playing on our own computer, and record it to a file.

Launch [Audacity](http://www.audacityteam.org) and set its input source to Pulse.

[Audacity > Edit > Preferences](audacitypref.png)

Press the **Record** button or go to the **Transport** menu > **Record**.

At first, you might notice that you're recording silence. So switch
over to `pavucontrol` and navigate to the **Recording** tab.

In the **Recording** tab, click the drop-down menu on the right and
set the sound source from **Built-In Stereo** (or whatever yours is
set to, depending on your system defaults) to **Monitor of**. This
sets the source of the sound from the physical device (in my case, the
desktop speakers I listen to music from) to a software *monitor* of
that device. Check Audacity again and you'll find that you're now
intercepting and recording your own system.

The same process holds true for video chatting with friends. If Pulse
doesn't know to send the input from your USB headset to your web
browser or video chat application, then unless it just happens to be
the default anyway, the sound isn't going to reach your video chat
application.

The same is true for playing audio. If you're playing a movie and not
hearing the sound, check Pulse. It could be that you're sending sound
to a non-active sound device, or to something that's been muted.

## Linux Plays Sound!

Yes, it's 2017 and Linux can play sound. But it can do more than that:
it can manage sound. And you can, too, as long as you learn the
tools. And, as always, don't panic.