Effects
Shape your audio with filters, gain effects, and custom audio processing. This page covers the built-in effects, effect chains, and integrating external effect libraries.
Try It: Real-Time Filter Control
Choose Oscillator or White Noise, then press Play. Drag Cutoff Frequency and Resonance to hear which frequencies the filter passes. Stop playback to change the source.
Synthesizers are oscillators plus filters.
Connect a cutoff slider
Use a lowpass filter on noise to hear the cutoff clearly. Add the button and slider to your page, then run the TypeScript in a browser module. A second click stops the preview and releases its filter.
<button id="filter-play">Play noise</button>
<label>Cutoff (Hz) <input id="cutoff" type="range" min="100" max="8000" value="800"></label>import type { Sound } from 'ez-web-audio'
import { createFilterEffect, createWhiteNoise } from 'ez-web-audio'
const button = document.querySelector<HTMLButtonElement>('#filter-play')!
const cutoff = document.querySelector<HTMLInputElement>('#cutoff')!
let stop: (() => void) | undefined
button.onclick = async () => {
if (stop) {
stop()
return
}
button.disabled = true
let noise: Sound | undefined
function release() {
if (stop !== release)
return
stop = undefined
cutoff.oninput = null
noise?.dispose()
button.disabled = false
button.textContent = 'Play noise'
}
stop = release
try {
noise = await createWhiteNoise()
if (stop !== release) {
noise.dispose()
return
}
const filter = createFilterEffect('lowpass', { frequency: cutoff.valueAsNumber, q: 1 })
noise.changeGainTo(0.12)
noise.addEffect(filter)
await noise.play()
if (stop !== release)
return
cutoff.oninput = () => { filter.frequency = cutoff.valueAsNumber }
button.textContent = 'Stop noise'
button.disabled = false
}
catch (error) {
release()
throw error
}
}
window.addEventListener('pagehide', () => stop?.())Effect types
| Factory | What it controls |
|---|---|
createFilterEffect | A single lowpass, highpass, shelf or other biquad filter |
createGainEffect | Signal level at one point in the chain |
createEQ | Low, mid and high bands |
createDelay | Echo time and feedback |
createReverb | Algorithmic or impulse-response reverb |
createDistortion | Waveshaping and drive |
createCompressor | Dynamic range |
wrapEffect | Wet/dry and bypass controls for an external processor |
FilterEffect
Filters modify the frequency content of audio. The FilterEffect wraps the Web Audio BiquadFilterNode with wet/dry mixing and bypass controls.
Filter Types
| Type | Description | Key Parameters |
|---|---|---|
lowpass | Passes frequencies below cutoff | frequency, q |
highpass | Passes frequencies above cutoff | frequency, q |
bandpass | Passes frequencies around center | frequency, q |
lowshelf | Boosts/cuts below frequency | frequency, gain |
highshelf | Boosts/cuts above frequency | frequency, gain |
peaking | Boosts/cuts around frequency | frequency, q, gain |
notch | Removes specific frequency | frequency, q |
allpass | Phase shift at frequency | frequency, q |
Filter recipes
- Bass boost. Use a
lowshelfat 200 Hz with a gain of 8 dB. Lower the overall level first to leave room for the boost. - Soften a bright source. Start with a lowpass cutoff of 8,000 Hz and a Q of 0.7. Move the cutoff down to remove more high frequencies.
- Telephone tone. Put a 300 Hz highpass before a 3,400 Hz lowpass. The pair leaves a narrow band of speech frequencies.
- Mains hum. A notch at 60 Hz with a Q of 30 targets a narrow band around the hum. Use 50 Hz for a 50 Hz recording.
Build and adjust an effect chain
Use sound.addEffects([highpass, lowpass, gain]) to connect effects in that order. Putting gain before distortion changes how hard the input hits the waveshaper. Putting it after distortion changes the level of the shaped sound. createGainEffect(0.5) halves the signal at that point in the chain.
Set effect.bypass = true to hear the sound without a processor. Set it back to false to restore the previous settings. Where you want a blend, use effect.mix: 0 gives the original signal, 1 gives the processed signal, and values between combine them.
sound.getEffects() lists the attached effects. sound.addEffect(effect, 0) inserts at the beginning. sound.removeEffect(effect) reconnects the remaining chain. If you remove an effect permanently, call effect.dispose(). Disposing a source also releases its attached effects.
To inspect the result, attach an analyzer with sound.setAnalyzer(analyzer) and read analyzer.getFrequencyData() in your drawing loop. It measures the signal after effects, gain and pan. Cancel the animation frame and call sound.setAnalyzer(null) when the display closes. Visualization demo
Three-band EQ
For a low shelf, midrange peak and high shelf in one effect, use createEQ. This example plays a file from your app and boosts its low band by 3 dB. Replace the URL with a recording you serve. Disposing the track on completion releases the EQ too.
<button id="eq-preview">Play with EQ</button>import type { Track } from 'ez-web-audio'
import { createEQ, createTrack } from 'ez-web-audio'
const preview = document.querySelector<HTMLButtonElement>('#eq-preview')!
preview.onclick = async () => {
preview.disabled = true
let track: Track | undefined
let closed = false
function release() {
if (closed)
return
closed = true
window.removeEventListener('pagehide', release)
track?.dispose()
preview.disabled = false
}
window.addEventListener('pagehide', release)
try {
track = await createTrack('/audio/song.mp3')
if (closed) {
track.dispose()
return
}
track.once('end', release)
const eq = createEQ({ lowFrequency: 320, highFrequency: 3200, low: 3 })
track.addEffect(eq)
track.changeGainTo(0.6)
await track.play()
}
catch (error) {
release()
throw error
}
}The low, mid, and high properties accept gain in dB, so an EQ slider can set them directly during playback.
Use a native or third-party effect
wrapEffect(ctx, waveshaper) gives a native WaveShaperNode the same mix and bypass controls. For an external library such as Tuna.js, install that library separately and construct its processor with the sound's AudioContext. Keep a reference to the processor to change its own controls. The routing example shows a complete WaveShaper comparison and a Tuna chorus adapter. Audio routing and custom effects
Next Steps
- Basic Playback - Load and play audio files
- Synthesis - Generate sounds with oscillators
- Audio Routing - Connect sounds to custom effects
- Synth Drum Kit - Build synthesized percussion
- Core Concepts - Understand the audio system architecture