Making Music with Clojure and Overtone
This week I attended the London Clojure Dojo for the first time. It was a special edition focussed on making music using Overtone. A couple of weeks ago I was presenting a Clojure tutorial at the FPDay conference in Cambridge, and met Sam Aaron there. Sam is one of the main proponents and developers of Overtone, and he came to London to give us an introduction - then it was up to us to see if we could make any interesting music, or at least noises.
Overtone is a piece of software written in Clojure that provides a front-end to a synthesiser - we were using SuperCollider. By writing and evaluating bits of Clojure code, we can send commands to the synthesizer to create sounds. Sam explained how the components of SuperCollider work internally, with synths, busses and buffers, but when we came to try it, it seemed like people could be more creating using Overtone by working at a higher level of abstraction, talking about notes and chord progressions, rather than frequencies and waveforms. Music seems to fit quite naturally with levels of abstraction, from notes we build chords, or fill in bars, then build these into bars and phrases, and maybe use these to assemble a higher level form like a Viennese waltz or a twelve-bar blues.
It was interesting to see that the group was made up of some people who knew a lot about programming, but not so much about music, others who were musicians, but didn’t really know much about programming, and some who fell in between. Some people came from the perspective of having learned an instrument, others were more familiar with synthesis and creating sounds, and then perhaps using software like Cubase or GarageBand to sequence sounds into compositions.
We split into groups and played around with trying to make some music, then at the end we came back together for a show (and listen) and tell. It was pretty impressive what people could come up with in just over an hour, with little prior knowledge of the system.
Nicholas, who trained at the Royal College of Music but later studied computing and now works in a startup as a programmer, used Overtone to create a piece of music inspired by Steve Reich’s Piano Phase. He took a sequence of notes and generated a repeated cycle in the Clojure code. Then he played two voices of this sequence at the same time, one very gradually speeding up, creating phase shifts with the version playing at a constant tempo.
Marius and his group took some of the example code as a starting point, playing a dubstep track, but then hooked up an iPad app touchosc that allowed them to interact with the music in realtime by using sliders on the screen of the iPad. They used this to modulate the pitch of the music, and superimpose another tune over the top.
For our own efforts, we contentrated on the lower level end of things, looking at how to create instruments and control the synthesiser by sending it messages. We created sequences of frequencies (pitches) and times to change frequency. In this way we could slide notes up or down over time. We realised that to do this better, and better emulate something like a piano glissando, we needed to make the steps in frequency proportional to the current frequency, as the frequency of a note doubles with every octave rather than increasing linearly, but we didn’t have time to figure out the maths. Here’s a snippet:
(definst square-wave [freq 440] (square freq)) (def times (take 220 (iterate #(+ 30 %) 0))) (defn change-pitch [t f inst] (at (+ t (now)) (ctl inst :freq f))) (defn falling-pitches [start] (take (/ start 2) (iterate dec start))) (defn rising-pitches [start] (take start (iterate inc start))) (defn slide [pitches inst] (map (fn [x y] (change-pitch x y inst)) times pitches)) (square-wave) (slide (falling-pitches 440) square-wave) (slide (rising-pitches 220) square-wave) (stop)
Overall a very interesting and enjoyable evening working on something fun with some clever people. I certainly hope to return to the dojo and to try out some more things with Overtone.