How to Play Tunes with Sonic Pi on the Raspberry Pi

By Richard Wentk

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.

image0.jpg

To make music, follow these steps:

  1. Launch Sonic Pi.

    When you launch Sonic Pi, you see a single command in the code window:

    play 70
  2. Click the Run button at the top left.

    Did you hear a note?

  3. If you didn’t hear anything, go back to the Prefs and ­experiment with the settings until clicking Run makes a noise.

  4. If you do hear a note, click in the code window and press the Backspace key a couple of times to delete 70.

  5. Now type 60 instead so that the code looks like this:

    play 60
  6. Click Run again.

    Can you hear how the note changed? The sound — the pitch — is lower.

    You can play notes together.

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

  8. 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
1 1
Half 0.5
Quarter 0.25
Eighth 0.125
Sixteenth 0.0625
Thirty-second 0.03125

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.