Stephen Smith's Blog

Musings on Machine Learning…

Playing Music by Writing Code

leave a comment »


The standard Raspbian Linux for the Raspberry Pi comes with an interesting program called Sonic Pi. This program lets you create music by writing code, the goal is that the coding is easy enough that anyone can learn it, and that you can code fast enough to play a live performance by writing the code as you go along, including jamming with other musicians. From my coding experience there is always a lot of pressure to write code faster, but here you need to write the next bit of code and stay on beat.

I was interested in this, primarily to use as a drum machine to accompany by very beginner guitar playing. Basically something a bit more interesting than a metronome. Sonic Pi will run on Windows, MacOS and Linux, but one of the goals of the program was to be as accessible as possible, so hopefully even low income school districts or third world countries can afford a Raspberry Pi or two.

Learning to Code

Like many programs on the Raspberry Pi, Sonic Pi is a backdoor way to try and get kids to learn how to code. Writing code to create music has been around since electronic synthesizers became digital, but how to do this was quite technical and required a lot of (then) expensive equipment. Sonic Pi comes with a lot sampled sounds so you can go quite far without a good sampler.

The programming language is based on Ruby and can do some quite powerful things. The emphasis in Sonic Pi tends to be on fairly straight forward functional programming and it recommended that you only use what is documented for Sonic Pi, but people have discovered hidden Ruby features that work if you try them.

The Sonic Pi language is described as a strongly timed language (a play on other strong typed languages) since music is all about timing and Sonic Pi is brilliant in how it keeps everything in time.

The language keeps things simply, so hopefully if a student can think musically then they can transfer that skill into programming via the Sonic Pi language. The hope being that this is a more interesting entry point to programming than say learning sorting algorithms.

The program is designed to be interactive, have lots of help and typical IDE type features like auto-complete. There is a vibrant online community and tons of examples. Additionally there is a free book available here.

Live Performances

People actually use their Raspberry Pi running Sonic Pi as a musical instrument. This isn’t just running a program they wrote previously for people to listen to. This is actually writing the code as people listen. It isn’t always a solo performance either, it could include jamming with other musicians either playing Sonic Pi or playing regular musical instruments.

The key part of the language to support this is the live loop, where you have a loop playing the music (or code) and it allows you to update the code and have the new code seamlessly take effect whenever you like without causing a break in the music.

This usage is often called live coding and there are actual clubs that have events where this happens called algoraves.

It’s hard to explain this, but if you check out some of the YouTube videos of such performances it’s easy to get the idea. A good introduction is Sam Aaron’s presentation at OSCON on how to start coding and playing Sonic Pi. Here is a Sonic Pi performance covering Daft Punk. Here is a jam session with a guitarist. And another example.

Some Code

At the most basic level you can get some noise out of Sonic Pi just by running the one line program:

play 100

This just plays a beep which decays to nothing. You can add another note with:

play 100
play 75

This actually plays the two notes at the same time, since the program doesn’t stop and wait for the first note to finish. To play the two notes one after the other we would do something like:

play 100
sleep 1
play 75

The notes are actual midi numbers from 0 to 254. The following diagram shows how they relate to the notes from a piano keyboard.

Beeps are all well and good, but you can get much more interesting notes by playing the built in samples. For instance:

sample :bd_haus
sleep 1
sample :ambi_choir

We aren’t very musical yet, but if we put it in a live_loop then it will play over and over.

live_loop :mySong do
    sample :bd_haus
    sample :ambi_choir
    sleep 1

Now we’re playing music and with the live_loop we can modify the code and when we hit run it seamlessly starts playing the new code allowing us to perform live by writing code.

Rather than including a lot of source code in this article, for more examples I’ll just provide a couple of links. Note that if you Google around, there are a lot of Sonic Pi samples that you can have a look at. A good little drum machine bit of code is this one. Since Sonic Pi is based on Ruby you can use Ruby string processing to convert ascii drum tabs into music as done here.


Sonic Pi is a pretty cool program. Basically you get a fairly sophisticated music synthesizer with your inexpensive Raspberry Pi. It’s fun to play with, and perhaps will motivate a few more kids to pick up coding. After all if you do learn how to program in Ruby with this, then you are well on your way to being a highly employable Ruby on Rails web developer. Combined with Scratch, the Raspberry Pi really offers some innovative ways to teach coding to students.


Written by smist08

November 25, 2017 at 9:25 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: