Music with code: Getting Started with Sonic Pi

Image for post
Image for post

I’m going to introduce you to making music with code, without assuming any knowledge of either.

Sonic Pi is a great way to learn either music or code. It’s also great if you’d like to play around with music without having to spend any money.

Mozart Requiem played with Sonic Pi

Get started by downloading and installing Sonic Pi. If you are on Windows or Mac you can download the installers from the Sonic Pi website. If you are on Ubuntu a community member has created a deb file for easier installation.

Once you’re all set up, you’ll see something like this.

Image for post
Image for post
Sonic Pi Program

You’re now ready to make your first sounds!

Type the following into the text area:

play 60

Then click the run on the top left corner of the program.

Image for post
Image for post
The buttons in this section let you run, stop and record your music.

Your first beep!

Try changing the number and hitting run again. Notice how the sound changes as the number goes up and down. You can even try numbers with a decimal point, like 72.5 try it out.

You can also use letter notes.

play :f

For a sharp note use an ‘s’ after the letter, for example :cs. For a flat note use a ‘b’ after the note, for example :ab. Don’t worry about it if you don’t know what those are, this is the only part of the tutorial where they’re mentioned.

By the way, rather than clicking run each time you can use a keyboard shortcut. On Windows it’s Alt-r and on Mac it’s Cmd-r.

Now with those first beeps under your belt, you’re ready for your next musical masterpiece: several beeps one after the other! You can do that with this code:

play 60
sleep 1
play 62
sleep 1
play 64
sleep 1
play 60

With sleep you can control the space between the beeps. A bigger number means more time between each sound.

Try changing the number after the sleeps and see what it sounds like.

You can also play the beeps at the same time, just don’t have a sleep between them. Try out this code:

play 57
play 61
play 64
sleep 1
play 64
play 68
play 71

Amazing! Music with code!

This bit’s quite handy. Often when you’re making music, you’ll want to repeat parts of it. Try typing up this next piece of code in Sonic Pi and see how it sounds.

3.times do
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
end

The 3.times part tells Sonic Pi to repeat a section three times. Changing the 3 will let you change how many times a section repeats.

The do and end parts tell the program where the section starts and end.

You can play around with this section by changing the number of repeats, changing the section that repeats or adding some music before and after the repeat. Have a play around with it to get more comfortable with the language.

You can stop the music whilst it’s playing using the stop button on the top left of the program. You can also use the Alt-r keyboard shortcut on Windows, or Cmd-r on Mac.

So far all the sounds you’ve made have been beeps. Sonic Pi comes with a few other instruments you can use, called ‘synths’. With use_synth you can change which instrument Sonic Pi is playing.

Here’s the previous section we were playing, using the “pluck” instrument.

use_synth :pluck2.times do
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
end

After playing this section and seeing how it sounds, you can try some of the other synths Sonic Pi comes with. Bring up the help section by using Alt-i on Windows or Cmd-i on Mac.

From there select the ‘Synths’ tab to view the instruments that come with Sonic Pi.

Image for post
Image for post
The synths section of the help system

So far, to make music, you’ve been using several lines of plays and sleeps.

With play_pattern you can give the program a list of notes. These notes will play one after the other, with a sleep of 1 in between.

Try this code out to see how it works.

use_synth :pulse
play_pattern [45,48,50]

With play_pattern_timed you can play notes with a sleep different than 1, or have a different sleep for each note.

use_synth :fm
play_pattern_timed [45,48,50,52,52], [1,1,1,2,2]

In this example the first list is the notes you are play, the second list is the sleep after each note. The above is similar to typing the below code, but much more concise!

use_synth :fm
play 45
sleep 1
play 48
sleep 1
play 50
sleep 1
play 52
sleep 2
play 52
sleep 2

If the list of sleeps is shorter than the list of notes, Sonic Pi will go over the the list of sleeps again as needed. Try out the following code.

use_synth :fm
play_pattern_timed [48,50,52,48], [1,2]

In that example the first note has a sleep of 1, the second has a sleep of 2. Then it repeats, so the third note has a sleep of 1 and the fourth note has a sleep of 2.

use_synth :fm
play_pattern_timed [48,50,52,48], 0.5

The same concept is true if you only use one number. Now all the notes will have a sleep of 0.5.

Another way to make things is bit more concise is when playing several notes at the same time. In music this is known as a chord. To play a chord you can use play_chord alongside a list of note.

use_synth :piano
play_chord [60,64,67]
sleep 0.5
play_chord [67,71,62]
sleep 0.5
play_chord [69,72,76]
sleep 0.5
play_chord [65,69,60,72]

I hope you enjoyed this tutorial and had some fun playing with the code. If you have any questions leave them in the comments and I’ll do my best to answer them.

I volunteer at Foodhall, a space for humans in the heart of Sheffield. Fueled by compassion, cannabis and coffee. All content is available at: https://ibby.blog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store