A little background:
I am not much into streaming music. Yeah, it’s 2021 and I still download my music. This post is for others like me who download their music and want an efficient way to manage their collection.
The first time I played music on the desktop, it certainly was in VLC Media Player. I got tired of that very soon when my music collection ran into gigabytes. Finding tracks was a nightmare and organization an even bigger nightmare. I started using Strawberry Music Player. Strawberry is a fork of the famous Clementine Music Player. It did solve most of the issues I had. It has multiple queue support, a great library manager, connection with online services etc. Best of all, it also has a Nyan Cat audio visualiser.
Enter MPD
MPD (music player daemon) is an audio player that has a server-client architecture. It can easily manage large music libraries. To put “large” into perspective, I am talking about libraries with over 50k tracks or almost a terabyte of music. Even such huge libraries are no sweat for MPD. Add to that, the server-client architecture allows many applications to control music playback - even you can write an app to control the MPD server (and I did that!)
Installing MPD
On Arch Linux, the package is called mpd
. You can install it via Pacman
sudo pacman -S mpd
Configuration
Get the example configuration file at /usr/share/doc/mpd/mpdconf.example
or from this link. Save it in ~/.config/mpd/
with name mpd.conf
.
Uncomment music_directory
(L 13), db_file
(L 28), pid_file (
L 43) and state_file
(L 50). Set a location for all these as you like. At the very least I suggest changing the music_directory
from “~/music” to “~/Music”. (notice the capital ‘M’). This is because the pre-created music folder starts with a capital M.
Scroll down to line 85 and uncomment bind_to_address
. Change the value to “localhost” or to a different address if you want. Next uncomment port
at line 90. Let it stay at the default value, which is “6600”.
Uncomment restore_paused
at line 101 and set value to “yes”.
Now we will enable ALSA output. Uncomment line 215 - 217 (both inclusive) and 223. There are more options you can change if you are familiar with them, but the current set up will give you a minimal and working MPD server. For example, I have set my mixer type to software, which fixes issues with volume control.
In the case of OSS the relevant part is just after the ALSA section. I will not detail it out, since if you are using OSS, you know better than me what you are doing.
Save the file, and we are ready to move forward.
In this post, I have suggested edits in the config multiple times. Every time you edit the config file, you need to restart the MPD server for the changes to take effect.
mpd --kill
mpd
Copying your Music Library
Copy your music folder/songs to ~/Music/
. As simple as that! Remember we set the music_directory
value to this folder? You can set that value to a different one if you don’t want to copy your library to a new place.
What do I do?
I am describing two set ups that I use.
-
On my PC - I have access to multiple external drives; no need to copy on internal hdd. So I utilize symlinks on my PC. I uncomment line 135 from
mpd.conf
. It tells MPD that it should follow symlinks while searching the music directory even if symlink target leads out of your music directory. Next, I create symlinks from my music directories on external locations. These symlinks are all stored in~/Music
. Now, MPD can discover music from external drives as well and you don’t need to copy all music to your installation root. If I have unplugged a specific drive, then MPD will simply skip over the songs from that drive. -
On my laptop - Needs to be carried around, can’t have my external drives always around. So all my music is copied on the internal HDD. This location is defined in the MPD config. HDD is mounted automatically on boot since I have it added to my
fstab
.
Launch The Server!
Now, you are ready to launch the server! Open a terminal and run mpd
. Exceptions about Tidal and wildmidi can be ignored. You can add this command to your startup script to have the server launch automatically when you log in. I have it in my i3 config.
There is also a systemd service that you can use.
systemctl --user enable mpd.service
This will start the server automatically when you log in.
Important : MPD can be launched as root also, but don’t do that please!
Adding a Frontend
What do you do after you have set up a server? You need some client software that can interact with your server. MPD, because its open source and has libraries in multiple languages, enjoys support for multiple frontends. A very popular one is ncmpcpp
. Its console based, and while I do like console applications, I also prefer managing music via a graphical application. So, I use Cantata, as it is feature rich and does a good job of interacting with MPD server. It also has an inbuilt tag editor and support for streaming from SoundCloud, Jamendo etc. It can also work without an MPD server by reading files directly off your storage.
Cantata has a self explanatory intro screen. It will ask for your host and port which you defined in mpd.conf
. After that, you are ready to play music!
Get to know MPC
MPC is a simple command line client to mpd. It is used to issue commands to MPD and get info about the current track, queue etc. You can install it from your package manager or compile from source
One command you should know right now is mpc update
. This re-scans your music directory and creates a database of stored tracks. I bind it to Mod+Shift+m. Cantata also has option to update database, but I prefer using wm keybindings.
Some more commands that I have in my config:
mpc seekthrough +00:00:10
: Seek the current playing track 10s forward. Replace argument with a different time duration or use minus instead of plus to seek backwards.
mpc next
: Skip to the next track
mpc prev
: Play the previous track
mpc toggle
: Play queue if paused, and pause if playing
mpc volume +2
: Raise volume by 2%. Use a different value or use minus instead of plus to reduce volume
I have bound these commands to different wm keybindings and it lets me control playback right from the keyboard without leaving my active application. Note that its not necessary to use mpc
. I mentioned it in this post because it allows easy control with terminal commands which can be used in your configs or wrapped in shell scripts.
Controlling MPD with your Android Phone
Now this is where the real fun starts. You can control MPD playback via your smartphone. This makes an easy set up, where you can have your MPD server on a Raspberry Pi and you control playback using your smartphone. And yeah, without using any spooky closed source proprietary applications.
Install one of the many MPD controlling apps available on the Play Store. I used MALP because it is open source and has a decent UI. On first launch, you will be on the profiles page. Tap the plus icon on top right and enter your server details. You can know your system IP address by running the ifconfig
command. Note the IP address of the interface you are connected to (wifi, ethernet, etc).
You should now have your phone connected to your MPD server!
Utilizing HTTP Streams
This is another superpower of MPD, that you can stream music over a network (even the internet!).
Go into your mpd.conf
and change lines 273 to 283 to the following
audio_output {
type "httpd"
name "My HTTP Stream"
encoder "lame" # optional, vorbis or lame
port "8000"
# bind_to_address "0.0.0.0" # optional, IPv4 or IPv6
# quality "5.0" # do not define if bitrate is defined
bitrate "128" # do not define if quality is defined
format "44100:16:1"
max_clients "0" # optional 0=no limit
}
You can increase the bitrate if you like. 256 is a sweet spot for http streaming of mp3 audio, but keep in mind that your FLAC/WAV music will be down-scaled before streaming. Also, I highly suggest changing the max_clients value to the number of simultaneous connections you would expect. In my configs, I set it to 1 because that’s what I need. If you are streaming music to multiple devices at once, set it to a higher value.
On your other device, open a terminal, and run the following command to start playback.
mpv http://<host-name>:8000
For example, I run the following
mpv http://192.168.29.129:8000
The host machine should be reachable via the other machine. If they are on same local network, all well and good. If not, you can still access the server outside your local network by port forwarding on your router.
You can also stream via icecast if you want. Here is the link to relevant Arch wiki entry. Icecast/MPD.
Music can be streamed to smartphone device also, but support is currently spotty. Often, buffering can take a few minutes. I personally don’t need to stream on my smartphone, so I never tried to find out a solution. I just control playback via my phone.
Music info/controls on your bar
Polybar has an MPD module that can display current track info and controls on your bar.
You can find the module code here.
A screenshot where I am running Cantata. The bar at bottom of the screen shows music controls and current track info.
Feel free to suggest corrections, improvements.
Thanks!