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.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s