Skip to content

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.

632 Hz
1.0
Resonance (Q)

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.

html
<button id="filter-play">Play noise</button>
<label>Cutoff (Hz) <input id="cutoff" type="range" min="100" max="8000" value="800"></label>
typescript
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 ​

FactoryWhat it controls
createFilterEffectA single lowpass, highpass, shelf or other biquad filter
createGainEffectSignal level at one point in the chain
createEQLow, mid and high bands
createDelayEcho time and feedback
createReverbAlgorithmic or impulse-response reverb
createDistortionWaveshaping and drive
createCompressorDynamic range
wrapEffectWet/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 ​

TypeDescriptionKey Parameters
lowpassPasses frequencies below cutofffrequency, q
highpassPasses frequencies above cutofffrequency, q
bandpassPasses frequencies around centerfrequency, q
lowshelfBoosts/cuts below frequencyfrequency, gain
highshelfBoosts/cuts above frequencyfrequency, gain
peakingBoosts/cuts around frequencyfrequency, q, gain
notchRemoves specific frequencyfrequency, q
allpassPhase shift at frequencyfrequency, q

Filter recipes ​

  • Bass boost. Use a lowshelf at 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.

html
<button id="eq-preview">Play with EQ</button>
typescript
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 ​