dcsimg

Music on the Command-Line

Remotely control your tunes with MPD and ncmpc.

Linux has several good music players (RealPlayer, Totem, Banshee), some great music players (XMMS, Rhythmbox), and one awesomely fantastic music player that beats those found on any other operating system: Amarok. Those are all very cool programs, with one thing in common: a GUI.

So what if you don’t want or need a GUI? What if you want to control the music your computer is playing from across the room? What if you want the ultimate in speed and stability? Then you want a music player that was built for the command-line. You want the one-two, knock-out combination of MPD (Music Player Daemon) and ncmpc.

Start by installing MPD. You can get the source code from http://www.musicpd.org/files.shtml, or via your distro’s repository. Debian and K/Ubuntu users, for instance, can simply use apt-get install mpd.

Once MPD is installed, you need to create a configuration file. If you control your machine, you’ll want it to end up at /etc/mpd.conf; if you can’t create that file, or you’d rather have it in your home directory, create ~/.mpdconf. The easiest way to create the file is to copy mpdfconf.example to the new location and then edit the new copy. The example configuration file is very well commented, so you should be able to figure things out. Nonetheless, here’s a useful /etc/mpd.conf file:

music_directory "/home/scott/music"
playlist_directory "/home/scott/.mpd/playlists"
db_file "/home/scott/.mpd/mpd.db"
log_file "/home/scott/.mpd/mpd.log"
error_file "/home/scott/.mpd/mpd.error"
pid_file "/home/scott/.mpd/mpd.pid"
state_file "/home/scott/.mpd/mpdstate"
user "scott"
bind_to_address "any"
port "6600"

MPD first scans your music collection and then creates a database listing every file that it found, hence the db_file parameter. The log_file and error_file options are obvious, while the pid_file makes it easier to kill the program if it wigs out. The state_file saves your current playlist, song, and location within that song so that all that can restored when you restart MPD.

To create and store playlists, specify the playlist_directory; be sure to manually create that directory, or MPD wonít start like it should the very first time. Specify the user so that if MPD is started by root, it wonít run as root, and will instead run as the user you choose. The bind_to_address and port are the defaults, and are used by the programs that connect to MPD and actually play the music indexed by MPD.

Finally, the first parameter, music_directory, points to the location of your music collection. In the case of /home/scott/music, this is actually a symbolic link pointing to an external drive. If you have multiple drives for you collection, simply include a soft link on music pointing to the other drive. MPD happily follows that link to index the songs found on that drive as well.

The first time you run MPD — by typing mpd, naturally — you need to wait while the program indexes all of your music. If you have a large music collection, this could take an hour or more. Once itís done, you need to make sure that MPD re-indexes your tunes on a regular basis to discover additions. The command to update the MPD database is mpc update, and if you just added or deleted some music, go ahead and type that in, and then wait. If you want something a bit more regular, add /usr/bin/mpc update as a cron job to your computerís nightly task list.

You actually have quite a few choices when it comes to a client to connect to MPD and play music. A nice list can be found at http://www.musicpd.org/clients.shtml. The best CLI client is ncmpc, which can be installed using your package manager or can be downloaded as source from http://www.musicpd.org/ncmpc.shtml.

Start ncmpc with ncmpc and the program should open. If you changed the host and port in your MPD conf file, though, type man ncmpc and read the instructions for connecting. Press the number 1 if you need Help.

Press 3 to browse your musicís directory structure. When you find a song you want to hear, use your arrow keys to go to it and press Enter. You should immediately hear it start playing. To add a song to the playlist, press Space instead of Enter. To play the tunes youíve selected, press 2 to view your playlist and then press Enter. To pause, press Shift-P.

MPD and ncmpc arenít for everyone, but the combination beautifully meets the needs of many people. In fact, even if you love Amarok, you may find yourself using ncmpc instead, simply because itís faster and simpler, and because you can change the music thatís playing without vacating the couch.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62