Skip to content

LFO

lfo(opts)

Returns an LfoSpec that can be passed to modulatable machine parameters (e.g. cutoff on vasynth). The LFO runs as a per-note OscillatorNode inside Web Audio API.

lfo(opts: {
rate: number; // cycle length in beats (4 = one cycle every 4 beats)
min: number; // minimum value (Hz)
max: number; // maximum value (Hz)
shape?: 'sine' | 'tri' | 'saw' | 'square'; // default: 'sine'
offset?: number; // start phase offset: 1=max, -1=min, 0=center (default). sine only.
}): LfoSpec

The lfo function is injected by the engine at runtime — it reads the current BPM automatically, so rate is always expressed in musical beats rather than seconds.

Example

function* song(ctx) {
const synth = vasynth({
wave: 'sawtooth',
cutoff: lfo({ rate: 4, min: 300, max: 3000, shape: 'sine' }),
gain: 0.5,
})
const key = scales(4, 'A3:minor')
while (true) {
yield cast(synth, key, seq(4, '0,2,4,3'), ctx)
}
}

Parameters

ParameterTypeDefaultDescription
ratenumberLFO period in beats. 4 = one full cycle every 4 beats.
minnumberMinimum modulation value.
maxnumberMaximum modulation value.
shapestring'sine'Oscillator shape: sine, tri, saw, square.
offsetnumber0Start phase offset. 1 = start at max, -1 = start at min, 0 = center. Applies to sine only. Range: −1 to 1.

Modulatable parameters

The following parameters accept an LfoSpec in addition to a plain number:

MachineParameter
vasynthcutoff

Notes

  • The LFO oscillator starts fresh on every note trigger (per-note).
  • BPM is captured at the moment the machine is created inside the generator loop, so tempo changes take effect from the next cycle.