Functional composition
Generating music with computers
An 8 a.m. music theory class with a mandatory attendance policy wasn't my idea of fun. Sometimes I'd joke that I could automate the whole thing. So I did.
The summer after my freshman year of college, I began translating the building blocks of Western harmony—notes, chords, and intervals—into Python code. I continued adding features throughout the school year, and by the time we got to Neo-Riemannian transformations, a program was checking my theory homework in seconds!
Though I didn't know it yet, I had the beginnings of my undergraduate thesis. Soon came my first serious endeavor in computer-generated music: rule-based generation of species counterpoint.
I. Gradus ad Parnassum
Species counterpoint was my first introduction to composition in college, so it seemed like a good place to start. The form is simple, designed for short composition exercises, and it follows a small set of rules. This means that, if we can model species counterpoint—and its rules—in a machine-readable format, even a naive, brute-force algorithm will suffice to generate music.
From beats to bytes
Counterpoint is inherently polyphonic—that is, it consists of multiple independent melodies played simultaneously. We can represent each melody as a sequence of events, either notes or rests, each with an associated duration. Notes contain pitch information (letter name, accidental, and octave number), while durations are represented as fractions of a whole note.
-- By representing pitch...
data Note = Note Letter Accidental Octave
data Letter = A | B | C | D | E | F | G
data Accidental = DoubleFlat | Flat | Natural | Sharp | DoubleSharp
type Octave = Integer
-- and time...
type Duration = Rational
-- we can encode a melody!
type Melody = [(Maybe Note, Duration)]
We can model each rule of species counterpoint as a function that takes a piece of (possibly unfinished) counterpoint as input and returns whether it follows the rule. Generating music is now a constraint satisfaction problem.
II. Giant Steps
Coming soon…