Functions and patterns
Two kinds of reusable code exist. A function computes a value and never makes a sound. A pattern is a reusable piece of a track: it plays notes at the cursor of whoever calls it.
Variables
let declares a variable; const a value that cannot change. Inside a block a let variable
can be reassigned with =. At the top level of a file both are constants.
const root = D3
composition "Variables" {
let step = 1/8
instrument harp: Harp {
let current = root
repeat 6 {
play current duration step
current = current + 3st
}
}
}A composition's own variables are visible in its tracks. Reassigning a const is an error:
composition "Constant" {
instrument harp: Harp {
const p = C4
p = D4
play p
}
}Functions
// The pitch `steps` scale steps above `from` in a scale given as a list.
fn step_up(notes: List<Pitch>, from: Number, steps: Number) -> Pitch {
let index = from + steps
let octaves = floor(index / len(notes))
return notes[index % len(notes)] + 12st * octaves
}
fn accent_on(beat: Number, strong: Number) -> Velocity {
if beat % strong == 0 {
return 0.9
}
return 0.55
}
composition "Functions" {
key G major
instrument marimba: Marimba {
let notes = scale(G3, major)
for i in 0..12 {
play step_up(notes, i, 2) duration 1/8 velocity accent_on(i, 3)
}
}
}- Parameters are typed; the return type follows
->. - A parameter can have a default (
step: Duration = 1/8). Arguments are given by position, then by name:f(1, step: 1/16). - A function body may contain
let, assignment,if,for,repeat,returnand calls. It cannot play notes: functions are pure, so calling one never changes the music by surprise. - Functions see their parameters and top-level definitions, not the variables of the place they are called from.
if exists as a statement and as an expression (if c then a else b):
for i in 0..8 {
let loud = if i % 4 == 0 then 0.9 else 0.5
if i == 7 {
play C5 duration 1/8 velocity loud
} else if i % 2 == 0 {
play C4 duration 1/8 velocity loud
} else {
play G4 duration 1/8 velocity loud
}
}Patterns
pattern alberti(c: List<Pitch>, times: Number = 2, step: Duration = 1/16) {
repeat times {
play c[0] duration step
play c[2] duration step
play c[1] duration step
play c[2] duration step
}
}
composition "Patterns" {
tempo 108bpm
instrument left: GrandPiano {
gain -6dB
alberti(chord(C3, major))
alberti(chord(F3, major))
alberti(chord(G3, dom7), times: 1)
alberti(chord(G2, dom7), times: 1, step: 1/16)
chord chord(C3, major) duration 1/2
}
}Calling a pattern runs its body at the caller's cursor and advances it by everything the
pattern played. A pattern can call other patterns and functions. Transforms set inside a
pattern (transpose, swing) do not leak out of it.
A pattern called inside transpose, tuplet or at is transformed like any other
statement:
pattern turn(p: Pitch) {
play p duration 1/8
play p + 2st duration 1/8
play p duration 1/8
play p - 1st duration 1/8
}
composition "Transformed Patterns" {
instrument flute: Flute {
turn(D5)
transpose 7st {
turn(D5)
}
tuplet 3 in 2 {
turn(D5)
turn(F5)
turn(A5)
}
}
}Lists
Lists are the main data structure. They hold values of one kind.
| Function | Result |
|---|---|
len(xs), sum(xs), contains(xs, x) |
size, sum, membership |
xs[i], xs.first, xs.last |
elements; xs[-1] is the last |
xs + ys, concat(xs, ys) |
concatenation |
reverse(xs), rotate(xs, n) |
reordered copies |
take(xs, n), drop(xs, n) |
the first n, all but the first n |
cycle(xs, n) |
n elements, repeating the list as needed |
min(…), max(…) |
of several values or of a list |
0..n, a..=b |
integer ranges |
Lists are values: xs = xs + [x] makes a new list, nothing is modified in place.
composition "Lists" {
instrument vibes: Vibraphone {
let row = [C4, D4, F4, G4, Bb4]
for n in row + reverse(drop(row, 1)) {
play n duration 1/8
}
for n in cycle(rotate(row, 2), 8) {
play n + 12st duration 1/16
}
}
}An index outside the list is an error at the place it happens (T3008), not a silent wrap.
Numbers
floor, ceil, round, abs, sqrt, pow, sin, cos, exp, ln, clamp(x, lo, hi)
and lerp(a, b, t) are built in, with the constants pi and tau. Angles are in radians.
composition "A Sine of Velocities" {
instrument piano: GrandPiano {
for i in 0..16 {
// One slow swell over sixteen notes.
play C4 + (i % 4) * 3st duration 1/16 velocity lerp(0.3, 0.95, sin(pi * i / 16))
}
}
}The complete list of built-in functions is in section 10 of the language specification.