Skip to content

Script Patterns

Any class instance with a query(cycle) method is a pattern. Send it to a track with << and resonon calls query() once per cycle. For worked examples, see Write a Pattern as a Class.

fn query(cycle) -> Number | Note | Array | Pattern

For each cycle, the track:

  1. Rebuilds the instance’s fields by replaying query() for every earlier cycle, starting from the field values the instance had when it was sent to the track.
  2. Calls query(cycle) with the cycle number (0, 1, 2, …).
  3. Converts the return value into the events of that cycle.
RuleConsequence
Fields assigned in query() carry to the next cycle.this.idx = this.idx + 1; advances once per cycle.
State is replayed from cycle 0.The same cycle always returns the same result, in any order and after any seek.
Fields are captured when the instance is sent to a track.Changing the original instance afterwards does not change the playing pattern.
cps is in scope inside query().The current tempo, in cycles per second.
Return valueResult
Number (60)A MIDI note filling the cycle
Note (C4)A note filling the cycle
Array (#[C4, E4, G4])A chord — all notes sound together for the cycle
Pattern ([C4 E4 G4])A sub-pattern, laid out across the cycle
[_]A rest — silence for the cycle

query() runs in a restricted evaluator, so that it is deterministic and safe to run away from the main thread.

SupportedNot supported
let, if/else, match, return, for, loopprint(), put()
Arithmetic, field and array accessAudio and MIDI operations
Calls to your own functions and methodsNative functions
Seeded rand() and choose() from std/random