← AI & Me Studios

03 Audio

Every sound, synthesized live with WebAudio — no audio files. The thruster roar, the score blips, and the crash.

Code
// ---------- audio (tiny WebAudio blips, no assets) ----------
let AC = null;
function audio() {
  if (!AC) { try { AC = new (window.AudioContext || window.webkitAudioContext)(); } catch (e) { return null; } }
  if (AC && AC.state === 'suspended') AC.resume();
  return AC;
}
function tone(f0, f1, dur, type, vol) {
  if (muted) return;
  const ac = audio(); if (!ac) return;
  const o = ac.createOscillator(), g = ac.createGain();
  o.type = type; o.frequency.setValueAtTime(f0, ac.currentTime);
  o.frequency.exponentialRampToValueAtTime(Math.max(1, f1), ac.currentTime + dur);
  g.gain.setValueAtTime(vol, ac.currentTime);
  g.gain.exponentialRampToValueAtTime(0.001, ac.currentTime + dur);
  o.connect(g); g.connect(ac.destination);
  o.start(); o.stop(ac.currentTime + dur);
}
/* Thruster firing: one short burst of filtered noise + a low thump per tap.
   The game thrusts in pulses, so the sound fires in pulses too — a fresh
   slice of the noise buffer each tap so repeats don't sound mechanical. */
let noiseBuf = null;
function thruster() {
  if (muted) return;
  const ac = audio(); if (!ac) return;
  if (!noiseBuf) {
    noiseBuf = ac.createBuffer(1, ac.sampleRate, ac.sampleRate);
    const d = noiseBuf.getChannelData(0);
    for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
  }
  const t = ac.currentTime;
  const src = ac.createBufferSource();
  src.buffer = noiseBuf;
  src.playbackRate.value = 0.7 + Math.random() * 0.25;
  const flt = ac.createBiquadFilter();
  flt.type = 'lowpass';
  flt.frequency.setValueAtTime(900, t);
  flt.frequency.exponentialRampToValueAtTime(280, t + 0.22);
  flt.Q.value = 0.8;
  const g = ac.createGain();
  g.gain.setValueAtTime(0.0001, t);
  g.gain.exponentialRampToValueAtTime(0.28, t + 0.012);
  g.gain.exponentialRampToValueAtTime(0.001, t + 0.24);
  src.connect(flt); flt.connect(g); g.connect(ac.destination);
  src.start(t, Math.random() * 0.5, 0.26);
  src.stop(t + 0.26);
  const o = ac.createOscillator(), og = ac.createGain();
  o.type = 'sine';
  o.frequency.setValueAtTime(110, t);
  o.frequency.exponentialRampToValueAtTime(48, t + 0.14);
  og.gain.setValueAtTime(0.16, t);
  og.gain.exponentialRampToValueAtTime(0.001, t + 0.15);
  o.connect(og); og.connect(ac.destination);
  o.start(t); o.stop(t + 0.15);
}
function sfx(kind) {
  if (kind === 'thrust') thruster();
  else if (kind === 'score') { tone(660, 660, 0.07, 'sine', 0.09); setTimeout(() => tone(880, 880, 0.09, 'sine', 0.09), 70); }
  else if (kind === 'crash') tone(160, 40, 0.3, 'sawtooth', 0.12);
}

// Every tap restarts the burn at full length — the rockets always fire
// when tapped. Tapping fast keeps the burn going.
function fireBurn() {
  burnT = BURN_TIME;
  sfx('thrust');
}
function emitExhaust() {
  for (let i = 0; i < 2; i++) {
    if (parts.length > 48) return;
    parts.push({ x: SHIP_X + (Math.random() - 0.5) * 12, y: shipY + 16, vx: (Math.random() - 0.5) * 40, vy: 120 + Math.random() * 120, life: 0.3 + Math.random() * 0.25, max: 0.55, r: 3 + Math.random() * 3 });
  }
}
function drawParts() {
  for (const p of parts) {
    const a = Math.max(0, p.life / p.max);
    ctx.fillStyle = 'rgba(255,' + Math.floor(140 + 60 * a) + ',60,' + (0.55 * a).toFixed(3) + ')';
    ctx.beginPath(); ctx.arc(p.x, p.y, p.r * (0.6 + 0.4 * a), 0, Math.PI * 2); ctx.fill();
  }
}
What it does

This file makes every sound in the game. There are no audio files — no MP3s, no downloads. Everything is synthesized live with the WebAudio API: oscillators and filtered noise, built from scratch each time a sound plays.

THE AUDIO CONTEXT

    let AC = null;
    function audio() {
      if (!AC) { try { AC = new (window.AudioContext ||
          window.webkitAudioContext)(); } catch (e) { return null; } }
      if (AC && AC.state === 'suspended') AC.resume();
      return AC;
    }

AC is the AudioContext — the object the browser uses to make sound. It's created lazily (the first time a sound is needed), not at load, because browsers block audio until the user has interacted with the page. If the context is 'suspended' (the browser paused it), audio() resumes it. If anything fails, it returns null and the sound functions quietly do nothing — the game never crashes over audio.

tone() — THE BLIP MAKER

    function tone(f0, f1, dur, type, vol) { ... }

tone() plays one short synthesized note:

  • f0 is the starting frequency, f1 is the ending frequency. The oscillator slides from one to the other over the note (an exponential ramp), which is what makes a blip sound like a blip instead of a flat beep.
  • dur is how long it lasts, in seconds.
  • type is the waveform: 'sine' (smooth), 'sawtooth' (harsh/buzzy).
  • vol is the volume.
  • The gain (volume) starts at vol and ramps down to 0.001 over the note, so it fades out instead of clicking off.

It's used for the score sound (two quick sine blips, 660 then 880 Hz) and the crash sound (a sawtooth sliding from 160 down to 40 Hz over 0.3 seconds — a falling, harsh buzz).

thruster() — THE ROCKET SOUND

The thruster is two sounds layered together, and it's the most interesting audio in the game:

1. FILTERED NOISE (the roar):

  • noiseBuf is a 1-second buffer of pure random noise, created once and reused. White noise is just random numbers, so it sounds like static.
  • Each tap plays a FRESH slice of that buffer (starting at a random offset, playback rate 0.7–0.95), so repeats don't sound mechanical — the same noise never plays twice in a row.
  • The noise runs through a lowpass filter whose cutoff slides from 900 Hz down to 280 Hz over 0.22 seconds. That falling filter is what makes it sound like a rocket burst rather than plain static.
  • The gain ramps up fast (0.012s) to 0.28, then fades to nothing over 0.24 seconds.

2. LOW THUMP (the body):

  • A sine oscillator sliding from 110 Hz down to 48 Hz over 0.14 seconds, at low volume. That's the deep "whump" under the roar.

The comment in the code says it plainly: "The game thrusts in pulses, so the sound fires in pulses too." One tap = one burst = one sound.

sfx(kind) — THE DISPATCHER

    function sfx(kind) {
      if (kind === 'thrust') thruster();
      else if (kind === 'score') { ... two tones ... }
      else if (kind === 'crash') tone(160, 40, 0.3, 'sawtooth', 0.12);
    }

Every other part of the game just calls sfx('thrust'), sfx('score') or sfx('crash') and doesn't care how the sound is made. That's good design: one place decides what each sound is, and the rest of the code just names the moment.

THE MUTE FLAG

muted is a boolean. tone() and thruster() both return immediately if it's true. The mute toggle lives in the input file (file 04) — tapping the top-right corner of the screen, or pressing M.