Chapter 12

Transforms

Reshape patterns with methods: arithmetic, time, structure, chance.

Arithmetic

setscale("C major")

up: note("c3 e3 g3").add(12)                     // up an octave
  .synth("FM").postgain(0.25)
turn: n("0 2 4").add("<0 7>").scale("C major")   // transpose every other cycle
  .synth("Wavetable").postgain(0.25)
steps: pianoroll("lead").sc().add(n("<0 2 -1>"))  // move by scale steps
  .synth("Wavetable").postgain(0.3)

_roll("lead", "48,0,8 52,8,4 55,12,4", { grid: 16, len: 16 })

A number moves notes in semitones; an n() moves them in scale steps, in the key they were last mapped through (else the setscale key). A note outside the key keeps its distance from the nearest one, so a passing tone stays a passing tone.

The set: add sub mul div mod set, then round abs floor ceil clamp, and the comparisons gte gt lte lt eq neq (1 or 0, handy as conditions).

Whose events?

When both sides have a rhythm, the result has both: each note is cut wherever the right side changes. That's the default, mix. Three modifiers pick differently:

WritePlays
.add(x) · .add.mix(x)both rhythms together
.add.in(x)this pattern's events, with x read at each onset
.add.out(x)x's events, with this pattern read at each onset
.add.squeeze(x)a whole cycle of x fitted into each event
note("c3 e3").add("0 7 12")
note("c3 e3").add.in("0 7 12")
note("c3 e3").add.out("0 7 12")
note("c3 e3").add.squeeze("0 7 12")

Every binop has them (.mul.in(vel("1 .5")), .set.out(…), .gte.in(…)), and they work the same on notes, samplers and controls. .when() is always in: its condition is read at the pattern's own events and never adds any.

Time

WriteDoes
.fast(f) · .slow(f)speed up · slow down; negative also reverses
.swing(amt, grid)delay every second slot: 1/3 is the triplet shuffle
.nudge(v)push each event late (or early) by part of its step
.rib(cycle, len)loop a stretch of the pattern forever
.hold(trig) · .seg(n)sample and hold at another pattern's onsets · n times a cycle

Structure

WriteDoes
.struct("1 ~ 1 1")take the rhythm from another pattern
struct("1 ~ 1 1").s("bd")at the head of a chain: the default note on that rhythm (also hold, seg, chord)
.mask("1 0 1 1")silence where the other pattern is off
.ply(n, fn?)repeat each event n times in its step; (x, i) => … changes each repeat
.echo(n, dt, fn?)n copies, dt cycles apart
.arp("0 1 2 1")play chords one note at a time
.bite(indices)cut four cycles into eighths and play them in a new order
.degrade(p)drop events at random
.vel(v) · .clip(v)velocity · note length as a multiple of its step
.legato()hold every note until the next one starts
.slide("0 1")glide into each note marked 1 from the note before, without a new attack
.octave("0 1 0 -1")move each note by whole octaves when it plays, whatever the chain did to its pitch
note("c3 e3 g3 b3").struct("1 ~ 1 1 ~ 1 1 ~")
note("[c3,e3,g3]").arp("0 1 2 1")
note("c3 e3").ply(3)
arp: note("<[c3,eb3,g3] [ab2,c3,eb3] [bb2,d3,f3] [g2,bb2,d3]>")
  .arp("0 1 2 3 2 1 0 1")
  .when("<0 0 0 1>", (x) => x.add(12))   // every fourth bar up an octave
  .echo(2, 3/16, (x) => x.vel(0.4))       // two quieter repeats, 3/16 apart
  .synth("Wavetable").postgain(0.45)
hats: s("hh*8").swing(1/3).degrade(0.2).postgain(0.45)

Conditions and chance

WriteDoes
.when(cond, fn)apply fn where cond is on: .when("1 0", (x) => x.add(12))
.sometimes(0.3, fn)apply fn to about 3 events in 10
choose("0", "3", "7")one option each cycle; ["7", 3] weights one
irand(8) · rand() · perlin()whole number 0–7 · 0..1 per read · smooth drift
cat(a, b) · seq(a, b)one pattern per cycle, in turn · each plays for its share of every cycle

Every random source is its own stream, seeded by where it sits in the buffer: two .when(rand().gte(0.7), …) pick different bars, and a song replays identically every time. Pass { seed: 1 } to make two move together.

Every argument above takes a pattern, not just a number: .ply("<2 4>"), .fast(macro1.range(1, 4)). If one doesn't, that's a bug.