Repository Public
1<!--2ORIX Sonata3File: Documentation/manual/algorithmic-composition.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Algorithmic composition89A Sonata program is executed completely when it is compiled: loops run, functions are called,10random numbers are drawn, and what remains is a fixed list of notes. That makes generative11music reproducible — the same source always gives the same piece — and safe: a program cannot12run for ever or consume unbounded memory.1314## Seeded randomness1516| Function | Result |17|---|---|18| `random()` | a number in [0, 1) |19| `random_int(lo, hi)` | an integer from `lo` to `hi`, both included |20| `choose(xs)` | one element of a list |21| `shuffle(xs)` | the list in a random order |2223All of them draw from a generator selected by `seed`. Change the seed to get another version24of the piece; keep it to get the same one, on any machine.2526```sonata27composition "Seeded" {28 tempo 120bpm29 key A minor30 seed 202631 instrument kalimba: Kalimba {32 let notes = scale(A4, pentatonic_minor)33 repeat 16 {34 play choose(notes) duration choose([1/8, 1/8, 1/4]) velocity 0.5 + random() * 0.435 }36 }37}38```3940Each track has its own stream of random numbers, derived from the seed and the position of the41track in the composition. Adding a note to one track does not change what another track draws.42To share one random result between tracks, compute it once at composition level:4344```sonata45fn random_row(notes: List<Pitch>, n: Number) -> List<Pitch> {46 let row = []47 for i in 0..n {48 row = row + [choose(notes)]49 }50 return row51}5253composition "Shared Row" {54 seed 955 // Evaluated once: both instruments get the same eight notes.56 let row = random_row(scale(D4, dorian), 8)5758 instrument flute: Flute {59 for n in row {60 play n + 12st duration 1/861 }62 }63 instrument cello: Cello {64 for n in row {65 play n - 12st duration 1/866 }67 }68}69```7071## Random walks and constraints7273`clamp` keeps a walk inside a range; a weighted choice is a list with repeated elements.7475```sonata76composition "Random Walk" {77 tempo 100bpm78 key C lydian79 seed 7780 instrument vibes: Vibraphone {81 let notes = scale_run(C4, lydian, 2)82 let index = 783 repeat 32 {84 // Small steps are three times as likely as leaps.85 index = clamp(index + choose([-1, -1, -1, 1, 1, 1, -3, 3]), 0, len(notes) - 1)86 play notes[index] duration 1/16 velocity 0.45 + random() * 0.387 }88 }89}90```9192## Rules and processes9394Anything computable can shape the music. A process that rotates a cell, a canon made by95delaying and transposing one line, a rhythm given by `euclid`:9697```sonata98pattern line(notes: List<Pitch>, step: Duration) {99 for n in notes {100 play n duration step101 }102}103104composition "Process" {105 tempo 132bpm106 let cell = [E4, G4, A4, B4, D5]107108 instrument first: Marimba {109 pan -0.4110 for shift in 0..5 {111 line(rotate(cell, shift), 1/8)112 }113 }114115 instrument second: Marimba {116 pan 0.4117 // The same process, one cell later and a fifth lower.118 rest 5/8119 transpose -7st {120 for shift in 0..4 {121 line(rotate(cell, shift), 1/8)122 }123 }124 }125}126```127128## Recursion129130Functions may call themselves. The depth of calls is limited to 64, which is plenty for131structures such as fractals and too little to hang:132133```sonata134// Each interval of `shape` is replaced by the whole shape placed on it.135fn grow(shape: List<Interval>, depth: Number) -> List<Interval> {136 if depth == 0 {137 return shape138 }139 let next = []140 for s in shape {141 for t in [0st, 7st, 3st] {142 next = next + [s + t]143 }144 }145 return grow(next, depth - 1)146}147148composition "Self-Similar" {149 tempo 120bpm150 instrument harp: Harp {151 for i in grow([0st, 7st, 3st], 2) {152 play C4 + i duration 1/16153 }154 }155}156```157158A recursion without an end is stopped with a located error:159160```sonata,error=Q6003161fn forever(n: Number) -> Number {162 return forever(n + 1)163}164165composition "Unbounded" {166 instrument piano: GrandPiano {167 play C4 velocity forever(0)168 }169}170```171172## Sonification173174Data in a list becomes music by mapping it: normalise the values, then choose what they175control — pitch, loudness, duration, position in the stereo field.176177```sonata178// Hourly temperatures, in degrees (an illustrative series).179const 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]180181composition "A Day of Temperature" {182 tempo 160bpm183 let lo = min(temperature)184 let hi = max(temperature)185 let notes = scale_run(C3, pentatonic_major, 3)186187 instrument data: Vibraphone {188 for t in temperature {189 let x = (t - lo) / (hi - lo)190 play notes[round(x * (len(notes) - 1))] duration 1/8 velocity lerp(0.4, 0.9, x)191 }192 }193}194```195196Two complete sonifications are in `Examples/`: `sonification.sonata` and197`tide_sonification.sonata`.198199## Limits200201The compiler stops a program that grows beyond these limits, with an error at the statement202that crossed the line. Nothing hangs and nothing is truncated silently.203204| Limit | Value | Code |205|---|---|---|206| Notes in a composition | 1 000 000 | Q6001 |207| Evaluation steps | 10 000 000 | Q6002 |208| Depth of calls | 64 | Q6003 |209| Length of a list | 100 000 | Q6004 |210| Tracks | 256 | Q6006 |211| Nodes in a patch graph | 4 096 | Q6007 |212213There is no `while`: every loop has a bound known when it starts.214