Music Player Daemon

Friday, Aug 20, 2021
#unix #rice #tips some roses in town

It’s beginning to feel like spring here, blossoms everywhere and I’m leaving the windows open for most of the day. But, we’re in lockdown – so I won’t be taking photos or hiking for a little bit. I’ve been listening to lots of music and figured I could write about my setup this morning. The first time I tried using mpd I found it very confusing. Lots to configure and setup just to listen to music and I couldn’t find any “basic” setup guides.

music player setup

I still liked the idea of a lightweight background process as a music player. Being able to control it with hotkeys, your phone, other computers, whatever. Eventually, I gave it another shot and figured out how to setup the daemon properly. I was using Debian back then and when you install the mpd package it enables and starts a systemd service. The service runs as it’s own user and reads a config in /etc/mpd.conf and looks for music files in /var/lib/mpd. This is actually a strange configuration. It makes sense in some scenarios, if you have a music server with a bunch of accounts and want them to be able to use it like a jukebox. For just playing my music on my laptop it didn’t really make sense and I thought this was the only way to use mpd.

Turns out you can just run mpd as your user and put a config file in ~/config/mpd/mpd.conf. I start mine by adding exec --no-startup-id mpd to my i3 config, but you could instead, add it to your login script. I don’t use systemd anymore, but when I did I disabled the systemd service and then enabled the systemd user service by running sudo systemctl --user enable mpd and it’ll run the service as your user logs in and kill it when their last session logs out. Once you get it running a basic config is pretty simple:

music_directory	"~/music"
playlist_directory "~/.config/mpd/playlists"
db_file "~/.config/mpd/mpd.db"
pid_file "~/.config/mpd/mpd.pid"
state_file "~/.config/mpd/mpdstate"
audio_output {
	type		"pulse"
	name		"My Pulse Output"
}

Make sure to actually create those directories or mpd will error when you start it. If you’re not using pulseaudio swap out the audio block, mpd supports jack, alsa, pipewire, oss, shout, and stuff like recording to files or webservers. There’s a nice example config usually installed somewhere like /usr/share/examples/mpd/mpdconf.example and they have a nice web version of the manual.

Clients

First, grab the mpc client. It’s just a basic cli client, but you can use it to setup some hotkeys. I only have play/pause, next/previous, but you can make it as complex as you’d like.

bindsym $mod+m exec --no-startup-id mpc toggle
bindsym $mod+comma exec --no-startup-id mpc prev
bindsym $mod+period exec --no-startup-id mpc next

I use ncmpcpp which is a pleasant, feature-full, tui client. Honestly it might be a little overkill, but I’m happy with it. I use a really basic config and haven’t really changed the bindings. Checkout the man pages and arch wiki for lots of tips and tricks.

# Progress Bar ..
progressbar_look = "━━─"
# progressbar_boldness = "yes"
progressbar_color = "black"
progressbar_elapsed_color = "red"

# Colors ..
colors_enabled = "yes"
empty_tag_color = "red"
statusbar_color = "red"
state_line_color = "black"
state_flags_color = "red"
main_window_color = "white" 
header_window_color = "black"

# Misc
media_library_primary_tag = album_artist
display_bitrate = "yes"
autocenter_mode = "yes"
centered_cursor = "yes"

On Android there’s a client called M.A.L.P. (it’s on F-Droid). It looks like most of those slick android music clients, but you can use it to control your home mpd-based speaker system or whatever which is pretty cool. There’s also web clients, graphical clients, iOS clients, and more listed on the mpd project’s page.

Library

Most mpd users have large “offline” collections of music, but there’s actually an alternative client-compatible daemon called mopidy which lets you use Spotify, SoundCloud, and others as music sources. I’ve never really used Spotify or any of that stuff. I get the majority of my music from bandcamp, ripping cd’s, and if those aren’t possible then sailing the high seas 🌊. I use a tool called beets to import new music, automatically tag it (using the MusicBrainz database), and fetch the correct album art. It’s such an amazing tool and I don’t think it’s all that well known.

directory: ~/music
library: ~/.local/share/beet/library.db
plugins: fetchart embedart lastgenre missing fromfilename convert
import:
  move: yes
paths:
  default: $albumartist/$album%aunique{}/$track $title
  singleton: $artist/Singles/$title
  comp: Compilations/$album%aunique{}/$track $title
  albumtype:soundtrack: Soundtracks/$album/$track $title
convert:
  never_convert_lossy_files: yes
  extension: mp3

This is my config, it’s really straight forward and all the options are well documented on the website. The convert feature is particularly nice. I use it to periodically export my music collection to my phone, I have it only convert non-lossy files and can exclude portions of my library.

Normally, when I get a new album I run beet import path/to/album and it interactively searches and finds my album, adds the correct metadata and then moves it to my music folder. If it can’t figure out the album from existing metadata and filenames you can enter search terms or even type in the tags yourself.

Album Art

In that screenshot earlier I have the album art open. I’m using a little shell script my friend Henry wrote a while back called aart. It works like this:

  1. Use mpc to figure out what’s playing
  2. Get album art by checking for names like cover.png in the album’s folder
  3. Or using ffmpeg to extract album art metadata from the music file itself
  4. Copy the art to a cache directory
  5. Open the cache image in an image viewer (sxiv by default)

Whenever the song changes aart will repeat this process and copy over the new album art. Most image viewers like sxiv or feh will automatically display the new file, but you could easily extend the script to send your image viewer a signal if needed. If the image viewer is killed aart closes itself. It’s a great little tool and is much more simple and reliable than those album art viewers that search the web for album art when your song changes.