Reader small image

You're reading from  Raspberry Pi Projects for Kids (Second Edition)

Product typeBook
Published inApr 2015
Reading LevelBeginner
Publisher
ISBN-139781785281525
Edition1st Edition
Languages
Right arrow
Author (1)
Daniel Leonard Bates
Daniel Leonard Bates
author image
Daniel Leonard Bates

Daniel Bates is a computer science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors and when he gets home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi Foundation since 2011 and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to and Raspberry Pi Projects for Kids (First Edition), both published by Packt Publishing.
Read more about Daniel Leonard Bates

Right arrow

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...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi Projects for Kids (Second Edition)
Published in: Apr 2015Publisher: ISBN-13: 9781785281525

Author (1)

author image
Daniel Leonard Bates

Daniel Bates is a computer science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors and when he gets home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi Foundation since 2011 and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to and Raspberry Pi Projects for Kids (First Edition), both published by Packt Publishing.
Read more about Daniel Leonard Bates