## Cybernetic music

Is it okay to post art on a science blog? Well this is kind of science, so I guess it’s kind of okay.

Here is a litte piece of computer-generated music that I created yesterday:

As Twitter user @DanieleTatti noted, it sounds like a sort of Scottish raga. But what I wanted to post about was the algorithm used to generate that ever changing sequence of pitches and warbles. It’s quite a simple idea – simple enough in fact that the whole piece is generated by the following 140 characters of SuperCollider code:

```play{ar(r=RLPF,Saw.ar([200,302]).mean,5**(n={LFNoise1.kr(1/8)})*1e3,0.6)
+r.ar(Saw ar:Amplitude.kr(3**n*3e3*InFeedback.ar(0)+1,4,4),1e3)/5!2}```

Lately, I’ve been reading about cybernetics (specifically, I’m reading the really rather excellent book The Cybernetic Brain by Andrew Pickering), and it’s got me thinking about homeostasis and feedback-based control systems. This code defines a drone consisting of two filtered sawtooth oscillators at fixed frequencies, as well as another filtered sawtooth oscillator whose frequency can vary. (I’ll call this one “the oscillator.”) The oscillator’s frequency is chosen by listening to the output sound (that is, the drone combined with the oscillator’s output), and choosing its frequency based on the overall amplitude of this signal. The amplitude-following mechanism (which is built into SuperCollider) has a bit of a time lag in it, so what happens is that when the drone and the oscillator are out of phase, the oscillator’s frequency will decrease over time, whereas when they’re in phase (and hence louder), the frequency increases.

For reasons I have some intuitive understanding of but would have a hard time showing mathematically, this tends to result in the system falling into stable states where the frequency of the drone and the oscillator are in a nice integer ratio. The phenomenon is presumably closely related to oscillator entrainment, which I’ve also experimented with musically, but which does’t tend to sound as good for some reason.

The human perception of sound is such that we find integer ratios to sound consonant, particularly when the integers involved are small. For example, a frequency ratio of 3/2 is a perfect fifth, and Pythagoras’ discovery of this was in many ways the genesis of physics as a numerical science.

There are several sources of variation in this code, which prevent the oscillator from remaining in the same frequency all the time. The output of the amplitude tracker is multiplied by a slowly (pseudo-randomly) changing value before being sent to the oscillator’s frequency; but also the drone has some variation in it, and the system is sensitive to this as well. The drone has a slowly changing filter, and it consists of two tones at frequencies 200 and 302 Hz, which is slightly wider than a perfect fifth, so there’s a beat frequency of 2 Hz. Sometimes you can hear the oscillator doing a slow vibrato as it responds to these beat frequencies. It’s quite satisfying to listen to this piece knowing that the oscillator is “listening” to and responding to the sound of its output. This is very rarely the case with computer-generated music.

You can hear the oscillator’s frequency dropping to zero at the end. This is a response to me turning the volume down, but if I hadn’t done that the piece would have gone on forever.

It was quite a lot of work to fit this idea into 140 characters. I originally had a 200-ish character one that was a lot more complicated, but in the end I think I prefer this one.

As you might have guessed, creating 140-character pieces of music is a hobby of mine. I post them at unpredictable intervals on Twitter as @headcube. I’ve only recently started posting audio clips of them, so if you want to listen to the full back catalogue (or to hear more than two minutes of this piece), you’ll have to install SuperCollider. It’s available for every platform, and if you’re on linux you should be able to get it via your package manager.