Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Projects for Kids (Second Edition)

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

Product type Book
Published in Apr 2015
Publisher
ISBN-13 9781785281525
Pages 146 pages
Edition 1st Edition
Languages
Author (1):
Daniel Leonard Bates Daniel Leonard Bates
Profile icon Daniel Leonard Bates

Chapter 7. Building Beats with Sonic Pi

In this chapter, we're going to use a new application called Sonic Pi to create music using code. We will see how it's possible to create tunes and how the power of programming allows us to experiment and change things much more easily than we can with physical instruments.

This chapter is centered on sound, so you will need some speakers or headphones that can plug into the Raspberry Pi's audio port, or an HDMI monitor with built-in speakers.

Sonic Pi


Sonic Pi is a program designed specially for the Raspberry Pi. It allows us to create music by writing code. You can run Sonic Pi by choosing Sonic Pi in the Programming menu on the Raspberry Pi's desktop. Here's what you'll see:

Right at the top, there are all the buttons for controlling Sonic Pi's behavior. We can start and stop a tune playing, save our code, and record our own sounds. We can also decrease or increase the size of the text that our code is written in and neaten it up.

Below those buttons, on the left is the Code Editor, where we will type all of our code. Note that there are multiple Workspace tabs. These allow us to have multiple programs open at a time and switch between them easily. On the right is the Log. This will display information about every note that is played so that you can see what Sonic Pi is doing.

At the bottom is the Help system. This has example code and descriptions of all the sounds and programming features available.

Sonic Pi uses its own text...

Getting started with Sonic Pi


Getting Sonic Pi to create a sound is very simple. Type the following code in the Code Editor and click on Run:

play 60

You should hear a tone.

Note

If you do not hear anything, verify that your speakers are switched on and that the sound is not muted.

Here, we represent different notes as numbers; this is a convenient representation that the Raspberry Pi understands. A higher number represents a higher note. Try it for yourself; change the number and click on Run.

If you know a little about music, you might be familiar with the letter names of notes. Sonic Pi knows these too! Try this code:

play :C

This is the same note as before, but now it has a name. Any note from A to G will work, and you can also place a b or s after the name to make the note flat (lower) or sharp (higher).

Finally, we can put a number at the end of the note name to say which octave the note is in. Again, a higher number will give a higher pitched sound. Here's an example note that combines all...

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

New sounds


Up until now, we've been using the default sound in Sonic Pi, called beep, but there are many other sounds available.

Open up a couple of empty lines before the loop, type in use_synth, and then press the spacebar. Synth is short for synthesizer, which is a program or machine that generates sound. You will see a long list of sound names appear, with beep at the top. Choose one of the sounds by double-clicking on it or typing enough of its name so that it is the only option. Then press Enter. If you run your code now, you will hear the same notes, but the sounds will be different. Try a few different sounds to see which ones you like.

The use_synth function routine changes the sound of all notes until the next use_synth routine is used. It is possible to change the sound of notes as many times as you like within a song. We can even play a trick similar to the trick that we used when playing a random note. Try adding this line of code inside your loop:

  use_synth choose([:pretty_bell...

A real tune


So, we've had fun creating random tunes; now let's try making something a little more structured. Open a fresh workspace by clicking on one of the Workspace tabs underneath the Code Editor. This is where we'll create our new tune. You can switch back to your previous program any time by clicking on its tab.

In this section, we're going to build on the same piece of code, getting the sound we produce closer and closer to a tune you might know. How far will you get before you recognize the tune? A complete code listing is at the end of this chapter just in case you get stuck.

Here's the very first version of the code— you might recognize it already!

  play :G
  sleep 1
  play :G
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1

This code is simple but long. It's very easy to miss a line, and it's boring to write sleep 1 so many times. Let's tidy it up a little by writing a function...

Code listing


This section gives a complete listing of the code used in this chapter. You can refer to it if your code is not working to see what changes need to be made. Some of the code blocks have been moved around within the program to keep similar parts of the program together. This can make things easier to read, but it is completely optional. The following is the complete code used in the chapter:

pace = 0.8

define :play_notes do |notes, durations|
  together = notes.zip(durations)
  together.each do |note, duration|
    if one_in(2)
      speaker = -1
    else
      speaker = 1
    end
    play note, release: pace * duration, pan: speaker
    sleep pace * duration
  end
end

define :drum do
  sample :drum_tom_lo_hard, attack: 0, sustain: 0, release: 0.2
end

define :play_beat do |beat|
  beat.each do |pause|
    drum
    sleep pace * pause
  end
end

rhythm = [1, 1, 1, 0.75, 0.25, 1, 0.75, 0.25, 2]
line1 = [:G3, :G3, :G3, :Eb3, :Bb3, :G3, :Eb3, :Bb3, :G3]
line2 = [:D4, :D4, :D4, ...

Summary


In this chapter, we used Sonic Pi to create our own music. We went from random sounds to a full tune with a drumbeat and saw how the programming ideas you learned in previous chapters can be used naturally to describe music.

Throughout this book, you have learned about the Raspberry Pi and what it can be used for. You also learned some core programming concepts and saw how they apply to Scratch, Python, and Sonic Pi. They apply to many other programming languages too. We saw how programming can be a creative skill and can be used to create games or build useful tools. Above all, I hope you've found programming fun. It's a really valuable skill to learn and can provide unlimited entertainment.

If you've enjoyed this book and would like to continue your Raspberry Pi exploration, here are a few related books from Packt Publishing that you might find interesting:

  • Scratch 1.4: Beginner's Guide

  • Raspberry Pi Cookbook for Python Programmers

  • Instant Minecraft: Pi Edition Coding How-to

  • Raspberry...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Projects for Kids (Second Edition)
Published in: Apr 2015 Publisher: ISBN-13: 9781785281525
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 $15.99/month. Cancel anytime}