Sonic Pi 3 says hello to Raspberry Pi GPIO

One of the requests that Sam Aaron, the Designer and Developer of Sonic Pi has received again and again over the last couple of years to allow Sonic Pi to be interactive with external devices such as buttons or LEDs connected to the Raspberry Pi GPIO, or external midi keyboards or synths. Also to allow audio feeds to be fed directly into Sonic Pi, for further processing and combination with the sounds being generated by Sonic Pi. Several ways have been tried over the years, with varying success, including efforts by yours truly to get Sonic Pi interacting with pictures on a Web Browser, building a jukebox application to play Sonic Pi files, interacting with Pimoroni’s Flotilla Devices, and controlling Sonic Pi with a Processing Sketch. In fact, Sonic Pi 2.11.1 included an undocumented feature which allows Sonic Pi to react to OSC cues being sent to it from an app running on the same machine.

The latest version 3.0 is aptly named the IO release, because it now allows Sonic Pi to interact easily with the outside world, both by midi signals (in and out), OSC messages (in and out) and to allow direct audio feeds to be accepted. This has taken a long time, so that Sam could develop a new timing system which could integrate these signals so that they could function in strict time with the music being produced by internal Sonic Pi command such as play and sample. It is now also possible for the midi and OSC messages to be sent to and from  a remote machines to the one on which Sonic Pi is operating, provided that it is accessible via a network connection.

This particular article concentrates on making Sonic Pi interact with the Raspberry Pi GPIO, and in particular to allow Sonic Pi to control the operation of LEDs and to be controlled by the input from a push button connected to a GPIO pin. Other input and output sources connected to the GPIO  can equally be controlled by and control Sonic Pi.

The mechanism used is to capture OSC messages sent out from Sonic Pi, to perform the control, and to send back to Sonic PI OSC messages containing data which can be used to affect what it is playing. OSC stands for Open Sound Control. To quote from Wikipedia
“OSC is a content format developed at CNMAT by Adrian Freed and Matt Wright comparable to XMLWDDX, or JSON. It was originally intended for sharing music performance data (gestures, parameters and note sequences) between musical instruments (especially electronic musical instruments such as synthesizers), computers, and other multimedia devices. OSC is sometimes used as an alternative to the 1983 MIDI standard, where higher resolution and a richer parameter space is desired. OSC messages are transported across the internet and within local subnets using UDP/IP and Ethernet. OSC messages between gestural controllers are usually transmitted over serial endpoints of USB wrapped in the SLIP protocol.”
In fact Sonic Pi has always used OSC messages as a means of internal communication between its various parts: The Graphical User Interface (written in Qt) the processing code and server (written in Ruby) the Synthesizer scsynth (part of SuperCollider), and in this release an Erlang module (used to help the timing of midi messages) and two helper Apps o2m and m2o used in converting midi messages to and from OSC format. OSC is now used much more widely than for just controlling musical instruments, and this example, controlling hardware devices such as LEDs is a case in point.

As those of you who have experimented with the GPIO, connecting LEDs, buttons, buzzers, motors etc to them will know, the easiest way to access them is via the excellent python gpiozero library developed by Ben Nuttall, Dave Jones and others. Users of Sonic Pi, on the other hand are used to working with the Ruby language, (although they may have used it without realising this!). OSC is agnostic. It consists of a text address followed by optional variables of various types. The Sonic Pi end is taken care of internally. At the other end, we need something to accept OSC messages, and pass them on to gpiozero library commands to control LEDs etc, and in the reverse direction, to receive input from a pushed button (via gpiozero calls) and produce a suitable OSC message to send back to Sonic Pi.

There are several python OSC libraries, but the one I have opted to use is called python-osc and it is easily loaded into python on your Raspberry Pi, using the command

sudo pip3 install python-osc although it is probably already in your python distribution
The gpiozero library should also be installed in recent Raspbian Jessie distributions, and I assume it will be in the up and coming Stretch distribution. If not, it can be installed using sudo apt install python3-gpiozero
The final package needed is pigpio, which can be installed with sudo apt install pigpio

This package contains a daemon called pigpiod which needs to be running for the programs to work. It is best setup to run automatically on boot, and then you can forget about it.
To do this type sudo systemctl enable pigpiod.service and follow it with a reboot. If you want to disable it later you can use sudo systemctl disable pigpiod.service

So with this preliminary work out of the way, lets look at setting up a demonstration of the two-way control in action. First, you will need a copy of Sonic Pi 3.0 This is released on a Mac, and this can be used, as the program will accept connections to and from Sonic PI running on an external machine. A version is also released for the Raspberry PI with a caveat. It requires the Stretch version of Raspbian, which is still beta and not yet released. If you want to give it a try, you need to start with a fresh Raspbian Jessie build (I used the 2017-07-05 release) and then manually upgrade this to Stretch. I used the article here taking note of a caveat at Once Stretch is released of course this will not be a problem. If you don’t want to install SP 3 yet on your Pi, you can still run the program on Raspbian Jessie as far as the GPIO is concerned, and control it from an external Sonic Pi 3.0 running on a Mac.

I built up a circuit of 3 LEDs and one Push Button on my RaspIo ProHAT, but if you have any other breadboard which you can connect to your Raspberry Pi GPIO then you can use that. The LEDs and push button can be connected to any suitable pin numbers, and if you want to use different ones, you just have to adjust the pin numbers allocated to the LEDs and button in my program so suit. With the ProHAT you don’t need any series resistors with the LEDs but if you usually utilise these on your breadboard, then you can include them. Here is a picture of the circuit I used. Likewise you can you any colour of LEDs that you have to hand: they can even all be the same colour. If so I suggest you don’t alter the program LED names to suit the colours you choose at least until you have it working. Then you can do so with care if you want, but remember there are places such as in the handle_leds function and the“/led/control”,handle_leds,”r”,”b”)  (see the program listing later) that you might want to alter as well, so tread carefully.

