Music Player Daemon

From ArchWiki
Revision as of 11:45, 8 June 2009 by Iphitus (Talk | contribs) (New Setup Instructions)

Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

What is mpd?

mpd (Music Player Daemon) is an audio player that has a server-client architecture. mpd runs in the background as a daemon, manages playlists and a music database, and uses very few resources. In order to interface with it, you need a separate client. More information can be found on their website

Daemon Install Procedure

Sync and install with pacman:

# pacman -S mpd

Configure mpd

For more information about mpd configuration visit http://mpd.wikia.com/wiki/Configuration

Timeline of mpd's behavior on a typical working setup

  • mpd is started on boot by Template:Filename, by including it in the DAEMONS array. (Or, this can be done manually each session by running Template:Codeline with root privileges).
  • Since mpd is now started as root, it first reads the Template:Filename file.
  • mpd reads the user variable in the Template:Filename file, and changes from root to this user.
  • mpd then reads the contents of the Template:Filename file and configures itself accordingly.

Notice that mpd changes the running user from root to the one named in the Template:Filename file. This way, uses of "~" in the configuration file point correctly to the home user's directory, and not root's directory. It may be worthwhile to change all uses of "~" to "/home/username" to avoid any confusion over this aspect of mpd's behavior.

Quick Single User Installation

  • As root, check if Template:Filename exists and delete the file if it does. This is safe.

Mpd comes with an example configuration file, available at Template:Filename. This file holds an abundance of information on mpd configuration, and holds default mixer values you can simply uncomment.

# cp /etc/mpd.conf.example /etc/mpd.conf

Never put this file in the user's directory like some tutorials suggest. This would complicate things and most of the time doing this is useless (notice you're reading a quick installation howto). If you previously created a Template:Filename file in your home, remove it now. This is important to prevent conflicts. When placing it in /etc, like we do here, mpd will be able to run as a daemon at boot. Otherwise, a script would be needed to run mpd AFTER the user has logged (like kdm or ~/.fluxbox/startup) or it would require a manual move each time. For a single music collection, the method used here is simply better, even while the collection is shared to multiple users. Also, don't be afraid about root privileges: even while mpd runs as a daemon, it never completely runs as root since it automatically drops its root privileges after execution.

To make audio output actually work ensure that you have setup audio card and mixer correctly. See ALSA. Do not forget to unmute the required channels in alsamixer, raise the volume and save changes with alsactl store. See Template:Filename if it still doesn't work.

Make sure your card can do hardware mixing (most of them can, including onboard audio). Or else this could cause problems with multiple sound playback. For example, this can prevent Mplayer from playing back sound while the mpd daemon is running, returning an audio error message stating the device is busy.


We can now move on to edit the file, once again, as root:

# EDITOR /etc/mpd.conf

New Setup Instructions

Keeps the setup in /var and uses "mpd" as default user instead of cluttering up ~/. This is the way the arch package is installed.

Edit Template:Filename to reflect as such. Or for something easier, just copy /etc/mpd.conf.example and set your music directory in that as it has the same defaults.

music_directory       "/home/user/music"         # Your music dir.
playlist_directory    "/var/lib/mpd/playlists"
db_file               "/var/lib/mpd/db"
log_file              "/var/log/mpd/mpd.log"
error_file            "/var/log/mpd/mpd.error"
pid_file              "/var/run/mpd/mpd.pid"
state_file            "/var/lib/mpd/mpdstate"
user                  "mpd"
# Binding to address and port causing problems in mpd-0.14.2 best to leave
# commented.
# bind_to_address       "127.0.0.1"
# port                  "6600"

Read steps below carefully, permissions need to be set correctly.

  • Keep user as "mpd", double check to make sure user "mpd" home directory is set to /var/lib/mpd
$ cat /etc/passwd | grep mpd
  • Also make sure /var/lib/mpd is owned by mpd:mpd.
$ ls -l /var/lib/ | grep mpd
  • Now as root create the files you specified in Template:Filename, if directories do not exist, create those as well. This is not required if you use the defaults in the arch package.
