“7 years” in Sonic Pi (Part 1)

Sonic Pi is a way of writing music through code. This is a step-by-step tutorial aimed at beginners to Sonic Pi on how to code the riff from 7 Years by Lukas. (Note if you are intending to use this in a classroom and want to play the track to demonstrate how this sounds, there is a radio edit as well as the original version.)

Music is created in Sonic Pi in spaces called buffers. You can play the first note by opening Sonic Pi and typing the following the words “play 68” into a new buffer.

sonic pi 7 yr 1

Click on the Run button to hear how this sounds.
sonic pi run buttonYou can also write the notes as their letter names if you can read music, but here we’ll stick to numbers to represent the notes.

The next three notes are 67, 65 and 63. Type these into Sonic Pi and your code will look something like this:sonic pi 7 yr 2

When you run this code, you’ll notice that all the four notes play as a chord. That’s because computers do exactly as they are told. Unless you add code to wait in between each of the notes, the notes will play as a chord.

In this case, the first four notes are semiquavers in music which means each note lasts 1/4 of the beat. So we need to wait 1/4 of a beat in between each note. We can do this by using the command “wait 0.25”. Try typing this in between each of the play commands. Do you need a wait command after the fourth note as well?

Your code should now look something like this:

sonic pi 7 yr 3

There are now 6 more notes to code. These are the same two notes repeated three times and they are quavers which means they last for 1/2 of a beat.

The code for the first two notes from these six is:

sonic pi 7 yr 4

Can you add the rest of the code to complete the melody?

In the pop song, this pattern of notes plays over and again as a riff. To make this happen in Sonic Pi, we need to put all the notes inside a loop. Go back to the start of your code and create a new line which says “loop do”. We then need to tell the loop where to stop, so you need to type the word “end” as the last line of your code.

All the text inside the loop now needs to be indented, i.e. moved to the right. There is a handy shortcut in Sonic Pi to tidy up your code automatically. Press Alt + M on a Windows / Linux PC or Command + M on a Mac to do this.

Run your code again and check that it loops over and over again.

Finally the beep sound that Sonic Pi defaults to is pretty cool for 7 Years, but there are other synth sounds you can use too. Inside your loop, and before your code for the notes, type the “use_synth :” command and then experiment with different sounds. We like “use_synth :pretty_bell” but what’s your favourite?

The whole code will look something like this:

sonic pi 7 yr 5


In part 2, we’ll look at how to add a beat to this tune to make it sound even better.


Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s