Home

About

Archives

Creations

MPD on OS X

2010/03/22

Having recently transformed my Macbook Pro into a desktop computer (by way of exploding thermos), I wanted to put it to good use, even in its hobbled state. I noticed that MPD is installable via Macports, and I thought it would be cool to use my immobile laptop as a music server. With this slick little app, I can now listen to any of the music I have on my hard drives at home with my laptop while I’m out and about. This wasn’t too tough to set up, either. Here’s how to do it.

First, an intro for the uninitiated. MPD is a client/server-model music player. It catalogues and serves up your music files, which you access using one of a whole host of clients. It’s small, fast, and it’s been my music player of choice for a while now.

So let’s get started. Before you do anything, make sure you have Macports installed correctly. Then, go to a terminal and install MPD:

sudo port install mpd

Additionally, you’ll want to install one or more clients. For a solid graphical client you can use Theremin, and for a command-line interface I’d suggest one of either mpc or ncmpc:

sudo port install mpc
sudo port install ncmpc

Now, you need to configure MPD. There are several ways to do this, but I’m going to take you through a single-user setup. This way, you won’t have to run MPD as root, and you’ll keep all your configuration, playlist and database files under your home directory. Various MPD setups are well-documented elsewhere on the web, but here’s mine in brief:

  • Create an MPD directory and required files in your home folder:

    mkdir ~/.mpd
    mkdir ~/.mpd/mpd.playlists
    touch ~/.mpd/mpd.state
    touch ~/.mpd/mpd.error
    touch ~/.mpd/mpd.pid
    touch ~/.mpd/mpd.log
    touch ~/.mpd/mpd.database
    
  • Copy MPD’s config file at /opt/local/etc/mpd.conf, to your MPD directory:

    sudo cp /opt/local/etc/mpd.conf ~/.mpd/
    
  • Edit it to look something like this:

    user                "<username>"
    # music_directory should point to wherever your music lives
    music_directory     "/Users/<username>/Music"
    playlist_directory  "/Users/<username>/.mpd/mpd.playlists"
    db_file             "/Users/<username>/.mpd/mpd.database"
    log_file            "/Users/<username>/.mpd/mpd.log"
    error_file          "/Users/<username>/.mpd/mpd.error"
    pid_file            "/Users/<username>/.mpd/mpd.pid"
    state_file          "/Users/<username>/.mpd/mpd.state"
    bind_to_address     "any"
    port                "6600"
    
  • Create MPD’s database (this could take a while, depending on how many music files you have):

    /opt/local/bin/mpd -v --no-daemon --create-db
    
  • When that finishes, <Ctrl>-C to kill it and restart MPD:

    /opt/local/bin/mpd --no-daemon &
    

It’s important to add the —no-daemon flag when running MPD on OS X. Something about the Macports version causes it to fail otherwise. The & just frees the process from the terminal.

At this point you should check that your music has been catalogued correctly. Use whatever client you grabbed above to list your music. Check the manual for your client if you’re not sure how to do this — each is a little different. With MPC it’s simply:

mpc listall

If your files are there, you’ve successfully built MPD’s database and should be able to play any of the files in your library. If not, double-check your MPD configuration.

Now, on to streaming. MPD is highly flexible in that you can set it up with any of a whole host of audio outputs. Edit MPD’s config file again, this time adding a streaming output:

audio_output {                                                                      
  type        "httpd"
  name        "Stream"
  # encoder   "vorbis"      # optional, vorbis or lame
  port        "9000"
  # quality   "5.0"         # do not define if bitrate is defined
  bitrate     "128"         # do not define if quality is defined
  format      "44100:16:1"
}

This will route MPD’s audio to HTTP port 9000 on your machine. At this point, (assuming you have the rest of MPD set up correctly), if you restart MPD…

killall mpd
/opt/local/bin/mpd --no-daemon &

… you should be able to stream music to any computer on your local network. You can try it from your Mac itself by starting MPD playing and opening the address http://localhost:9000/ in a stream-capable audio player (like VLC).

Now, streaming music across your local network is neat, but it isn’t all that useful in and of itself. In order to stream your music while outside your local network, you need to open up port 9000 to the outside world (or whatever port you specified for your streaming output in your MPD config — I’ll assume 9000 for now) . This means mapping public port 9000 on your router to private port 9000 on your Mac. You can do this using your router’s configuration interface (things get very different at this point, depending on your network setup… Google is your friend here). Once this is done, and assuming there are no firewalls or other nonsense in the way, opening http://your.public.ip:9000 with your favorite streaming music player will let you listen to anything in your music library from wherever you can get Internet access.

So, at this point you can listen to whatever happens to be playing. But there’s a problem — you can’t control MPD remotely. How do you start, stop and change songs when you’re out and about?

The fix here is similar to that above. MPD listens on port 6600 for clients, and again, while that will work automatically locally, you’ll need to map one of your router’s public ports to your computer’s port 6600 in order to control MPD from the outside. Doing as you did when forwarding public port 9000 to MPD’s audio stream, map a public port to MPD’s listening port, 6600 (it doesn’t matter which public port you choose — I used 9001 — just remember your choice).

With that set up, you’re almost there. Most MPD clients allow you to specify a host and port with which they’ll try to connect to MPD. Set up your client to use your new public IP and MPD port. With graphical clients, you can usually use the preferences window to set these. With command line clients, it’s typically something like this:

ncmpc --host=your.public.ip --port=9001

or

mpc --host your.public.ip --port 9001

You’ve now got full access to all of your music, no matter where you are. But there’s one last problem. With streaming set up, what if you want to play your MPD library like normal at home, through your computer’s speakers? MPD is routing audio to the HTTP stream instead of just playing it. To fix this, we add another, OS X-specific audio output to MPD’s config file (this is totally undocumented as far as I know, but it works):

audio_output {
  type "osx"                                                                        
  name "OSX"
}

Now, when MPD plays music it’ll send it to your speakers and broadcast it via HTTP. Some clients, like ncmpc, even have the ability to toggle outputs, in case you want one and not the other.

If you’re having issues, take a look at my OS X mpd.conf for reference, and feel free to email me with questions if you can’t quite get things working. Otherwise, if everything went as planned, you’re now armed with a powerful, flexible music server that you can access from anywhere.


2010-07-04: Thanks to Rune Flobakk for catching my errors!