Making a Raspberry Pi Boombox (AKA UNIAC Software In Detail)

Introduction

Most of you who see this post will have seen the original UNIAC post before this, and hopefully some will have seen the post detailing the hardware. For those who haven’t, UNIAC is a Raspberry Pi based Spotify playing boombox that is controlled with buttons on the front and displays status using Nixie tubes. This post will cover the software used to make UNIAC work — both tools developed by others and the software I wrote myself.

While I am writing this from the perspective of documenting UNIAC, the software could be pretty easily recycled to work with any display and buttons, you’d just have to rewrite the display and button event functions to support your hardware.

Also note that I did a major ripup of the UNIAC software after the original post, so this version does not use Mopidy, MPD, or Mopidy-Spotify. And if any of you have used those tools before, you’ll understand that’s a good thing.

I will start with a disclaimer however: I am an electrical engineer, not a software engineer, so this design will probably make software guys sick to their stomach. Sorry in advance…

Third Party Software

The first thing to understand about the architecture used in UNIAC is that control and playback of music are two separate processes. Raspotify is a software package that connects to Spotify and makes your RPi act as a Spotify endpoint. Essentially a dedicated speaker. There are a couple of similar packages that can give you a Spotify client on a RPi, but Raspotify has a trivial installation process:

curl -sL https://dtcooper.github.io/raspotify/install.sh | sh

The second part, Spotipy, is a Python library that you give permissions on your (paid) Spotify account. It allows you to see current playback status, and control playback on your Spotify devices (computers, speakers, etc). By combining these two, you can have a full Spotify instance running on your device with programmable control.

The two pieces are connected by the UNIAC Python script. UNIAC sets up a spotipy connection and then controls what device the playback occurs on. Since the name of the UNIAC Raspotify instance is constant, it simply has to tell Spotify to playback there, and music will play on the UNIAC hardware. This has the added benefit that I can ‘cast’ whatever I’m currently listening to over to UNIAC and it will just continue to play.

UNIAC / Spotify control.

The last piece worth mentioning is eSpeak, a relatively primitive speech synthesizer that takes strings from the command line and announces them. This lends UNIAC its characteristic robot voice when announcing settings information and playlist names.

Hardware Libraries

Path between the controls, the Display, and UNIAC.

The hardware interfaces are pretty straightforward, mostly. The buttons are controlled by an i2c GPIO expander, and Adafruit has the MCP230XX library to support it. For other GPIO, I use RPi.GPIO.

The last, and most unique library is the NixieDisplay library. It is a custom library that I developed to communicate with my Teensy 3.2 based multiplexing, crossfading Nixie display. In the first prototype, I used Taylor Edge SmartNixie modules instead of my custom display, so I also wrote a library supporting them on the RPi, though I’m not using it on the finished version.

These libraries are pretty straightforward. You print numbers to the tubes to display and forget about it.

The VU-Meter only has an audio input, so it doesn’t have any software control and doesn’t appear on any of these block diagrams.

UNIAC Software Architecture

The UNIAC software is essentially an interrupt driven button handler. There is a master ‘Menu’ class in Menu.py to which arbitrary ‘Modes’ are attached. An attached mode must provide a function handler for each physical button as well as a display update handler (and a few other helper functions).

When a button is pressed, the Menu object calls the current Mode’s handler for that button.

Button Press Event

The UNIAC.py script creates a Menu instance and attaches the modes defined in that script. The modes (currently) are: clock, track time, date, alarm clock, change playlist, and options.

Modes attached to Menu in UNIAC.py

For instance, when the ‘Plus’ button is pressed in the track time mode, the menu object will use Spotipy to progress to the next track, but when ‘Plus’ is pressed in the alarm mode, it will advance the alarm time by one.

The ‘Mode’ button is a special case that does not call an attached handler function but instead changes the selected mode in the Menu object.

The Menu object also requires that each mode have a display update handler. This handler is called by the master UNIAC.py script in an infinte loop to update the display every 100 ms.

