Built-in Functions
These functions are available in every script without imports — they form the prelude. Functions from standard library modules (std/random, std/generative, std/signals, std/effects, std/instruments) need a use statement first.
Collections
Section titled “Collections”| Function | Returns | Meaning |
|---|---|---|
Array(...items) | Array | Array from arguments |
length(array) | Number | Element count |
slice(array, start, end) | Array | Sub-array [start, end) |
concat(a, b) | Array | New array with both arrays’ elements |
range(end) / range(start, end) | Array | Integers [start, end) |
These free functions mirror the array methods — length(a) and a.length() are equivalent.
Array(…items)
Section titled “Array(…items)”Build an array from its arguments — equivalent to the #[...] literal.
let a = Array(1, 2, 3); // same as #[1, 2, 3]length(array)
Section titled “length(array)”Return the number of elements.
let n = length(#[1, 2, 3]); // 3slice(array, start, end)
Section titled “slice(array, start, end)”Extract a sub-array from start (inclusive) to end (exclusive).
let mid = slice(#[10, 20, 30, 40], 1, 3); // #[20, 30]concat(a, b)
Section titled “concat(a, b)”Return a new array with the elements of both.
let all = concat(#[1, 2], #[3, 4]); // #[1, 2, 3, 4]range(end) / range(start, end)
Section titled “range(end) / range(start, end)”Generate consecutive integers — from 0 with one argument, from start with two.
let a = range(4); // #[0, 1, 2, 3]let b = range(2, 6); // #[2, 3, 4, 5]Higher-Order Functions
Section titled “Higher-Order Functions”| Function | Returns | Meaning |
|---|---|---|
map(array, fn) | Array | Transform each element |
filter(array, fn) | Array | Keep elements passing a predicate |
fold(array, init, fn) | Value | Reduce to a single value |
map(array, fn)
Section titled “map(array, fn)”Apply fn(element) to each element, collecting results into a new array.
let doubled = map(#[1, 2, 3], fn(x) { return x * 2; }); // #[2, 4, 6]filter(array, fn)
Section titled “filter(array, fn)”Keep only elements for which fn(element) returns a truthy value.
let evens = filter(#[1, 2, 3, 4], fn(x) { return x % 2 == 0; }); // #[2, 4]fold(array, init, fn)
Section titled “fold(array, init, fn)”Reduce the array to a single value with fn(accumulator, element), starting from init.
let sum = fold(#[1, 2, 3], 0, fn(acc, x) { return acc + x; }); // 6Patterns & Rhythm
Section titled “Patterns & Rhythm”| Function | Returns | Meaning |
|---|---|---|
Pattern(...notes) / Sequence(...notes) | Pattern | Sequential pattern |
Stack(...notes) | Pattern | Simultaneous (layered) pattern |
Alternating(...elements) | Pattern | One element per cycle |
euclid(hits, steps, pattern [, rotation]) | Pattern | Euclidean rhythm |
viz(pattern [, cycles] [, start_cycle]) | NUL | ASCII pattern visualization |
Pattern(…notes) / Sequence(…notes)
Section titled “Pattern(…notes) / Sequence(…notes)”Build a sequential pattern from arguments — the function form of [C4 D4 E4]. Sequence is an alias.
let melody = Sequence(C4, D4, E4); // same as [C4 D4 E4]Stack(…notes)
Section titled “Stack(…notes)”Build a simultaneous pattern — all elements play at once, like [C4, E4, G4].
let chord = Stack(C4, E4, G4);Alternating(…elements)
Section titled “Alternating(…elements)”Build a pattern that plays one element per cycle, like <C4 D4 E4>.
let alt = Alternating(C4, D4, E4);euclid(hits, steps, pattern [, rotation])
Section titled “euclid(hits, steps, pattern [, rotation])”Distribute hits onsets as evenly as possible over steps slots (Bjorklund algorithm), playing pattern at each hit. The optional rotation shifts the result. Also available as a pattern method.
let tresillo = euclid(3, 8, [C2]);let rotated = euclid(3, 8, [C2], 2);viz(pattern [, cycles] [, start_cycle])
Section titled “viz(pattern [, cycles] [, start_cycle])”Print an ASCII visualization — by default 4 cycles starting at cycle 0.
viz([C4 D4 E4].euclid(3, 8));viz([C4 D4], 8); // 8 cyclesMusic Theory
Section titled “Music Theory”| Function | Returns | Meaning |
|---|---|---|
Scale(name_or_intervals) | Scale | Scale from a name or semitone array |
Key(root, name_or_scale) | Key | Key for quantization and degrees |
scale(root, name_or_scale) | Array | Notes of a scale from a root |
scale_degree(root, name_or_scale, degree) | Note | Single scale degree (1-indexed) |
chord(root, name [, inversion]) | Array | Notes of a chord |
interval(note, name) | Note | Note at a named interval |
scale_names() | Array | Available scale names |
chord_names() | Array | Available chord names |
load_scala(name) | Scale | Scale from a Scala tuning file |
Scale(name_or_intervals)
Section titled “Scale(name_or_intervals)”Create a scale from a built-in name or a custom semitone interval array. Scale values have their own methods (name(), intervals(), degree(n), mode(n), contains(n), length(), period()).
let major = Scale("major");let custom = Scale(#[0, 2, 4, 5, 7, 9, 11]);let dorian = major.mode(2);Key(root, name_or_scale)
Section titled “Key(root, name_or_scale)”Create a key from a root note and a scale name or Scale value — used with in_key/quantize on patterns.
let k = Key(C4, "minor");let tune = [0 2 4 7].in_key(k);scale(root, name_or_scale)
Section titled “scale(root, name_or_scale)”Return the notes of a scale built on a root.
let notes = scale(C4, "major"); // #[C4, D4, E4, F4, G4, A4, B4]scale_degree(root, name_or_scale, degree)
Section titled “scale_degree(root, name_or_scale, degree)”Return a single scale degree (1-indexed).
let fifth = scale_degree(C4, "major", 5); // G4chord(root, name [, inversion])
Section titled “chord(root, name [, inversion])”Return the notes of a chord. Positive inversions move the lowest note up an octave; negative ones move the highest note down.
let m7 = chord(C4, "min7"); // #[C4, Eb4, G4, Bb4]let first = chord(C4, "major", 1); // #[E4, G4, C5]interval(note, name)
Section titled “interval(note, name)”Return the note at a named interval above a note.
let g = interval(C4, "P5"); // G4scale_names() / chord_names()
Section titled “scale_names() / chord_names()”List the available scale and chord names.
show(scale_names());show(chord_names());load_scala(name)
Section titled “load_scala(name)”Load a custom tuning from a Scala (.scl) file as a Scale.
let just = load_scala("just_intonation.scl");Scales: "major", "minor", "dorian", "phrygian", "lydian", "mixolydian", "locrian", "harmonic_minor", "melodic_minor", "pentatonic_major", "pentatonic_minor", "blues", "whole_tone", "chromatic". Aliases: "ionian" = major, "aeolian" = minor.
Chords: "major", "minor", "dim", "aug", "7", "maj7", "min7", "dim7", "aug7", "sus2", "sus4", "6", "min6", "9", "maj9", "min9", "add9", "power". Aliases: "maj" = major, "min" = minor, "dom7" = 7, "5" = power.
Intervals: "m2", "M2", "m3", "M3", "P4", "tritone", "P5", "m6", "M6", "m7", "M7", "P8". Aliases: "TT" = tritone, "oct" = P8.
Math & Timing
Section titled “Math & Timing”| Function | Returns | Meaning |
|---|---|---|
round(value [, places]) | Number | Round to decimal places (default 0) |
clock() | Number | Monotonic time in nanoseconds |
sleep(seconds) | NUL | Pause script execution |
setbpm(bpm [, beats_per_cycle]) | NUL | Set tempo (default 4 beats per cycle) |
round(value [, places])
Section titled “round(value [, places])”Round to the given number of decimal places.
let a = round(3.7); // 4let b = round(3.14159, 2); // 3.14clock()
Section titled “clock()”Return monotonic time in nanoseconds — useful for measuring elapsed time.
let start = clock();let elapsed = clock() - start;sleep(seconds)
Section titled “sleep(seconds)”Pause script execution. Patterns keep playing — only the script waits.
sleep(0.05);setbpm(bpm [, beats_per_cycle])
Section titled “setbpm(bpm [, beats_per_cycle])”Set the tempo. The second argument changes how many beats make up a cycle (default 4).
setbpm(120);setbpm(90, 3); // 90 BPM in 3/4 timeIntrospection
Section titled “Introspection”| Function | Returns | Meaning |
|---|---|---|
show(value) | NUL | Print detailed info about any value |
type(value) | String | Type name |
routing(...tracks) | NUL | Print the audio routing graph |
midi_routing() | NUL | Print MIDI ports, slots, and clock config |
show(value)
Section titled “show(value)”Print a detailed, human-readable description of any value.
show(#[1, 2, 3]);show([C4 D4 E4].fast(2));type(value)
Section titled “type(value)”Return the type name as a string.
let t = type(42); // "Number"let u = type(#[1, 2]); // "Array"routing(…tracks)
Section titled “routing(…tracks)”Print the audio routing graph — the whole graph with no arguments, or just the given tracks.
routing();midi_routing()
Section titled “midi_routing()”Print the MIDI routing configuration: connected ports, track slots, and clock settings.
midi_routing();Project Metadata
Section titled “Project Metadata”| Function | Returns | Meaning |
|---|---|---|
project_title(title) | NUL | Set the project title |
project_artist(artist) | NUL | Set the artist name |
project_bpm(bpm [, beats_per_cycle]) | NUL | Set the project tempo |
Project metadata names render output folders and tags exported files.
project_title("My Track");project_artist("Producer Name");project_bpm(140);Cycle Callbacks
Section titled “Cycle Callbacks”| Function | Returns | Meaning |
|---|---|---|
on_cycle(fn) | NUL | Run a function at the start of every cycle |
off_cycle() | NUL | Clear all cycle callbacks |
prime_cycles() | NUL | Fire callbacks for cycles 0 and 1 immediately |
on_cycle(fn)
Section titled “on_cycle(fn)”Register a callback that runs on the interpreter thread at the start of each cycle, receiving the cycle number. It has full interpreter access — assign patterns, call extensions, do I/O.
on_cycle(fn(cycle) { show(cycle);});off_cycle();off_cycle()
Section titled “off_cycle()”Remove all registered cycle callbacks.
prime_cycles()
Section titled “prime_cycles()”Fire the callbacks for cycles 0 and 1 synchronously, so patterns set inside on_cycle exist before playback starts.
on_cycle(fn(cycle) { show(cycle);});prime_cycles();off_cycle();Runtime
Section titled “Runtime”| Function | Returns | Meaning |
|---|---|---|
reload_ext(name) | NUL | Hot-reload a native extension |
reload_ext("my_extension"); // pick up a rebuilt native extensionTo clear the whole runtime to a clean state, use the RESET; transport statement.
Standard Library Modules
Section titled “Standard Library Modules”These functions need a use statement before they’re available.
std/random
Section titled “std/random”use "std/random" { rand, choose, choose_weighted, rand_true, choose_true };
let r = rand(0); // deterministic float [0, 1)let note = choose(1, #[C4, E4, G4]); // seeded picklet biased = choose_weighted(2, #[C4, G4], #[0.8, 0.2]);let t = rand_true(); // true random [0, 1)let any = choose_true(#[C4, E4, G4]); // true random pick| Function | Returns | Meaning |
|---|---|---|
rand(seed) | Number | Deterministic random in [0, 1) — same seed, same value |
choose(seed, array) | Value | Deterministic element pick |
choose_weighted(seed, array, weights) | Value | Deterministic weighted pick |
rand_true() | Number | Non-deterministic random in [0, 1) |
choose_true(array) | Value | Non-deterministic element pick |
The seeded variants always return the same result for the same seed, which keeps patterns seekable and reproducible — pass the cycle number as the seed for per-cycle variation.
std/generative
Section titled “std/generative”use "std/generative" { stream, markov };
let walk = stream(C4, fn(prev, cycle) { return prev.transpose(12);});
let chain = markov(2, #[ #[0.3, 0.7], #[0.6, 0.4]], 0);| Function | Returns | Meaning |
|---|---|---|
stream(initial, fn(prev, cycle)) | Pattern | Stateful pattern — each cycle derives from the previous value |
markov(num_states, transitions, initial) | Pattern | Markov chain over state indices, driven by a transition matrix |
Other modules
Section titled “Other modules”| Module | Exports | Reference |
|---|---|---|
std/signals | Sine, Saw, Tri, Square (+ *2 bipolar variants), Rand, Perlin, signal, signal_ramp, automation, Cc, Cc_learn, hz_to_periods, sec_to_periods | Signals |
std/effects | Effect, Delay, Lowpass, Highpass, Bandpass, Notch, Allpass, Peak, Reverb, PlateReverb, Overdrive, Distortion | Instruments & Effects |
std/instruments | Sample, Kit, Sampler, SamplerMelodic, Instrument, plugin_scan | Sampler & Sample, Instruments & Effects |
Audio, MIDI & Transport
Section titled “Audio, MIDI & Transport”These prelude functions are documented on their dedicated reference pages.
| Function | Meaning | Reference |
|---|---|---|
AudioTrack([name]) | Create an audio track | Track Methods |
Effect(name [, plugin_id]) / Instrument(name [, plugin_id]) | Load a CLAP plugin | Instruments & Effects |
plugin_scan() | Scan plugin directories | Instruments & Effects |
scope(signal [, label]) / scope_remove(signal) / scope_clear() | Oscilloscope display | Signals |
timeline(length) | Arrange patterns at cycle offsets | Transport |
render(cycles) / render(track, cycles) / render_master(cycles) | Render audio to WAV | Transport |
midi_export(pattern, cycles [, path]) | Export a pattern to a MIDI file | Transport |
MidiTrack(channel [, velocity] [, port_alias] [, name]) | Create a MIDI track | MIDI |
midi_connect / midi_disconnect / midi_ports / midi_connected | MIDI output connections | MIDI |
midi_input_connect / midi_input_disconnect / midi_input_ports | MIDI input connections | MIDI |
midi_clock_send / midi_clock_follow / midi_clock_status / midi_clock_offset / midi_delay | MIDI clock & timing | MIDI |
audio_devices / audio_output / audio_output_channels | Audio device selection & channel routing | Configuration |
audio_latency() / audio_cpu_load() | Engine latency and CPU info | Configuration |
recording_offset([ms]) | Recording latency compensation | Configuration |
See Also
Section titled “See Also”- Language Methods — methods on arrays, strings, dicts, and iterators
- Pattern Methods — methods on pattern values
- Mini-Notation Grammar — pattern literal syntax