Skip to content

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.

FunctionReturnsMeaning
Array(...items)ArrayArray from arguments
length(array)NumberElement count
slice(array, start, end)ArraySub-array [start, end)
concat(a, b)ArrayNew array with both arrays’ elements
range(end) / range(start, end)ArrayIntegers [start, end)

These free functions mirror the array methodslength(a) and a.length() are equivalent.

Build an array from its arguments — equivalent to the #[...] literal.

let a = Array(1, 2, 3); // same as #[1, 2, 3]

Return the number of elements.

let n = length(#[1, 2, 3]); // 3

Extract a sub-array from start (inclusive) to end (exclusive).

let mid = slice(#[10, 20, 30, 40], 1, 3); // #[20, 30]

Return a new array with the elements of both.

let all = concat(#[1, 2], #[3, 4]); // #[1, 2, 3, 4]

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]
FunctionReturnsMeaning
map(array, fn)ArrayTransform each element
filter(array, fn)ArrayKeep elements passing a predicate
fold(array, init, fn)ValueReduce to a single value

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]

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]

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; }); // 6
FunctionReturnsMeaning
Pattern(...notes) / Sequence(...notes)PatternSequential pattern
Stack(...notes)PatternSimultaneous (layered) pattern
Alternating(...elements)PatternOne element per cycle
euclid(hits, steps, pattern [, rotation])PatternEuclidean rhythm
viz(pattern [, cycles] [, start_cycle])NULASCII pattern visualization

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]

Build a simultaneous pattern — all elements play at once, like [C4, E4, G4].

let chord = Stack(C4, E4, G4);

Build a pattern that plays one element per cycle, like <C4 D4 E4>.

let alt = Alternating(C4, D4, E4);

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);

Print an ASCII visualization — by default 4 cycles starting at cycle 0.

viz([C4 D4 E4].euclid(3, 8));
viz([C4 D4], 8); // 8 cycles
FunctionReturnsMeaning
Scale(name_or_intervals)ScaleScale from a name or semitone array
Key(root, name_or_scale)KeyKey for quantization and degrees
scale(root, name_or_scale)ArrayNotes of a scale from a root
scale_degree(root, name_or_scale, degree)NoteSingle scale degree (1-indexed)
chord(root, name [, inversion])ArrayNotes of a chord
interval(note, name)NoteNote at a named interval
scale_names()ArrayAvailable scale names
chord_names()ArrayAvailable chord names
load_scala(name)ScaleScale from a Scala tuning file

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);

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);

Return the notes of a scale built on a root.

let notes = scale(C4, "major"); // #[C4, D4, E4, F4, G4, A4, B4]

Return a single scale degree (1-indexed).

let fifth = scale_degree(C4, "major", 5); // G4

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]

Return the note at a named interval above a note.

let g = interval(C4, "P5"); // G4

List the available scale and chord names.

show(scale_names());
show(chord_names());

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.

FunctionReturnsMeaning
round(value [, places])NumberRound to decimal places (default 0)
clock()NumberMonotonic time in nanoseconds
sleep(seconds)NULPause script execution
setbpm(bpm [, beats_per_cycle])NULSet tempo (default 4 beats per cycle)

Round to the given number of decimal places.

let a = round(3.7); // 4
let b = round(3.14159, 2); // 3.14

Return monotonic time in nanoseconds — useful for measuring elapsed time.

let start = clock();
let elapsed = clock() - start;

Pause script execution. Patterns keep playing — only the script waits.

sleep(0.05);

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 time
FunctionReturnsMeaning
show(value)NULPrint detailed info about any value
type(value)StringType name
routing(...tracks)NULPrint the audio routing graph
midi_routing()NULPrint MIDI ports, slots, and clock config

Print a detailed, human-readable description of any value.

show(#[1, 2, 3]);
show([C4 D4 E4].fast(2));

Return the type name as a string.

let t = type(42); // "Number"
let u = type(#[1, 2]); // "Array"

Print the audio routing graph — the whole graph with no arguments, or just the given tracks.

routing();

Print the MIDI routing configuration: connected ports, track slots, and clock settings.

midi_routing();
FunctionReturnsMeaning
project_title(title)NULSet the project title
project_artist(artist)NULSet the artist name
project_bpm(bpm [, beats_per_cycle])NULSet the project tempo

Project metadata names render output folders and tags exported files.

project_title("My Track");
project_artist("Producer Name");
project_bpm(140);
FunctionReturnsMeaning
on_cycle(fn)NULRun a function at the start of every cycle
off_cycle()NULClear all cycle callbacks
prime_cycles()NULFire callbacks for cycles 0 and 1 immediately

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();

Remove all registered cycle callbacks.

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();
FunctionReturnsMeaning
reload_ext(name)NULHot-reload a native extension
reload_ext("my_extension"); // pick up a rebuilt native extension

To clear the whole runtime to a clean state, use the RESET; transport statement.

These functions need a use statement before they’re available.

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 pick
let 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
FunctionReturnsMeaning
rand(seed)NumberDeterministic random in [0, 1) — same seed, same value
choose(seed, array)ValueDeterministic element pick
choose_weighted(seed, array, weights)ValueDeterministic weighted pick
rand_true()NumberNon-deterministic random in [0, 1)
choose_true(array)ValueNon-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.

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);
FunctionReturnsMeaning
stream(initial, fn(prev, cycle))PatternStateful pattern — each cycle derives from the previous value
markov(num_states, transitions, initial)PatternMarkov chain over state indices, driven by a transition matrix
ModuleExportsReference
std/signalsSine, Saw, Tri, Square (+ *2 bipolar variants), Rand, Perlin, signal, signal_ramp, automation, Cc, Cc_learn, hz_to_periods, sec_to_periodsSignals
std/effectsEffect, Delay, Lowpass, Highpass, Bandpass, Notch, Allpass, Peak, Reverb, PlateReverb, Overdrive, DistortionInstruments & Effects
std/instrumentsSample, Kit, Sampler, SamplerMelodic, Instrument, plugin_scanSampler & Sample, Instruments & Effects

These prelude functions are documented on their dedicated reference pages.

FunctionMeaningReference
AudioTrack([name])Create an audio trackTrack Methods
Effect(name [, plugin_id]) / Instrument(name [, plugin_id])Load a CLAP pluginInstruments & Effects
plugin_scan()Scan plugin directoriesInstruments & Effects
scope(signal [, label]) / scope_remove(signal) / scope_clear()Oscilloscope displaySignals
timeline(length)Arrange patterns at cycle offsetsTransport
render(cycles) / render(track, cycles) / render_master(cycles)Render audio to WAVTransport
midi_export(pattern, cycles [, path])Export a pattern to a MIDI fileTransport
MidiTrack(channel [, velocity] [, port_alias] [, name])Create a MIDI trackMIDI
midi_connect / midi_disconnect / midi_ports / midi_connectedMIDI output connectionsMIDI
midi_input_connect / midi_input_disconnect / midi_input_portsMIDI input connectionsMIDI
midi_clock_send / midi_clock_follow / midi_clock_status / midi_clock_offset / midi_delayMIDI clock & timingMIDI
audio_devices / audio_output / audio_output_channelsAudio device selection & channel routingConfiguration
audio_latency() / audio_cpu_load()Engine latency and CPU infoConfiguration
recording_offset([ms])Recording latency compensationConfiguration