Sonic-Pi 2 : Live coding example

I mentioned in my last post that one area of use for which Sonic-Pi 2 is geared up is live coding. This is a process which enables you to start a loop running that contains a call to a defined function containing some Sonic-Pi code. You then comment out the loop, change the code contained in the defined function and click the Run button again. This redefines the code in the defined function, but the original loop is still running, and so what you hear alters. Using this technique, you can alter what you are producing in real time.

A very simplistic example is this:

define :foo do
play 60
sleep 0.25

loop {foo}

If you run this program you will hear a note repeating every 0.25 seconds. Then put  # at the beginning of the loop line and change the second line from play 60 to play 72
Now press Run again and the pulsing note will continue, but changed to a higher pitch. You have altered what you hear by changing the coding live.

The link below takes you to an mp3 file playing a live performance recorded from Sonic Pi running a similar but more complex defined function. You will hear the music altering as the piece progresses. This is achieved by altering the defined function as described above. Once again I will not publish the code at the moment (which runs on a pre-release version), as it may have to alter when Sonic-Pi 2 is released in the not too distant future. Once that has happened, I will publish this code and the code for the other pieces in the previous post, once I have made any modifications necessary for them to work.


Sonic-Pi 2 Performs


I have today published on youtube a performance compilation of 18 pieces which I have developed for Sonic Pi 2 (beta). You can view it here

6 pieces by Bach:
Minuet in G
2 Part Invention no 8
Trio 1, II and Polacca from
Brandenburg no 1
Prelude in C Major

Funeral Music for Queen Mary
by Henry Purcell

A Jigge Ye Firste
by Richard Reade

Coranta by William Brade

John South His Galiard,
Earl of Essex Galiard
by John Dowland

Revecy venire du printans
by Claude Le Jeune

Pastoral Symphony from
Handel’s Messiah

3 Beatles Songs:
Eleanor Rigby
Penny Lane and
When I’m sixty-four

Maple Leaf Rag by Scott Joplin

Puppet on a String
(Sandie Shaw arranged
by Alan Simmons)

A few of them started life as pieces on the original Sonic Pi, but the way they were written was very different, because of the severe limitation that version has on synchronising threads together. So all of these pieces have been developed over the last couple of months. They mostly use the same technique that I have developed for playing polyphonic music on Sonic Pi. Separate arrays hold the information the pitches of the notes for each part, and for their durations. For ease these are entered respectively using symbolic notation like :c4, :fs5, :gb3 (C 9octave 4), Fsharp octave 5, Gflat octave 3) for the pitches and variables I have defined such as q, sq, cd (quaver, semi-quaver and dotted-crotchet) for their durations. In a couple of pieces I add dynamic volumes for each note with a separate array using variables I have defined such as p, mp, mf and ff (piano, mezzo-piano, mezzo-forte and double-forte). These enable me to add crescendos and diminuendos to the music. In other pieces I set the overall volume for each section, altering them to provide colour. The other area that needs attention is the choice of synthesiser and the ASR (Attack, Sustain, Release) envelope associated with each note.

A couple of pieces have percussion parts: The Funeral Music for Queen Mary by Henry Purcell has a tenor drum, and A Jigge-Ye-Firste by Richard Reade has a sort of tabard accompaniment. I recorded my own sample notes for these, and developed a way to play them at the appropriate times alongside the notes being played.

Several pieces also feature ornaments in the form of trills. Initially I wrote these out note for note, but then I developed functions in Ruby (the native language in which Sonic Pi is written to make it easier to do. Likewise over the duration of writing these pieces I developed techniques to enable rits or changes in tempo to be catered for – not easy when the rit takes place over the duration of a trill ! You see this to good effect at the end of the first section of the Purcell piece. This also makes use of the pan attribute for a note which enables you to place it in the stereo spectrum. When the brass starts playing antiphonal music (two sections answering each other) I place them on the left and right to enhance the effect. If you have good stereo speakers you should be able to detect this.
Another useful feature I often use is to add a little reverberation using the with_fx reverb: command. This can “lift” the piece making it more interesting to hear.

You may wonder at the choice of repertoire. It is partly because I like Bach and early music, but also because these genre lend themselves very well to Sonic Pi. The pieces all have a strong rhythm, and are perhaps less dependent on frequent crescendoes and diminuendos or variations in tempo as might be the case in a romantic symphony. Such ways of colouring the music are not easy to achieve with Sonic Pi. Also, Sonic Pi will begin to show the strain on a Raspberry-Pi once you get beyond 4 or 5 note threads playing at the same time.

There is one major new use of Sonic-Pi 2 NOT included in these pieces. That is the use of Live Coding, whereby you can start a piece running in a loop, and then isolate that loop, leaving it running, but redefine the code within the loop, so that you produce real time changes in the music as it plays. I have experimented with this, and hope to add further offerings using this technique in the future.

In the meantime I hope that you enjoy the pieces in Sonic-Pi 2 Performs. I shall shortly be starting a series of video tutorials on youtube, which will explore some of the techniques of writing for Sonic Pi, and which will also in later videos feature the code of some of these pieces.

A zip file of the individual sound tracks can be downloaded here