# NAS-based music with a Raspberry Pi

This follows on from my [previous post](https://ktyl.dev/blog/2022/12/03/automount-nfs.html) about setting up NFS.

---

I have a large digitised collection of music, and have been experimenting with ways to set up a communal music player in my living room without defaulting to Spotify, or any other such streaming platform.
Thus far I have used an old laptop with as much music as it will fit loaded onto it, running [MPD](https://www.musicpd.org/) and plugged into some speakers.
Then, on the laptop (or usually, another, closer laptop) I can connect to the MPD instance with [ncmpcpp](https://github.com/ncmpcpp/ncmpcpp) to change tunes.
This is an OK solution, but has a few drawbacks: I'm limited to the disk of the laptop, the laptop uses more power than it needs to, and I kind of want that laptop back!

I had the luck to grab a Raspberry Pi from a pop-up store a few weeks ago, and felt that would make a perfect, low-power, unintrusive box to attach to the speakers.
Ostensibly, the Pi is overkill for just playing music, but it's better than a whole laptop and I'm sure I'll find other jobs for it to do as time goes on.

As for requirements, I have a desktop machine from which I often work from home, and would like my music collection available there too.
I also often use my laptop in the living room or kitchen, which is also in earshot of the speakers, and I'd like to be able to control the music from my laptop with ease - no cables.

Ideally, these should be stored in the same place, to save having to manage duplicate files and manually synchronising locations, since I am likely to add to my collection from a variety of locations.
I have spent enough time `rsync`ing albums between machines, life is too short even on a gigabit local network.

I've recently had the good fortune to acquire a Synology NAS, so I'm going to use that to host my music collection.
However, it's more than possible to jerry-rig a NAS using anything with a hard-drive - maybe even a second Pi.
Nothing I'm doing should be specific to Synology's hardware or software, as we'll be using [NFS](https://wiki.archlinux.org/title/NFS) to mount remote drives - but exposing an NFS shared folder to the network is therefore out of scope for this post.

## Set up a shared folder

The first step is to centralise my music storage.
To do this, I created a shared folder from my NAS' web interface, and exposed it to the network.

In my case, I had to specifically add permission for other devices to access the folder via NFS - such as the Pi, my desktop and my laptop.
It was therefore prudent to assign each of these machines a static IP on my network, so that the NAS can continue to recognise them.
I also had to set it to map all users to admin, but this is almost certainly a misconfiguration on my part - don't follow me for security advice, I am just tinkering!

My previous blog post goes into detail regarding setting up the NFS configuration.

## Setting up the Pi

My Pi is a Pi 4 Model B, with 4GB of RAM.
This is more than enough for my needs, and you should be able to get by with much less.
I went through the initial default setup, noticing that it's much, much slicker than it was on my gen 1 Pi, which ultimately landed me on a graphical desktop.

First, I set a hostname and enabled SSH access, since this is to be a headless machine.
For the same reason, I disabled the auto-launch of the graphical user interface.
I would have thought that if it's booted headless, it shouldn't think to launch a graphical session in the first place, but better safe than sorry.
The point of the thing is to sip power!

## MPD

Next, I installed MPD.
By default, MPD sets itself up with a `systemd` unit, so it connects as soon as I run `ncmpcpp` from the Pi itself.
After a reboot, this still seems to be the case, so I'm happy with the default installation.

I pointed it to the automounted music directory by editing `/etc/mpd.conf` and added it to the `audio` group:

```
music_directory     "/sleeper-service/Music"

group               "audio"
```

Configured an output for ALSA (I was not able to make it work with Pulse):

```
audio_output {
    type        "alsa"
    name        "My ALSA Device"
    mixer_type  "software"
}
```

We also have to add the `mpd` user to the `audio` group to allow it to access sound devices:

```
sudo usermod -G audio -a mpd
```

And enable the driver on boot for the 3.5mm audio jack in  `/etc/modules`:

```
sudo echo "snd-bcm2835" >> /etc/modules
```

I found I had errors with MPD failing to create a pid file, so I gave the `mpd` user ownership of the directory it was trying to create it in:

```
sudo chown -R mpd /run/mpd/
```

This was a bit of a weird one, since it didn't have this error to start with.
Nonetheless, after all of that, it works!
I'm able to play music by running `ncmpcpp` on the Pi itself.

## Remote access

The last thing to configure is access from remote machines.
I only intend to access it from the local network, so this is pretty straightforward.
First, to expose MPD to the network, I set its address and port in `/etc/mpd.conf`:

```
bind_to_address "192.168.1.17"
port            "6600"
```

Then, I need only specify the location of the Pi on the network in a local machine's `ncmpcpp` config:

```
mpd_host = "pifi"
mpd_port = "6600"
```

Of course, `pifi` is an entry in my remote machine's `/etc/hosts`.

It's possible you have multiple MPD installations - one on your remote machine, such as a laptop, as well as an installation like the Pi.
In that case, recall that `ncmpcpp` can be launched with different configs using the `-c` flag:

```
alias bops="ncmpcpp -c ~/.config/ncmpcpp/config.alt
```

## Wrapping up

That's all for now.
At some point in the future I'll write another post on making this setup more accessible.
I certainly like `ncmpcpp`, but it often garners a scoff from houseguests.
So, I'd like to pursue the ultimate goal of making it as straightforward to use as something like Spotify.
As always, I hope this was helpful and please don't hesitate to [get in touch](mailto:me@ktyl.dev)!
