MPD With PulseAudio
Background
I used PlexAmp for a few years. Plex’s constant upselling me on services when I already have a lifetime membership really soured me on their service. I don’t have a hole poked in my network for external Plex streaming, but having the option always made me nervous for those that do. Excellent UI but privacy/ownership compromises and constant barrage of “buy this video” or “stream our “free” tv station” really rubbed me wrong. Why am I hosting my own media to still be barraged with ads/requests to buy things?
A few months ago I switched to Navidrome and it’s very lovely. I paid for play:sub on iOS to stream music and it’s very servicable. I think this is a great option for many people. The Web UI is very pleasant and the hosting burden for someone who already has infrastructure is pretty low. I even recently got Authentik handling proxy auth for it, nice!
Something I’ve always been frustrated by, with all the different music streaming services and apps out there, is continuity. I almost always listen to full albums, cover-to-cover. This means that when I get home from a trip to the grocery store I need to either keep listening ot music on my Phone, or find my spot in the middle of a song/album and continue from there. I feel like it’s silly to be frustrated by this, but it also seems like such an obvious feature I wonder why no one has done it (or if I’ve just never found the toggle!).
MPD
Today I was using Beets to re-tag my music library (it was already pretty good thanks to Picard but I wanted to try something new) and I saw that the home-manager options for beets included configurations to send messages to MPD, this was the only plugin directly integrated into the config settings so I figured someone must love it very much to contribute the options back upstream. I’ve been vaguely aware of Music Player Daemon for a while but had never really understood what it was/how to use it effectively. Today I decided to dive in and set it up and find out!
MPD is a server application for playing music, it has various outputs for where the music can go. I think the easiest example would be running MPD on your laptop and streaming the audio using your laptop’s speakers. Sure that’s cool, but not very novel or worth using when apps like Strawberry Music Player exist, right? We’re all very used to the myriad of clients that can play music on our devices.
I picked the first console client off the MPD Client List and spun it up
with nix-shell -p ncmpc --command 'ncmpc --host bard.ts.frodux.in --port 6600'. I quickly checked the docs
and saw that I needed to load my library (have to build the database the first time), <ctrl>-u did this and
I could see my entire library! Time to get audio going somewhere.
PulseAudio
The server application bit above is really what clicked for me today, I already host Navidrome on a server, can I host MPD as well and stream to my laptop? Yes, but not before I learn more! I have Pipewire configured on my laptop and it’s configured to use some PulseAudio settings which it seems to have for compatibility. Reading up on PulseAudio I see that it can recieve audio from a remote stream, and MPD has audio outputs for both Pipewire and Pulseaudio. Because I was learning a lot at once, I ended up going with PulseAudio (replacing PipeWire) on my laptop becuase the settings and articles I was finding were all PulseAudio focused. It sounds like PipeWire is the newer hotness so that’s something to investigate in the future!
After a lot of editing, my PulseAudio configuration is surprisingly simple:
- Enable PulseAudio
- Allow TCP
- Allow-List anonymous clients for loopback and the entire Tailnet. (I will lock this down further later).
One thing that caught me off-guard and may be a configuration error is that I still needed a shared cookie for MPD and PulseAudio to communicate. The docs are vague on if you need a cookie for Anonymous access, at least in my experience the answer was: Yes. So make sure to create a shared cookie secret across hosts using PulseAudio to communicate.
Also after switching from PipeWire to PulseAudio things weren’t working correctly, rebooting fixed it right up. Never underestimate the power of turning it off and back on again!
PulseAudio Config1
hardware.pulseaudio = {
enable = true;
tcp = {
enable = true;
anonymousClients = {
allowAll = true;
allowedIpRanges = [
"127.0.0.0/8"
"10.88.0.0/16"
];
};
};
};
MPD to PulseAudio
Okay I have sound coming out of my laptop speakers again and theoretically a PulseAudio setup, time to tell
MPD to send sound there. That took a good bit of reading, the docs everywhere seem to assume you know all
about PulseAudio and MPD and that you’re just looking to connect the two. What I learned is there is a config
for the “default” audio sink in PulseAudio and it’s @DEFAULT_SINK@ so that’s key unless you have a very
specific sink you want your audio to go to (sink seems to be an output, like laptop speakers, HDMI 1/2,
headphone jack, etc). The pulse output takes a
hostname, a sink (and some other settings). One key here is I’m planning on having multiple audio outputs, so
I set enabled as no so that I have to turn it on (and it defaults to off). I don’t want all my devices to
start streaming a the same time.
Once I got the cookie issue mentioned above out of the way, I was able to send music to my laptop (bilbo) from my server (bard) and it was playing over the speakers near instantaneously!
MPD audio_output Config2
audio_output {
type "pulse"
name "Bilbo"
server "bilbo.centaur-vibe.ts.net"
sink "@DEFAULT_SINK@"
enabled "no"
}
Wrapup
This is super cool, after a bit more research I found an iOS Client for MPD,
Rigelian which can accept an HTTPD audio_output from MPD and play that on the
device. Now I have multiple outputs and I can turn off Frodo (my phone) and turn on Bilbo (my laptop) outputs
and continue listening without interruption. Is this over-engineered? Yeah totally, but I learned a lot about
Linux Sound Systems (we didn’t even talk about ALSA!) and audio streaming. It’ll take a few weeks for me to
see if this’ll be my new main audio streaming infrastructure, but I have a good foundation and some hope that
this will be something that will serve me well here on out!