Controlling Applications with a DJ Controller

Linux Programming

Controlling Applications with a DJ Controller

I enjoy mixing music with my DJ controller, but there are also times when I just want to listen to music without having to mix. Besides, not every song I want to listen to is part of my DJ library. For casual listening, I mainly use Spotify.

Unfortunately, Spotify's mobile app has some serious performance issues on my phone. In fact, it's probably the slowest app I use. When I open it, I sometimes have to wait one or two minutes before anything loads, even when I'm offline. Once it finally starts working, almost every button press triggers another loading animation that can last for ten seconds or more.

And that's not even the worst part. Sometimes the app gets stuck playing one-second snippets of a song, followed by silence, over and over again. On other occasions, simply turning off my phone's display causes the music to stop playing entirely.

I honestly don't understand how an app as seemingly simple as a music player can perform this poorly, especially when everything else on my phone runs just fine. For comparison, even XFCE running inside a virtual machine through Termux on my phone feels more responsive than Spotify.

Despite all of this, I don't want to switch to another music streaming service. Interestingly, Spotify's desktop application works perfectly fine, even though it's built with Electron. However, I don't want to turn on my workstation just to listen to some music.

I already have a laptop connected to my DJ controller as part of my setup. The problem is that whenever I want to skip a song or change the volume, I have to reach over to the controller and awkwardly use the laptop's trackpad. Half of it is even blocked by the controller's cables.

This got me thinking: why not use the DJ controller itself to control Spotify?

DJ controllers are normally designed to interact with dedicated DJ software, but they have plenty of knobs, buttons, and other controls that could be repurposed. So, I decided to build a solution that lets me use my DJ controller to control Spotify without having to open any DJ software.

DJ Controller vs. CDJs

When it comes to DJing, there are two common types of setups: DJ controllers and standalone CDJs paired with a mixer.

DJ controllers are generally the more affordable option and are particularly popular among beginners, including myself. A DJ controller is essentially a collection of knobs, buttons, and sliders that provide a physical interface to DJ software running on a computer. Most controllers communicate with the software through MIDI (or sometimes HID). The computer handles the actual processing, audio playback, and user interface. This means the controller itself doesn't need an expensive display or powerful hardware, keeping the overall cost relatively low.

A typical club setup, on the other hand, consists of two to four CDJs connected to a separate mixer. You can think of a CDJ as an advanced, standalone music player. You plug in a USB drive containing your music, browse your library using its built-in display, and load tracks onto the decks. CDJs also feature dedicated controls such as the iconic jog wheel, tempo fader, hot cues (essentially bookmarks for specific positions in a track), and loop controls.

Each CDJ outputs its own audio signal, which is routed through the mixer. The mixer allows the DJ to control the volume of each deck independently, adjust the EQ, apply filters, and combine the audio signals.

In my case, I use a DJ controller. This is actually what makes it particularly interesting for my project: since the controller communicates with software through MIDI, I can intercept these inputs and assign completely different functions to its physical controls. Instead of controlling DJ software, I can use the same knobs and buttons to interact with Spotify.

Reading MIDI Signals

I already knew that my controller uses MIDI, and I also knew that AlphaTheta provides an official MIDI message list for the DDJ-FLX4. However, at that point, I had barely any knowledge of MIDI. The only thing I knew was that MIDI could send note events. Looking at the PDF, I had no idea what most of the information actually meant.

To figure things out, I used aseqdump, a command-line utility that displays incoming MIDI events. Every time I pressed a button or turned a knob, it printed a corresponding event to the terminal.

After experimenting for a while, I discovered that my DJ controller mainly uses two types of MIDI messages: Note On and Control Change (CC).

Buttons are represented by note events. Pressing a button sends a Note On event with a velocity of 127. Releasing it sends a Note On event, with a velocity of 0. The note numbers themselves don't necessarily have any musical meaning here; they simply identify individual buttons on the controller.

Knobs and sliders, on the other hand, use Control Change messages. Each message contains a controller number identifying the physical control and a value ranging from 0 to 127, representing its current position. Surprisingly, these controls did not make use of Note Off events.

With this basic understanding, I was finally able to make sense of AlphaTheta's MIDI documentation and started writing my first program.

For the implementation, I used easymidi, an npm package that provides a simple interface for receiving and sending MIDI messages in Node.js. Getting started was pretty straightforward: I first wrote a small debugging program that displayed incoming MIDI events, then used the information from the MIDI message list to implement the corresponding Spotify controls.

Mapping Actions

Now that I was able to read the inputs, I needed to map the buttons to specific actions on the computer. The first feature I wanted to implement was the Play/Pause button. On the DDJ-FLX4, there are two Play/Pause buttons (one for each deck). I can use both to control the music. They send the same note but on different channels to distinguish the decks. My script simply listens for the note event, triggering the action regardless of which deck's button was pressed.

To control the music, my first idea was to rapidly prototype a custom Spotify client that would accept these inputs and use the Spotify API to control playback. However, I soon realized that building a full Spotify client was a massive undertaking. Fortunately, I discovered that Spotify exposes an MPRIS interface, which can be controlled using playerctl. I adjusted my script to run the appropriate playerctl command whenever it detected a button press.

This technique worked well for many buttons. The jog wheel was also fairly easy to implement: for every small unit it is rotated, it sends a MIDI event. My script detects a spin and skips 10 seconds. When a spin event is generated, it starts a 500ms timer. If another spin event occurs while the timer is running, the timer restarts. Once the timer triggers, the script checks how many events were received. If the count exceeds 200 events (about a half turn), it seeks 10 seconds forward or backward, depending on the direction of the spin.

For volume control, the DDJ-FLX4 has a dedicated knob that sends MIDI data with an absolute value. When adjusted, it sends a Control Change event with the new value in its payload. To adjust the volume, I didn't use the MPRIS volume interface because I wanted to control the system volume, not just the player's volume.

Finally, to scroll through playlists, I used xdotool. Since my DJ laptop runs on the X Window System, this was not a problem.