Sunday, September 13, 2009

Home audio server from Mac Mini (or any Linux/Unix)

I've read many posts about using computers as home media servers, but none of them provided the necessary functionality which I needed. In this post, I'd like to share my setup with a Mac sound server, and a Mac client. The process can be adopted to Linux easily.

The setup

First of all, I use a laptop as my main computer, and all of my music is in it. I bring it everywhere. At home, I have a Mac Mini. It is used mainly as a guest computer and for backup.

When I go home, I'd like to use my stereo to play my music. Plugging the sound cable into the laptop is too old-school, and it limits my movement in the flat, so I needed to find another solution.

I could use Apple's music sharing technologies (iTunes Music Sharing, iTunes Home Sharing - this is new in iTunes 9), but the problem is that it does not count in my laptop that I have listened the music. I have automatic playlists, which pick only songs which are not played recently, so that I don't listen music repeatedly. I also use iTunes DJ a lot, and remote libraries cannot be added to iTunes DJ, so it is also not working. I like my playlists, so it is not a good solution.

I had an Airport Express before, but it did not play music videos, just music. It would have been a good solution, though.

The best setup so far was that I mounted my iTunes library which relies on the laptop to the Mac Mini and started the iTunes instance in Mac Mini. It always took a couple of minutes to set up, so I did not use it frequently. Another reason why I did not like this was that I often started iTunes in the laptop accidentally when the iTunes run on the Mini, and that is dangerous: who knows what happens with your library file if two iTunes instance uses it. The last and most annoying problem was that if someone else used the Mac, then my iTunes was in the background, and could not use the audio output.

The solution

In my old Linux days, I used Esound to send music and sound effects to another computer. It was more than 10 years ago, but Esound is still around, I've found two blog entries which uses that to solve this problem: on MacOSXHints and on a DSLinux-related site. I improved them a little, and here is my solution:

The server

The server computer in my case is a Mac Mini. This computer is connected to the Hi-Fi, so this is turned into an audio server, like the Airport Express, but it also mixes the sound effects of the currently logged in user, so that guests also can listen to some music when they use the computer.

What you need to do the setup:
  • esound
  • netcat (I used gnetcat here.)
These can be installed through macports or might be available through fink, too. On modern Linux distros, these packages already installed.

I created a shell script, which starts the esound daemon and listens to incoming music. Put this script to any place, for example name it as /usr/local/sbin/
BINPATH=/opt/local/bin # macports binary directory
export HOME=/Users/root
$BINPATH/esd -as 1 &
while true; do
$BINPATH/gnetcat -l -p 7752 | $BINPATH/esdcat
Add executable rights to this and create a home directory for the root user: /Users/root.

Then create a launchd config file, which will start this shell script every time you restart your computer:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
<plist version="1.0">
Save this file as /Library/LaunchDaemons/esound.plist. Then load and start the script as root:
sudo launchctl load /Library/LaunchDaemons/esound.plist
sudo launchctl start esound
If you have a Linux box, you probably want to tweak the path of the binaries, and use the rc.d mechanism to start these jobs.

Now what you have is a music server computer, which listens accepts sound input on TCP port 7752, and automatically restarted in every reboot.

The client

The client is in my case also a Mac (a laptop). I used the following software in it:
  • esound
  • netcat (I used the standard nc command here)
  • SoundFlower
You can install esound and netcat as you did for the server. SoundFlower is a Mac application, so you should not have problem installing that.

Start SoundFlowerbed and set up SoundFlower(2ch) as a default input and output channel in Audio Setup.

Then start esd, esdrec and netcat to send the music to the server:
esd &
esdrec | netcat servername 7752
Start your iTunes, and Voila!

When you are finished and you want to listen to music again in your laptop, you just need to stop esd, esdrec and netcat, plus you can just map SoundFlower output to the Built-in Output.


The original tips uses music compression, but I've found it not necessary. Uncompressed music uses about 170k/s bandwidth, it is fine for a decent wireless router. If you have problems with the bandwidth, you could take a look at the referenced tips how to do the music compression.

No comments:

Post a Comment