Chapter 5

Modulation

Any control can move: LFOs, drawn shapes, envelopes, knobs.

Wherever a value goes, a moving signal can go instead. Modulation runs inside the audio engine, sample-accurate, not as a stream of messages.

LFOs

sine · saw (rises) · isaw (falls) · tri · square · rand · perlin (smooth drift). Each runs 0..1, and .range(lo, hi) rescales it.

The rate is in cycles: sine(0.25) is one pass every four cycles, aligned to the bar grid, so a slow sweep is in the same place every time round. Write a unit for a fixed speed: sine('0.5hz'). A rate in cycles can be a pattern, sine("<1 2 4>"), and the shape carries on from where it was each time the rate changes.

bass: note("c2 c2 c3 c2 eb2 c2 g2 bb1").synth("Wavetable")
  .fx("Filter").param("Mode", "ladder")
  .param("Cutoff", sine(0.25).range(0.3, 0.7))
  .param("Resonance", tri(0.5).range(0.2, 0.7)).postgain(1)

.range()'s bounds can move too: sine().range("0.2 0.3", 0.8) sweeps from a stepped floor. Bounds with a unit sweep in that unit (sine().range("1x", "8x")), and .snap() moves every value to the nearest of a set: a list, or values stacked with commas ("1,2,4"), which can pattern ("<[1,2,4] [1,3,6]>").

bell: n("0 4 7 4").scale("C minor").synth("FM")
  .param("Op 2 Level", 0.7).param("Mod 2 to 1", 0.6)
  .param("Op 2 Ratio", sine(0.5).range("1x", "8x").snap([1, 2, 3, 4, 6, 8]))
  .postgain(0.5)

An LFO drives the channel strip as well as a device:

pad: note("<[c3,eb3,g3] [ab2,c3,eb3]>").synth("Wavetable")
  .param("Osc 1 Table", "PWM").param("Osc 1 Position", tri('0.3hz'))
  .fx("Filter").param("Cutoff", sine().range("0.3 0.4", 0.65))
  .pan(sine(0.5).range(-0.6, 0.6))
  .postgain(perlin(0.5).range(0.25, 0.4))

Drawn shapes

The shape editor: a curve rising and bending back down, with points to drag.
Double-click lfo to draw the shape.

lfo("…", { rate, mode }) is a shape you draw. Double-click the word lfo, drag points and bend segments, and the code is written as you go. The two ends move together while they sit at the same level, which keeps a loop from clicking; alt moves one alone.

B switches to the pencil, which draws over whatever it passes (shift draws a straight line). With the arrow, drag around points to select them and mark the span they sit in (its edges land on sixteenths of a cycle; alt places them freely): they drag together, {mod+d} plays the span twice (inserting its time), {mod+c}, {mod+x} and {mod+v} copy, cut and paste it, and {mod+l} (or right-click) keeps only the span, as the whole loop. Ends that differ are faded to meet halfway over the loop's first and last sixteenth, drawn dashed until the next edit; alt keeps the jump. Right-click a span to simplify it: its points thin to the ones the line needs.

smooth (0 to 1, the slider under the shape) plays the drawing rounded off and keeps its points as drawn: the editor shows what plays as a ghost under them. It is written into the definition, _shape("wobble", "…", { smooth: 0.3 }), and lfo() takes it too.

mode
freeloops on its own clock (the default)
retriggerrestarts on every note
envelopeplays once per note, then holds
pad: note("<[c3,eb3,g3] [ab2,c3,eb3]>").synth("Wavetable")
  .param("Osc 1 Table", "Vocal")
  .param("Osc 1 Position", lfo("wobble", { rate: 0.5 }))
  .fx("Reverb").postgain(0.4)

_shape("wobble", "0,0 0.5,1,-3 1,0")

A drawn shape is kept by name: the _shape(…) line at the foot of the buffer is what the editor writes (it folds away), and lfo("wobble") plays it. A few come built in, sine, triangle, saw, isaw, square, pluck, swell and stairs, and names pattern: lfo("<pluck swell>").

Envelopes

