Control Spotify and Pulseaudio with a Behringer X-Touch One
I've recently bought a Behringer X-Touch One MIDI DAW controller to control REAPER when I'm recording audio.
After the Covid-19 lockdown started, we were told to work from home. I got a Intel NUC from work to take home and connected it to my monitor and bought a USB switch box to switch my keyboard and mouse between my home PC and my work NUC.
Because my speakers are connected to my home PC, I was running Spotify from there and controlled it using either my tablet or the Spotify web player on my work NUC. This worked allright, but I wanted to be able to more quickly pause, play or skip a track.
Using the X-Touch One for this seemed like a perfect solution. Using Python and mido I could control everything on the controller and listen for inputs from it. After that I found out that media players on Linux Mint can be controlled using D-Bus and the dbus-python library.
After I got it all set up, I could control Spotify with the controller and information about the current song playing was sent to the display of the controller. I made the text in the display scrollable using the knob above it.
The next thing I wanted to do was control the audio volume with the slider. I achieved this using the pulsectl library to get the current main audio sinks volume, setting the slider to it and update the volume when the slider moved.
Then I wanted to allow other media players using D-Bus to be able to be controlled. With some more work I was able to control them all. Chrome, VLC, XPlayer, Rhythmbox, Celluloid. They all worked thanks to D-Bus.
The last thing that bugged me at that point was that it was becoming a bit of a mess in de code. I had to run a GLib MainLoop, or else DBus would not work. I had to connect to PulseAudio twice because I could not wait for events and write to it at the same time in a single connection. Also I wanted to compile a to a single static binary so I would not need to install a whole development environment every time I wanted to install the program.
That's when I decided to rewrite the whole thing in Go. And so I did using the gomidi, godbus and pulseaudio libraries. It solved all of these problems.
I have it working now for two weeks, use it every day while working and had no problems at all.
The source code is available on Github:
The python version: DemonTPx/midi-dbus-controller
The go version: DemonTPx/midi-media-controller