← Return to Learn Hub
Audio Synthesis

Synthesizing Sci-Fi Audio in Pure JavaScript with the Web Audio API

By Keshav Kumar • Procedural Audio Synthesis & Web Audio API ‪ 6 min read • Last updated: November 2026

Why download megabytes of MP3 and WAV files when your web browser can synthesize crisp mechanical keyboard clicks, warning sirens, and cyberpunk hums in real time using pure mathematics? Here is your guide to procedural audio synthesis.

The Drawbacks of Pre-Recorded Audio Files on the Web

Traditional web applications play sound effects by creating <audio> HTML elements loaded from external audio files. While convenient for long musical tracks, this approach is fundamentally flawed for interactive sound design:

The Solution: Native Web Audio API Synthesis

The W3C Web Audio API allows developers to generate, modulate, and filter audio signals in real time using client-side JavaScript. Instead of streaming audio samples, you construct a processing graph of audio nodes connected to your device's audio destination (speakers or headphones).

Crafting Mechanical Keyboard Clicks

A mechanical key click consists of a sharp, very brief burst of mid-frequency sound with a rapid exponential decay envelope. Here is the exact synthesis function used in HackerScreen:

function playKeyClick() {
    const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
    const osc = audioCtx.createOscillator();
    const gain = audioCtx.createGain();

    // Triangle wave provides a subtle 'clack' without harsh buzz
    osc.type = 'triangle';
    osc.frequency.setValueAtTime(320 + Math.random() * 160, audioCtx.currentTime);

    // Rapid volume decay over 20 milliseconds
    gain.gain.setValueAtTime(0.04, audioCtx.currentTime);
    gain.gain.exponentialRampToValueAtTime(0.0001, audioCtx.currentTime + 0.02);

    osc.connect(gain);
    gain.connect(audioCtx.destination);

    osc.start();
    osc.stop(audioCtx.currentTime + 0.02);
}

Generating Hollywood Cyber Alarms and Sirens

Emergency breach alerts require alternating frequency shifts that grab the listener's attention. By using a sawtooth wave and scheduling discrete frequency steps, we create an authentic klaxon siren:

function playBreachSiren() {
    const audioCtx = new AudioContext();
    const osc = audioCtx.createOscillator();
    const gain = audioCtx.createGain();

    osc.type = 'sawtooth';
    // Shift frequency between 440 Hz and 880 Hz
    osc.frequency.setValueAtTime(440, audioCtx.currentTime);
    osc.frequency.setValueAtTime(880, audioCtx.currentTime + 0.12);

    gain.gain.setValueAtTime(0.06, audioCtx.currentTime);
    gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.25);

    osc.connect(gain);
    gain.connect(audioCtx.destination);
    osc.start();
    osc.stop(audioCtx.currentTime + 0.25);
}

Ambient Cyberpunk Drones with Low-Pass Filters

To create the deep, atmospheric hum of a heavy mainframe server room, we route a low-frequency sawtooth oscillator through a BiquadFilterNode configured as a low-pass filter with a cutoff frequency around 180 Hz. This dampens harsh high-frequency harmonics, producing a rich, cinematic sub-bass rumble that runs continuously with near-zero CPU impact.

Browser Autoplay Compliance

Modern web browsers require explicit user interaction before allowing audio playback. Always initialize or resume your AudioContext inside a user-initiated event listener—such as a button click or keyboard press—to ensure consistent sound playback across Chrome, Safari, Edge, and Firefox.

Web Audio AudioNode Graph Routing: Mechanical Keyclick Synthesis

OSCILLATOR NODE Square / Sawtooth Freq: 800Hz → 200Hz Exponential pitch drop BIQUAD FILTER Bandpass Filter Q-factor = 3.0 Adds metallic clack GAIN ENVELOPE Attack: 2ms (0 → 0.3) Decay: 35ms (→ 0.001) Zero audible clicks DESTINATION ctx.destination Hardware DAC Speakers

Authoritative References & Technical Sources

Test Web Audio in Prank Terminal

Hear procedural audio synthesis in action with live clicks, alarm sirens, and Access Granted harmonic chimes.

Launch Simulator →