Audio programming with Sonic Pi!

Audio programming with Sonic Pi!

Launching Sonic Pi

Raspberry Pi Sonic Pi

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!

What is Sonic Pi?

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.

Launching Sonic Pi

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.

Launching Sonic Pi

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

Explanation of Sonic Pi interface

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.

Sonic Pi interface

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.

Sonic Pi interface

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.

Sonic Pi Interface

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.

Sonic Pi

Figure 6

Let’s play some sounds!

Ok, let’s get right into playing some sounds! I tried out the sample script from the tutorial on Raspberry Pi’s official website.

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.

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.

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.

use_synth

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.

use_synth

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.

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!

How to Arrange Sounds

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.

Sound Duration

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.

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.

When you want to choose from a set of values, use “choose”. When you know what values you want to set, use this option.

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.

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.

Let’s Play Some Chords

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.

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!

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.

Compose While Listening Using live_loop!

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.

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.

Summary

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!

Breadboard with temperature sensor

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!