Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

Add the Apache-2.0 licence3efe3da
JavaScript44 lines2.3 KB
export function createAudio() {  let ac = null, master = null, hum = null, humGain = null, muted = false;  function ensure() {    if (ac) return true;    const C = typeof window !== 'undefined' && (window.AudioContext || window.webkitAudioContext);    if (!C) return false;    ac = new C(); master = ac.createGain(); master.gain.value = muted ? 0 : 0.5;    const comp = ac.createDynamicsCompressor();    comp.threshold.value = -18; comp.ratio.value = 6;    master.connect(comp); comp.connect(ac.destination);    hum = ac.createOscillator(); hum.type = 'sawtooth'; hum.frequency.value = 55;    humGain = ac.createGain(); humGain.gain.value = 0.015;    const lp = ac.createBiquadFilter(); lp.frequency.value = 300;    hum.connect(lp); lp.connect(humGain); humGain.connect(master); hum.start();    return true;  }  function tone(type, f0, f1, dur, vol, delay) {    const t = ac.currentTime + (delay || 0);    const o = ac.createOscillator(), g = ac.createGain();    o.type = type; o.frequency.setValueAtTime(f0, t);    o.frequency.exponentialRampToValueAtTime(Math.max(1, f1), t + dur);    g.gain.setValueAtTime(vol, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dur);    o.connect(g); g.connect(master); o.start(t); o.stop(t + dur + 0.02);  }  const sounds = {    crash() { tone('sawtooth', 160, 30, 0.5, 0.5); tone('square', 90, 25, 0.4, 0.3); },    orb() { tone('sine', 700, 1400, 0.12, 0.25); },    warn() { tone('square', 440, 440, 0.12, 0.15); tone('square', 440, 440, 0.12, 0.15, 0.2); },    alarm() { tone('square', 110, 90, 0.25, 0.12); },    shrink() { tone('sawtooth', 70, 30, 1.2, 0.3); tone('sine', 45, 25, 1.5, 0.35); },    boost() { tone('sawtooth', 200, 900, 0.3, 0.12); },    whoosh() { tone('sawtooth', 1200, 300, 0.12, 0.08); },    countdown() { tone('sine', 660, 660, 0.15, 0.25); },    win() { tone('triangle', 523, 523, 0.2, 0.3); tone('triangle', 659, 659, 0.2, 0.3, 0.15); tone('triangle', 784, 784, 0.4, 0.3, 0.3); },  };  return {    unlock() { if (ensure() && ac.state === 'suspended') ac.resume(); },    play(name) { if (ac && !muted && sounds[name]) sounds[name](); },    engine(f) { if (ac) hum.frequency.value = 45 + 50 * f; },    setMuted(m) { muted = !!m; if (master) master.gain.value = muted ? 0 : 0.5; },    get muted() { return muted; },  };}