Book Image

Raspberry Pi Projects for Kids (Second Edition)

By : Daniel Leonard Bates
Book Image

Raspberry Pi Projects for Kids (Second Edition)

By: Daniel Leonard Bates

Overview of this book

Table of Contents (14 chapters)

Creating a tune


Any real piece of music is going to contain more than one note. Type the following code in the Code Editor and click on Run. What do you hear?

play 60
play 65
play 72

That might not have sounded the way you expected! All the notes played at the same time. Sonic Pi will play every note it sees until it reaches a sleep command, upon which it waits for a certain amount of time and then continues. This is useful when we want to play multiple notes at once, but to create a tune, we need them all to be separate. Put a sleep command after each play command, like this:

play 60
sleep 1
play 65
sleep 1
play 72
sleep 1

That's better! All the notes now play one after another. The number after the sleep command is the number of seconds to wait before playing the next note (or notes). Again, we can use any number we like, and the number can be different for each sleep command.

Now, music is largely based on repetition, so let's learn how to create loops in Sonic Pi. It's very simple—add a loop...