The build of a Linux-based music streamer

Linux was the obvious choice, as I needed an embedded system that fits on the 4GB internal eMMC storage. No GUI was necessary, as users will not interact with the player directly. I chose Debian with no additional packages. I could have gone for Arch or some other lighter distro to slim it down even further, but it wasn’t necessary.

I only left a 64MB block for the UEFI boot partition and the rest went for the system with no swap space. This works well and I’ve never run out of ram. However, there was an issue with GRUB, as the motherboard is quite specific on where it expects the bootloader within that partition.

By default, Debian creates it at /EFI/debian/grubx64.efi which needs to be moved to /EFI/BOOT/bootx64.efi. There are other files in the /EFI/debian folder that GRUB needs, so I copied /EFI/debian to /EFI/BOOT and renamed grubx64.efi to bootx64.efi. This did the trick.

The next steps were installing sudo, ssh, git and the driver for the wifi module:
apt install sudo openssh-server git firmware-brcm80211
Then reinsert the broadcom module:
sudo modprobe -r brcmsmac ; sudo modprobe brcmsmac
Add our user to the sudo group:
sudo adduser <username> sudo
And enable and start the ssh server:
systemctl enable ssh
service ssh start

This is also a good time to set up the wifi connection, but there are multiple options to do so. My favourite tool is nmcli. One thing I would suggest is turning off power saving for the wifi card, as that will allow us to connect to the player much more quickly.

The above takes care of the basic stuff and allows us to reach the server by SSH.

We need to add an audio service and the music player itself. For the audio service, I chose Pulseaudio, and the player will be Spocon. I also needed some extra tinkering to make the sound card work, as it has a bespoke software in Windows that allows the user to choose the sound output, which needs to be replaced by some python code in Linux. This is specific to this sound card, so if you’re using something else, it’s not necessary.

To install Pulseaudio and its dependencies:
apt install libasound2 libasound2-plugins alsa-utils alsa-oss pulseaudio pulseaudio-utils
To allow playback for a specific user, you need to add them to the below groups:
usermod -aG pulse,pulse-access <username>
To start the service:
pulseaudio -G
You can list the sound outputs and their settings with the below command:
pacmd list-sinks

The python code for the sound card:
https://github.com/IcyEyeG/supremefx-hifi

Spocon:
https://spocon.github.io/spocon/

The next step is to set up autostart for these services using systemd. I would like everything to start when the system boots up, without requiring a user to log in. First the pulseaudio service, then the python script setting up the sound card’s outputs and finally, Spocon.

Systemd allows us to set up different types of dependencies between services, which can be seen in the [Unit] section of the entries.

This is the entry for the script that initializes the sound card:

[Unit]
Description=A script to initialize the sound card and set up the gain and output
After=multi-user.target pulseaudio.service

[Service]
User=root
Type=simple
ExecStart=python3 /home/robert/supremefx-hifi/supremefx-hifi.py

[Install]
WantedBy=multi-user.target

This is for pulseaudio:

[Unit]
Description=Pulseaudio service

[Service]
User=root
ExecStart=pulseaudio --daemonize=no --system --disallow-exit
Restart=always

[Install]
WantedBy=default.target

And finally for spocon:

[Unit]
Description=SpoCon
Wants=network-online.target
After=network.target network-online.target pulseaudio.service
Requires=network-online.target pulseaudio.service

[Service]
Type=simple
User=spocon
Group=spocon
Restart=always
RestartSec=10
PermissionsStartOnly=true
WorkingDirectory=/opt/spocon
ExecStartPre=/bin/sh -c 'until ping -c1 spotify.com; do sleep 5; done;'
ExecStart=/usr/bin/java -jar /opt/spocon/librespot-java-api-v1.6.2.jar

[Install]
WantedBy=multi-user.target

They are quite self-explanatory, so I’d rather not go into the details. These files need to go to /lib/systemd/system/ and then systemd needs to be updated with sudo systemctl daemon-reload.

Running pulseaudio as root without a user login is a little different than the way it’s usually set up, but it’s not impossible. The root user also needs to be added to the pulse and pulse-access group to permit it to use the service, and the same applies to the spocon user. This can be done with usermod -aG pulse root.

The setup is still not rock solid, as sometimes Spotify refuses to find the target and I have to restart spocon with systemctl restart spocon.service, but aside from that, it works well and sounds amazing.

The sound card has actually been upgraded a bit with Texas Instruments OPA827 operational amplifiers, which are much nicer than the original LM4562s. Oh, and finally the knob arrived for the rotary encoder, which completes the exterior. I had to cut off a little bit from the shaft, but now it looks like a finished product.

One month later…

Hope you enjoyed the read, if you have any questions, leave them in the comments below.

Pages: 1 2

Leave a Reply

Your email address will not be published. Required fields are marked *