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.
The call
Section titled “The call”fn query(cycle) -> Number | Note | Array | PatternFor each cycle, the track:
- 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. - Calls
query(cycle)with the cycle number (0, 1, 2, …). - Converts the return value into the events of that cycle.
| Rule | Consequence |
|---|---|
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 values
Section titled “Return values”| Return value | Result |
|---|---|
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 |
What query() may do
Section titled “What query() may do”query() runs in a restricted evaluator, so that it is deterministic and safe to run away from the
main thread.
| Supported | Not supported |
|---|---|
let, if/else, match, return, for, loop | print(), put() |
| Arithmetic, field and array access | Audio and MIDI operations |
| Calls to your own functions and methods | Native functions |
Seeded rand() and choose() from std/random |