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.
Let’s jump right in!
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.
You’re now ready to make your first sounds!
Making a sound
Type the following into the text area:
Then click the
run on the top left corner of the program.
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.
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-rand on Mac it’s
Making several sounds
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:
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:
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 part tells Sonic Pi to repeat a section three times. Changing the
3 will let you change how many times a section repeats.
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-rkeyboard shortcut on Windows, or
Changing the sound up
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
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.
More concise code
So far, to make music, you’ve been using several lines of
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.
play_pattern_timed you can play notes with a sleep different than 1, or have a different sleep for each note.
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!
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.
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.
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.
That’s all Folks!
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.