Tiny project - Listen to your music library anywhere

For all the folks out there who do not use spotify or google music, there is always this problem of having to carry your music somewhere somehow and have a limited choice on your music selection that you want to carry with you. With a Raspberry pi and an attached USB disk you can now easily have access to your library with a web and streaming client on your cellphone or desktop.

The stack that will allow us to do this is an mpd daemon, which will take care of managing the library and controlling the music, which in turn will also encode and connect to our second pilar of this neat architecture, an icecast server so that it can be heard as a stream. The ympd web client will take care of having a neat interface to control the mpd daemon. To access the ympd web client, nginx as a reverse proxy was the obvious choice as it is very lightweight, this also enables us to have ssl encryption and basic user authentication. The only issue that I have found so far and did not dug deep further into it is that due to the fact that you are buffering your stream, when you change music, stop it or press play to hear it, sometimes it takes up to 15 seconds for the change to reach your listening device. I can live with this, although it is a small nuisance.
Below my config file for MPD with some comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
### Self explanatory section
music_directory "/data/music/"
playlist_directory "/home/david/mpd/playlists/"
log_file "/home/david/mpd/mpd.log"
pid_file "/home/david/mpd/pid"
state_file "/home/david/mpd/state"
sticker_file "/home/david/mpd/sticker.sql"


user "david"
bind_to_address "localhost"
port "6604"

follow_outside_symlinks "no"
input {
plugin "file"
}
# This takes care of connecting to the icecast server.
# The type is 'shout' since icecast is a fork of shoutcast.
audio_output {
type "shout"
encoding "mp3"
name "my cool stream"
host "localhost"
port "8000"
mount "/david"
mixer_type "david"
password "ohaplaintextpassword"
bitrate "320"
format "44100:16:2"
}
# I had to add this as mpd was nagging about not having a decent interface for output.
audio_output {
type "null"
name "fake out"
}
filesystem_charset "UTF-8"
id3v1_encoding "UTF-8"

Now, for the horrible icecast configuration based on XML I am only going to paste here the only interesting part that I have added.

1
2
3
4
5
<mount>
<mount-name>/david</mount-name>
<fallback-mount>/silence.mp3</fallback-mount>
<fallback-override>1</fallback-override>
</mount>

This allows for the stream to fallback to another music file. This is due to the fact that when you setup a playlist on ympd it will stop the stream and start again, leaving your streaming client hanging and disconnecting from the stream. Annoying.

Now that we have all these configuration files set and dusted, we need to make sure that this thing stays up and running if it crashes or after a reboot. I really like to use supervisord for these things.
Here are my ympd-david and mpd-david configuration files for supervisor:

1
2
3
4
5
6
[program:mpd-david]
command=/usr/bin/mpd --stdout --no-daemon --verbose /home/david/.config/mpd.conf
user=david
[program:ympd-david]
command=/usr/bin/ympd -h localhost -p 6604 -w 8904 -u david
user=david

This is pretty lightweight and a Raspberry Pi can easily run this for yourself or for the whole family if you want to, I sure am having fun being able to listen to my music anywhere without ads or resorting to other music providers ;-).