Sunday, September 9, 2012

Installing MPD on Ubuntu Server

I have been working on a project for my 1 1/2 year old daughter this weekend and just got it working before church this morning. We have a stereo/cd deck in her room and while useful for helping her to go sleep from time to time, there was no way to turn it on or control it without going into her room. Generally, she would wake up more seeing us in her room and would cry/beg for us to get her out of bed if we had to go back in and put some music on to help her go to sleep.

Enter MPD (Music Player Daemon). MPD is a service (a.k.a. daemon in GNU/Linux terms) that allows you to play music from a remote system's hard drive through that same remote system's audio hardware letting you control it from your local system (as I am using it; it does more, but I haven't fully explored it yet). There are many clients you can use to control the music service (Android, Linux, Microsoft, Apple).

The Setup

I already have a FoxConn nt330i nettop running Ubuntu 10.04 x86 Server dedicated to my daughter Summer hosting a motion server that streams a webcam feed of her crib to our local network allowing my wife to check on her during nap times (the grandparents have access too since they live in other states). I figured I could find a way to add in some sort of media playback even though this is a "headless" server (no keyboard or screen hooked up to it, and no GUI running; just a command line server). I found some cheap Harmon/Kardon desktop speakers I had stored away in a box and plugged them in, turned them on, and set them at a medium volume level. I did some Google searches and came across several explanations of basic setups for MPD on desktop versions of Ubuntu, but nothing clear on how to set this all up start to finish on a headless box which does not have any audio playback support by default. This lead me to document what I did in this blog entry should I need to do it again or should someone else be in the same boat as me and not know what pieces are missing from the server version to get the full audio setup working.

Getting It Working

These steps will get you from a clean Ubuntu 10.04 x86 Server install to working MPD controlled by a remote client.
  1. Install Ubuntu 10.04 Server (x86-64 should work, I have only tested on x86). Install options shouldn't affect the rest of this guide, so pick what you normally do when setting up a new box. This should work on other versions of Ubuntu, Debian, Debian-based, or any other version of Linux, but you might have to find your own sources for these packages, resolve dependencies, and compile from source. Test in a virtual machine if you want to try out a different OS. Or you could use Ubuntu.
  2. Install software that is needed for audio, MP3, and MPD.
  3. sudo apt-get install alsa lame mpd
  4. Set your sound levels using the Alsa sound mixer
    sudo alsamixerYou will see a picture like below with your sound output options. Make sure the output you are using is turned up and that the box at the bottom of that channel doesn't have an "m" in it ("m" signifies it is muted). I am using the front audio out jack so I have adjusted the channels marked "master" and "front" using the left and right arrows to select each channel, the "m" key to unmute it, and the up and down arrow keys to adjust the volume level (by default everything is muted and turned all the way down).
    Alsa Mixer
  5. Set your MPD options. Generally all you need to do is make note of the music_directory parameter and the bind_to_address parameter. The music_directory can be anything you want (I set mine to /music and copied all my music into that folder). The bind_to_address is set to "localhost" by default in Ubuntu even though the config file says the default is "any". I set mine to any just in case my IP address changes. You can hard code yours to match your IP address if you want.
    sudo nano /etc/mpd.conf
    update the two lines below in this file
    music_directory        /music
    bind_to_address       any
    press ctrl+o to save (output) the file, followed by ctrl+x to exit the file
  6. Create your music directory if it does not exist.
    sudo mkdir /music
  7. Copy your music into the music directory. You can copy it from a USB flash drive or somewhere on the network for example. I have a central home server that all my data is backed up to and hosted on, so I used rsync to copy all of my MP3s to Summer's computer. Notice the trailing slashes on the paths in rsync. Those are important (and a discussion for another time).
    sudo rsync username@sourceserver:/all/of/my/music/ /music/
  8. Set the music directory permissions. As noted in the mpd.conf file, the mpd user needs read access on the music, and since this isn't private tax data, I just set it to read all for all users.
    sudo chmod -R a+r /music
  9. Restart MPD to apply new settings.
    sudo service mpd restart
    If you get any error messages after issuing this command such as "could not bind to address x.x.x.x" or "failure to read from /music (or whatever you put for your music_directory)" double check the edits you just made by revisiting step 5 paying special attention to anything that might have a typo or wrong IP address in it.
  10. Connect to the server using whatever MPD supporting client you wish (I use an Android app on my tablet, my wife uses a Windows 7 client on her laptop). A list of these clients can be found on the MPD wikia link above. Consult the application's documentation for how to configure connection settings.
  11. Queue up music and see if it works! You might want to start with lower audio levels than what I did, these setting worked for me, but yours may turn out a lot louder depending on what soundcard and speakers you have.
Now Summer falls asleep from time to time with the soothing sounds of Native American flutes or classic lullabies. Since she is still teething, the music helps her relax and fall asleep faster, and can help calm her down when she wakes up with a particularly bad tooth ache. I just started some more quiet flute music for her while typing this when I noticed her fuss a little in her sleep on the webcam.

5 comments: