Good riddance to PulseAudio! (or “Settling on a Linux sound server”)

I have a love-hate relationship with PulseAudio: it has a lot of great features, but sometimes it’s more resource-hungry than I would like, and it also crashes more often than I’d like. Actually, the only reason why I got PulseAudio was because Skype 4.3.0.37 required Pulse and dropped ALSA support; I would have stayed with plain ALSA had it not been for Skype (on that note, I really hate Skype, for more reasons than one).

PulseAudio logo

PulseAudio

Let’s get the basics out of the way. The most basic way to get sound on Linux is through ALSA. ALSA, which stands for Advanced Linux Sound Architecture, is built into the Linux kernel and it provides a way for programs to use your computer’s sound cards. ALSA is great, but it’s difficult to do many things in it, such as controlling application volumes or controlling which programs take from which input sources. This is where a sound server comes in.

A sound server is a program that is run by the user (run in “user space“, as it’s called, in contrast to “kernel space”) to ease audio management and to provide extra features. Two popular sound servers for Linux are PulseAudio and JACK. PulseAudio is aimed at regular desktop users and it comes with features like per-application volume control and control of input sources. JACK is aimed at professionals and comes with features like low latency and the ability to plug one program’s audio/MIDI output into the input of another.

PulseAudio is included in many modern Linux distros as the default sound server, including Ubuntu and its derivatives. It’s intuitive, simple to use, and has lots of appealing features. That said, many things about it irk me. When I used PulseAudio on my Arch system, it would use up to 12% CPU on idle (quite unreasonable) and would occasionally crash in the middle of the night, and when it did crash, any applications emitting sound would stop and no sound would come out of my alarm clock cron job because PulseAudio wasn’t up, or if it was up, it couldn’t connect properly.

I dug around the log to see what I could find and found this:

Apr 15 02:15:02 jason pulseaudio[27896]: [pulseaudio] module.c: Failed to load module "module-esound-protocol-unix" (argument: ""): initialization failed.
Apr 15 02:15:02 jason pulseaudio[27896]: [pulseaudio] main.c: Module load failed.
Apr 15 02:15:02 jason pulseaudio[27896]: [pulseaudio] main.c: Failed to initialize daemon.
Apr 15 02:15:02 jason pulseaudio[27890]: [pulseaudio] main.c: Daemon startup failed.

Interesting. I went into the ~/.pulse/default.pa configuration file and commented out these lines to prevent the module from loading:

### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix
.endif

I no longer got that error message, but it still didn’t really help the crashes. However, as much as I like troubleshooting, I had enough of Pulse and wanted to move off it.

Recall that the only reason why I got PulseAudio was because Skype had a dependency on it, and the only reason why I have Skype is because the people I need to communicate with have Skype. I tried using it without Pulse like I used to, and I even tried apulse, but both were without success. It meant that I had to keep PulseAudio around, at least for Skype. Ideally, PulseAudio would only run when Skype was up, and the way I solved that problem is with a wrapper script:

#!/bin/sh
# save Master volume
#master_volume=`amixer get Master | tail -1 | awk '{print $3}'`

# check if pulseaudio is already running
pidof pulseaudio >/dev/null 2>/dev/null; existing_pulse=$?

# start pulseaudio, because Skype refuses to use anything else
pulseaudio --start

"${SKYPE:-/usr/bin/skype}" "$@"

# kill pulseaudio if it was started in this script
[ existing_pulse -eq 0 ] || pulseaudio --kill

#restore-alsa
# restore original volume
#amixer set Master $master_volume

Now, every time I want to use Skype, I just call this script instead of the actual executable.

Great, now I have something to start and stop Pulse on demand. Unfortunately, I’ve been spoiled by Pulse and couldn’t survive on plain ALSA anymore (I tried), so I needed a sound server. That left me with JACK. I had JACK already, and I’ve used it on occasion with PulseAudio on top (when the JACK server starts, PulseAudio detects it and connects itself as a client). I decided to give JACK a try.

JACK logo

JACK Audio Connection Kit

Since JACK doesn’t try to be such a general sound system like Pulse does (Pulse has support for relaying sounds over the network), it’s much lighter than Pulse. I had originally thought that the low-latency nature of JACK would mean that it uses more resources, but that turned out not to be the case. On idle, JACK uses a negligible amount of CPU resources, and as a result, my battery lasts longer.

What makes JACK even better is that I can redirect the audio output of one program to the input of another, much like plugging in a cable into the programs’ jacks (hence the name). To illustrate, suppose I’m on a Skype call; I connect my browser’s audio output to Skype so that the person on the other end can listen to whatever I’m playing. It can even do this for MIDI connections, so I can connect my MIDI keyboard to my computer and easily have Rosegarden or MuseScore send signals for my keyboard to play (it even lights up!).

JACK audio connections dialog

JACK audio connections in QjackCtl

What I do miss in JACK that I had with Pulse is per-application volume control. However, I figured that any properly-written application that outputs sound (think media players) would have its own volume control, so it’s not a big deal. Also, I recently moved to KMix as my sound mixer, and it has support for controlling the volume of some JACK clients (mostly media players with command-line controls, from what I can tell).

As great as JACK sounds, it’s not for everyone. Its setup is a little more complex than Pulse, and your user must take exclusive control of the sound card, which means no other users can use the sound card (there are ways around it, but those are even more complicated). Since I’m the only one who uses my system, though, I’m quite fine with that restriction and am very pleased with JACK. Although Pulse is still around for Skype, I can still satisfyingly say “good riddance” to PulseAudio. Now to convince people to get off Skype…

Advertisements

$ cat your_comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s