Skip to content

Change a Pattern Every Cycle

When the next pattern depends on more than one value, compute it in a callback. on_cycle runs it once per cycle with the cycle number, and what it sends lands on that cycle.

use "std/instruments" { SamplerMelodic, Kit };
let lead = AudioTrack("lead");
lead.set_instrument(SamplerMelodic(Kit("keys")));
on_cycle(fn(cycle) {
let note = 60 + (cycle % 12); // one step up each cycle, then back down
lead << Sequence(#[note]);
});
PLAY;

Seed any random choice with cycle, so the result is the same on every playback — see Make a Reproducible Random Choice.

off_cycle() removes every callback.

When resonon follows a MIDI clock, call prime_cycles() before the clock starts, so the first cycle is not empty — see Sync to a MIDI Clock.