In this activity we are going to play some notes with
Sonic Pi.
The song "Johnny Works With One Hammer" is a fairly simple tune, listen to someone singing it:
Did you hear how the sound goes higher and lower? Sonic Pi will play higher sounds when you tell it to play higher numbers, and lower sounds with lower numbers. Try pasting this into Sonic Pi and pushing play to see how it sounds:
# Johnny part one
use_bpm 200
play_pattern [53, 53, 53, 57, 60, 53, 53]
Each of those numbers is a different sound, what we would call a different note. You heard higher numbers as higher sounds, which are higher notes. The
use_bpm part is just telling Sonic Pi how fast to play.
But let's get back to our song. We've seen that we can play high and low sounds by telling Sonic Pi to play different numbers.
The number pattern for the first part of "Johnny Works With One Hammer" would be:
53 53 53 57 60 53 53
55 60 60 57 53 53
I've done the first line of the song, try pasting it into Sonic Pi and then completing the second line of numbers.
# Johnny part one
use_bpm 200
play_pattern [53, 53, 53, 57, 60, 53, 53]
play_pattern []
Did that sound like the song we are trying to play? It was close, but some of the notes are supposed to be longer than others. So instead of
play_pattern we can use
play_pattern_timed, like this:
# Johnny part one and two, with timing
play_pattern_timed [53, 53, 53, 57, 60, 53, 53], [0.25, 0.25, 0.25, 0.25, 0.5, 0.25, 0.25]
play_pattern_timed [55, 60, 60, 57, 53, 53], [0.5, 0.25, 0.25, 0.5, 0.25, 0.25]
The second list of numbers on each line is the amount of time that Sonic Pi will play the note for. Notice that the note
60 will be played for a longer time (
0.
5 seconds). In musical notation we say that the shorter notes are "quarter notes" because they take up a quarter of a bar. The longer notes here are half notes. See what the notes would look like for this part of the song.
A quarter note is sort of a filled-in circle, a half note looks kind of hollow.
Now let's try to make our program easier for humans to read by storing the note patterns and timing patterns before we actually play them:
# Johnny part one and two, with timing
play_pattern_timed [53, 53, 53, 57, 60, 53, 53], [0.25, 0.25, 0.25, 0.25, 0.5, 0.25, 0.25]
play_pattern_timed [55, 60, 60, 57, 53, 53], [0.5, 0.25, 0.25, 0.5, 0.25, 0.25]
That's starting to look complicated, but hopefully you can see how it can be broken down into parts. There are four "arrays" there, which are basically lists of numbers. The first one is called
notes1 and it contains the notes for the first line of the song. The array called
timings1 is a list of how long to play each of those notes.
Then we just say
play_pattern_timed notes1, timings1 when we want to have Sonic Pi play that list of notes with those timings.
Let's try it including variables as well. Will this play the same song?
# Johnny with variables and arrays
quarter = 0.25
half = 0.5
notes1 = [53, 53, 53, 57, 60, 53, 53]
timings1 = [0.25, 0.25, 0.25, 0.25, 0.5, 0.25, 0.25]
notes2 = [55, 60, 60, 57, 53, 53]
timings2 = [0.5, 0.25, 0.25, 0.5, 0.25, 0.25]
play_pattern_timed notes1, timings1
play_pattern_timed notes2, timings2
In this example, we said the time for the shorter notes (quarter notes) should be a quarter of a second (0.25) and the time for a longer note (half note) should be half of a second (0.5). We could also have said:
quarter = 1.0/4
half = 1.0/2
or even:
quarter = 1.0/4
half = quarter/2
In case you're curious, the reason it is 1.0/4 instead of 1/4 is that we need to tell Sonic Pi that we want the numbers to be type float instead of integer. If we said1/4 it would equal 0. Don't worry about this yet though.
Hopefully you are starting to see that there can be a lot of math in music.
So far we have done the first half of the song "Johnny Works With One Hammer". If I show you the musical notation version of the whole song, do you think you could translate it into something that Sonic Pi could play? It's a little bit like figuring out a code, you need to figure out what numbers mean the same thing as the notes in the musical notation.
You can also try out different instruments by putting a line like one of these at the top of your code:
use_synth :pretty_bell
use_synth :fm
So now you can make music with Sonic Pi. Try some other songs, and tell your friends.