beanz Magazine

Algorithmic Music

Ben Gesoff on Flickr

Some digital tools to help you create your own unique, ever-changing symphony with nothing but some code and a computer!

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
  end
  sleep (ring 0.25, 0.25, 0.5).choose
end
 
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
  end

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

https://gibber.cc/

Supercollider

https://supercollider.github.io/

FoxDot

https://foxdot.org/

The book on the topic of Algorithmic Music

https://www.oxfordhandbooks.com/view/10.1093/oxfordhb/9780190226992.001.0001/oxfordhb-9780190226992

Also In The December 2019 Issue

30+ ideas for STEAM-theme gifts for kids of all ages!

Visual storytelling apps are a great way for kids to document and explore their lives.

Meet 16-year old Astronaut StarBright, whose activism is inspiring the next generation of STEM fans.

Dive into the first “console wars” and learn how more bits led to bigger and better games.

From the start of computing history, people have tried to optimize the software programming process. This includes having two coders work together to code software.

Explore the solar system and test your knowledge of space through this fun coding activity.

Learn how procedural generation can be used to create infinite maps, music, and worlds to explore.

The Wayback Machine lets you travel back in time to see old websites. Plus the Internet Archive has thousands of vintage games, software, books, and more.

Online research skills are critical for software programmers. It's how you learn any language, by searching for error messages and looking up reference material.

How rural America connected itself to the phone grid using barbed wire, glass bottles, and even corncobs!

Meet Cozmo, the clever new robot that’s bringing AI concepts to life for kids as young as 5-7 years old.

Throw some festive ornaments on a virtual Christmas Tree in this fun introduction to functional programming.

How the Internet of Things could improve education, from VR to accessibility to facial recognition.

No one wants to deal with viruses over the holidays. Here’s how to protect your new devices!

Some digital tools to help you create your own unique, ever-changing symphony with nothing but some code and a computer!

Take a peek into the importance — and the struggle — of getting truly random data.

Interesting stories about computer science, software programming, and technology for December 2019.

Links from the bottom of all the December 2019 articles, collected in one place for you to print, share, or bookmark.