May 31, 2007

Music Player Daemon rocks your net

Author: David A. Harding

The Music Player Daemon (MPD) is a very different, and very cool, way to play back music online. Unlike typical music-playing applications, MPD does not have a graphical (or even command-line) interface. Instead, a variety of clients interact with MPD over the network.

This means multiple applications (clients) can interact with MPD simultaneously. One client might have an iTunes-style graphical user interface, another might run from the command line, and a third might display the currently playing song in the status bar of a Web browser or text editor. About 60 clients can already communicate with MPD, and at least 40 more are being developed.

Licensed under the GNU GPL, MPD has been available for several years, and is packaged for most popular GNU/Linux distributions. Debian, Ubuntu (using the universe repository), Mandriva, and Gentoo users can install the mpd package or ebuild. Fedora and openSUSE don't include MPD, but you can find unofficial RPMs for those distros online. To compile MPD from source, start at the install page on the MPD Wiki and follow the instructions for General Installing From Source.

A single configuration file, /etc/mpd.conf, controls MPD's behavior. It includes five path settings:

######################## REQUIRED PATHS ########################
music_directory                 "~/music"
db_file                         "~/.mpd/mpd.db"
playlist_directory              "~/.mpd/playlists"
log_file                        "~/.mpd/mpd.log"
error_file                      "~/.mpd/mpd.error"
################################################################

MPD can play only audio files in the directory defined by the music_directory setting, or its subdirectories. The daemon will follow symbolic links (symlinks) in the music directory that point to directories and files outside the music directory -- so, for example, if podcasts are stored in ~/podcasts/, you can make them available to MPD by creating a symlink from the podcasts directory to a file within the music directory:

ln -s ~/podcasts/ ~/music/podcasts

Most music files contain metadata such as artist name, track name, and track length. MPD indexes this metadata and stores the results in the file defined by the db_file setting, where clients can display and search it.

Like most music player applications, MPD imports and exports M3U-formatted playlists. In MPD, playlists are read from, and written to, the directory defined by the playlist_directory setting.

The final two mandatory settings listed above, log_file and error_file, define the two files used, respectively, for informational and error logging.

There are about 60 other optional configuration settings listed in the manual page for mpd.conf. Most of the additional settings are for special cases: for example, one setting can send the audio output from MPD to a Shoutcast-compatible server like Icecast. (Interested? See the article on the MPD wiki.)

Once you've configured the settings the way you want them, you can start a custom-compiled MPD by running the command mpd. If you got MPD from a distribution-provided package, start (or restart) MPD with /etc/init.d/mpd restart.

MPD clients

One more thing is required to make MPD fully functional: an MPD client.

Although you can control MPD using telnet, I suggest starting with a graphical client. There are more than a dozen graphical clients listed on the MPD wiki as ready for general use. I've chosen to describe Sonata, an attractive GTK+ (GNOME style) client, but most everything in this section applies to all graphical clients, whether they are for GNOME, KDE, GNUStep, Mac OS X, or some other platform.

Sonata is included in more distributions than MPD, and typically called sonata. You can find source code and instructions for compiling Sonata on the download page of the Sonata Web site. Start Sonata by running the command sonata.

If you run Sonata on the same computer MPD is installed on, you don't need to configure it. Otherwise, right-click anywhere within Sonata, select preferences, and type into the Host field the hostname or IP address of the computer hosting MPD. Sonata presents a tabbed interface; the second tab from the left, Library, features all audio files and subdirectories in the directory configured as MPD's music_directory. Double-click on a file to add it to the current playlist, and press the play button at the top of the screen to begin playback.

Sonata's user interface is similar to most other graphical music players; only one option stands out as not intuitive. In the Library tab, right-clicking within the library creates a menu with the option Update Library. If you click this option, Sonata will rebuild the db_file database. If you add files to, or remove them from, the music directory, use this option to tell MPD to find the changes. Once the library update is complete, any changes in the music directory will be reflected in Sonata's Library tab.

While a graphical client, like Sonata, is attractive and easy to use, a command-line client can be used in scripts. The command-line client for MPD is mpc. It's distributed from a page on the same Web site as MPD, and is available in every distribution that distributes MPD.

As with Sonata, if mpc is installed on the same host as MPD, no configuration is necessary. Otherwise, set the environment variable MPD_HOST to the hostname or IP address of the computer hosting MPD. For example, my MPD is on the computer named callisto:

export MPD_HOST=callisto  # for bash, zsh, and other Bourne style shells
set    MPD_HOST=callisto  # for csh and tcsh shells

Once it's installed and configured, using mpc is easy. For example, to pause the currently playing file, wait one minute, and resume playing, type the following at the command line:

mpc pause && sleep 1m && mpc play

Anything a graphical client can do, mpc can also do: play, pause, change the volume, seek (fast forward and rewind), toggle the random and repeat modes, save and load playlists, add and remove songs from the current playlist, and update the database. An example script, from the mpc manual, illustrates how a script using mpc can load and begin playing a M3U-formatted playlist downloaded in Mozilla Firefox:

#!/bin/bash
mpc clear
cat $1 | mpc add
mpc play

Save this script as mpc-load-playlist, make it executable (chmod +x mpc-load-playlist), and the next time a playlist is downloaded in Mozilla Firefox, use mpc-load-playlist to open that playlist in the "What should Firefox do with this file?" dialogue. The code above will clear the current playlist, add to the playlist the URLs (which are links to audio files) listed in the M3U file, and then begin playing the playlist. To play the URLs, MPD will stream audio from the Internet.

As I mentioned earlier, there are about 60 mature MPD clients. A few, from a broad collection of platforms, are listed below:

  • ncmpc, a curses (console-based) client with vi-like key bindings
  • gmpc, another attractive GTK+ client
  • kmp, a QT (KDE-style) client
  • QMPDClient, another QT client
  • FoxyTunes, a Mozilla Firefox extension that interfaces with MPD
  • EMMS, the Emacs MultiMedia System, interfaces with MPD
  • Pitchfork, an attractive AJAX/PHP client

You can find the complete list of clients, and much more information, on the MPD wiki.

Click Here!