Preamble

Bitperfect audio on linux (or any OS, really) means your music player’s transport must connnect directly to the sound hardware without there being a mixer in the way.Why? Two reasons:

  1. In order to mix, incoming audio streams all need to be resampled to a common audio format (usually 48kHz/16bit). This rides roughshod over those 192kHz/32bit high-resolution files you paid good money for from HDAudio or wherever.
  2. Adding additional software to the signal path cannot improve audio quality.

To connect music players direct to sound cards on linux the choice seems to be between ALSA (long history, well-understood) and Jack (pro-audio production capabilities). For the audiophile ALSA will do just fine – we don’t need the flexibility and low latency Jack offers.

Configure the high-quality audio player

I chose gmusicbrowser as my iTunes-a-like media library. This guide tells you how to configure it to talk directly to ALSA so as to ensure bitperfect playback. The guide doesn’t reference the fact that gmusicbrowser supports a few different audio transports, and it is within the chosen transport where you need to make the configuration listed in the guide. I have chosen gstreamer as the transport, on the basis that it has a vast number of audio format plugins in its library (especially that it supports ALAC and AAC). Obviously this means you need to install a few plugins to get a good range of audio format support, but that’s not the focus of this howto – I’ll leave it as an exercise for the reader.

Audio for the rest of the system

Now then, didn’t I say something about “no mixer” earlier on? That’s going to be a pain in the butt if I want to have my web browser, video player, etc. all sharing the same soundcard, so what to do? Well, on linux Pulseaudio is the de facto solution for shared soundcard usage, and is installed in pretty much every linux distribution out there. Pulseaudio’s architecture is interesting in that it doesn’t boot up when the OS boots up. Instead it runs on demand, when an app tries to access it. The only problem is that the linux GUI (KDE Plasma in my case) simply tries to access it as soon as that GUI starts up, consequently giving Pulseaudio exclusive control of the sound card. This means that, because we configured it to talk directly to the card via ALSA, gmusicbrowser will complain that it cannot access the sound device when we try to play music.

So then, how do we get these two audio management solutions to co-exist?

Switching between Pulseaudio and bitperfect audio

At a high level, to configure the system to be able to switch between our two ‘audio modes’, we do this:

  1. Set Pulseaudio to not spawn automatically.
  2. Start Pulseaudio explicitly when the linux GUI starts.
  3. Use a script to toggle whether Pulseaudio is running or not.

Here’s those steps in detail:

  1. To tell Pulseaudio not to spawn automatically, create a file named client.conf in your home directory at the following path:

~/.config/pulse/client.conf

This file needs to contain just the following line:

autospawn = no

  1. To manually start Pulseaudio, we simply need to run it and tell it to run as a daemon:

pulseaudio -D

It is trivial to create a bash script that executes the above, then call that script when your GUI starts up. I’m running gentoo with the KDE Plasma 5 GUI, so I simply go into the System Settings application and browse to Startup and Shutdown->Autostart then click Add Script.. to add a call to my script that executes Pulseaudio. YMMV.

  1. I’ve written a script that will detect whether or not Pulseaudio is running, then execute it or kill it as necessary to toggle its state. Here’s the content of that script:

#!/bin/sh
# obtain the pid of the pulseaudio process and count the number of return values
case “$(pidof pulseaudio | wc -w)” in

0)  # pidof returned nothing so pulseaudio is not running, hence start it (daemonized)
pulseaudio -D
;;
*)  # pidof returned at least one value so pulseaudio running, hence kill it
# pulseaudio –kill
# use killall to handle cases where there happens to be more than one instance running
killall pulseaudio
;;
esac

I’m not going to insult your intelligence by telling you how to save that in a file and make it executable. With the script in hand, in KDE Plasma it is trivial to add a button to the taskbar panel (using the Quicklaunch widget) that can run any chosen script. One can also alter the icon the button uses, so as to select a ‘speaker’ icon from the library of available icons. In this way, I have a button on the taskbar that can toggle whether Pulseaudio is running or not.

Day-to-day usage

With the above setup, on startup the system will be using Pulseaudio and all apps will share soundcard usage, the system-wide Pulseaudio Volume Control application will work and so will hardware volume keys on the keyboard. As soon as I want to switch to audiophile-quality playback I click the button which shuts down Pulseaudio. From that moment, the system-wide volume controls stops working, as do the hardware volume control keys, and no normal app (web browser etc.) will be able to play audio. Any application that is configured to talk directly to ALSA will however now be able to do so, and gain sole usage of the soundcard. In this way, gmusicbrowser can play back those high resolution audio files in their native bit rate. Volume control is managed only within each such piece of software.

Other info

The reason to start the system up with Pulseaudio enabled is that certain software (I’m looking at you Firefox) might not output sound at all if it doesn’t find Pulseaudio when it is started. I would also like to express my thanks to Rizlaw and yay101 from the head-fi.org forums, whose threads at that site were pivotal to my understanding whilst working out this configuration.