The “bottom” end of all the LEDs are connected to ground, as is the bottom connection on the switch. The advantage of the RaspIo ProHAT is that the connections to the GPIO p[ins are laid out in numerical order. I used pin 20 for the Red, 17 for the Blue and 14 for the Yellow LED, with the botton connected to pin 10. NOTE: you can use whatever colour LEDs you like. I suggest initially you

The first program that I used, was to test that the connections to these devices were working.
#checks the leds and the pushbutton are working

from gpiozero import LED,Button
from time import sleep
from signal import pause


button.when_pressed= b.on


Run this using python3  If the circuit is correctly connected up, then all three LEDs should light for 2 seconds, and thereafter, when they have gone out, pusihing the button should light the yellow led while the button remains pushed. All being well, you can quit the program using ctrl-C. If not, check all the connections, and especially that the LEDs are connected the right way round (the lead with the “flat” side on the rim of the LED, which is the shorter lead connected to ground), and that you have plugged the LEDs into the correct GPIO pins.

The main program is shown below.

#!/usr/bin/env python3 written by Robin Newman, July 2017 
#Provides the "glue" to enable the GPIO on Raspberry Pi
#to communicate with Sonic Pi. Sonic Pi can control LEDs etc,and receive
#input from devices like push buttons connected to GPIO pins
#Sonic Pi can be running either on the Raspberry Pi,
#or on an external networked computer

#The program requires gpiod daemon to be running. Yu can install this with
#sudo apt-get update followed by sudo apt-get install pigpio if you don't have it
#best to set it up to auto start on boot using
#sudo systemctl enable pigpiod.service          (followed by a reboot)
#The program also requires gpiozero to be installed and python-osc

from gpiozero import LED, Button
from pythonosc import osc_message_builder
from pythonosc import udp_client
from pythonosc import dispatcher
from pythonosc import osc_server
from time import sleep
import argparse
import sys

#These are representative buttons and leds used in initial tests
#You can specify your own pin numbers if differnt
button = Button(10)
yellow = LED(14)
blue = LED(17)
red = LED(20)

#This function is called when the button connected to the GPIO is pushed
def msg():
    yellow.on() #turn yellow led on
    for x in range(15): #send data for playing some notes to Sonic Pi via OSC
        #sender client set up in __main__ below
        sender.send_message('/play', 48 + [0,2,4,5,7,9,11,12,11,9,7,5,4,2,0][x])
        sleep(0.1) #Turn yellow led off to indicate that data transfer is completed

button.when_pressed = msg #this is where the msg routing is activated
 #This is activated when /led/control OSC message is received by the server.
 #two arguments r and b contain 0 or 1 and are used to control the red and blue leds
def handle_leds(unused_addr,args, r,b):
    print("Sent from Sonic Pi",r,b)
    if r==1:
    if b==1:

#The main routine called when the program starts up follows
if __name__ == "__main__":
    try: #use try...except to handle possible errors
        #first set up and deal with input args when program starts
        parser = argparse.ArgumentParser()
        #This arg gets the server IP address to use. or
        #The local IP address of the PI, required when using external Sonic Pi
        default="", help="The ip to listen on")
        #This is the port on which the server listens. Usually 8000 is OK
        #but you can specify a different one
              type=int, default=8000, help="The port to listen on")
        #This is the IP address of the machine running Sonic Pi if remote
        #or you can omit if using Sonic Pi on the local Pi.
              default="", help="The ip Sonic Pi is on")
        args = parser.parse_args()
        if args.ip=="" and args.sp !="":
            #You must specify the local IP address of the Pi if trying to use
            #the program with a remote Sonic Pi aon an external computer
            raise AttributeError("--ip arg must specify actual local machine ip if using remote SP, not")
        #Provide feed back to the user on the setup being used    
        if args.sp == "":
            print("local machine used for SP",spip)  
            print("remote_host for SP is",args.sp)
        #setup a sender udp-client to send out OSC messages to Sonic Pi
        #Sonic Pi listens on port 4559 for incoming OSC messages
        sender=udp_client.SimpleUDPClient(spip,4559) #sender set up for specified IP
        #dispatcher reacts to incoming OSC messages and then allocates
        #different handler routines to deal with them
        dispatcher = dispatcher.Dispatcher()
        #A specimen handler routine handle_leds is specified
        #which deals with the OSC message /led/control being received"/led/control",handle_leds,"r","b")
        #The following handler responds to the OSC message /testprint
        #and prints it plus any arguments (data) sent with the message"/testprint",print)
        #Now set up and run the OSC server
        server = osc_server.ThreadingOSCUDPServer(
              (args.ip, args.port), dispatcher)
        print("Serving on {}".format(server.server_address))
        #run the server "forever" (till stopped by pressing ctrl-C)
    #deal with some error events
    except KeyboardInterrupt:
        print("\nServer stopped") #stop program with ctrl+C
    #Used the AttributeError to specify problems with the local ip address
    except AttributeError as err:
    #handle errors generated by the server
    except OSError as err:
       print("OSC server error",err.args)
    #anything else falls through