# touch /var/lib/mpd/db
# touch /var/lib/mpd/mpdstate
# touch /var/run/mpd/mpd.pid
# touch /var/log/mpd/mpd.log
# touch /var/log/mpd/mpd.error
  • MPD needs to have +x permissions on ALL parent directories to your music collection (ie. if it's located outisde of "mpd" home directory /var/lib/mpd).
  • By default useradd sets permissions on home dir to 1700 drwx------. So if your like me you will need to change permissions of /home/user
  • Example... my music collection is located /home/user/music.
$ chmod a+x /home/user
$ chmod -R a+x /home/user/music

Note: This is not good security practice and may be unnecessary (YMMV). M1sha 11:47, 7 June 2009 (EDT)

  • Now change the ownership of the directories and files you created to user "mpd"
# chown -R mpd:mpd /var/lib/mpd
# chown -R mpd:mpd /var/run/mpd
# chown -R mpd:mpd /var/log/mpd
  • If your music collection is contained under multiple dir. you can make symbolic link under /var/lib/mpd then set 'music_dir' in mpd.conf to the directory holding the symbolic links. Remember to set permissions accordingly on the directories being linked.
  • Create mpd database as root.
# mpd --create-db

Continue below

Alternative Setup

Useful if you you want to run for example an icecast server. MPD neet not be started with root permissions. The only reason why MPD needs to be started as root (by being called from Template:Filename) is because the default files and folders in the default configuration file points to directories owned by root (the /var directory). A less common, but perhaps more sensible, approach is to make MPD work with files and directories owned by a normal user. Running MPD as a normal user has several advantages: (1) can easily have a single directory ~/.mpd (or any other directory under /home/username) for all MPD configuration files, (2) no read/write permission errors, and (3) more flexible calls to MPD by using Template:Filename instead of including 'mpd' in the Template:Filename DAEMONS array.

The following steps shows how to run MPD as a normal user.

$ mpd /home/username/.mpd/config
  • Note that you don't have to put a "&" at the end of the line here, since MPD will automatically daemonize itself.

Lastly, delete the entry 'mpd' from your DAEMONS array in Template:Filename, as we are not running it as root anymore.

Multi-MPD Setup

If you want a second MPD daemon (e.g., with icecast output to share music over the network) to use the same music and playlist as the one above, simply copy the above configuration file and make a new file (e.g., Template:Filename), and only change the log_file, error_file, pid_file, and state_file parameters (e.g., Template:Filename, Template:Filename, and so on); using the same directory paths for the music and playlist directories would ensure that this second MPD daemon would use the same music collection as your first one (e.g., creating and editing a playlist under the first daemon would affect the second daemon as well, so that you don't have to create the same playlists all over again for the second daemon). Then, call this second daemon the same way from your Template:Filename above. (Just be sure to have a different port number, so as to not conflict with your first MPD daemon).

Troubleshooting

Autodetection failed

During the start of mpd, it tries to autodetect your set-up and configure output and volume control accordingly. Though this mostly goes well, it will fail for some systems. It may help to tell mpd specifically what to use as output and mixer control. If you copied Template:Filename over from Template:Filename as mentionned above, you can simply uncomment:

Example for alsa output type:

audio_output {
	type			"alsa"
	name			"My ALSA Device"
	device			"hw:0,0"	# optional
	format			"44100:16:2"	# optional
}

Example for alsa mixer:

mixer_type			"alsa"
mixer_device			"default"
mixer_control			"PCM"

Note: in case of permission problems when using ESD with mpd run this as root:

# chsh -s /bin/true mpd

Tips

To get rid of timeouts (i.e. when you paused music for long time) in gpmc and other clients uncomment and increase Template:Codeline option in Template:Filename.

If files and/or titles are shown in wrong encoding, uncomment and change Template:Codeline and Template:Codeline options. Note that you cannot set encoding for ID3 v2 tags. To workaround this you may use external tag readers.

If you want to use another computer to control mpd over a network, the Template:Codeline option in Template:Filename will need to be set to either your IP address, or Template:Codeline if your IP address changes frequently. Remember to add mpd to the Template:Filename file to enable external access.

Client Install Procedure

Install a client program for mpd. Popular options are:


Install with:

# pacman -Sy mpc
# pacman -Sy ncmpc
# pacman -Sy ncmpcpp
# pacman -Sy ario
# pacman -Sy sonata
# pacman -Sy gmpc

Extra stuff

Last.fm scrobbling

To scrobble your songs to Last.fm using MPD there are several alternatives.

mpdscribble

mpdscribble is another daemon, but is only available in the AUR. This is arguably the best alternative, because it's the semi-official MPD scrobbler and uses the new "idle" feature in MPD for more accurate scrobbling. Also, you don't need root access to configure it, because it doesn't need any changes to /etc at all. Visit the official website for more information.

To install mpdscribble, just install it from the AUR and do the following (not as root):

username = <your last.fm username>
password = <md5 sum of your last.fm password> # Generated using "echo -n password | md5sum"
host = <your mpd host> # defaults to $MPD_HOST or localhost
port = <your mpd port> # defaults to $MPD_PORT or 6600
log = ~/.mpdscribble/mpdscribble.log
cache = ~/.mpdscribble/mpdscribble.cache
verbose = 2
sleep = 1
musicdir = <your music directory>

Sonata

The easiest way, if you don't care about having to have a program window open all the time, is using Sonata which is a graphical frontend to MPD. It has built-in support for Last.fm scrobbling in its preferences. The downside of this is that Sonata doesn't cache your songs if for some reason you don't happen to have an Internet connection at the time of playing.

lastfmsubmitd

lastfmsubmitd is a daemon which is available in the "community" repository. To install it, first edit Template:Filename and add both Template:Codeline and Template:Codeline to the Template:Codeline array in Template:Filename.

Last.fm playback with lastfmproxy

lastfmproxy is a python script that streams a last.fm music stream to another media player. To setup, install from AUR: Template:Codeline and then edit Template:Filename. If you plan to only stream to mpd on the same host, just edit the login info.

Note: Since it installs to a read only directory but it requires read/write access for features like saving previously listened to stations, it would be wise to copy Template:Filename to your home directory.

Start lastfmproxy with Template:Codeline and visit http://localhost:1881/ in your web browser. To add a last.fm station navigate to http://localhost:1881/ followed by the lastfm:// url. Example: http://localhost:1881/lastfm://globaltags/punk . Navigate back to http://localhost:1881/ and download the m3u file by selecting the Start Listening link. Simply add it to your music library path.

Never play on start

If you don't want mpd to always play on your system start, but yet you want to preserve the other state information, add the following lines to your Template:Filename file:

   ...
   stat_busy "Starting Music Player Daemon"
   # always start in paused state
   awk '/^state_file[ \t]+"[^"]+"$/ {
       match($0, "\".+\"")
       sfile = substr($0, RSTART + 1, RLENGTH - 2)
   } /^user[ \t]+"[^"]+"$/ {
       match($0, "\".+\"")
       user = substr($0, RSTART + 1, RLENGTH - 2)
   } END {
       if (sfile == "")
               exit;
       if (user != "")
               sub(/^~/, "/home/" user, sfile)
       system("sed -i \x27s|^\\(state:[ \\t]\\{1,\\}\\)play$|\\1pause|\x27 \x27" sfile "\x27")
   }' /etc/mpd.conf
   /usr/bin/mpd /etc/mpd.conf &> /dev/null
   ...

