Repository Public
1<!--2ORIX Sonata3File: Documentation/manual/functions-and-patterns.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Functions and patterns89Two kinds of reusable code exist. A **function** computes a value and never makes a sound. A10**pattern** is a reusable piece of a track: it plays notes at the cursor of whoever calls it.1112## Variables1314`let` declares a variable; `const` a value that cannot change. Inside a block a `let` variable15can be reassigned with `=`. At the top level of a file both are constants.1617```sonata18const root = D31920composition "Variables" {21 let step = 1/822 instrument harp: Harp {23 let current = root24 repeat 6 {25 play current duration step26 current = current + 3st27 }28 }29}30```3132A composition's own variables are visible in its tracks. Reassigning a `const` is an error:3334```sonata,error=S200735composition "Constant" {36 instrument harp: Harp {37 const p = C438 p = D439 play p40 }41}42```4344## Functions4546```sonata47// The pitch `steps` scale steps above `from` in a scale given as a list.48fn step_up(notes: List<Pitch>, from: Number, steps: Number) -> Pitch {49 let index = from + steps50 let octaves = floor(index / len(notes))51 return notes[index % len(notes)] + 12st * octaves52}5354fn accent_on(beat: Number, strong: Number) -> Velocity {55 if beat % strong == 0 {56 return 0.957 }58 return 0.5559}6061composition "Functions" {62 key G major63 instrument marimba: Marimba {64 let notes = scale(G3, major)65 for i in 0..12 {66 play step_up(notes, i, 2) duration 1/8 velocity accent_on(i, 3)67 }68 }69}70```7172* Parameters are typed; the return type follows `->`.73* A parameter can have a default (`step: Duration = 1/8`). Arguments are given by position,74 then by name: `f(1, step: 1/16)`.75* A function body may contain `let`, assignment, `if`, `for`, `repeat`, `return` and calls. It76 cannot play notes: functions are pure, so calling one never changes the music by surprise.77* Functions see their parameters and top-level definitions, not the variables of the place78 they are called from.7980`if` exists as a statement and as an expression (`if c then a else b`):8182```sonata,fragment83for i in 0..8 {84 let loud = if i % 4 == 0 then 0.9 else 0.585 if i == 7 {86 play C5 duration 1/8 velocity loud87 } else if i % 2 == 0 {88 play C4 duration 1/8 velocity loud89 } else {90 play G4 duration 1/8 velocity loud91 }92}93```9495## Patterns9697```sonata98pattern alberti(c: List<Pitch>, times: Number = 2, step: Duration = 1/16) {99 repeat times {100 play c[0] duration step101 play c[2] duration step102 play c[1] duration step103 play c[2] duration step104 }105}106107composition "Patterns" {108 tempo 108bpm109 instrument left: GrandPiano {110 gain -6dB111 alberti(chord(C3, major))112 alberti(chord(F3, major))113 alberti(chord(G3, dom7), times: 1)114 alberti(chord(G2, dom7), times: 1, step: 1/16)115 chord chord(C3, major) duration 1/2116 }117}118```119120Calling a pattern runs its body at the caller's cursor and advances it by everything the121pattern played. A pattern can call other patterns and functions. Transforms set inside a122pattern (`transpose`, `swing`) do not leak out of it.123124A pattern called inside `transpose`, `tuplet` or `at` is transformed like any other125statement:126127```sonata128pattern turn(p: Pitch) {129 play p duration 1/8130 play p + 2st duration 1/8131 play p duration 1/8132 play p - 1st duration 1/8133}134135composition "Transformed Patterns" {136 instrument flute: Flute {137 turn(D5)138 transpose 7st {139 turn(D5)140 }141 tuplet 3 in 2 {142 turn(D5)143 turn(F5)144 turn(A5)145 }146 }147}148```149150## Lists151152Lists are the main data structure. They hold values of one kind.153154| Function | Result |155|---|---|156| `len(xs)`, `sum(xs)`, `contains(xs, x)` | size, sum, membership |157| `xs[i]`, `xs.first`, `xs.last` | elements; `xs[-1]` is the last |158| `xs + ys`, `concat(xs, ys)` | concatenation |159| `reverse(xs)`, `rotate(xs, n)` | reordered copies |160| `take(xs, n)`, `drop(xs, n)` | the first `n`, all but the first `n` |161| `cycle(xs, n)` | `n` elements, repeating the list as needed |162| `min(…)`, `max(…)` | of several values or of a list |163| `0..n`, `a..=b` | integer ranges |164165Lists are values: `xs = xs + [x]` makes a new list, nothing is modified in place.166167```sonata168composition "Lists" {169 instrument vibes: Vibraphone {170 let row = [C4, D4, F4, G4, Bb4]171 for n in row + reverse(drop(row, 1)) {172 play n duration 1/8173 }174 for n in cycle(rotate(row, 2), 8) {175 play n + 12st duration 1/16176 }177 }178}179```180181An index outside the list is an error at the place it happens (T3008), not a silent wrap.182183## Numbers184185`floor`, `ceil`, `round`, `abs`, `sqrt`, `pow`, `sin`, `cos`, `exp`, `ln`, `clamp(x, lo, hi)`186and `lerp(a, b, t)` are built in, with the constants `pi` and `tau`. Angles are in radians.187188```sonata189composition "A Sine of Velocities" {190 instrument piano: GrandPiano {191 for i in 0..16 {192 // One slow swell over sixteen notes.193 play C4 + (i % 4) * 3st duration 1/16 velocity lerp(0.3, 0.95, sin(pi * i / 16))194 }195 }196}197```198199The complete list of built-in functions is in section 10 of the language specification.200