How to create music from Pi with Sonic Pi
One evening, while I was surfing aimlessly on the web I came across The art of code, an awesome talk by Dylan Beattie in which he discusses the many ways that code can be used to produce and even be art itself. If you haven’t seen it yet, I highly recommend it. During that talk, I first found out about Sonic Pi which is a free, simple yet powerful music creation tool using code based on Ruby. Inspired by the presentation, I decided to nurture my creativity and write a script in Sonic Pi which plays the digits of Pi as musical notes. After playing a bit with this incredibly fun tool, I decided to share my knowledge with you, so let’s dive in.
First, we have to install Sonic Pi, by going to this page and downloading the installer for your operating system. Sonic Pi is available for Windows, macOS, and Raspberry Pi OS.
After installing Sonic Pi, let’s open it. We are greeted with a compact yet rich graphical user interface.
Let’s start by playing a note. We can do that by writing play and then a colon followed by a note. Let’s play the C note.
To hear our first note, press the play button or Alt+R. We can define the octave by adding a number after the note. Let’s play C from the fifth octave.
We can also play multiple notes at the same time, by writing multiple play instructions.
If we want to play the notes sequentially, we can add a sleep instruction between them.
The parameter of sleep represents the duration of the pause. For example, sleep 1 means pause for 1 bar. By default, SonicPi is set at 60 beats per minute (bpm), so in this case, it means a pause of 1 second. We can also define the bpm by writing use_bpm followed by the desired value.
Now that we managed to play some notes, let’s create a loop that plays the notes 2 times.
We can create a loop by writing x.times do where x is the number of times we want to loop over. After that, we write the instructions that we want to loop over, and finally, we close the loop with the end keyword. Also, note that we are now playing at 120 bpm.
It is also possible to loop forever by writing loop do instead of x.times do. Let’s write such an example.
Now, that we know the basics, let’s write a script to play the Pi constant. We will put the digits of Pi in a variable and we will loop over each digit and play a note. We will map the digits to the C major scale. As the C major scale has 8 notes, we will map the remaining digits to a higher octave. Basically, whenever we would encounter a 0, we would play a C4, for 1 D4, for 7 C5, for 8 D5, and so on. So let’s write the code.
The script can be adjusted for however many digits of Pi we want. You can even use the first 100 000 digits of Pi if you want to. I have created a Youtube channel where you can listen to the sound of Pi or e constant. You can also find the code and other math-related music on Github.
In this article, we have just scratched the surface of what Sonic Pi can do. There are some nice examples of what Sonic Pi is capable of. Also, the documentation is very well written with many easy-to-use examples.
I hope I sparked your interest in Sonic Pi with this small article. Let me know what you think of Sonic Pi, what you want to create with it, or what should I create next. I am looking forward to listening to your creations!