Music, one of the original programming languages


Had a thought the other day. Music notation is a programming language.

A musical score is a set of sequential instructions. A performer reads them left to right, top to bottom, and executes each one in order. Pitch, duration, volume — all specified. The musician is the processor.

Think about it.

A time signature is a clock rate. It tells the performer how many beats per bar and which note gets the beat. That’s a tick speed. 4/4 is your 120Hz refresh rate.

Bar lines are delimiters. They chunk the instructions into digestible blocks. Same reason we use semicolons and line breaks in code — structure for the human reading it, not the machine.

Repeats, da capo, first and second endings — that’s branching logic. Conditional execution. “If first pass, play this. If second pass, skip to here.” Beethoven was writing if/else statements in 1824.

And a performer? They’re executing the program in real time. Reading ahead, managing tempo, handling dynamics. That’s a runtime.


Here’s the opening of Beethoven’s Ode to Joy. Public domain since forever.

Ode to Joy — score

E E F G. G F E D. C C D E. E dot D, D held.

Simple. Beautiful. A sequence of instructions.

Now here’s the same thing as a Sonic Pi script:

# Ode to Joy — Beethoven, 1824
# The original "program", now literally one

use_bpm 120
use_synth :piano

# Bar 1: E E F G
play :E4, release: 0.4
sleep 0.5
play :E4, release: 0.4
sleep 0.5
play :F4, release: 0.4
sleep 0.5
play :G4, release: 0.4
sleep 0.5

# Bar 2: G F E D
play :G4, release: 0.4
sleep 0.5
play :F4, release: 0.4
sleep 0.5
play :E4, release: 0.4
sleep 0.5
play :D4, release: 0.4
sleep 0.5

# Bar 3: C C D E
play :C4, release: 0.4
sleep 0.5
play :C4, release: 0.4
sleep 0.5
play :D4, release: 0.4
sleep 0.5
play :E4, release: 0.4
sleep 0.5

# Bar 4: E. D D
play :E4, release: 0.6
sleep 0.75              # dotted quarter
play :D4, release: 0.2
sleep 0.25              # eighth note
play :D4, release: 0.8
sleep 1                 # half note — held

The mapping is 1:1. Note name becomes a symbol. Duration becomes a sleep value. Tempo becomes use_bpm. Instrument becomes use_synth. Bar lines become comments — the machine doesn’t need them, but we do.

That play / sleep pattern is exactly what a performer’s brain is doing. Execute an action. Wait a precise duration. Execute the next one.


Here’s a nice side point. Jacquard’s programmable loom — the direct ancestor of modern computing — used punched cards. Those cards were inspired by the barrel mechanisms in music boxes and automated organs. Devices that were, in a real sense, reading and executing stored programs. Of music.

The lineage from musical automation to general-purpose computing is not a metaphor. It’s documented history.


One more thing. This one’s fun.

An octave has eight notes. C D E F G A B and back to C. Eight values. That’s octal. Base 8.

Computers already speak octal — it’s just a way of representing numbers using digits 0 through 7. ASCII characters have octal values. And if you map each octal digit to a note on the scale:

0 = C    4 = G
1 = D    5 = A
2 = E    6 = B
3 = F    7 = C'

…then every character in every program ever written can be played as a sequence of three notes.

So here’s print("hello world!") as music:

char  ASCII  octal   notes
───────────────────────────
 p     112    160    D B C
 r     114    162    D B E
 i     105    151    D A D
 n     110    156    D A B
 t     116    164    D B G
 (      40    050    C A C
 "      34    042    C G E
 h     104    150    D A C
 e     101    145    D G A
 l     108    154    D A G
 l     108    154    D A G
 o     111    157    D A C
        32    040    C G C
 w     119    167    D B C
 o     111    157    D A C
 r     114    162    D B E
 l     108    154    D A G
 d     100    144    D G G
 !      33    041    C G D
 "      34    042    C G E
 )      41    051    C A D

Sixty-three notes. That’s hello world as a melody.

Every program you’ve ever written has a song inside it. Most of them probably sound terrible. But they’re in there.


Music notation isn’t Turing complete. It doesn’t have variables or arbitrary state. Computer scientists will rightly point out it’s a notation system, not a programming language in the formal sense.

Fair.

But as a system for encoding complex instructions in a portable, reproducible, executable format — centuries before Babbage, before Lovelace, before anyone punched a card — music got there first.

Hat tip to whoever, wherever, first scratched a note onto a page and said “play this.” They wrote the first program.


Links:

  • Sonic Pi — free, runs on everything, perfect for this kind of thinking
  • Ode to Joy — public domain since 1824