Modulate Filter Frequency with LFO in Tone.js
This article explains how to use a Low-Frequency Oscillator (LFO) to modulate audio parameters in Tone.js, focusing specifically on automating a filter’s cutoff frequency. You will learn how to create a synthesizer, set up a bandpass or lowpass filter, configure an LFO with specific frequency and amplitude boundaries, and connect them together to create dynamic, sweeping synthesizer sounds.
Step 1: Create the Synthesizer and Filter
To modulate a filter, you first need a sound source and a filter
node. In Tone.js, you can connect a Synth to a
Filter, and then route the filter to the master output
(toDestination).
// Create a lowpass filter with a base frequency of 1000Hz
const filter = new Tone.Filter({
type: "lowpass",
frequency: 1000
}).toDestination();
// Create a synth and route its output through the filter
const synth = new Tone.Synth().connect(filter);Step 2: Configure the LFO
An LFO (Low-Frequency Oscillator) outputs a sub-audio
control signal used to automate other parameters. When configuring the
LFO, you define: * frequency: How fast the modulation
occurs (e.g., “2hz” or 2 cycles per second). * min: The
lowest value the parameter should reach. * max: The
highest value the parameter should reach.
// Create an LFO that sweeps between 200Hz and 2000Hz twice per second
const lfo = new Tone.LFO({
frequency: "2hz",
min: 200,
max: 2000
});Step 3: Connect and Start the LFO
In Tone.js, you can connect an LFO directly to any audio parameter of
type Signal. To modulate the filter’s cutoff frequency,
connect the LFO to filter.frequency. Finally, you must call
.start() on the LFO for the modulation signal to begin
generating.
// Connect the LFO output directly to the filter's frequency parameter
lfo.connect(filter.frequency);
// Start the LFO
lfo.start();Step 4: Play a Note
Once the connections are established and the LFO is started, trigger a note on your synthesizer. As the note plays, you will hear the filter frequency rise and fall automatically according to the LFO’s rate and depth.
// Trigger a note to hear the modulated filter effect
synth.triggerAttackRelease("C3", "2n");