dark mode light mode Search Menu

Algorithmic Music

Ben Gesoff on Flickr

What does programming have to do with writing music?

The two are closer than you might think! On a fundamental level, they’re both a kind of notation for writing instructions. In other words, sheet music is a kind of program for a musician—one where they translate notes on a page into actions with an instrument or their voice.

What if, though, sheet music is just one way of describing music? After all, how would you write instructions like this in sheet music?

  1. Take all chords in A-minor key.
  2. Play a random one of these chords for a random duration between 1 and 2 seconds.
  3. Every 4th iteration, arpeggiate the chord.
  4. Go to step 2.

Now, mind you, that’s not going to be a great song but the point is that it’s impossible to describe randomness and conditional decisions in sheet music.

Heck, it’s not even easy to play! Try picking up any instrument (except the piano since it’s far too heavy to lift) and start playing random notes and chords. It’s pretty hard. Humans are not good at coming up with even pseudo-random or conditional patterns on our own.

Is hope lost then? Can we not create infinitely changing music?

Well, what if we treat the computer not as a tool for the final mixing and editing of music but rather as the instrument itself?

This is where we enter the world of algorithmic music (or algomusic, for short). We can play lines of code like you’d pluck a string or toot a note.

Now, I don’t just mean “write a program and let the computer make the music,” although that’s cool too. I mean write lines of code and play them as you go, creating an actual live performance: live-coding if you will.

For the rest of this article, we’ll cover some of the major tools and ideas behind live-coding & algomusic and how to get started!

The story of algorithmic music is the story of how you choose to represent time. You need to describe repeating elements, simultaneous performances of multiple instruments, and the arrangement of parts in time.

There’s two major ways I see time represented in algomusic: the first is using loops and iteration where notes are explicitly sequenced, which is what tools like Sonic Pi use, and the other is by chaining together operations on patterns, which is the domain of tools like TidalCycles. Loosely speaking, Sonic Pi is like imperative programming — using statements and commands — while TidalCycles is like functional programming — evaluating inputs and outputs to change data.

Sonic Pi is probably one of the best known algomusic tools. It was specifically designed to work on devices like the Raspberry Pi and be easy to learn for folks who don’t know how to code or write music! When you write code in Sonic Pi you’re actually writing code in Ruby, a mostly easy to pick up programming language.

Here’s an example of a simple Sonic Pi composition of my own weird ambient music:

live_loop :heckbeat do
  with_fx :bitcrusher, bits: rrand_i(5,8)  do
	sample (ring :bd_haus, :bd_fat, :drum_bass_hard, :drum_bass_soft, :drum_heavy_kick, :drum_tom_hi_soft).choose
  sleep (ring 0.25, 0.25, 0.5).choose
live_loop :heckmelody do
  use_synth :prophet
  with_fx :echo, phase: 0.5, decay: 0.8  do
	play (scale :ab2, :locrian, num_octaves: 3).choose, amp: 0.3, release: 0.25
	sleep 0.25

You have two live_loops that run, each simultaneously playing different parts of the music. You have to describe, in order, what happens in each live_loop and how long to rest between notes in each loop.

On the other hand, in my favorite live-coding system, TidalCycles, treats time as a thing that constantly thumps away, with one cycle per second, and you describe patterns of what notes and how many are played in each cycle. To me this makes music feel a lot more like stanzas in sheet music, but where the more notes you cram in they’re automatically downgraded from wholes to quarters to eighths and so on to make the stanza fit in one cycle. You feed patterns through chains of operations and transformations and special effects that themselves have their own patterns for how they operate it.

Where to begin with algomusic? You might start with Gibber, a web browser live coding system, if you don’t want to download software.

So what should you do with TidalCycles and other algo music tools? Well, start by trying to make any music you like! Make little tunes, ambient techno, beats to rap over. Anything!

Once you get a little more confident, try making music live, practicing like it’s a performance.

Then? Try showing off and performing!

Learn More

Gibber: an in-browser live coding system






The book on the topic of Algorithmic Music