With Scratch we’ve learned how to operate under the logic of programming. The next step is to then use that within a programming language – the problem is that many of the available languages can look a little intimidating. This is where Sonic Pi comes in, offering a very simple language style that can ease you in to the basics of working with code.
It’s quite straightforward to use as – Sonic Pi allows you to choose from a small selection of instruments and select a tone to play with it. These can be turned into complex melodies using loops and threads and even some form of user input.
Step 01 Install Raspbian
If you’ve installed the latest version of Raspbian, Sonic
Pi will be included by default. If you’re still using a slightly older version, then you’ll need to install it via the repos. Do this with:
$ sudo apt-get install sonic-pi
Step 02 Get started with Sonic Pi
Sonic Pi is located in the Education category in the menus. Open it up and you’ll be presented with something that looks like an IDE. The pane on the left allows you to enter code, and then you can save and preview it as well. Any errors are displayed separately from the output.
Step 03 Your first note
Our first thing to try out with Sonic Pi is simply being able to play a note. Sonic Pi has a few defaults already pre-set, so we can get started with:
Press run and the output window should show you exactly what is happening.
Step 04 Set the beat
For any piece of music, you’ll probably want to set the beat. We can start by putting:
At the start of our code. We can then test this out by creating a string of midi notes using play_pattern.
Step 05 Advance your melody
We can start making some complex melodies by using more of Sonic Pi’s functions. You can change the note type by using with_synth, reverse a pattern, and even create a finite loop with the x.times function. ‘Do’ and ‘end’ signify the start and end of the loop.
Step 06 Play a concert
Using the in_thread function, we can create another thread for the Sonic Pi instance and have several lines of musical code play at once instead of in sequence. Here we’ve made it create a series of notes in a random sequence.
Full code listing
with_tempo 200 play_pattern [40,25,45,25,25,50,50] 2.times do with_synth “beep” play_pattern [40,25,45,25,25,50,50] play_pattern [40,25,45,25,25,50,50].reverse end play_pad “saws”, 3 in_thread do with_synth “fm” 6.times do if rand < 0.5 play 30 else play 50 end sleep 2 end end 2.times do play_synth “pretty_bell” play_pattern [40,25,45,25,25,50,50] play_pattern [40,25,45,25,25,50,50].reverse end