Sound Programming

Much can be programmed, and that includes sound. In the digital world, sound is typically represented by sequences of about 90 kB per second, so “printing” sound is merely a matter of printing bytes. As such, any general purpose language can be used to generate sounds.

However, it’s boring to create a program that does nothing but print bytes, and it’s potentially difficult to make those bytes sound nice; we want abstractions to simplify matters for us: instruments, drums, musical notes, and a high-level program structure. Many programming languages have good libraries that allow us to achieve just that, but to keep it simple we’ll focus on how to program sound in two languages designed to output sound: ChucK and Live-Sequencer.

Let’s create some sounds.

The square wave

We’ll start with ChucK and a small square wave program:

ChucK is an imperative language. Instructions on how to install and run it can be found on its website, along with other useful information. You can listen to the above sound here.

To do the same in Live-Sequencer, we must find a square wave “instrument” and use that.

Live-Sequencer differs from ChucK in that it is functional, but another major difference is that while ChucK (in general) generates raw sound bytes, Live-Sequencer generates so-called MIDI codes, which another program converts to the actual audio. Live-Sequencer has a couple of funky features such as highlighting which part of one’s program is played; read about it and how to install and run it at this wiki. You can listen to the above sound here.

Something more advanced

Let’s try to create a small piece of music which can be expressed easily in Live-Sequencer (listen here):

When you play the program from the Live-Sequencer GUI, the code in use is highlighted:

Highlighting of sound
Highlighting of sound

The same could be expressed in ChucK, but the comparison wouldn’t be fair. While Live-Sequencer is designed for describing melodies, ChucK’s purpose is sound synthesis, which is more general. We’ll create something more fitting of ChucK’s capabilities, while still focusing on the use of instruments (listen here):

Algorithmic composition

Why not have the computer generate the melody as well as the sound? That sounds like a great idea!

Enter L-systems. An L-system has an alphabet and a set of rules, where each rule is used to transform the symbol on the left-hand side to the sequence of symbols on the right-hand side. We’ll use this L-system to generate music:

If we evaluate a L-system on a list, the system’s rules are applied to each element in the list, and results are concatenated to make a new list. If we assign each symbol to a sequence of sounds and run the L-system a few times, we get this.

Using an L-system is one of many ways to take composition to a high level. L-systems can be used to generate fractals, which are nice.

And so on

Many abstractions in sound generation allow for fun sounds to happen. Interested people might want to also take a look at e.g. Euterpea, Pure Data, or Csound.

Originally published here.