Display Updater

This code was actually based off of another (unfinished) project of mine which used two buttons and a character LCD. By changing the displayUpdate function and the button handlers, this script could be used for virtually any menu interface with multiple ‘pages’.

Interfacing with Spotipy

As discussed in the preceeding section, UNIAC is controlled through a series of button handlers. To interface with Spotipy, most of the modes used in UNIAC inherit from the mpdGeneral class.

mpdGeneral class definition

This class has a few static variables and references the globally available spotipyLogin.sp object, which is an instance of the bare Spotipy API created by the spotipyLogin.py script. It includes things like track status, play, pause, loading a defined playlist from URI, listing available playlists, et cetera.

The UNIACConfig class allows settings like alarm time, current playlist, and settings to be pickled to a config file and loaded on restart. The other classes (including the option menu mode) use this class to handle i/o to the settings file, ‘UNIAC.conf’.

UNIACConfig class.

The remainder of the classes are more or less straightforward implementations of their self explanatory modes and hook the various parts and pieces together.

The last piece of secret sauce, which is not on git, because it is in m gitignore is spotipyLogin.py, a simple script which holds my Spotify login credentials and makes a logged in Spotipy object available.

The redacted version of spotipyLogin.py

UNIAC Supervisor

Finally, there is a UNIAC supervisor script. This is an independent Python process which checks if the UNIAC script is running and if not launches it. It also checks that only one instance of the script is running, and if a second copy has started somehow, kills all but the first process. It’s not strictly necessary, but is a nice-to-have feature. I ran without it for years, with only occasional failures.

UNIAC supervisor script.

Conclusion

The UNIAC software is a combination of several bits and pieces of code which allows a button driven paged menu system to control the Spotify web API and stream music for plaback locally on the RPi, as if it were a commercial product. The interface is ideally suited to a ‘Nixie Boombox’ but could be used with any oddball display you wanted with a little modification.

Thanks for reading.

UNIAC Hardware In Detail

After sharing my last post on Reddit and Hackaday, I’ve gotten lots of kind feedback — and I deeply, deeply appreciate it all. More than anything else, it makes me regret not sharing this project with you sooner.

That said, the biggest ask has been for a detailed build guide. Unfortuantely, I didn’t document the build process in enough detail to write up a step by step guide, and I just don’t have the energy to build an entire new copy right now.

What I can do, however, is share discuss the design choices I made and share the build files. This post will focus on the hardware design of UNIAC, and I will follow it with a post detailing the software in greater detail.

Schematics

The schematic capture and layout was done in Eagle, and broken out into pages. It’s not perfect, by any means, but it’s clean enough.

Anyway, let’s go through the sections.

Raspberry Pi, Buttons, Sound Card, Amplifier

This section has:

The Raspberry Pi itself — note that the footprint says “Raspberry Pi Model B+”, but is actually sized for the Pi Zero W. I recycled an existing symbol because the header pinout is the same across both versions of the Pi, and simply forgot to rename it in Eagle. It also specifies a 7805 5V regulator to power the Pi off the input 12V bus. In practice I used a drop in switch mode replacement for the 7805 from Murata.

The button interfaces — supports two options to connect buttons. The first one, Buttons A, is for directly connecting to the motherboard. I used this option in the first two prototypes and decided to keep the option open here in case the button breakout board had problems, since I had the board space. The button connections here include a provision for +12V to illuminate the buttons, a shared switched ground line to control button illumination, resistors to pull up the buttons and caps for debounce.

Buttons B is a header for the button board, shown later. It breaks out the I2C bus connected to the RPI as well as a 3.3V rail for powering an offboard IC and a 12V rail for powering button illumination. Finally it has an interrupt pin so that the Pi can detect when the button board needs to be polled.

Finally it has footprints for I2S DAC (sound card) and Adafruit class-D amplifier. Not too much to see there.

VU-meter section

