Synthesizing Zero-Bandwidth Brown Noise, Rain, and Alpha Binaural Audio with the Web Audio API
Streaming 10-hour ambient YouTube loops or Spotify playlists while working on a plane, train, or spotty Wi-Fi wastes bandwidth and constantly risks mid-roll ads or dropped buffers. Instead of shipping multi-megabyte MP3
Streaming 10-hour ambient YouTube loops or Spotify playlists while working on a plane, train, or spotty Wi-Fi wastes bandwidth and constantly risks mid-roll ads or dropped buffers.
Instead of shipping multi-megabyte MP3 loops, we can synthesize brown noise, rain, and binaural beats procedurally in ~50 lines of vanilla Web Audio API code. Everything runs offline in a PWA or WebView2 desktop shell with zero network requests.
You can try the finished offline mixer right in your browser here: Lark Ambient Focus (Free Offline PWA) (MIT source on GitHub).
1. Generating Warm Brown Noise via Leaky Integration
White noise has equal energy per frequency, which sounds harsh and hissy at high frequencies. Brown (Brownian) noise drops 6 dB per octave by integrating white noise with a small leak factor so the signal never drifts into DC clipping:
const bufSize = audioCtx.sampleRate * 4;
const brownBuf = audioCtx.createBuffer(1, bufSize, audioCtx.sampleRate);
const brownData = brownBuf.getChannelData(0);
let lastOut = 0;
for (let i = 0; i < bufSize; i++) {
const white = Math.random() * 2 - 1;
lastOut = (lastOut + 0.02 * white) / 1.02;
brownData[i] = lastOut * 3.2;
}
const brownSrc = audioCtx.createBufferSource();
brownSrc.buffer = brownBuf;
brownSrc.loop = true;
const brownGain = audioCtx.createGain();
brownSrc.connect(brownGain).connect(audioCtx.destination);
brownSrc.start();
A 4-second looping AudioBuffer at 48 kHz uses less than 1 MB of RAM, loops imperceptibly, and costs virtually zero CPU.
2. Shaping Bandpass-Filtered Rain
For a gentle rain texture, start with a separate 4-second white-noise buffer and route it through a BiquadFilterNode tuned to bandpass around 1100 Hz with a moderate Q = 0.7:
const rainBuf = audioCtx.createBuffer(1, bufSize, audioCtx.sampleRate);
const rainData = rainBuf.getChannelData(0);
for (let i = 0; i < bufSize; i++) rainData[i] = Math.random() * 2 - 1;
const rainSrc = audioCtx.createBufferSource();
rainSrc.buffer = rainBuf;
rainSrc.loop = true;
const rainFilter = audioCtx.createBiquadFilter();
rainFilter.type = "bandpass";
rainFilter.frequency.value = 1100;
rainFilter.Q.value = 0.7;
const rainGain = audioCtx.createGain();
rainSrc.connect(rainFilter).connect(rainGain).connect(audioCtx.destination);
rainSrc.start();
3. Stereo-Panned Alpha Binaural Drone (10 Hz)
Feeding slightly offset sine waves (200 Hz left, 210 Hz right) into two StereoPannerNode instances creates a subtle 10 Hz Alpha beat (or 40 Hz Gamma at 240 Hz right) without needing any audio assets:
const oscL = audioCtx.createOscillator();
const oscR = audioCtx.createOscillator();
const panL = audioCtx.createStereoPanner();
const panR = audioCtx.createStereoPanner();
oscL.frequency.value = 200;
oscR.frequency.value = 210; // 10 Hz Alpha offset
panL.pan.value = -0.8;
panR.pan.value = 0.8;
const binGain = audioCtx.createGain();
oscL.connect(panL).connect(binGain);
oscR.connect(panR).connect(binGain);
binGain.connect(audioCtx.destination);
oscL.start();
oscR.start();
4. Click-Free Volume Sliders
Whenever a slider moves, avoid zipper noise by smoothing gain transitions with setTargetAtTime:
nodes.brownGain.gain.setTargetAtTime((brownPct / 100) * 0.25 * mult, audioCtx.currentTime, 0.05);
nodes.rainGain.gain.setTargetAtTime((rainPct / 100) * 0.12 * mult, audioCtx.currentTime, 0.05);
nodes.binGain.gain.setTargetAtTime((binPct / 100) * 0.08 * mult, audioCtx.currentTime, 0.05);
Try It Live (Zero Telemetry, Offline PWA)
- Web App (runs offline after first load): https://larkandlever.github.io/ambient-focus/app/
-
Overview & Windows Native
.exePro Edition ($9): https://larkandlever.github.io/ambient-focus/ - Source Code (MIT): https://github.com/larkandlever/lark-ambient-focus
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.