This time we’re going to play with Sonic Pi! It comes pre-installed with Raspbian. I wanted to try it ever since I started using Raspberry Pi, and saw its cute little icon. I was a bit hesitant to use it, but then my boss decided to show me how!
This post will be a two part series with the first being a beginner’s guide that I have prepared on my own. The second part will be a walk-through of a problem set with the guidance of my boss. By the way, you don’t need to know much about music to enjoy this. So, don’t worry because all I know is what I learned playing the recorder in school, and I still got a lot out of it!
Sonic Pi is an open source audio programming environment. It comes pre-installed with Raspbian. Besides being available on Raspberry Pi, Mac OS and Windows versions have been released, and can be downloaded for free.
There are also tutorials posted on the official Raspberry Pi website.
Let’s launch Sonic Pi now and see what it’s all about! Click “Programming” and then “Sonic Pi” in the menu.
It takes a little bit to load, so don’t panic and wait.
Figure 1
After a short wait, you should see a screen that looks like Figure 2. By default, the screen is divided into three sections. The top left is for coding, the top right is the output for the log when you run the program, and the bottom is the help section that displays tutorials and more.
There is also a “Preferences” menu, but in the default settings it is not displayed.
Please refer to the tutorial page on Raspberry Pi’s official website for a detailed explanation of the screen layout.
WORKSHEET | Raspberry Pi Learning Resource
Figure 2
The basic functions are accessed by the ten buttons at the top of the screen. They are intuitively color coordinated by type for ease of understanding.
Figure 3
The four pink buttons on the left side are used mostly for programming.
“Run” and “Stop” are for starting and stopping the script in the workspace. To save the script, click “Save”. To record audio while running the program, click “Rec”. Recordings are saved in the “wav” format.
Figure 4
The orange buttons on the right are for changing the way that the script in the workspace is displayed. “Size-”and “Size+”change the font size, and the “Align” button adjusts the indentation of the script.
Figure 5
The blue buttons are for transitioning between different screens. The “Info” button displays a window with information about the developer and version information. “Help” and “Prefs” allow transitions to each of those panels respectively. These can also be opened by right clicking.
Samples, tutorials, and lists of functions and variables are all organized in the “Help” window. Clicking the button at the top right of the “Help” window frees up the window. Try expanding it for easier viewing.
Figure 6
Ok, let’s get right into playing some sounds! I tried out the sample script from the tutorial on Raspberry Pi’s official website.
1 |
play 60 |
It played a note! That was “do”. By the way, what is this “60”? The answer is on the same page under “MIDI NOTES AND MUSIC NOTES.”
Music Notes to MIDI Note Values
C | D | E | F | G | A | B |
60 | 62 | 64 | 65 | 67 | 69 | 71 |
That number corresponds to the MIDI note number of the sound! The numbers in the table skip to make room for the sounds of the black keys on the keyboard.
1 |
play :c4 |
Entering a colon (:) in front of the desired note like this also allows you to designate notes to play. Playing “C4” sounds the same note as “60” from the table above.
When playing a series of notes, entering “sleep” in between them allows you to put breaks between them. Without the “sleep” all of the notes will just play at once, so be sure to use it when you want to play a sequence of sounds. The unit of “sleep” is seconds, and it can be designated in decimals as well.
1 2 3 |
play 60 sleep 0.5 play :c4 |
Did you hear the same sound twice?
With this setup, the default sound will play. It is also possible to choose which sounds to play. To adjust the types of sounds see the “Synths” tab of the “Help” window.
Figure 7
To choose the type of sound, use the “use_synth” command. Putting a space after “use_synth” displays a list of sound names for easy selection.
Figure 8
Sonic Pi is equipped with 30 different sounds! I wanted to hear them all, so I wrote a script and ran it!
I played all the 30 sounds with one second interval. There are not only instrumental sounds, but also beeps and other noises. There are also a few sounds that could accent music compositions. Each different type of sound reverberates in a different way as well.
For those who want to try, take a look at the script below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
use_synth :beep play 60 sleep 1 use_synth :bnoise play 60 sleep 1 use_synth :cnoise play 60 sleep 1 use_synth :dark_ambience play 60 sleep 1 use_synth :dsaw play 60 sleep 1 use_synth :dull_bell play 60 sleep 1 use_synth :fm play 60 sleep 1 use_synth :gnoise play 60 sleep 1 use_synth :growl play 60 sleep 1 use_synth :hollow play 60 sleep 1 use_synth :mod_beep play 60 sleep 1 use_synth :mod_dsaw play 60 sleep 1 use_synth :mod_fm play 60 sleep 1 use_synth :mod_pulse play 60 sleep 1 use_synth :mod_saw play 60 sleep 1 use_synth :mod_sine play 60 sleep 1 use_synth :mod_tri play 60 sleep 1 use_synth :noise play 60 sleep 1 use_synth :pnoise play 60 sleep 1 use_synth :pretty_bell play 60 sleep 1 use_synth :prophet play 60 sleep 1 use_synth :pulse play 60 sleep 1 use_synth :saw play 60 sleep 1 use_synth :sine play 60 sleep 1 use_synth :square play 60 sleep 1 use_synth :supersaw play 60 sleep 1 use_synth :tb303 play 60 sleep 1 use_synth :tri play 60 sleep 1 use_synth :zawa play 60 |
Now that we’ve got a handle on the coding rules, we are done with the first half! The second half will be a problem set by my boss!
In Sonic Pi, by adding parameters, sounds can be altered, allowing for different arrangements. Parameters are explained in the “2. synth” tutorial in the “Help” window.
I’ve taken the most important points from that.
Parameter | Description | ||||
amp | Volume | ||||
pan | Stereo Speaker (left, right, center) Settings | ||||
rele | ase | F | ade | -out Timing Adjustment | |
atta | ck | Fa | de | -in Timing Adjustment | |
sust | ain | D | ura | tion of Sound |
“Attack”, “sustain”, and “release” sound a little strange. A look at the diagram from the tutorial will clear things up for you.
Figure 9(2.4 Duration with Envelopes)
Setting “attack” will cause the sound to rise up as if fading in. “Release” is the opposite of that. It makes the sound dissipate and fade out. “Sustain” allows you to maintain the volume settings established with “amp” for a set amount of time.
The default values of all three depend on the type of sound, and that can be checked in the “Synths” tab of the “Help” window.
Okay, let’s run a script and compare the differences.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
use_synth :pretty_bell play :c4 sleep 1 play :d4,amp:2 sleep 1 play :e4,pan:1 sleep 1 play :f4,attack:2 sleep 3 play :g4,release:2 sleep 2 play :a4,sustain:2 sleep 3 play :b4,pan:1,attack:3,release:2,sustain:1 sleep 6 play :c4,pan:-1,attack:1,release:3,sustain:2 |
I tried playing “Do, Re, Mi, Fa, Sol, La, Si, Do” adding changes to each note. Each parameter leaves a really different impression!
The length of the audio is increased when using “attack”, “sustain”, and “release”, so I used “sleep” to make sure that the notes don’t overlap.
The “play” parameter is a fixed value. Other than setting it to a fixed value, it is also possible to use a function.
If you want to set it to a random value, use “rrand”. The range of the value can be set, allowing for even decimal values to be returned.
1 |
rrand(1,3) |
When you want to choose from a set of values, use “choose”. When you know what values you want to set, use this option.
1 |
choose([50,60]) |
To find out which values were chosen, open the “log” window to check the output logs.
Let’s run a script and see what happens.
1 2 3 4 5 6 |
use_synth :saw loop do play choose([:c4,:d4,:e4,:f4,:g4,:a4,:b4]), amp:rrand(1,3), pan:choose([-1,0,1]),attack:rrand(1,3), release:rrand(1,3),sustain:rrand(1,3) sleep rrand(1,5) end |
The sound type and “pan”values have been determined so I’ve used “choose” on them. “amp”, “attack”,“sustain”, and “release” can take decimal values, so I’ve used “rrand”. This time, I also used “rrand” on the “sleep” command.
“loop do – end” is a repeating command. When you want it to stop, click the STOP button.
You can tell when a parameter is set to random by looking at it in the “Log” window. Of course, you can also tell when a sound is random just by listening, right? Telling the differences in “pan” is easier when using headphones.
To play chords, use the “chord” command. The chord names are set as arguments of a function, but as seen below, the basic notes, major chords, and minor chords can be expressed as seen below.
1 2 |
play chord(:c4) play chord(:c4,:minor) |
Using “play_pattern” allows you to not use the chord itself, but the chord as a scale of its individual notes. The function works the same way as the “chord” command does.
It’s a bit difficult to put into words, so let’s play some notes, and try to tell the difference!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
use_synth :pretty_bell play chord(:c4),release: 3 sleep 3 play chord(:g4), release: 3 sleep 3 play chord(:a4,:minor),release: 3 sleep 3 play chord(:f4),release: 3 sleep 3 play_pattern chord(:c4) play_pattern chord(:g4) play_pattern chord(:a4,:minor) play_pattern chord(:f4) |
I played the chords“C-G-Am-F”. Some of you may have heard this combination before. It’s from “Let It Be”!
Basic chords can be produced easily enough that even people who can’t play instruments can produce music quite easily.
One of the best things about Sonic Pi is the “live_loop” function that allows you to test out scripts in real time!
The video above started with the script below.
1 2 3 4 5 6 7 8 9 10 |
live_loop :loopname do play chord(:c4) sleep 2 play chord(:g4) sleep 2 play chord(:a4,:minor) sleep 2 play chord(:f4) sleep 2 end |
Assign any name you want to “loopname” in the first line.
After rewriting the script and clicking the “Run” button, the
“Log”window will reset. Starting with the next iteration, the audio will play reflecting the new contents of the script. It will keep playing until you click “Stop”. If an error comes up in the script, the previous iteration will play again without the need for updating the script. This function allows the script to run without stopping in the middle.
This feature is very useful. It allows you to arrange and compose while listening to your creation in real time.
And when you get really good, you’ll be able to do stuff like this!
It’s a collaboration between electric guitar and Raspberry Pi! Amazing!
The newly written script plays without having to stop the performance, allowing for these types of sessions. The script is displayed on the screen, so if you have some knowledge of Sonic Pi, this is enjoyable on another level other than just listening.
Sonic Pi is a software that is enjoyable for everyone, regardless of skill level. You can use it to learn music while programming, or compose music while learning programming. People who can do both can dive right into full
-scal
e composition.
The
software allows you to experience the scripts you write as sounds instantly, which made learning the coding rules very easy. I really lost track of time using the “live_loop” feature, never missing a beat or losing focus.
Next time, we’ll have some long awaited fun with electronic components!
This is a new part that I got! I put it into the breadboard immediately, and I have been told that it’s a temperature sensor. But it doesn’t look like a thermometer at all. Can this thing really measure temperature?!
How do we connect it to Raspberry Pi? What kind of values does it take? What do those three legs do?
Just looking at it brings so many questions! I have a feeling that this next article is going to be a tough one!