Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Projects for Kids (Second Edition)

You're reading from   Raspberry Pi Projects for Kids (Second Edition) Leverage the power of programming to use the Raspberry Pi to create awesome games

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781785281525
Length 146 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Daniel Leonard Bates Daniel Leonard Bates
Author Profile Icon Daniel Leonard Bates
Daniel Leonard Bates
Arrow right icon
View More author details
Toc

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 chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime