Sonic Pi

Music with Code

Learn It

  • Sonic Pi is an application that allows you to create music using code.
  • In this project you'll learn how to use the techniques you have learned in Computer Science to generate music.
  • If you want to access these resources from home and your have a Raspberry Pi, you'll find Sonic Pi is already installed. If you're on Windows or OSX then you can download and install Sonic Pi here.

Try It

  • Open up Sonic Pi.
  • You should see a screen that looks something like this.

SonicPi.png

  • You can start to write your code in the code workplace Buffer 0

Code It

  • Try typing the following line.
play 48
  • Now press Alt and the r key to run the code and make sure you have your headphones on.

Code It

  • So that was a single note. Let's try playing some more.
play 48
play 52
play 55
  • You'll notice that when you hit Alt+r this time, that the three notes play simultaneously.
  • If we don't want that, and instead want to put a break in between the notes being played, we need to use the sleep function. This takes a time in seconds as an argument.
play 48
sleep 0.6
play 52
sleep 0.6
play 55
sleep 0.6
play 55

Code It

  • Does that sound a little familiar?
  • Let's add some simultaneous notes into it. As you saw earlier, if we don't use the sleep function then notes are played at the same time.
play 48
sleep 0.4
play 52
sleep 0.4
play 55
sleep 0.4
play 55
play 48
sleep 0.4
play 52
play 55
sleep 0.4
play 52
play 55
play 64
play 67

Code It

  • Sounding good?
  • Let's finish off the first bar of the the first four bars of The Blue Danube.
play 48
sleep 0.4
play 52
sleep 0.4
play 55
sleep 0.4
play 55
play 48
sleep 0.4
play 52
play 55
sleep 0.4
play 52
play 55
play 64
play 67
sleep 0.4
play 48
play 64
play 67
sleep 0.4
play 52
play 55
sleep 0.4
play 52
play 55
play 67
play 64
sleep 0.4
play 67
play 64
play 45

Try It

  • Now try and play the second 4 bars
  • Here's the sheet music: link to the sheet music
  • Here's a guide for which notes or which

32ef44be07737fdbe431c8c40f330a33.jpg

  • And here's a table showing the play arguments for the notes.
Name Value
Bass C 48
Bass D 50
Bass E 52
Bass F 53
Bass G 55
A 57
B 59
C 60
D 62
E 64
F 65
G 67
Treble A 69
Treble B 71
Treble C 72

Badge It

  • Choose a simple tune or create your own short composition using Sonic Pi.
  • Use the Rec button to record it playing and save it as a .wav file.