The next page of the schematic is a direct copy of my IN-13 VU Meter circuit. I won’t go into a full description of the theory, but it uses a quad rail to rail op-amp to save parts (I usually populate with an LMC660), and MPSA42 high voltage small signal transistors to drive the IN-13s. There is also an ATTINY85 micro running this code to make it act as a log converter. This saves parts and board area vs an analog implementation as well as allowing the VU meter to go to sleep if there’s been no audio for a couple of minutes.

Nixie display section

The third page of the schematic covers the Nixie display. This design was inspired by Dave Jones’ (of EEVBlog) Nixie tube driver design, which attempts to save transistors by using multi transistor ICs. Ultimately, I’m not very happy with that part of the design as there seems to be enough leakage that minor ghosting is visible on the tubes. I’ll definitely revisit it for my next Nixie project.

The controller for the display is a Teensy 3.2 which accepts commands to display values on the Nixies and INS-1 neon lamps via I2C and supports crossfading between digits.

Button breakout board

Finally, The button board uses an I2C GPIO expander IC to provide the same functionality as the individual button headers shown earlier, but without having nearly as many wires. The downside is that it requires an extra IC to do the job, and once the buttons are in the case, they’re soldered to the PCB and are pretty much stuck for good.

Layouts

The UNIAC mainboard layout is pretty compact. There’s not too much to talk about. Most of the size was defined by the need to have the Nixies at the top, bargraph tubes in the middle, and buttons at the bottom. Except for the routing around the Nixies themselves, there were no space constrained areas. I did choose to put the Teensy and RPi near the edges, so that their USB ports were practically accessible, however.

UNIAC Mainboard.

I went to great lengths to calculate minimum creepage distances and trace widths, which I used as design rules to ensure the Nixie display would be safe while being as small as I could make it. When routing my HV traces, I assumed 200 V DC and used a calculator like this one. I ended up using approximately 16mil or 0.4mm as my minimum trace clearance.

Closeup of the HV nets for the Nixies.

For trace width, I used 4PCB’s calculator, and found that even 600 mA would only cause a 10˚C rise for a 6 mil (0.15 mm) wide trace, way more than any of my HV traces were going to carry.vI also chose to keep the ground plane far away from the HV areas for added safety. Given that the Nixies are multiplexed and connect to ground only at a few points, it made sense and decluttered the design.

Finally, we go to the ‘Button Board’. The front panel control buttons are mounted to the front of the case and then soldered to this PCB. It’s a fairly simple design, but it has a couple of notches to clear the standoffs that mount the mainboard and speakers to the case.

Button board.

The biggest pain about this board was getting it fabricated. The buttons have wide pins that need to be fabricated as small slots. This is because the pins are so wide and closely spaced that large enough circular holes actually overlap each other.

It took three tries to get the boards fabricated. I had to make sure that the slots were on the correct layer in the footprint and had to specifically reach out to the PCB vendor in advance to ask them to send checkplots to confirm that the board would get fabbed correctly.

The first order was from AllPCB without asking in advance and they simply milled the small circular holes instead of slots. The second time, they promised that they would send me checkplots, but just fabbed the board without doing so. They also ignored my customer complaint.

It was at that point that I switched to PCBWay, who provided checkplots as requested, but they actually had the slots correct on the first try, resulting in great boards. My only regret is that their matte black soldermask is slightly shinier than AllPCB’s so the boards don’t quite match the mainboard.

The Eagle files for the switch board and main board can be found here.

BOM

The bill of materials, that is, the list of all the components used, is fairly long. It lists all the components that go on the main board, as well as the button board, and all of the off board components that I could think of. Since it’s a big, ugly table, I’m just going to attach it as a csv. The major parts themselves are detailed in the first UNIAC post.

Anyway, the full BOM can be found here.

Enclosure

Making the enclosure was a blast. My workflow is terrible and costs me several pieces of plastic on each new design I come up with, to be honest. I design the acrylic pieces as 2D Inkscape images and handle the ‘3D Modeling’ in my head. It’s even lower rent than DaveCAD!

