Experiments · Web Audio
Making sound with code: a musician’s introduction to the Web Audio API

Two pages on this site make sound out of nothing but code: hypnagogia, five dreamlike worlds whose drone and rhythm react to how you play, and Vėjas S-1, a concept pocket synthesizer with a "wind engine". Neither loads a single audio file for its instrument. Everything is generated live with the Web Audio API, which ships in every modern browser.
If you make music and have never touched it, here is the shortest useful introduction I can give.
The mental model: patch cables
Web Audio is a modular synth. You create nodes – sources, filters, gains, effects – and connect them with .connect(), ending at ctx.destination, the speakers.
const ctx = new AudioContext();
const osc = ctx.createOscillator(); // source
const amp = ctx.createGain(); // volume
osc.type = 'sawtooth';
osc.frequency.value = 110; // A2
osc.connect(amp).connect(ctx.destination);
amp.gain.value = 0.2;
osc.start();
Rule one: sound starts with a click
Browsers block audio until the user interacts with the page. That is why hypnagogia opens with Enter with sound / Enter without sound. Create or resume the context inside a click handler:
button.addEventListener('click', () => ctx.resume());
It's also a good design moment: tell people to put on headphones before the first sound, not after.
Envelopes make notes
An oscillator that is just switched on and off clicks. Shape the volume over time with automation instead:
function note(freq, t = ctx.currentTime) {
const o = ctx.createOscillator(), g = ctx.createGain();
o.frequency.value = freq;
g.gain.setValueAtTime(0, t);
g.gain.linearRampToValueAtTime(0.3, t + 0.01); // attack
g.gain.exponentialRampToValueAtTime(0.001, t + 1.2); // release
o.connect(g).connect(ctx.destination);
o.start(t); o.stop(t + 1.3);
}
Schedule against ctx.currentTime, never with setTimeout alone – the audio clock is sample-accurate, the JavaScript timer is not.
Wind is filtered noise
The idea behind Vėjas' wind engine is old and simple: white noise through a band-pass filter, with the filter's centre slowly moving. Stack a few layers with different ranges and you get gusts, whistles and rumble.
const len = ctx.sampleRate * 2;
const buf = ctx.createBuffer(1, len, ctx.sampleRate);
const data = buf.getChannelData(0);
for (let i = 0; i < len; i++) data[i] = Math.random() * 2 - 1;
const noise = ctx.createBufferSource();
noise.buffer = buf; noise.loop = true;
const bp = ctx.createBiquadFilter();
bp.type = 'bandpass'; bp.frequency.value = 600; bp.Q.value = 3;
const lfo = ctx.createOscillator(), depth = ctx.createGain();
lfo.frequency.value = 0.15; // one gust every ~7 s
depth.gain.value = 400; // sweep ±400 Hz
lfo.connect(depth).connect(bp.frequency);
noise.connect(bp).connect(ctx.destination);
noise.start(); lfo.start();
Generative means it listens back
In hypnagogia the world starts as a resting drone; bass and rhythm rise out of it the more you play, and settle when you stop. The general technique is an energy value: every interaction adds to it, it decays every frame, and you map it to parameters – filter cutoff, the volume of a bass layer, how often a percussion voice triggers.
let energy = 0;
canvas.addEventListener('pointermove', () => energy = Math.min(1, energy + 0.02));
function tick() {
energy *= 0.995; // slow decay
bassGain.gain.setTargetAtTime(energy * 0.4, ctx.currentTime, 0.5);
filter.frequency.setTargetAtTime(300 + energy * 3000, ctx.currentTime, 0.3);
requestAnimationFrame(tick);
}
setTargetAtTime glides to the new value, so the music breathes instead of jumping.
Things I wish I had known earlier
- Put a limiter at the end. A
DynamicsCompressorNodebefore the destination saves ears when ten voices stack up. - Reuse, don't leak. Oscillators are one-shot and cheap; filters and buses should be created once and reused.
- Respect reduced motion and silence. Always offer a way in without sound, and a mute key (M) that works everywhere.
- Mobile is quieter and smaller. Test on phone speakers – the bass you love might not exist there.
Go and make some noise: hypnagogia (headphones) and Vėjas S-1 (play the keyboard rows A W S E D F…). And if you want the finished, human-made version of all this, it's here.
Written by Rukas Waves – music producer from Lithuania who builds free browser games on the side. Questions or corrections: hi@ehog.io.