At first sight this may look a bit horrendous, but it is not as bad as it seems. There are some initial comment lines. These point out the prerequisites for the program to operate, which have been discussed earlier in this post.  Then the required libraries are imported. The next section defines the pins used by the three LEDs and the single button used in the demonstration. These should of course match what you actually use. The following function msg defines what will happen when a press on the button is detected: first the Yellow led is turned on, then a loop starts which sends a sequence of OSC messages addressed to “/play” with data consisting of a single byte note information. These are separated by a delay of 0.1 seconds, and send the number 48 up to 60 then back to 48 following the values for a major scale. (At the other end, Sonic Pi will receive and decode these OSC messages and play the scale. When the OSC messages have all been sent the yellow led is turned off again and the function finishes.
This is followed by a line which actually calls this function when the button is pressed:

button.when_pressed = msg #this is where the msg routing is activated

Thus every time the button is pressed, this OSC messages will be sent. The destination is specified by the –sp argument sent when the program is started, which contains the destination IP address of the receiving Sonic PI program. If the –sp argument is omitted then it is sent to the local Pi machine.
Then follows a function definition

def handle_leds(unused_addr,args, r,b):

This will be called when the OSCserver receives an incoming OSC message with the address “/led/control” specified further down the program. The routine receives the arguments named r and b each of which will contain either the number 1 or the number 0. These are used to control the state of the red and blue LEDs. This is handled by straight-forward gpiozero commands, namely red.on(), and blue.on(), following the logic set out in the function. The syntax of the routine may seem a bit odd. (It actually follows a format given in the example which comes with the python-osc installation, which took me a little while to sort out). The first argument unused_addr, is in fact the OSC address to which we are responding. We are going to ignore it, as we already know it as the routing is only called when this address is matched. The second args, as far as I can see merely specifies that an argument list will follow, and it is followed by the named list of arguments expected, in this case r and b.

The __main__: routine is called when the program first starts and is initialised. It has several tasks to carry out. First it has to obtain some arguments fed in to the program on the command line when it is started. There are three arguments it can respond to, all of which have default values if they are omitted. The first argument set up is specified by –ip. If this is NOT included then it will be set to “” the internal loopback IP address of the local machine. This will be valid and will work, whether the Pi is connected to a network or is standalone. In the case where it is connected to a network and the PI has an allocated network address e.g., then you MUST specify this address IF you want to talk to a computer running Sonic Pi on a different machine. In that case it won’t work if you use the address. The second optional parameter is the port on which the OSC server will listen.  It is set by default to 8000 which will be satisfactory in most cases, but if you want to use a different one you can specify it by including –port 7500 for example on the command line when the program is called. It must math the port Sonic Pi is listening on of course. The third parameter which can be specified is
–sp. This specifies the IP address of Sonic PI, and will be used when you want to talk to an external machine running Sonic Pi. So two example lines to start the program might be:
./        (if you want to use the local machine and Sonic PI running on it, or)
./ –ip –sp   (if running on a PI with network address and wanting to use Sonic Pi running on a separate machine with address

Following setting up the args to be used, which includes a certain amount of logic to prevent non-working combinations, a sender udpclient is set up, using the resulting Sonic PI ip address ( or a specified external address) to which the OSC messages from the previously discussed msg function will be sent when the button is pressed.
The next thing to be set up are the calls to any handler routines required when an incoming OSC message is detected by the oscserver. This is handled by the dispatcher. For each handler required, a is used to match an incoming OSC address, and specify the function which should be used to handle it. It also passes on any arguments to the OSC address, such as the data to turn on/off the LEDS. The first entry in the argument list for is the OSC message address to match, In our example “/led/control” although I have included a second dispatcher which will match the OSC address “/testprint”, and will print out this and any arguments of the message. (We will try it out later). The second argument in our example is the function name to be used to handle the OSC message, in our case handle_leds. This is followed by the names of any arguments which will also be sent, in our case “r” and “b” You don’t specify the types, only the names to give to the functions. Basically this dispatcher logic enables the server to send the received message and its information to a dedicated handler and to return as quick as possible to a state when it can detect any following message. It is essentially an event handling setup.
The final part of the initialisation is to start the oscserver, with the required address and port, and the section ends with this running indefinitely, until it is interrupted, with the line”

A few lines follow. I am not a Python Guru, and in fact only use the language occasionally, but I have tried to add some error handling, so that the program exits cleanly, and if some anticipated errors occur the user is given appropriate feedback. This is done by running the ___main__ section inside a try:  ….  except: structure. The exceptions handled at the end are:

KeyboardInterrupt: ctrl-C is used to stop the program. This ensures it exits cleanly.
AttributeError: I attributed errors in setting up the –ip and –sp combination of addresses. It is triggered if a non-working combination is specified.
OSerror: The OSCserver can generate some errors, and these are passed on here.

I am sure a more professional Pythion programmer could do better in this department, but I think it makes a reasonable start at error handling.

The Sonic PI end
So far we have discussed the GPIO end of things, and the “glue” necessary to communicate with it using OSC messages. Now for what is needed at the Sonic Pi end. Here is a program which I used to interact with the setup we have discussed.

#Demo program gpiocontrol.rb to control GPIO devices from Sonic Pi
#and to receive data to play from a GPIO button being pressed
#written by Robin Newman, July 2017

SPoschandler_server = "" #adjust for your own setup
use_osc SPoschandler_server,8000 #SPoschandler_Server on port 8000

define :all_off do
  osc  "/led/control",0,0
define :do_testprint do
  osc "/testprint","This is a test message with data",1,2,"The end"

##| all_off #uncomment these two lines to turn all leds off and stop
##| stop

live_loop :testprinting do
  do_testprint # try out the testprint OSC message. Should print on the Pi terminal screen
  sleep 2

live_loop :test do
  n= sync "/osc/play"
  puts n
  use_synth :tri
  play n,sustain: 0.05,release: 0.2

live_loop :m do
  s=scale(:c4,:minor,num_octaves: 2).tick
  # play s,release: 0.1
  #uncomment ONE of the next two lines to alter the response of the LEDS
  #if s >note(:c4)+12
  if look%2==0
    osc "/led/control",1,0
    osc "/led/control",0,1
  sleep 0.1

It starts by specifying the IP address of the Pi running the OSCserver using the script. This will by “” if running Sonic Pi on the Pi, or the IP address of the Pi if Sonic Pi is running on a remote machine. There follows the definition of a function alloff, which can be used to send an OSC message to turn the LEDs off. This message will be
“/led/control”,0,0 The address is “/led/control” and the data two 0, which will be used to turn off the red and blue LEDs.
A second function do_testprint is defined. This will send the OSC message
“/testprint”,”This is a test message with data”,1,2,”The end”  The address is “/testprint” which will be responded to by the second handler function specified in the script. In this case it merely prints out the address and all the arguments sent in the terminal window where the script is running.
The next two lines which are normally commented out, can be uncommented, and the program then run again in Sonic PI. In this case, it will just send the alloff command, turning off the blue and red LEDs before stopping. Normally you will leave these commented out.
Now we test out the do_testprint function by calling it every 2 seconds inside live_loop :testprinting , and sending a message to the OSCserver, which should be displayed on the screen of the terminal in which it is running.
We come to the main part of the program. This consists of two live loops. In Sonic Pi, these are two sections of code which run repeatedly, and at the same time. The first live_loop :test waits to receive an OSC message addressed to “/osc/play”. You will recall that when the button is pressed, the OSCserver sends a sequence of OSC messages addressed to “/play”. Sonic PI automatically inserts /osc in front of any external OSC messages it receives. This is to identify the source as being external, because Sonic Pi works on a myriad number of internal OSC messages, and incoming midi messages are also converted to OSC format, but in this case preceded by /midi. It helps maintain the sanity of the user (and the program) in processing such diverse sources. When it receives a message addressed to “/osc/play” it extracts the single piece of data (a numeric note value) to the variable n. It selects an internal synth :tri and then plays the note using the command
play n, sustain: 0.05,release: 0.2
It then restarts the loop awaiting the next OSC messages. The use_real_time command ensures a very rapid response with minimal delay. In fact this loop will be triggered 15 times in quick succession, by 15 OSC messages sent out each time the button connected to the GPIO pins is pressed.
The second live loop :m runs concurrently with the :test loop, and continuously plays within Sonic Pi the notes in a two octave scale of :C4 :Minor, each one lasting for 0.1 seconds.
There are two choices that can be selected in the loop as it progresses through this two octave scale, which can be selected by uncommenting ONE of the two if statements The first one if s > note(:c4)+12 will carry out the following command to send the OSC message “/led/control”,1,0 if the note value s is in the higher of the two octaves being played. Otherwise it will send the OSC message “/led/control”,1,0 So in this scenario the LEDs will change over every octave, with the red one lit for the higher octave, and the blue one for the lower octave.
If the other if statement is selected (uncommented) instead, then the statement is
if look%==0 This may take some explaining to newcomers to Sonic Pi. The note being played is selected using s=scale(:c4, :minor,num_octaves:2).tick The scale( ) function selects ALL of the notes in the range, and the .tick steps through them one by one. Each time the loop starts another iteration, tick is increased by one. (In fact the scale notes are held in what’s called a ring. This means that when tick reaches the end of the scale list it starts again at the beginning or wraps round, even though the value of tick keeps on increasing by 1 each time the loop restarts. look is a function which returns the current value of tick WITHOUT increasing it….it lets you “look” at the value of tick. So the if statement which says if look%2==0 does this. look%2 means get the remainder when the current value of look is divided by 2. This will either be 0 or 1 and alternates each time you go round the route. So in this case the two different OSC messages are sent alternately each time a note is played. The net effect is that you will see the LEDs flashing much more rapidly than before. The nice thing about the live loop is that each time you alter the if statement and press RUN again it all happens seamlessly without any interruption. This is the way Sonic Pi is designed to work. Each time the live loop starts an iteration, if the code within it has altered, it is adapted as the loop restarts, thus synchronising the changes with the notes being produced.

So that has been a mammoth slog through a lot of material, but I hope that it has helped you to understand how the whole process works. To finish with, here is a pracitcal to do list to try put the whole process.
1) Connect the circuitry to the GPIO on your PI, then switch it on.
2) Download the three programs required, (to run on the PI and interact with the GPIO) and gpiocontrol.rb, a Sonic Pi program to run within Sonic Pi 3.0, whether you are using it on your PI, or on a different external Computer. Finally a program to run on your Pi to test the correct working of the three LEDs and push-button before using the program. (If you have your Sonic Pi on an external machine you will need the gpiocontrol.rb program there).

The programs are all available from my Gist Site here

3 Install the and script on your Pi, in any suitable location. (I put mine in a folder entitled SPandGPIO in the Pi folder
4 Check that you do have pigpio installed and that pigpiod is running.
ps -ae |grep pigpiod should show it with a pid such as 319 ? 00:59:13 pigpiod
If it isn’t running see the discussion earlier in the article about getting it going.
5) Start a terminal window (from Accessories in the Pi Main Menu)
6) type in the terminal window
cd SPandGPIO #the folder my programs were dlownloaded to
You should see the three LEDs light for 2 seconds then go out. Subsequently pushing the button should light the Yellow LED while the button is pushed. Use ctrl-C to quit the program.
7) Start Sonic Pi 3.0 on your Pi, or on your external computer and load in the program controlgpio.rb. Don’t run it yet.
8) Go back to the terminal window on the PI and type the following (assuming you are in the folder where the programs are downloaded) to set execute permissions, and then to run the program using the appropriate comand for your setup.
chmod 755
#if working on your Pi with local Sonic PI
#if working with an external Sonic PI
#you will need the IP address of your PI use the command ip address to get it
./ –ip –sp 192.1.128
#where was MY PI address, substitute yours, and was MY remote machine substitute yours.

Now move to Sonic Pi and run gpiocontrol.rb You should hear the two octave scale Sonic Pi is playing and you should see the Red and Blue led flashing in time with the notes. If you press the Botton connected to your GPIO you should hear Sonic PI play some additional notes, each time you press the button.
If you change over the commented if line in the live_loop :m (while Sonic Pi is running, and then press run again, the frequency with which the LEDs are flashing should change over as described previously.
If you look at the terminal window on the Pi you should see details of all the incoming OSC commands printed there. If you press Run again on Sonic PI you should see the /testprint message show on the screen with all its arguments printed as well.
If you stop Sonic Pi running, then one of the two flashing LEDs will remain lit. You can switch it off from Sonic PI by uncommenting the two lines at the start and re-running which sets both LEDs to off. (remember to re-comment these two lines again to get the main program to work subsequently.

To finish you can stop the script by pressing ctrl-C in the terminal window.

There is a video of a development version of this project which you can see here