env({ attack, decay, sustain, release, curve }) is an ADSR started by each of the track's notes. Times are in seconds, sustain is a level.

bass: note("c2 c2 c3 c2 eb2 c2 g2 bb1").synth("Wavetable")
  .fx("Filter").param("Mode", "ladder")
  .param("Cutoff", env({ attack: 0.01, decay: 0.2, sustain: 0.1 })
    .range(0.25, 0.7)).postgain(0.45)
kick: s("bd*4").postgain(0.45)

Every note starts the attack again, even while the last one is still held; retrigger: false starts it only on the first of notes that overlap. dur() is the current note's length in seconds, for anything that should scale with it.

.attack(), .decay(), .sustain(), .release() and .adsr() set a synth's own amp envelope, in the same units: note("c3").synth("Wavetable").adsr(0.005, 0.3, 0, 0.2) is a pluck.

Velocity

Bare, without a call, vel is the velocity of the note the track is playing, from 0 to 1, held until the next note. Accents open the filter:

bass: note("c2 c2 c3 c2 eb2 c2 g2 bb1").vel("1 0.4 0.7 0.4").synth("Wavetable")
  .fx("Filter").param("Mode", "ladder")
  .param("Cutoff", vel.range(0.25, 0.7)).postgain(0.45)

A sample's velocity is its gain. vel(0.5) and .vel(0.5) still set it.

Combining

Signals combine with ordinary arithmetic, and the engine works it out every sample, so a combination is as sharp as any one of them: env().mul(sine(4)) is an envelope with a tremolo in it.

bass: note("c2 ~ c2 eb2").synth("Wavetable")
  .fx("Filter").param("Mode", "ladder")
  .param("Cutoff", env({ decay: 0.3, sustain: 0 }).mul(sine(8).range(0.6, 1)).range(0.2, 0.75))
  .postgain(0.5)

A track's audio is a signal too, audio("name"), running from -1 to 1. Patched onto a control it moves the control around a resting point: audio("mod").mul(0.25).add(0.5) swings a quarter either side of the middle. Several can be added up, and .dry(0) keeps the source out of the mix. Patched onto an oscillator's phase it is FM:

mod: note("<c4 eb4>").synth("Wavetable").param("Osc 1 Phase Rand", 0).adsr(0.001, 0.4, 0, 0.1).dry(0)
bell: note("<c3 eb3>").synth("Wavetable").param("Osc 1 Phase Rand", 0).adsr(0.001, 2, 0, 1)
  .param("Osc 1 Phase", audio("mod").mul(0.5).add(0.5)).postgain(0.35)

Turning a patched control's knob in its device window moves the resting point, the number in the last .add().

Following a level

Given a track, bus or audio input instead of an object, env("name") is that source's level, from 0 to 1: it rises over attack and falls over release, in seconds. Turned upside down it ducks one track under another:

kick: s("bd*4").postgain(0.5)
pad: note("<c3 eb3>").synth("Wavetable").adsr(0.3, 0, 1, 1).postgain(0.3)
  .gain(env("kick", { release: 0.3 }).mul(-1).add(1))

Smoothing

.smooth(t) glides any signal toward each new value instead of jumping, arriving after about t seconds: a stepped pattern, an LFO, a knob, audio or a level.

bass: note("c2*8").synth("Wavetable")
  .fx("Filter").param("Cutoff", "<0.2 0.7 0.4 0.9>*2".smooth(0.15)).postgain(0.45)

Knobs

WriteIs
macro1 … macro8the knobs in the session tab
midicc("Twister")(12)controller 12 on a MIDI device (any part of its name), through the browser's Web MIDI (not in Safari)
cc()after const cc = midicc("…"): the next knob turned on the device, written into the call as cc(12, 1)
osc("/1/fader3")a value sent over OSC to port 57160
auto("sweep")an automation lane drawn in the arrangement (Arranging)
// macro1 is the first knob in the session tab
bass: note("c2 c2 eb2 c2 g1 c2 bb1 c2").synth("Wavetable")
  .fx("Filter").param("Mode", "ladder").param("Cutoff", macro1.range(0.4, 0.8))
  .postgain(1)