Play a Melody

Play and Sleep


Do this

Experiment with the sleep command for a few minutes. You can try smaller and bigger numbers as an argument.

What if you use

sleep 2

or

sleep 0.5

?

What do smaller numbers do? What about big ones?

Once you’re ready, try your knowledge with the little quiz below!


What does sleep 1 do?
What happens if you use sleep 0 between two play commands?

Educator notes

Two or more notes make a harmony that is defined by the intervals between of these notes. Interval means the distance between two notes. For example, the interval from 60 to 64, is 4 semitones. This is also known as the major third. We also mentioned the major chord of C, and you might have noticed the notes played were 60, 64, 67 (or C, E and G). Number 60 (C) is the root note for the chord, 64 (E) is the second note and 67 (G) is the third note of the chord. The intervals of 4 and 7 semitones, or the major third and the fifth, are the building blocks for a major chord.

sleep 1 makes Sonic Pi sleep for one beat / quarter note. You might have wondered if it makes Sonic Pi sleep for one second. That’s also true! Sonic Pi’s clock is set by default to a tempo of 60 BPM (Beats Per Minute). And with that tempo, a beat happens to be the duration of one second. We’ll introduce tempo and BPM soon enough…


Using Note Names


Buffers

Now it’s time to have a look at the row of buttons below your programming panels. They’re labeled Buffers and each one of them contains a page where you can store your code. Think of them like the tabs in your web browser — you’ll find them really useful! Buffers are numbered and they start from 0. In programming, counting always starts from 0 instead of 1. Funny isn’t it! Now choose an empty buffer and follow the instructions below!



Do this

Time limit: 5 minutes

  • Copy and paste the following code into an empty buffer and run it. 
play :C5
sleep 1
play :G5
sleep 1
play :G4
sleep 0.5
play :B4
sleep 0.5
play :D5
sleep 0.5
play :F5
sleep 0.5
play :E5
sleep 0.25
play :C5
sleep 1.75 
  • Change the values of both play and sleep commands to create a unique melody line of your own. 
  • Remember that you can also use note numbers if you feel more comfortable with that
  • Alternatively, you can create a melody from scratch! If you know how to play piano or keyboard, you’ll probably find this online keyboard player useful.

Online web keyboard

Nice to know

If you are wondering how to get the black keys with note names - the "sharps and flats" it's easy! Just write either s for sharp or b for flat after the note name and before the octave like this:

play :Cs4
#plays C-sharp from the fourth octave
play :ab5
#plays A-flat from the fifth octave
play :fs3
#plays F-sharp from the third octave

By the way, the hash signs tell Sonic Pi that the line is a comment. This means that anything on that line will be skipped by Sonic Pi but can be read by us. This way you can use them to write notes and explanations to your code. You'll learn more about them in the next chapter.


What's the difference between :C3 and :C4 ?

Educator notes

The colon in front of note names is Ruby language syntax for a Symbol. Symbols are often used in programming to map information that is easy to understand for us humans to something that is easy to understand for computers. In this case, we are mapping names of notes to numbers.

Why use numbers instead of note names at all, if note names are more familiar to most people? 

Numbers are very practical when programming! By using numbers, it's possible to change sounds over time programmatically. You can add, subtract, divide and multiply - or even make the note value dependent on an other musical parameter altogether.

Also, not everyone is familiar with note names - using numbers may be more comfortable to some. In this way, it's good to have options.

If your learners are having trouble with errors, ask them to debug the code instead of giving a direct answer. Refer to the troubleshooting guide. Some common mistakes when editing a code are:

  • Forgetting a space between the command and the argument
  • Having a typo in the command
  • Having a wrong type of argument
  • Forgetting the colon from note name symbol
  • Using a comma instead of a period to indicate decimal numbers

If you are interested in the relationship between the numbers and notes, here is a handy table that you can use as a reference. 

Midi notes and numbers

Here Comes Tempo!


Do this
Change the tempo of your song with the use_bpm command. use_bpm only affects commands after it, so place it at the beginning of your code if you want to change the tempo of your whole song!

What happens when you change the value of use_bpm?
What does BPM stand for?

Educator notes

BPM is a standard abbreviation for beats per minute and it is used to define tempo in many different musical styles. It may be a very familiar expression to many of your students, especially if they are into electronic music.

Repeat the Melody


Do this

Create a repetition block in your code! You can repeat as many times as you want by changing the number in front of the .times command.

Remember to start your repetition with do and end it with end

4.times do
  #put your play and sleep commands here
end

When you are done, answer the questions below


Copy this code to a new buffer and answer the questions below

2.times do
  play :c4
  sleep 0.5
  play :d4
  sleep 0.5
  play :e4
  sleep 0.5
  play :c4
  sleep 0.5
end
How many times will :c4 play in the code above?
How long is the code block in beats? Hint: count the sleep values together with the repetition.

In depth: Repeat Notes Inside a Repeat


Educator notes

Learning to see where a code block starts and where it ends is an essential skill for a beginner programmer. Students often forget to close a block they’ve started.

Tip: ask your students to make sure every “do” they write has a corresponding “end” somewhere in the code. Nested code blocks (blocks inside each other) can be especially tricky at first!

Nice to know: in Sonic Pi (and Ruby where it lends its syntax) code blocks are marked with do and end. In some programming languages code blocks are instead created inside curly brackets: { }