Sound matters

Ever wondered how to put a network to good use with sound matters? Here’s how it’s done at my place.

A simple setup looks like this:

image0

This diagram illustrates the use of the Music Player Daemon (MPD, wiki, wiki) on a centralized music library and audio output, with multiple MPD client programs (in blue) acting as remote controls over the network. The MPD remote control protocol understands simple commands like “play this item of the library,” “pause,” “forward,” etc. Both files and Internet audio streams (eg web radios) can be added to the MPD library, by uploading the files or radio playlists to an “upload” directory on the audio server.

This setup is robust and sufficient when audio is played from files and web radios. However, as suggested by the right side of the diagram, the setup becomes incomplete when audio is played from other sources such as Youtube, iTunes or Spotify.

At the cost of some extra complexity, the following configuration complements the setup above:

image1

This setup uses PulseAudio (PA) as a multiplexer on the audio server. In this configuration, the Music Player Daemon speaks to the audio output via PA. PA in turn accepts audio from multiple sources, and this opportunity can be exposed on the network. PA offers multiple network interfaces: its own PA “native” protocol, an EsounD compatibility layer, and a local process interface via a command named pacat which can be coupled with netcat to create raw access to PA from the network.

PA-aware music players are available on most platforms, except, at the time of this writing, on MacOS X where PulseAudio is not yet supported. Until it is, an alternative is to be found in the coupling of SoundFlower with EsounD. The former can be installed from its own download page, while the latter can be obtained via MacPorts or DarwinPorts. In this setup, SoundFlower provides a loopback interface where the audio output from any application becomes visible as audio input on the SoundFlower interface. It then becomes possible to use esd, EsounD’s audio server, to record (esdrec) the audio output from local applications and stream it, via netcat, to the audio server on the network.

Other sites have it that the EsounD protocol can be used directly on the network, by connecting esdrec with esdcat on one side, and telling esdcat to access the PulseAudio server directly on the other side. Unfortunately personal experience suggests that esdcat, in the latest versions of the EsounD software, cannot speak to a PulseAudio server. Hence the proposed use of netcat and pacat as a replacement.

The relevant commands:

# open network port 16002 to receive raw data for PulseAudio
while true; do nc -l 16002 | pacat --latency-msec=1; done

# connect the audio input on OS X to a remote raw sound sink via esdrec and netcat
esd -tcp -bind ::1 & sleep 2 && esdrec -s ::1 | nc <SERVER> 16002

YMMV. Any suggestions for improvements are welcome.