## The jukebox
Lately, I've been obsessed with the old way of listening to music. You know: from your own collection, in order, one album at a time. No randomness, no infinite playlists, no album of the day, no ads, and no algorithms. When I got my hands on an Adafruit MacroPad RP2040 I knew what I had to do: make my own ugly and over-engineered music player. I present to you the "MacroPad Jukebox".
With this gizmo, I can play my offline music collection the way I like it.
The music collection is loaded onto the Raspberry Pi and arranged in a series of playlists. I use a large sd card for both the OS and the music files, but an external USB drive would also work. As long as the files are accessible to the media player.
The media player is VLC. This media application has an amazing number of features for customization. One of my favorites is the Remote Control interface that allows me to fully control the application via a TCP socket.
The MacroPad RP2040 controls the playback of the music collection by sending commands to a small service in the Raspberry Pi. The rotary encoder controls the selection of the album. The keypad has the usual playback controls: play, pause, stop, etc.
Finally, I can connect the Raspberry Pi audio output to my favorite speaker or headphones. I can even use it in my car!
In this guide, I provide all the code and the instructions to assemble my MacroPad Jukebox. After that, I explain in more detail how all the pieces work.
## Bill Of Materials
- Raspberry Pi
- Adafruit MacroPad RP2040
- Data Cable USB C to A
- Power Cable micro USB
- Wired headphones or speaker with 3.5mm cable.
- Micro SD Card
## Source Code
The project has two programs: a Go service that runs inside the Raspberry Pi, and the Circuit Python code for the MacroPad.
https://github.com/carlosolmos/macropadjukebox
## Wiring
## Assembly and Installation
### 1. Prepare the Raspberry Pi
Prepare a Raspberry Pi —I used an old model 3B— with the latest version of the Raspberry Pi OS with desktop (11 bullseye) from the official website. Go through the WiFi settings, the updates, and the generic desktop configuration.
#### VLC
Make sure VLC is installed. Download a sample mp3 file and test the player using any audio output —speaker or headphones.
pi@raspberrypi:~ $ wget https://download.samplelib.com/mp3/sample-6s.mp3
#### Go SDK
I found it easier to build the software inside the RPi to avoid any incompatibilities. Install the golang SDK that is available for the RPi OS via `apt` packages:
pi@raspberrypi:~ $ sudo apt install golang
It is also possible to build the binary for ARM in another computer and upload it to the RPi.
### 2. Get the project's source code.
Clone or download the project's source code repository from GitHub:
pi@raspberrypi:~ $ git clone https://github.com/carlosolmos/macropadjukebox
### 3. Prepare the Macropad RP2040
Prepare the MacroPad RP2040 for CircuitPython programming following this Quickstart guide from Adafruit: https://learn.adafruit.com/adafruit-macropad-rp2040/circuitpython
I used CircuitPython 7.3.3 but newer versions should work.
> MacOS Warning: There seems to be a problem with programming the MacroPad firmware in MacOS Ventura (13.0.1) that I found the hard way. The Finder does not let you move the code files to the device. There is more information in this article. Again, I ended up working directly in the RPi and avoided all this trouble.
#### Get the MacroPad CircuitPython libraries
Now that the MacroPad is ready for CircuitPython code, we need to load the libraries that this project needs. The libraries are available as a bundle in this URL: https://circuitpython.org/libraries. Download and extract the package for the CircuitPython version that was loaded in the MacroPad.
#### Load the project python code and the CircuitPython libraries
Copy the following libraries from the bundle folder to the `/lib` folder in the MacroPad drive.
adafruit_bitmap_font
adafruit_debouncer.mpy
adafruit_display_shapes
adafruit_display_text
adafruit_hid
adafruit_macropad.mpy
adafruit_midi
adafruit_pixelbuf.mpy
adafruit_simple_text_display.mpy
adafruit_ticks.mpy
neopixel.mpy
Copy the project python files to the root of the MacroPad drive. Replace any existing files.
macropadjukebox/macropad/code.py
macropadjukebox/macropad/boot.py
The MacroPad drive should contain these files:
Power cycle the MacroPad for the new code to load. After booting up, the display should show the playback controls.
It is possible to inspect the running code in the MacroPad. I recommend using the Mu Editor in CircuitPython mode and its serial console. Push some keys and watch the messages.
### 4. Build and install the jukebox service.
#### Build the Go service
Use the `go build` command to create the executable for the service:
pi@raspberrypi:~ $ cd macropadjukebox/
pi@raspberrypi:~/macropadjukebox $ go build -o jukebox main.go
Copy the binary to the home directory —optional, but recommended.
pi@raspberrypi:~/macropadjukebox $ cp jukebox /home/pi
### 5. Prepare the music collection
In the root folder of the repo, there is a sample configuration file `config-example.json`. Make a copy in the home directory. Name it `config.json`.
pi@raspberrypi:~/macropadjukebox $ cp config-example.json /home/pi/config.json
Copy some files from your music collection to the Raspberry Pi —VLC can play just about any audio format. I like to use the `/home/pi/Music` folder but any other path should be okay.
Create a separate playlist file `{playlist_name}.m3u`, with the paths to your songs, for each collection or album you want to load in the Jukebox MacroPad.
Adjust the `config.json` file with the playlists you created. Leave the `_collections_` entry as `00` —this entry is necessary for the MacroPad code.
### 6. Test the Jukebox
Connect the MacroPad to the RPi, and the headphones or speakers.
Start VLC with RC from a terminal
pi@raspberrypi:~ $ vlc --extraintf rc --rc-host localhost:8888
Run the `jukebox` service from another terminal.
pi@raspberrypi:~ $ ./jukebox
The Playlists should be loaded to the MacroPad. Use the rotary encoder to select an album and push it to play it.
See the `jukebox` service log messages to verify the commands received from the MacroPad.
Now play around with the MacroPad keypad to control the playback: pause, play, volume up, volume down, etc.
If you've been using the MU serial console you should see the log messages from the MacroPad.
### 7. Prepare the service to start at boot time
The jukebox should start automatically when powered on. Use `crontab` in the Raspberry Pi to launch the service at boot time.
The project code has a bash script named `runboot.sh`. I prepared this file to start VLC in headless RC mode, and the `jukebox` service. I added a few pauses to account for the serial port initialization.
#!/bin/bash
set -x
set -e
sleep 5
cd /home/pi
cvlc --rc-host "localhost:8888" -I rc > /dev/null 2>&1 &
sleep 5
./jukebox > /home/pi/jukebox.log 2>&1 &
Copy the script to the home folder and make sure it is executable.
pi@raspberrypi:~ $ cp macropadjukebox/runboot.sh ./runboot.sh
pi@raspberrypi:~ $ chmod +x runboot.sh
Create an entry in the `crontab` to execute the script at boot time.
pi@raspberrypi:~ $ crontab -e
@reboot cd /home/pi && ./runboot.sh &
### Rock on!
This is it. Disconnect any monitor, keyboard, and mouse from the RPi. Reboot and test the Jukebox. Wait about 15 seconds for everything to start. I know that is terrible, but practice your patience, or go to the end of this post for a list of possible improvements.
## So you want to know how all of this works
Here is the long version of how this project works. Please remember this is a scrappy project, so don't expect efficiency, beautiful code, or clever ideas. The ultimate goal here is to learn new things by tinkering with these devices.
https://carlosolmos.dev/posts/the-macropad-jukebox/
## What's next?After completing the project I realized there is a number of things I could do to improve this project:
- A better mechanism to load new music without editing the config file.
- More media actions: random, fast-forward, rewind, etc.
- Show the playing song title, maybe even the track progress in the MacroPad display.
- Try the RPi OS Lite (no graphics) version
- Dynamically find the serial port from the MacroPad
- Flash some LEDs or play a sound when the Jukebox is ready to rock.
> All songs used during the making of this project have been acquired legally and are meant for personal use.
Comments