banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Leveraging MPD (Music Player Daemon)


by Peter Kelly (critter)

About six years ago, a few paragraphs in a PCLinuxOS Magazine article were devoted to describe the powerful, but low on resource usage, Music Player Daemon. You can find the article here. The reason that the author skipped so lightly over it is, as he states in the article, that there is really not very much to say about it. It plays music. But it is a 'roll up your sleeves and get stucked in' sort of set up that not many users would want to be bothered with.

This music player can be controlled locally or over the network through clients, will use multiple outputs if required, has the ability to decode many types of music files, and maintains a database of your music files. In line with the Unix/Linux philosophy, it aims to 'do one thing and to do it well.' It does not, therefore, do video – it plays music.

Although the setting up of MPD is not difficult, neither is it obvious or particularly straight forward, particularly for users not used to having to roll up their sleeves to get something working. This raises a few questions.

  • Why bring up the subject now? Well, in six years a lot has happened in the Linux world that makes this utility much easier to integrate into a Linux/Unix environment.

  • Why would anybody want the hassle of setting up such a system?

    The big advantage of MPD is that it is so lightweight that it will happily play music on the most humble of hardware without getting in the way of using the computer for other, less frivolous purposes. Its memory requirements and CPU usage are barely noticeable.

  • Are there any other advantages over easier to use, feature-rich graphical applications like Amarok, Clementine or Deadbeef?

    Most dedicated music players, when installed, bring with them a whole host of libraries and files which they depend upon in order to function. MPD is is a simple beast. It runs daemonized, which means that it is loaded into memory and then sits around listening for requests for its services, but otherwise being quite unobtrusive. MPD is a server bound to a port on an IP address. This means that it is capable of supplying its services independently to other computers on the network, although this function is not necessary for standalone use.

  • This sounds scary, client-server stuff. Is it difficult to set up and use?

    No, you only need a few simple lines of text to adjust in the configuration files and a couple of things to type into a terminal, all of which I will show you. The documentation for MPD is not necessarily aimed at beginners. The best of the available documentation is often part of the documentation for a particular distribution such as Arch or Debian, but this can be frustratingly difficult to follow when it deviates from the files and structure of your own distribution. This is the reason for this article, to try to simplify the setup for PCLinuxOS users who want a low resource music playing utility.

The instructions that follow are for a single user system, which makes the set up so much easier, avoiding all those awkward file permission problems. All the configuration files will be in the user's home directory, and will be owned by the user.

As MPD is a server, you also need a client application to request and control the services it supplies. The most basic MPD client is called MPC which, if you hadn't already figured it out, stands for Music Player Client, and it is available from the PCLinuxOS repositories. Although MPC is is a command line only utility, it is very powerful and is ideal for setting up the system. MPC is good if you want to control playback of your music from scripts or from short-cut key bindings, such as the multimedia keys found on many keyboards today. I will cover this way of using MPC later.

Most people, though, I am sure would prefer to have a graphical 'front-end' client, rather than a terminal based client, to handle musical things. Fortunately, there are some very nice graphical clients available for us to download and use in the PCLinuxOS repositories. First though, we need to cover the basics of the system, and to know just what MPD and clients, such as MPC, are capable of.

To get started, install MPD (if it is not already installed) and MPC from the repositories, and we'll expand on how to use MPC once it is up and running.

After installing MPD, there will be a global configuration file in the /etc directory named MPD.conf. This is a long file with lots of comments, but way too complicated for the basic system that I have in mind, so I will create a simple configuration file in my home directory. When MPD is started, it looks for a configuration file in the home directory of the user that launches it and, if found, will use that file in preference to the /etc/MPD.conf file. I shall also need to create a few files and directories in my home folder for MPD to use. Having everything in my home folder means that nothing interferes with the system, and I can edit them without needing root permissions.

The music player daemon needs to know where the music files are stored, as well as where the playlists are. It also needs files to store the music database, its log file, its process id number (so that it may be easily stopped and started), and a file that holds the state of MPD so that it may resume play after stopping. We'll put these files in a hidden directory so that our home directory doesn't get too cluttered. Open a terminal and make sure that you are your normal user (not root) and in your home directory. The following code will then make the required files and directories for us:

mkdir -p .MPD/playlists
touch .MPD/{MPD.db,MPD.log,MPD.pid,MPDstate}



Now open your favorite text editor enter the following text, changing the file paths to reflect your own system settings. This will be your own personal MPD configuration file:


music_directory "/home/user/Music"
playlist_directory "/home/user/.MPD/playlists"
db_file "/home/user/.MPD/MPD.db"
log_file "/home/user/.MPD/MPD.log"
pid_file "/home/user/.MPD/MPD.pid"
state_file "/home/user/.MPD/MPDstate"
bind_to_address "localhost"
port "6600"
log_level "default"
audio_output {
type "pulse"
name "pulse audio"
format "44100:16:2"
}
audio_output {
type "alsa"
name "alsa audio"
format "44100:16:2"
}

Save the file in your home directory as .MPDconf. Make sure that you include the leading period.

I have two audio output sections here, one for alsa and one for PulseAudio. That's ok. PCLinuxOS can use either, and MPD will choose the one that is currently being used.

Open the PCLinuxOS Control Center and click on System then Manage System Services. Make sure that MPD is checked to run on boot. Now you may have to set MPD to run at login, if it doesn't start automatically. How you do this really depends upon the desktop environment you are using. I'm using Mate, which has a startup applications option in the Mate Control Center. The file that you need to start on boot up is /usr/bin/MPD. If you have trouble discovering exactly how to do this for your desktop, then you might try reading one of the excellent PCLinuxOS magazines special editions: there is one for most of the supported desktop environments. They can be downloaded in pdf format at http://pclosmag.com/special.html.

