Algorithmic composition
A Sonata program is executed completely when it is compiled: loops run, functions are called, random numbers are drawn, and what remains is a fixed list of notes. That makes generative music reproducible — the same source always gives the same piece — and safe: a program cannot run for ever or consume unbounded memory.
Seeded randomness
| Function | Result |
|---|---|
random() |
a number in [0, 1) |
random_int(lo, hi) |
an integer from lo to hi, both included |
choose(xs) |
one element of a list |
shuffle(xs) |
the list in a random order |
All of them draw from a generator selected by seed. Change the seed to get another version
of the piece; keep it to get the same one, on any machine.
composition "Seeded" {
tempo 120bpm
key A minor
seed 2026
instrument kalimba: Kalimba {
let notes = scale(A4, pentatonic_minor)
repeat 16 {
play choose(notes) duration choose([1/8, 1/8, 1/4]) velocity 0.5 + random() * 0.4
}
}
}Each track has its own stream of random numbers, derived from the seed and the position of the track in the composition. Adding a note to one track does not change what another track draws. To share one random result between tracks, compute it once at composition level:
fn random_row(notes: List<Pitch>, n: Number) -> List<Pitch> {
let row = []
for i in 0..n {
row = row + [choose(notes)]
}
return row
}
composition "Shared Row" {
seed 9
// Evaluated once: both instruments get the same eight notes.
let row = random_row(scale(D4, dorian), 8)
instrument flute: Flute {
for n in row {
play n + 12st duration 1/8
}
}
instrument cello: Cello {
for n in row {
play n - 12st duration 1/8
}
}
}Random walks and constraints
clamp keeps a walk inside a range; a weighted choice is a list with repeated elements.
composition "Random Walk" {
tempo 100bpm
key C lydian
seed 77
instrument vibes: Vibraphone {
let notes = scale_run(C4, lydian, 2)
let index = 7
repeat 32 {
// Small steps are three times as likely as leaps.
index = clamp(index + choose([-1, -1, -1, 1, 1, 1, -3, 3]), 0, len(notes) - 1)
play notes[index] duration 1/16 velocity 0.45 + random() * 0.3
}
}
}Rules and processes
Anything computable can shape the music. A process that rotates a cell, a canon made by
delaying and transposing one line, a rhythm given by euclid:
pattern line(notes: List<Pitch>, step: Duration) {
for n in notes {
play n duration step
}
}
composition "Process" {
tempo 132bpm
let cell = [E4, G4, A4, B4, D5]
instrument first: Marimba {
pan -0.4
for shift in 0..5 {
line(rotate(cell, shift), 1/8)
}
}
instrument second: Marimba {
pan 0.4
// The same process, one cell later and a fifth lower.
rest 5/8
transpose -7st {
for shift in 0..4 {
line(rotate(cell, shift), 1/8)
}
}
}
}Recursion
Functions may call themselves. The depth of calls is limited to 64, which is plenty for structures such as fractals and too little to hang:
// Each interval of `shape` is replaced by the whole shape placed on it.
fn grow(shape: List<Interval>, depth: Number) -> List<Interval> {
if depth == 0 {
return shape
}
let next = []
for s in shape {
for t in [0st, 7st, 3st] {
next = next + [s + t]
}
}
return grow(next, depth - 1)
}
composition "Self-Similar" {
tempo 120bpm
instrument harp: Harp {
for i in grow([0st, 7st, 3st], 2) {
play C4 + i duration 1/16
}
}
}A recursion without an end is stopped with a located error:
fn forever(n: Number) -> Number {
return forever(n + 1)
}
composition "Unbounded" {
instrument piano: GrandPiano {
play C4 velocity forever(0)
}
}Sonification
Data in a list becomes music by mapping it: normalise the values, then choose what they control — pitch, loudness, duration, position in the stereo field.
// Hourly temperatures, in degrees (an illustrative series).
const temperature = [4, 3, 3, 2, 2, 3, 5, 8, 11, 14, 16, 18, 19, 19, 18, 16, 13, 11, 9, 8, 7, 6, 5, 4]
composition "A Day of Temperature" {
tempo 160bpm
let lo = min(temperature)
let hi = max(temperature)
let notes = scale_run(C3, pentatonic_major, 3)
instrument data: Vibraphone {
for t in temperature {
let x = (t - lo) / (hi - lo)
play notes[round(x * (len(notes) - 1))] duration 1/8 velocity lerp(0.4, 0.9, x)
}
}
}Two complete sonifications are in Examples/: sonification.sonata and
tide_sonification.sonata.
Limits
The compiler stops a program that grows beyond these limits, with an error at the statement that crossed the line. Nothing hangs and nothing is truncated silently.
| Limit | Value | Code |
|---|---|---|
| Notes in a composition | 1 000 000 | Q6001 |
| Evaluation steps | 10 000 000 | Q6002 |
| Depth of calls | 64 | Q6003 |
| Length of a list | 100 000 | Q6004 |
| Tracks | 256 | Q6006 |
| Nodes in a patch graph | 4 096 | Q6007 |
There is no while: every loop has a bound known when it starts.