Audio Server

Playing with MPD and Bluetooth

2020-03-31, updated on 2020-07-07

MPD Server

While searching for solutions for streaming music from my phone to my server, I came across MPD (Music Player Daemon). Although not quite what I was looking for, it allows you to play music on the server and control playback from a computer or phone. I wasn't originally looking for something like this, but I figured it'd be nice to play around with it and see how it would work with my setup.

So I got another audio cable and routed it from the speakers to the old computer I use as a server. The speakers have an extra audio input port which is nice because I didn't need to buy a splitter. A minor problem I found with this is that it makes the speakers about half as loud when both inputs are plugged in. But, it's not a big deal.

I then started messing around with MPD. I installed it on my server (running Debian 10) and followed this tutorial on configuring it. I like this tutorial because it's the most recent one I could really find, and it includes instructions on how to use apache2 for album art. I didn't have any conflicting issues with my current apache2 server.

I wasn't able to get pulseaudio working, so I just left it with the default alsa configuration (which works just fine).

So with this configured, I could play music downloaded onto my server and control it with my phone or laptop. I used M.A.L.P and Cantata as MPD clients because those seemed to work for me.

Edit: I was able to get pulseaudio to work. It turns out I just misread something in the tutorial (oops!). Having Bluetooth use pulseaudio and MPD use ALSA was a bit silly, and that also prevented them from switching between the two, so having both using pulseaudio is definitely preferred.

Bluetooth

AKA, the worst thing to setup on a headless linux server

Somehow I broke the bluetooth module that came with this X60t laptop, so I had to buy a dongle. I got this kinivio dongle because it's one of the few I could find that's confirmed to work on linux.

Using bluetoothctl, I was able to pair my phone. I installed pulseaudio and related bluetooth packages so I could stream music on my phone to the server, which would play through the speakers.

However, I ran into issues with pulseaudio and bluetooth. Pulseaudio isn't intended to run systemwide (for security reasons) and it's only run in the userspace. This isn't an issue if there is a desktop environment present, but in a headless server it won't work. I can login via ssh to start the daemon, but when I logout, the daemon stops. This causes the entire bluetooth service to fail.

I could have mitigated this just by having a user always running the daemon, but that's not very graceful. After a reboot I'd have to login and run some commands, which makes it being a headless server kind of stupid.

I reached out on the internet a bit and I learned that you can run systemd unit files from a user. I did this by making the following files.


~/.config/systemd/user/bt-agent.service

[Unit]
Description=Bt-agent service

[Service]
ExecStart=/bin/bt-agent

[Install]
WantedBy=default.target
        

~/.config/systemd/user/pulseaudio.service

[Unit]
Description=Pulseaudio service

[Service]
ExecStart=/bin/pulseaudio

[Install]
WantedBy=default.target
        

Then I enabled the unit files and lingering, which allows processes like these to run without a user logged in. This means that they would run at boot, without the need for logging in.

Update: I had to add a new dedicated user with these unit files because the daemons would exit if I logged in and closed my session. But with this, it works as you'd expect.

Final Thoughts

With this, my audio server was just about complete. I could play downloaded music, or stream music from YouTube using Newpipe over bluetooth. I personally don't like headphones or earbuds (even wireless) because I sometimes listen to music for hours, and my ears or head starts to hurt if I keep them in for that long. I also like to be able to hear ambient noises, and having earbuds or headphones in also make me more sensitive to hearing my tinnitus.

Overall, I'm content with my setup, and I've been using it for months without any complaints.


Back