Procedural Audio Generation in Modern Web Browsers
Ambient noise apps usually require downloading large looped audio files (10MB–50MB) over the network. This introduces noticeable looping seams, consumes significant mobile data, and delays initial playback. With the HTML5 Web Audio API, authentic ambient noise can be synthesized procedurally in milliseconds.
Generating Real-Time White Noise Buffers
A white noise generator creates an in-memory AudioBuffer filled with random PCM float samples between -1.0 and +1.0, connected to an AudioBufferSourceNode set to loop infinitely:
const bufferSize = audioCtx.sampleRate * 2;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = Math.random() * 2 - 1;
}
const noise = audioCtx.createBufferSource();
noise.buffer = buffer;
noise.loop = true;
GainNode Volume Mixing Architecture
Each channel feeds into a dedicated GainNode before terminating at audioCtx.destination. This enables real-time logarithmic volume adjustment without audio clicks or clipping artifacts.