Cover image for TherePi - Music Making with a Raspberry Pi

TherePi - Music Making with a Raspberry Pi

DIY Raspberry Pi Theremin Midi Project: Making Music with Python and Raspberry Pi


 7 March 2024   |     4 minute read   |   By Kevin McAleer   |   Share this article on

Videos

For every project I create, I often make a corresponding YouTube video. Sometimes, there might be more than one video for a single project. You can find these videos in this section.

Explore more through these this dedicated videos.

This is a fun project built using a Raspberry Pi to create a Theremin like Musical Instrument, with two HC-SR04 Range Finders. Its pronouced Thera (as in Therapy), Pie - Thera-Pie.


Software Architecture

TherePi uses the Python Mido library, to send midi data from the Raspberry Pi Zero across to another computer running Garageband, to produce sounds based on the distances read from the two range finders; one for volume, and one for pitch.

Unlike the Theremin, the TherePi can auto-tune to specific notes, making it slightly easier to play.


Delving into the Theremin World

Our today’s project draws inspiration from a unique musical instrument - the theremin. Invented almost a century ago by Russian inventor and spy Leon Theremin, this instrument boasts the unique characteristic of not requiring any physical contact from a performer. Instead, it generates haunting, ethereal sounds based on the movement of a performer’s hands near its two antennas. This unusual instrument grabbed the limelight in various 1950s sci-fi movies due to its otherworldly sound profile.

Given this background, our session goal is to create a 21st-century reincarnation of this timeless classic, which we’ll call the “TheraPi”. We’ll convert distances into musical notes, send MIDI data over the network using Python, and create notes with rangefinders. For our testing, we’ll connect to GarageBand on a Mac (but remember, you can use any MIDI software).


Getting started with our TheraPi

Our TheraPi uses rangefinders instead of antennas to sense the position of a performer’s hands. You can move your hand in a 45-degree sweep to change the volume and move it up and down to modulate the pitch. We’ll use a mapping function to map distances to specific notes, effectively giving the performer far greater control over the music they produce.

In order to transmit our musical notes, we’ll leverage a powerful tool from the musical realm: the Musical Instrument Digital Interface (MIDI). This tool allows us to control sound qualities like velocity, speed, and applied effects. We’ll whip up some Python code, generate MIDI messages, and send them across to our GarageBand setup on the Mac.

If you’re itching to get coding, you can find all the necessary code snippets on my GitHub page. For those who want to follow along the entire process, let’s dive straight into the code.


The Code

Whether you’re a seasoned Python pro or a beginner just getting started, I’ve got you covered. The primary components of our code involve the use of the GPIOZero library for Python to interact with the Raspberry Pi’s hardware, mapping functions to translate between distances and MIDI notes, and the mido library to handle MIDI message creation.

Some things to highlight in our code include the use of separate scripts for the MIDI sender and receiver aspects. The sender script consists of a simple loop that sends a series of pre-determined MIDI messages to the receiver script. The receiver script, on the other hand, accepts MIDI messages from the sender and then outputs the corresponding sound through GarageBand on the Mac.


The files

  • midi_basic.py is a simple program to test that midi is working on your computer
  • midi_player.py is a simply midi file player - it will read in the contents of the file and send it to the midi_receiver.py server on the local machine
  • midi_receiver.py listens for connections and then plays the midi messages received on the local machine
  • midi_sender.py sends a couple of test notes to the midi server specified in the HOST variable
  • therepi.py is the main program for running with the therepi hardware - it will use the two range finders to specic which notes to play and how loud to play then depending on the distance read from each sensor

Next Steps

Programming isn’t the only thing on our plate. You will also need to work on designing the physical setup that holds everything together. For instance, you might need to make a few hardware adjustments to the Raspberry Pi setup and design a base fixture to hold the rangefinders at specific angles.

Once you have your 21st-century theremin debugged and assembled, you can then entertain yourself with playing some eerie tunes, maybe even delving into some of the sound experiments that original theremin innovators like Carolina Eek delved into in their professional careers.


The Joy of Creating

Remember, the journey is often just as enjoyable as the destination. The process of creating something unique can be a great learning experience that hones your coding skills and sparks your creativity. So grab your Raspberry Pi, your rangefinders, a dash of Python code, and let’s make music together!



Code

View Code Repository on GitHub - https://www.github.com/kevinmcaleer/therepi

3D Models

Here are the 3D printable STL files:

TherePi Top

The top section of the TherePi

TherePi Bottom

The bottom section of the TherePi