This will change the player status to "paused", if it was stopped while playing. Next, you want this file to be preserved, so mpd updates won't erase this edit. Add (or edit) this line to your Template:Filename:

NoUpgrade = etc/rc.d/mpd

MPD & Alsa

Sometimes, when you use other audio output, e.g: some web pages containing Flash applets, mpd cannot reproduce anything anymore (until you restart). The error looks something like: (if you search the file Template:Filename)

Error opening alsa device "hw:0,0": Device or resource busy

And here is the solution (dmix saving our life again). Apply these lines in your Template:Filename:

audio_output {
        type                    "alsa"
        name                    "Sound Card"
        options                 "dev=dmixer"
        device                  "plug:dmix"
}

And then restart with Template:Codeline.

Searching on Internet I found the reason for why that happens in Gentoo's wiki:

  • The sound card does not support hardware mixing (uses dmix plugin)
  • An application does not work with ALSA with it's default settings

High CPU usage with ALSA

When using MPD with ALSA, users may experience MPD taking up lots of CPU (around 20-30%). This is caused by most sound cards supporting 48kHz and most music being 44kHz, thus forcing MPD to resample it. This operation takes lots of CPU cycles and results into high usage.

For most users the problem should be solved by telling MPD not to use resampling by adding Template:Codeline into audio_output-part of Template:Filename.

Example from Template:Filename:

audio_output {
   type			"alsa"
   name			"My ALSA Device"
   auto_resample		"no"
}

Some users might also want to tell dmix to use 44kHz as well. More info about tuning performance of your MPD can be found from: MPD wiki

Control mpd with lirc

There are already some clients designe for communications between lircd and mpd, however, as far as the practical use, they aren't very useful since their functions are limited.

It's recommended to use mpc with irexec. mpc is a command line player which only sends the command to mpd and exits immediately, which is perfect for irexec, the command runner included in lirc. What irexec does is that it runs a specified command once received a remote control button.

First of all, please setup your remotes as referred to the Lirc article.

Edit your favored lirc startup configuration file, default location is Template:Filename.

Fill the file with the following pattern:

begin
     prog = irexec
     button = <button_name>
     config = <command_to_run>
     repeat = <0 or 1>
end

An useful example:

## irexec
begin
     prog = irexec
     button = play_pause
     config = mpc toggle
     repeat = 0
end

begin
     prog = irexec
     button = stop
     config = mpc stop
     repeat = 0
end
begin
     prog = irexec
     button = previous
     config = mpc prev
     repeat = 0
end
begin
     prog = irexec
     button = next
     config = mpc next
     repeat = 0
end
begin
     prog = irexec
     button = volup
     config = mpc volume +2
     repeat = 1
end
begin
     prog = irexec
     button = voldown
     config = mpc volume -2
     repeat = 1
end
begin
     prog = irexec
     button = pbc
     config = mpc random
     repeat = 0
end
begin
     prog = irexec
     button = pdvd
     config = mpc update
     repeat = 0
end
begin
     prog = irexec
     button = right
     config = mpc seek +00:00:05
     repeat = 0
end
begin
     prog = irexec
     button = left
     config = mpc seek -00:00:05
     repeat = 0
end
begin
     prog = irexec
     button = up
     config = mpc seek +1%
     repeat = 0
end
begin
     prog = irexec
     button = down
     config = mpc seek -1%
     repeat = 0
end

There are more functions for mpc, run Template:Codeline for more info.

External links