Audio Routing & Custom Effects
Press Play Oscillator to hear a clean 200 Hz sine wave, then Add Distortion. Turn up the amount to add harmonics. Use the wet/dry mix to blend them with the original tone, or bypass the effect to compare the two.
The chain every sound flows through, from source to speakers.
Effects wrap and route automatically.
Tip: Try different distortion amounts and mix levels. The bypass toggle lets you A/B compare the processed vs unprocessed signal.
Wrap a native effect
Add this button and its click handler to your page to hear a two-second comparison. The first second is dry. Halfway through, the Waveshaper mix rises to 70%. The curve maps each input sample to an output level. A steeper slope near zero gives the sine wave more high-frequency content.
<button id="compare-distortion">Compare dry and distorted</button>import { createOscillator, getAudioContext, wrapEffect } from 'ez-web-audio'
const button = document.querySelector<HTMLButtonElement>('#compare-distortion')!
button.onclick = async () => {
button.disabled = true
try {
const ctx = await getAudioContext()
const osc = await createOscillator(ctx, { frequency: 200, type: 'sine', gain: 0.2 })
try {
const shaper = ctx.createWaveShaper()
const amount = 400
shaper.curve = Float32Array.from({ length: 4097 }, (_, i) => {
const x = i / 2048 - 1
return ((3 + amount) * x * 20 * Math.PI / 180) / (Math.PI + amount * Math.abs(x))
})
shaper.oversample = '4x'
const effect = wrapEffect(ctx, shaper)
effect.mix = 0
osc.addEffect(effect)
await osc.play()
await new Promise(resolve => setTimeout(resolve, 1000))
effect.mix = 0.7
await new Promise(resolve => setTimeout(resolve, 1000))
await osc.fadeOut(0.04)
}
finally {
osc.dispose()
}
}
finally {
button.disabled = false
}
}In a slider handler, assign a new curve to shaper.curve to change the distortion amount. Set effect.mix between 0 (dry) and 1 (fully processed). Switching effect.bypass to true temporarily routes around the effect and preserves the mix setting for the next comparison.
Signal flow and cleanup
A Sound or Oscillator routes its source through the effects in insertion order, then through gain and pan to its destination. wrapEffect(ctx, node) adds a dry path around the processor and a wet path through it. Build the source and processor with the same AudioContext.
Disposing the source also disposes its attached effects. To remove a processor during playback, call osc.removeEffect(effect). You can then attach the same effect elsewhere, or call effect.dispose() to release it.
Other processors
Impulse-response reverb
Supply your own impulse-response recording and pass its URL to await createReverb(ctx, impulseUrl, { mix: 0.3 }) in a playback handler. Attach the returned reverb with sound.addEffect(reverb). For reverb without a recording, use createReverb(ctx, { decay: 2, mix: 0.3 }).
Delay with feedback
createDelay(ctx, { time: 0.5, feedback: 0.4, mix: 0.3 }) returns an effect you can add to the same chain. The first echo arrives half a second after the dry signal. Feedback routes some of that echo back into the delay for further repeats.
External libraries
An external processor needs a Web Audio input that can receive the signal and a way to connect its output. A native processing node such as a WaveShaper supplies both. A library can also expose an input AudioNode and a connect(destination) method.
For example, install Tuna.js separately to use its chorus. The following JavaScript belongs inside your playback handler after you have created sound and obtained its ctx. Import Tuna and wrapEffect at module scope. Tuna.js
// Module imports
import { wrapEffect } from 'ez-web-audio'
import Tuna from 'tunajs'
function addChorus(ctx, sound) {
const tuna = new Tuna(ctx)
const chorus = new tuna.Chorus({ rate: 1.5, feedback: 0.2, delay: 0.0045 })
const effect = wrapEffect(ctx, chorus)
effect.mix = 0.5
sound.addEffect(effect)
return chorus // Keep the original object to change its rate while playing.
}Next Steps
- Sampled Drum Kit — Multi-zone velocity-sensitive pads
- Synth Keyboard — Oscillator playground with filter controls
- Why Timing Drifts — Master Web Audio's scheduling system