Now reboot the system. This is not strictly necessary, but it will make sure that everything so far is ok, and it should build the database for you. Depending on how many music files you have, this could take a few minutes. You might want to start with a music directory with just a couple of sub-folders containing the music files, and then later update the database to your full music collection once the system is established.

Check that MPD is running:



If not, then simply type the command MPD to start it up. If MPD was not running but this command starts it and comes back with no errors, then the MPD is not starting at boot and that situation needs to be addressed. Similarly, if you get an error, then you should check the previous instructions carefully.

Now check the files the we made earlier and you should see some changes, after the database has been built. Be patient if you have many, many files.



The database file should now be quite large. If not, try executing MPC update and wait a minute or so. This uses the client utility to tell MPD to update its database.

We are now ready to test the set up. Bear with me on this - MPC is not the easiest client to use manually, but for testing purposes, it is fine. To play music, we need a list of files to play. A playlist is simply a text file containing a list of the filenames of music files and MPD will locate these files, if they exist, in its database. The playlist should be saved into the playlist directory we created, and should have an extension of .m3u.

As we want to make sure that everything is set up correctly, we will use MPC's search feature to create a playlist for us. The MPC client searches the metadata found in music files, and can search on fields such as artist, album, genre etc. Choose an artist that you know is in your collection. We'll do the search and re-direct the results straight into a playlist. The search command finds the files, the load command loads a playlist for use and the playlist command displays the list of songs available in the playlist.



OK! That worked. It's not the best way, but it does prove that MPC and MPD are talking and we now have a playlist to work with. Try the following MPC commands:

MPC stats       To get some database information
MPC play       To play the first file in the playlist
MPC next       To play the next file
MPC play 6       To play the sixth file in the playlist
MPC pause       To pause playing

Type MPC help in a terminal to see a long list of the commands that MPC can accept.



If you got similar results, you're good to go and we can look at an easier to use, graphical client. Then perhaps, we will see what this thing can do.

The music player daemon, MPD, is capable of satisfying the needs of underachieving souls like myself through and up to audiophiles so demanding that they like to tune their own pianos, from 'graphical interface only' users to bearded geeks who live in a monochrome world of tty terminals and kernel optimisation. Me, I prefer simple, but with a little cream.

Judging from the number of client applications that have been written to interface with MPD, it just must be good.

One of the very best graphical clients for me is Gnome Music Player Client or gMPC, and luckily for us, it's available in the PCLinuxOS repositories. On its first run, you are presented with a set up wizard dialog, but you can safely just accept the defaults and then explore things later.



Everything that you might need is in there including graphics, lyrics and a playlist editor. Create a new playlist, jump to the database and find the files you want to add, select copy, jump back to the playlist editor and right click to paste them in. It's really quite intuitive.





I really don't need to tell you any more. Just try it, explore it and enjoy it.

There are other client applications available from the PCLinuxOS repositories. PyMPD is a nice, minimalist player written in python. Sonata threw up permission problems for me, so I abandoned it. ncMPC is a curses-based client designed to be run in a terminal which, in these days of graphical environment domination, seemed rather pointless. If you need a terminal based client then ncMPC is probably the one to go with, as it is very comprehensive. You pays your money...

My own preference is to utilise conky, the system monitor, which has a whole slew of MPD connected features. This, in conjunction with a few shell helper scripts and our old friend MPC, does exactly what I want. No more than I want or need, so it is perfectly optimised to my own system.



My music files are stored as MP3 files, each album in its own folder with multiple volumes in subfolders, and each folder or subfolder containing album art in the form of a single file named 'folder.png.' The size of the graphics file is not particularly critical, as it is scaled by conky to 150x150 pixels to suit my display. If the image is missing, then a default image is shown.





It's difficult to say just how efficient with resource usage this system actually performs. This set up with MPD, & MPC seems to be using about 13 MB of memory, and conky another 17MB. Allowing a little more for caching of the read ahead buffers, let's say 35MB tops. CPU usage on my dual core laptop processor increases by only around 1% when activated and playing music, which is pretty good. It shouldn't impact other applications, even on very minimal systems.

As MPD utilises playlists, I decided to add a little playlist creation script, imaginatively named create-playlist.sh. It will allow you to visit folders in your music collection and automatically create a playlist, overwriting any existing playlist. It then opens the playlist in a text editor for you to organise the tracks. The final playlist is copied to both the music directory and to the MPD playlist directory so that it is immediately available for selection. There are better playlist editors out there but this one does the job for me and, having the script code, I can alter it as time demands.

If you want the lyrics, then the final script should help. If the lyrics are not available on that site, you'll get the message “Sorry, We don't have lyrics for this song yet,” but most popular songs seem to be there. The output from the script is not perfect, but I decided to accept it as it is rather than spend too much time on refining it. You will need to have wget and zenity installed from the PCLinuxOS repositories to use the scripts.



I've included my code below for anyone who wants a heads up on controlling MPC with scripts, and on showing the activity in conky. I have highlighted the parts that will need to be changed to work on a different system.

Start conky with conky -c ~/.conkyrc_MPD.

Script 1 is called automatically by the conky resource file.

Scripts 2-4 are bound to hotkeys as shown in the conky display.

Script 5 I have bound to the multimedia start/stop music key on my laptop.

Script 6 Fetches the lyrics of the current song.

Conky color references can be found here: http://www.graphviz.org/doc/info/colors.html#x11


Conky resource file .conkyrc_MPD
















Previous Page              Top              Next Page