Front Panel. Bent along the red horizontal lines which are not visible in finished enclosure.
Rear Panel. Bent along (Faint) dotted red vertical lines, which are not visible in the final enclosure.

A raw, laser-cut friendly Inkscape SVG file can be found here, with the front and back on various layers.

Conclusion

Well folks, that’s the hardware in a nutshell. The software will be another post.

Refactoring The UNIAC Playback Code

One of the most problematic parts of the UNIAC project has been the music playback software. The original software used the Mopidy music player for command line playback. Mopidy-spotify provided Spotify connectivity to the software. The mpd python library then allowed me to control playback programmatically.

Old Mopidy + MPD based flow

This had several problems.

  1. Mopidy-spotify is partially broken. Spotify themselves no longer support this interface, and haven’t since 2016. To get it working at all requires a hacked fork of Mopidy-spotify which might break forever at any time.
  2. Response between Python and Mopidy can be really laggy and requires a continuous ping (every 30 seconds) to keep the connection alive.
  3. Loading a new playlist in Mopidy requires significant caching and can have tens of seconds of lag, making the whole system unreasonably slow.

This was all a legacy of the fact that I had designed the software for UNIAC in 2015 and the Mopidy workflow was the only one I could find for getting Spotify playback on a Raspberry Pi. In early 2020, after hearing about Spotifyd from the Diskplayer project, I was awoken to more modern alternatives.

After some rework, I ripped out Mopidy and mpd, and replaced it with raspotify as a player and Spotipy to interface with Spotify.

New Raspotify + Spotipy based flow

This has several advantages.

  1. The control API that Spotipy uses is actively supported by Spotify
  2. Raspotify makes the Pi show up as a Spotify speaker, meaning active playback can be bounced between devices.
  3. Spotipy is controlling my Spotify state, not Mopidy, so it can actually control playback of other devices that are actively playing music.
  4. By the same token, pausing and unpausing on other clients (phone, laptop, etc.) works seamlessly.
  5. Music is streamed, not buffered. This means that loading a new playlist is as instant as it is on the desktop client.

All in all, this means that the switch resulted in a much more pleasant user experience. On top of that, the system is generally more stable than it was before.

Plus, setting up Raspotify was orders of magnitude less difficult than Mopidy. Raspotify managed to work automagically after apt-get installing it and setting it up as a service. This was unlike Mopidy, which took forever to point at the right sound card and get working.

There were some hiccups in figuring out how to connect to Spotify with Spotifyd, but the biggest tricks were:

  1. Learning that the credential saving path should be a full filename, not a path to a directory.
  2. Realizing that the redirect URI could be “http://localhost”. To make that work, all you have to do is run the login script from the command line on your Pi and then copy the URL it feeds you to a browser on a desktop machine. Once you accept the terms, all you have to do is copy the ‘broken’ URL that it takes you to back into your Pi command line and hit enter. Things should work from there.

If you’re looking to build a Raspberry Pi / Spotify player, avoid MPD and go for a Spotipy / Raspotify combo!

The revised code can be found here.

Mopidy and Multiple Audio Streams From the Command Line on Raspberry Pi

Background

I feel like almost everyone who has had to deal with anything outside the most mundane audio setup on Linux machines has experienced the pain I’m about to describe. On Mac and Windows machines, audio seems to ‘just work’ (TM) (R) (C) — and on linux desktops like Ubuntu it seems to be fine too.

PCM5102A Module, product photo from Amazon.com

For my UNIAC project however, I went a little off the rails (dun-dun-dun). UNIAC 2.0 uses a Raspberry PI Zero W, which doesn’t come with any onboard analog audio output, forcing me to use a third party sound card. Since the project had a nice backplane motherboard, I wanted a module I could just plop down on the board as if it were a component. Ultimately, I settled on a PCM5102A module like this one, which took audio as I2S samples and cost all of $8. For the final, working version of this, I used Raspbian Buster (July 2019).

As an aside, I created an EAGLE Footprint for the module, so you can incorporate one into your own projects if so desired.

