In March, members of the Which? Digital engineering team were given the opportunity to attend BathRuby 2016. BathRuby is an annual conference focusing on everything Ruby, with talks from a wide array of industry professionals. This year, one talk in particular piqued my interest as both a Ruby developer and a musician. This was Xavier Riley’s talk, titled “Rocking out in Ruby – A playful introduction to Sonic Pi”.
Sonic Pi is a simple programmable synth, which interprets Ruby code in order to produce sound and, with the right code, music. The software is available on most major platforms and is developed by Sam Aaron and a handful of contributing developers. The purpose of the project is to provide access to both music and programming to everyone, although it’s main audience is in the classroom, with a curriculum available on their website.
It is a free to use musical instrument, which can be mastered without extensive knowledge of musical theory and you can start with as little as a single line of code. If you already have a computer you can start right now. If not, a £30 Raspberry Pi is all you need to start, making this truly accessible to anybody. There is also a version of the software that can be launched from a USB storage device, with no installation necessary, so you can use it on a public computer.
The simplest way to make a sound using Sonic Pi is to use the play command followed by a colon, letter and number:
play :c4
The letter represents the note you want to play (a-g) and the number is the octave (a higher number equals a higher note). The play command can also interpret midi notes, which are represented by a simple number. For example, C4 can be represented using 60. Add additional play commands and Sonic Pi will play a chord, so for a simple C Major chord:
play :c4 play :e4 play :g4
If you want to play a number of sequential notes, you can use the sleep command followed by a number (in seconds) to leave a gap between the notes. With these two commands alone, you can construct a tune of your own or recreate almost any simple song.
There are many other commands available within Sonic Pi, which range from fine-tuning the sounds being produced to structural concepts, such as loops. The latter becomes very useful when it comes to coding quickly or even live coding.
A quicker way to program sequential notes is to use the ‘play_pattern_timed’ function, which uses beats rather than time with a comma separating each beat. This command still uses the same formats for notes, but now nil is used for rests rather than the sleep command. This function can be encapsulated inside a function to allow it to be referenced and played from elsewhere in your code.
Several phrases can be played simultaneously by separating each in an ‘in_thread’ block. This is great if you want the synth to play multiple parts at the same time, such as a melody over a drum track. This function utilises the Ruby Thread class in order to implement concurrent processes, which perfectly mimics multiple musicians playing together at the same time.
It’s possible to loop and repeat certain musical phrases a number of times in order to structure your tune. The following example of “Twinkle Twinkle Little Star” uses many of these commands and at 16 lines, it’s a great introduction to slightly more complex concepts in the software.
use_bpm 100 use_synth :pulse use_synth_defaults release: 0.2, mod_rate: 5, amp: 0.6 1.times { part_a } 2.times { part_b } 1.times { part_a } define :part_a do play_pattern_timed([:c4,nil,:c4,nil,:g4,nil,:g4,nil,:a4, nil,:a4,nil,:g4,nil,nil,nil,:f4,nil, :f4,nil,:e4,nil,:e4,nil,:d4,nil,:d4, nil,:c4,nil,nil,nil], [0.25]) end define :part_b do play_pattern_timed([:g4,nil,:g4,nil,:f4,nil,:f4,nil,:e4, nil,:e4,nil,:d4,nil,nil,nil], [0.25]) end
This code results in the following audio being output from Sonic Pi:
This example shows that Sonic Pi can interpret functions, which can include musical phrases. This makes it possible to include many different phrases to form a kind of library for use within the software. This becomes especially useful if you plan on live coding.
Sonic Pi allows for many “live loops” to be synchronised and played alongside each other. These special loop functions can be edited whilst the synth is running and will incorporate the new code when the run button is next pressed. This extends the usefulness of Sonic Pi from bedroom use to the live environment and I’m sure with enough mastery, this could replace a synth or keyboard in a band situation.
Here’s a great example of what can be achieved with live coding.
Sonic Pi is a great way to get into music production or the programming world. It’s also great fun if you already have knowledge of either or both. It’s free and easy to start, with a large number of resources and examples out there to get you on your way. There’s even an export feature so you can take your creation and import it into a Digital Audio Workstation to add to other instruments and develop your creations into full songs.
You can download Sonic Pi from their website.
Thanks for the kind words. You might be interested to know that live coding really is blossoming into a performance genre in its own right. Here’s a recent review from Rolling Stone where they talk about one of my live coding sets as “transcending the present”! http://www.rollingstone.com/music/live-reviews/moogfest-2016-was-it-actually-the-future-of-music-20160523
LikeLiked by 1 person
Hi Sam. I really enjoyed getting to grips with Sonic Pi and writing about it, so thank you for creating such a great tool (and an open source one at that!).
Also, thanks for sharing the Rolling Stone article. It’s great to see that live coding is getting more attention and becoming its own art form. Keep up the good work!
LikeLike