How to Play Tunes with Sonic Pi on the Raspberry Pi
With Sonic Pi on the Raspberry Pi, you can make some noise by creating music. The following figure shows an example of a simple music track.
To make music, follow these steps:
Launch Sonic Pi.
When you launch Sonic Pi, you see a single command in the code window:
Click the Run button at the top left.
Did you hear a note?
If you didn’t hear anything, go back to the Prefs and experiment with the settings until clicking Run makes a noise.
If you do hear a note, click in the code window and press the Backspace key a couple of times to delete 70.
Now type 60 instead so that the code looks like this:
Click Run again.
Can you hear how the note changed? The sound — the pitch — is lower.
You can play notes together.
Edit the code so that it looks like the following and click Run:
play 60 play 64 play 67
Those notes make a nice sound.
Edit the code so that it looks like this and click Run again:
play 60 play 61 play 62 play 63 play 64 play 65
That’s not such a nice sound, is it?
Musicians know which notes sound good together, and which notes don’t. Music that sounds nice all the time is boring, so most music has a mix of nice and not-so-nice sounds and notes.
Playing with time in Sonic Pi
You don’t have to play all the notes at the same time. You can tell Sonic Pi to sleep between notes to make a gap, like this:
play 60 sleep 1 play 64 sleep 1 play 67
Sonic Pi reads the 1 as 1 whole beat. Most music uses shorter notes. You usually divide beats into half-beats, quarter-beats, and eighth-beats. For very fast music, you can use sixteenths or even thirty-seconds.
Because Sonic Pi was made by programmers and not musicians, you have to give it a beat fraction as a decimal number. The following table is a cheat sheet for beat counting.
|Beat Fraction||Sleep Time|
Try the following:
play 60 sleep 0.125 play 64 sleep 0.5 play 67 sleep 0.25 play 64 sleep 0.125 play 60
Using different beats creates a rhythm, which makes a tune more interesting.
Beats work well when all the numbers add up to a whole number — 1, 2, and so on. They don’t have to, but strange things happen if you try to play tunes with a different number of beats at the same time.
Live coding in Sonic Pi
Sonic Pi is designed for live coding — which means you can try out sounds and note patterns without stopping other sounds and note patterns.