## Swarm Robotics in the Classroom

dark mode light mode Search Menu
Search

# Code to the Beat with Sonic Pi

Rainer Stropek on Flickr

Creating music with a computer at home has been possible since the 1980s home computer boom. We have seen computers with keyboard overlays, interface cards to connect real instruments, and virtual instruments created from mixing others. But for this project we are going to use a piece of software called Sonic Pi to create musical compositions using nothing but code. The language used by Sonic Pi is called Ruby and it is a popular language for scripting projects and on the web. But fear not as this code is easy to read and Sonic Pi has a great resource to help us in our goal to make a simple tune in 36 lines of code!

### FOR THIS PROJECT YOU WILL NEED

• A computer running Windows or Mac OSX
• Sonic Pi!

### In this project we will learn

• How a loop can be used to repeat a sequence of code indefinitely.
• How an iterative loop can be used.
• How a conditional can be used to alter an instrument choice.
• How a variable can be used to store a value.

### Getting Started

Open the Sonic Pi application and on first boot you will see a blank screen. Let’s take a look at the layout. The large space in the top left is a workspace and in here we write the code that will become our music. At the bottom of the screen is a reference section which provides examples, tutorials and a language reference. This section is super helpful and very easy to use. To the right of the window is a scope used to visualize our music, and under that is a simple console to show the output of our code.

We start the code for this composition by creating two variables. A variable is a container, into which we can store data. In this case we store two numbers, the first is a float, a number with a decimal place which will be used to control how long a thread will pause for. More on this later! The second variable controls how long our loop will pause for before repeating.

The next step is to create a loop and Sonic Pi has a special loop called a live_loop which acts just like a forever loop in Scratch and a while True loop in Python. The live_loop will carry on forever. A live_loop needs to be named and in our case we chose bounce because the music has a bouncy feel. Inside of the live_loop we use a thread which is a way for Sonic Pi to handle multiple instruments at once. You may have already done something like this in Scratch as we can have many sections of blocks for one sprite. For example a section to handle moving the sprite, and a section to handle when the sprite has touched another sprite. Have you noticed that there are two lines have do at the end? This tells us that this is the start of the loop.

The next loop is an iterative loop which in this case will do something eight times. It will play a pre-recorded sample bd_fat and each time the loop iterates the amplitude (volume) will increase until it reaches eight steps. The use of tick handles increasing the value each time the loop goes round. Then we tell the code to sleep (pause) for the number of seconds stored in the thread_sleep variable. Can you see the two end on lines 8 and 9? They close the in_thread and 8.times loops that we created on lines 4 and 5. Every loop starts with do and ends with end.

We now create another thread which will play a piano sound using a synthesiser to emulate the instrument. It will play a G4 note following the same amplitude and timing as the previous thread.

Another thread is used to play a sequence four times. It will play a G4 note which increases in volume each time the loop iterates. Then it will sleep for 0.5 seconds, play a pre-recorded sample elec_blup, then sleep for 0.5 seconds, play a C4 note with increasing volume before sleeping for 0.25 seconds.

The last thread! Here we shall use a conditional test to “toss a coin” and play either a “drum_heavy_kick” or “drum_cymbal_closed” sample. The one_in(2) command is just like tossing a coin and making a decision based on that. If the value returned is true then the heavy_kick sample is played, else if the value is false, the code will play the other sample. This gives our music a little random element each time the piece is played, like an electronic Jazz musician! After the conditional test is complete it ends before sleeping for 0.5 seconds and then ending the thread. Then after telling the live_loop to sleep for the value stored in the loop_sleep variable, the live_loop closes and our loop is complete, along with the code for this project.

When you are ready, click on the run button in the top left of the window. This will play your code / music and highlight any errors. There is so much more to Sonic Pi and I urge you to take this project further and create elaborate musical projects.

We’ve learnt to