We are going to make some funky songs on the Raspberry Pi, which features amazing HD (high-definition) quality video playback, sports high quality audio and has the ability to play 3D games.
For this exercise you'll need a set of headphones, speakers or a monitor with built-in speakers.
Let's play some sounds.
This is the Sonic Pi application interface; it has three windows. The largest one is for writing your code, and we call it the Programming Panel. The top right hand window is the Output Panel, and it displays information about your program as it runs. Underneath is the third window; this is the ‘Error Panel', which displays information if there is a problem or a bug with your program.
-
Launch Sonic Pi by clicking on the Main Menu, then Education, and then Sonic Pi.
-
Select Workspace 1.
-
Type:
play 60
-
Click on the play icon at the top of the screen. What happens?
-
What happens if you type
pley 60
and click on the play icon?This is an example of a bug in your code. In later activities, if the error panel displays text you will know that you have a bug that you need to fix. It could be that you have misspelt a word like
play
. -
Now type:
play 60 play 67 play 69
-
Click on the play icon at the top of the screen. What happens?
The computer is playing each note in sequence (one after the after), but it is happening so fast that to us they sound like they are playing at the same time.
-
We need to tell the computer to pause between each note. We can do this by typing the following after each
play
:sleep 1
The value entered after the word
sleep
represents time in seconds. Using the value 1 represents one second. What would you type for half a second? -
Now write a sequence of
play
andsleep
to make a cool sounding tune!
Now you have mastered the basics of Sonic Pi, let's code a tune!
The values that you have been typing after the word play
represent notes; in fact, they are MIDI note numbers. This means we can translate songs played on a piano into Sonic Pi!
We are going to code Frère Jacques. The song begins with:
C D E C
or 60 62 64 60
in MIDI notes.
Music Notes to MIDI Note Values
C | D | E | F | G | A | B |
---|---|---|---|---|---|---|
60 | 62 | 64 | 65 | 67 | 69 | 71 |
-
Select Workspace 2.
-
Type the following code:
play 60 sleep 0.5 play 62 sleep 0.5 play 64 sleep 0.5 play 60 sleep 0.5
-
Now click on the play icon at the top of the screen and it will play the first part of Frère Jacques.
This first section plays twice. How could you repeat it? You could type the same section out again, or we could start to introduce loops to your code.
-
At the top of your code, above the first
play 60
, type:2.times do
-
And at the bottom of your code, below
sleep 0.5
, type:end
-
Click on the play icon at the top of the screen. What happens?
Let's play this part in Sonic Pi.
In the example below, you can see that some lines of code are indented. This makes it easier to read your code and check for any bugs if it does not work when you press the play button. You can press the space bar twice to indent a line of code.
2.times do play 60 sleep 0.5 play 62 sleep 0.5 play 64 sleep 0.5 play 60 sleep 0.5 end
-
Complete the Frère Jacques song by using the table below to translate musical notes into Sonic Pi note values.
C D E C C D E C
E F G E F G
G A G F E C G A G F E C
C D E C C D E C
-
Create your own tune using the things that you have learned so far.
It's time to make your tune sound more interesting! We can do this by changing the synthesizer sounds it is using. The default Sonic Pi synth is called "pretty_bell"
.
To use a different synth, you need to add the code: with_synth "name of synth"
above the sequence of code you want to use it in.
In this example, "fm"
is the name of the synth:
with_synth "fm"
2.times do
play 60
sleep 0.5
play 67
sleep 0.5
end
"pretty_bell"
"dull_bell"
"beep"
"saw_beep"
"fm"
-
At the top of your code, above the
2.times do
, add the following:with_synth "fm"
-
Click on the play icon at the top of the screen.
-
Change the synth, using the list of synths to try above, to find one that you like the sound of.
Currently, your Sonic Pi version of Frère Jacques is playing in a set key. It always plays the same notes from the same octave. To give your program the ability to use a different set of notes we can use a function.
Functions are a named group of statements. You could put the first part of Frère Jacques in a function like this:
def frere
play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5
end
You can then call the function to play by typing frere
. However, this does not solve our problem of the song not using a different key. Let's solve this problem now!
-
Open a new workspace in which to store your code.
-
Let's begin by creating a function for the first part of the song:
def frere(n) play n sleep 0.5 play n + 2 sleep 0.5 play n + 4 sleep 0.5 play n end
If you press the play button, nothing will happen. You need to call the function underneath by leaving a space and typing
frere 60
. The value of the parametern
here is 60, but you can change this. -
Change the value of
n
by typingfrere 40
orfrere 80
.You will hear the tune play in a different key each time.
Music often has a repeating backing track, with a separate melody played over the top. So far in Sonic Pi you have played one tune. Let's try playing two tunes at the same time!
-
Click on a new workspace tab.
-
The code we use to play two tunes at the same time needs to be between
in_thread do
andend
. -
Underneath
in_thread do
, type your tune. Here I've used Frère Jacques from the earlier activity:in_thread do with_synth "saw_beep" 2.times do play 60 sleep 0.5 play 67 sleep 0.5 end end
This first 'thread' will act as the melody of your music. Underneath, you can type the code for your backing track or baseline.
-
Type:
in_thread do with_synth "pretty_bell" 30.times do play 49 sleep 1 end end
Unless otherwise specified, everything in this repository is covered by the following licence:
Sonic Pi Taster by the Raspberry Pi Foundation is licenced under a Creative Commons Attribution 4.0 International License.
Based on a work at https://github.com/raspberrypilearning/sonic-pi-taster