paulgorman.org/technical

MPD (Music Player Daemon)

MPD runs as a daemon. Configure MPD to run either as a system service, or (more typically) run it directly as your user:

$ mpd

MPD does many traditional music player things, like:

What MPD doesn’t do is let a user directly control that stuff. Sure, we could telnet to localhost:6600 (or wherever MPD is configured to listen), but generally we’ll want to use on the numerous music player clients that provide an interface between the user and MPD.

$ telnet localhost 6600
Trying ::1...
Connected to localhost.
Escape character is '^]'.
OK MPD 0.20.0
stats
uptime: 1412
playtime: 16
artists: 768
albums: 540
songs: 6801
db_playtime: 1902630
db_update: 1513708491
OK
^]
telnet> Connection closed.

MPD comes with one very simple command-line client called mpc. mpc may not be friendly enough to be your everyday music player, but it’s great for learning about MPD. Play with the commands described by mpc help.

$ mpc
Django Reinhardt - All the Things You Are
[paused]  #7195/7773   0:03/2:55 (1%)
volume: n/a   repeat: off   random: on    single: off   consume: off
$ mpc help
Usage: mpc [options] <command> [<arguments>]
mpc version: 0.28

Options:
  -v, --verbose              Give verbose output
  -q, --quiet                Suppress status message
  -q, --no-status            synonym for --quiet
  -h, --host=<host>          Connect to server on <host>
  -P, --password=<password>  Connect to server using password <password>
  -p, --port=<port>          Connect to server port <port>
  -f, --format=<format>      Print status with format <format>
  -w, --wait                 Wait for operation to finish (e.g. database update)

Commands:
  mpc                                                   Display status
  mpc add <uri>                                         Add a song to the current playlist
  mpc crop                                              Remove all but the currently playing song
  mpc current                                           Show the currently playing song
  [...]

Configuration

Unless we specify a different configuration file when starting MPD, it looks for $HOME/.mpdconf and falls back to /etc/mpd.conf. My ~/.mpdconf file:

# See mpd.conf(5).
bind_to_address "~/.mpd/socket"
bind_to_address "127.0.0.1"
replaygain "off"
music_directory "~/Music"
playlist_directory "~/.mpd/playlists"
db_file "~/.mpd/database"
log_file "~/.mpd/log"
pid_file "~/.mpd/pid"
state_file "~/.mpd/state"
sticker_file "~/.mpd/sticker.sql"

(MPD defaults assume the existence of the ~/.mpd/ directory. Maybe it creates it? To be safe mkdir ~/.mpd.)

See the sample configuration file.

After modifying the configuration file, restart MPD:

$ pkill -HUP mpd

Database Updates

MPD should automatically update its database when files in its music directory change. However, I sometimes need to prod it:

$ mpc update

Playlists

MPD has one active playlist at a time, also called the queue. Save the current playlist to a file or load a different playlist:

$ mpc lsplaylists
$ mpc save myfavorites
$ mpc load jazz

Show songs in the current playlist, and clear the current playlist (“clear” is safe — it doesn’t destroy the contents of a saved playlist):

$ mpc playlist
$ mpc clear

Load every library song into the current playlist:

$ mpc listall | mpc add

MPD loads and saves playlists in its playlist_directory (i.e. ~/.mpd/playlists/).

If we want to manipulate playlists outside of an MPD client, or to interchange playlists with other programs, there are few things to know:

ncmpcpp

ncmpcpp is a powerful but horribly-named Curses client. Also, it’s default keybindings are not ideal for me. See ncmpcpp(1) and /usr/share/doc/ncmpcpp/bindings.gz.

$ zcat /usr/share/doc/ncmpcpp/bindings.gz > ~/.ncmpcpp/bindings

My more vim-like key bindings:

def_key "k"
  scroll_up
def_key "j"
  scroll_down
def_key "ctrl-b"
  page_up
def_key "ctrl-f"
  page_down
def_key "l"
  next_column
def_key "h"
  previous_column
def_key "H"
  show_help
def_key "L"
  show_lyrics

Unix Socket

There are a couple of commands MPD only accepts over a unix socket connection. Use socat to connect:

$ socat - UNIX-CONNECT:$HOME/.mpd/socket
OK MPD 0.20.0
config
music_directory: /home/paulgorman/Music
OK

Some versions of netcat (e.g., OpenBSD’s) also support this, like nc -U ~/.mpd/socket.

Sun Dec 31 10:48:14 EST 2017