Coding Music with Sonic Pi
It’s that time of year again – time for the Articulate Hackathon! This year has been kind of hectic, so I didn’t actually decide what to work on for the weekend until the very start of the event.
The week prior, I was in Amsterdam for OSCON where I saw Sam Aaron demo Sonic Pi as a tool for live music coding. It was very impressive, as you can see for yourself. So with that fresh in mind, I thought, what the hey, I’ll play around with Sonic Pi!
Try to code a “song” with Sonic Pi.
Overall, I’m pretty happy with the little 2:08 minute track that I put together with Sonic Pi. It took some time figuring out the complexities of threading and how to keep timing on point across various “layers”. But in the end, I still didn’t hate listening to it.
Ultimately, the track includes:
- 20 Measures
- 5 FX Filters
- 9 Samples
- 4 Synths
The code is relatively straight forward. I’ve defined my sound components as methods at the top of the file, then defined additional methods which piece those components together to form layers, and then finally called my layer methods sequentially to compose the structure of the song.
My favorite components are “train”, “zawa”, “burp”, and “ghostly keys”. “train” was one of the first random things I put together while playing with chords and panning – I always like the sound of trains in music. “zawa” and “ghostly keys” experiment with randomization – choosing various chords, cutoffs, pans, and release & sleep durations. That said, randomization is not always random in Sonic Pi – if you execute my code on your local Sonic Pi setup, you’ll hear the exact same sounds that I hear. There are ways to create actual randomization. Finally, “burp” is a favorite component because it is literally a sample of a burp slowed way the hell down and pitch-shifted, sliced, echoed, and reverbed like whoa.
This was certainly a fun little project!
REGARDING SONIC PI
The app is a bit clunky – at least, if your a highly-functioning writer of code. It’s extremely basic, which is appropriate given it’s large target audience of teachers and students, but it can feel like a straightjacket to anyone proficient with hotkeys. Saving your code is tedious – you have to select a name and location every single time.
Working with Sonic Pi, the language, was fairly simple. There is no obvious file format, though the syntax is essentially Ruby-esque – including loops, conditionals, variables, functions, and threading (which allows you to play multiple sounds at once). Threading did not seem to work entirely as described, but I was able to figure it out enough to get the sounds I wanted.
The app includes a thorough documentation area including a fairly in-depth in-app tutorial; complete documentation of all synths, FX, samples, and built in methods; there is even a collection of example compositions ordered by level of complexity.
Sonic Pi has a fairly active repository with plenty of open PRs and issues, so I can only imagine it will keep improving from this point on.
Advertised as, “A free live coding synth for everyone,” I’d agree that anyone could figure it out with the help of such great documentation. You should give it a try!