The Solution

To get it all working, we need the OS to do four things:

  1. Recognize the new I2S sound card.
  2. Use it by default.
  3. Provide some software volume control.
  4. Mix multiple audio streams at once

PCM5102A Drivers

These no-name PCM5102A modules are sketchy at best, and come with no meaningful support and no manual. Fortunately, they use the same IC as the Hifiberry DAC+ project. After getting this thing going I feel somewhat bad for not buying their board, but the form factor is just not what I need.

In theory you can follow the Hifiberry DAC+ setup instructions to get basic output going – but don’t use them if you want software volume control. For me following those instructions resulted in a weird situation where the software volume control didn’t actually stick after reboot, so I recommend staying away.

Volume Control

What did work for me, was a modified version of these instructions. To get basic volume control working, I used the first few steps of those instructions. I’ll duplicate them here in case the site goes down.

Software setup

First we need to disable the onboard sound by editing alsa-blacklist.conf:

sudo nano /etc/modprobe.d/alsa-blacklist.conf

Add:

blacklist snd_bcm2835

Save and exit (^X, Y, enter).

Now, to set the IO, edit config.txt:

sudo nano /boot/config.txt

Add:

dtoverlay=hifiberry-dac

Remove or comment (#) the line:

dtparam=audio=on

Leave the line:

dtparam=i2s=on

commented out.
Save and exit (^X, Y, enter).

Next add some alsa (sound) configuration:

sudo nano /etc/asound.conf

Paste the text below:

pcm.!default {
  type hw
  card 0
}
ctl.!default {
  type hw
  card 0
}

Save and exit (^X, Y, enter).

It is no harm to do a reboot. So:

sudo reboot

Test if everything is OK:

aplay -l

This should return:

**** List of PLAYBACK Hardware Devices **** card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [] Subdevices: 1/1 Subdevice #0: subdevice #0

If you have an amplifier you can connect via a phono lead you can now test it via: speaker-test -D -c -twav So:

speaker-test -D default -c 2 -twav

This is a continuous test saying ‘front left’ and ‘front right’ alternately from the appropriate speakers. Enter ^C to stop.

Adding Software Volume Control

This is an optional step that adds a software volume control into the sound processing sequence.

sudo nano /etc/asound.conf

add the following:

pcm.sftvol {
   type softvol 
   slave.pcm "plughw:0" 
   control { 
     name "PCM" 
     card 0 
   }

Enter or change the pcm.!default section to:

pcm.!default { 
   type plug 
   slave.pcm "sftvol" 
 }

Save and exit (^X, Y, enter).
Now we can test it, as before:

speaker-test -D default -c 2 -twav

This should give alternating “front right” and “front left” from the respective speakers.

Great, but what about multiple streams?

Here’s where we go off the rails. We need to add a software mixer to this cozy little ALSA setup. Fortunately, DMIX is a thing. There are a few dmix examples out there. What we need to do, however, is connect dmix to softvol to a PCM sound device. This I could not find documented clearly anywhere on the internet, so here we are.

pcm.!default {
  type plug
  slave.pcm "softvol"
}

pcm.softvol {
  type softvol
  slave {
    pcm "dmix" #redirect the output to dmix
  }
  control {
    name "PCM" #override PCM slider to set softvol lvl globally
    card 0
  }
}

Great, Now What?

To get MOPIDY to work (my ultimate goal here), I had to do two things:

  1. Install mopidy-alsamixer.
  2. Edit ~/.config/mopidy/mopidy.conf

In mopidy.conf, I had to change the audio section to this:


#mixer = software
mixer = alsamixer
output = alsasink device=plug:softvol
#mixer_volume = 
#output = autoaudiosink
#buffer_time = 

And then I added an alsamixer config as follows:

[alsamixer]
card = 0
control = PCM
min_volume = 0
max_volume = 100
volume_scale = cubic

Finally after weeks of pain, I’ve arrived at a configuration that works!