// Sled Volt sound: every sound is synthesised with WebAudio (no samples, no downloads): runner hiss that follows your // speed, wind, jump, landing thump, coin chimes that climb a scale along a streak, power-ups, crash, VOLT boost, // grind, the Rimehorn's roar and a procedural music loop. Mute and music toggles are remembered. (c) 2026 CyberMax. export class Sound { constructor() { this.ctx = null; this.on = true; this.music = true; this.started = false; try { const s = JSON.parse(localStorage.getItem('sledvolt:sound') || 'null'); if (s) { this.on = s.on !== false; this.music = s.music !== false; } } catch { /* private mode */ } } save() { try { localStorage.setItem('sledvolt:sound', JSON.stringify({ on: this.on, music: this.music })); } catch { /* */ } } /** Call from a user gesture (tap / key): creates and resumes the audio context. */ unlock() { if (!this.ctx) { const AC = window.AudioContext || window.webkitAudioContext; if (!AC) return; this.ctx = new AC(); const c = this.ctx; this.master = c.createGain(); this.master.gain.value = this.on ? 0.8 : 0; this.master.connect(c.destination); this.comp = c.createDynamicsCompressor(); this.comp.threshold.value = -14; this.comp.ratio.value = 4; this.comp.connect(this.master); this.sfx = c.createGain(); this.sfx.gain.value = 0.9; this.sfx.connect(this.comp); this.mus = c.createGain(); this.mus.gain.value = this.music ? 0.32 : 0; this.mus.connect(this.comp); const len = c.sampleRate * 2; this.noise = c.createBuffer(1, len, c.sampleRate); const d = this.noise.getChannelData(0); for (let i = 0; i < len; i++) d[i] = Math.random() * 2 - 1; // continuous layers: hiss (runners on snow), wind, grind this.hiss = this.loopNoise('bandpass', 2400, 0.6); this.wind = this.loopNoise('lowpass', 600, 0.4); this.grindL = this.loopNoise('bandpass', 5200, 8); this.grindT = c.createOscillator(); this.grindT.type = 'square'; this.grindT.frequency.value = 1150; this.grindTG = c.createGain(); this.grindTG.gain.value = 0; this.grindT.connect(this.grindTG); this.grindTG.connect(this.sfx); this.grindT.start(); this.beat = 0; this.nextT = 0; this.intensity = 0; this.timer = setInterval(() => this.schedule(), 90); } if (this.ctx.state === 'suspended') this.ctx.resume().catch(() => {}); this.started = true; } loopNoise(type, freq, q) { const c = this.ctx; const src = c.createBufferSource(); src.buffer = this.noise; src.loop = true; const f = c.createBiquadFilter(); f.type = type; f.frequency.value = freq; f.Q.value = q; const g = c.createGain(); g.gain.value = 0; src.connect(f); f.connect(g); g.connect(this.sfx); src.start(); return { f, g }; } toggle() { this.on = !this.on; if (this.master) this.master.gain.setTargetAtTime(this.on ? 0.8 : 0, this.ctx.currentTime, 0.05); this.save(); return this.on; } toggleMusic() { this.music = !this.music; if (this.mus) this.mus.gain.setTargetAtTime(this.music ? 0.32 : 0, this.ctx.currentTime, 0.1); this.save(); return this.music; } /** Every frame while a run is on: speed (units/s), on ground, airborne, grinding, boosting. */ drive(s) { if (!this.ctx) return; const t = this.ctx.currentTime, k = Math.min(1, Math.max(0, (s.v - 30) / 70)); const ground = s.alive && s.ground && !s.grind; this.hiss.g.gain.setTargetAtTime(ground ? 0.05 + k * 0.12 : 0, t, 0.05); this.hiss.f.frequency.setTargetAtTime(1600 + k * 2600 + (s.duck ? 600 : 0), t, 0.1); this.wind.g.gain.setTargetAtTime(s.alive ? 0.04 + k * 0.1 + (s.air ? 0.08 : 0) + (s.boost ? 0.12 : 0) : 0.02, t, 0.2); this.wind.f.frequency.setTargetAtTime(400 + k * 900 + (s.boost ? 900 : 0), t, 0.2); this.grindL.g.gain.setTargetAtTime(s.grind && s.alive ? 0.16 : 0, t, 0.03); this.grindTG.gain.setTargetAtTime(s.grind && s.alive ? 0.018 : 0, t, 0.03); this.intensity = s.alive ? k : 0; this.playing = s.alive; } quiet() { if (!this.ctx) return; const t = this.ctx.currentTime; for (const L of [this.hiss, this.wind, this.grindL]) L.g.gain.setTargetAtTime(0, t, 0.08); this.grindTG.gain.setTargetAtTime(0, t, 0.05); } tone(type, f0, f1, dur, vol, when = 0, out) { const c = this.ctx; const t = c.currentTime + when; const o = c.createOscillator(); o.type = type; o.frequency.setValueAtTime(f0, t); if (f1 !== f0) o.frequency.exponentialRampToValueAtTime(Math.max(20, f1), t + dur); const g = c.createGain(); g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(vol, t + 0.008); g.gain.exponentialRampToValueAtTime(0.0001, t + dur); o.connect(g); g.connect(out || this.sfx); o.start(t); o.stop(t + dur + 0.05); } puff(type, f0, f1, dur, vol, q = 1, when = 0) { const c = this.ctx; const t = c.currentTime + when; const src = c.createBufferSource(); src.buffer = this.noise; src.playbackRate.value = 0.8 + Math.random() * 0.4; const f = c.createBiquadFilter(); f.type = type; f.Q.value = q; f.frequency.setValueAtTime(f0, t); f.frequency.exponentialRampToValueAtTime(Math.max(40, f1), t + dur); const g = c.createGain(); g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(vol, t + 0.01); g.gain.exponentialRampToValueAtTime(0.0001, t + dur); src.connect(f); f.connect(g); g.connect(this.sfx); src.start(t, Math.random()); src.stop(t + dur + 0.05); } /** Game events from core.js step(). */ play(e) { if (!this.ctx || !this.on) return; const SCALE = [0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24]; switch (e.type) { case 'lane': this.puff('bandpass', 900, 2600, 0.12, 0.12, 2); break; case 'jump': this.tone('sine', 260, 520, 0.16, 0.16); this.puff('highpass', 1200, 3000, 0.1, 0.06); break; case 'ramp': this.tone('sawtooth', 180, 720, 0.35, 0.08); this.puff('bandpass', 600, 4000, 0.4, 0.14, 1.5); break; case 'duck': this.puff('lowpass', 1500, 500, 0.12, 0.08); break; case 'slam': this.puff('bandpass', 2400, 400, 0.15, 0.12, 1.5); break; case 'land': this.tone('sine', e.from === 'ramp' ? 140 : 110, 45, 0.2, e.from === 'ramp' ? 0.4 : 0.25); this.puff('lowpass', 900, 200, 0.18, 0.2); break; case 'coin': { const n = SCALE[Math.min(SCALE.length - 1, (e.n || 1) % 11)]; const f = 880 * Math.pow(2, n / 12); this.tone('triangle', f, f, 0.12, 0.09); this.tone('sine', f * 2, f * 2, 0.18, 0.04, 0.03); break; } case 'frost': this.tone('sine', 1760, 2637, 0.25, 0.08); this.tone('triangle', 2637, 2637, 0.3, 0.05, 0.06); break; case 'power': case 'gift': [523, 659, 784, 1047].forEach((f, i) => this.tone('square', f, f, 0.1, 0.05, i * 0.06)); break; case 'boost': this.puff('bandpass', 300, 5000, 0.6, 0.25, 1.2); this.tone('sawtooth', 110, 880, 0.55, 0.09); break; case 'near': this.puff('bandpass', 3500, 1200, 0.14, 0.12, 4); this.tone('sine', 1320, 1760, 0.08, 0.04, 0.02); break; case 'smash': case 'smashed': case 'shieldbreak': this.puff('lowpass', 3000, 300, 0.3, 0.35); this.tone('square', 220, 80, 0.2, 0.08); break; case 'stunt': [784, 1047, 1319].forEach((f, i) => this.tone('triangle', f, f, 0.12, 0.06, i * 0.05)); break; case 'stumble': case 'bump': this.tone('sine', 330, 140, 0.25, 0.2); this.puff('lowpass', 1200, 300, 0.2, 0.25); this.roar(0.7); break; case 'crash': case 'caught': this.puff('lowpass', 2500, 80, 0.9, 0.5); this.tone('sine', 90, 30, 0.8, 0.5); this.tone('square', 440, 110, 0.5, 0.06, 0.05); if (e.type === 'caught') this.roar(1.2); break; case 'grind': this.tone('square', 1500, 1200, 0.06, 0.05); break; case 'edge': this.tone('sine', 160, 120, 0.06, 0.08); break; case 'finish': [523, 659, 784, 1047, 1319].forEach((f, i) => this.tone('triangle', f, f, 0.18, 0.08, i * 0.08)); break; case 'ui': this.tone('sine', 900, 1200, 0.05, 0.06); break; case 'go': this.tone('square', 880, 880, 0.18, 0.08); break; case 'count': this.tone('square', 440, 440, 0.12, 0.07); break; case 'record': [659, 784, 988, 1319, 1568].forEach((f, i) => this.tone('triangle', f, f, 0.2, 0.08, i * 0.09)); break; default: break; } } roar(len) { const c = this.ctx; const t = c.currentTime; const o = c.createOscillator(); o.type = 'sawtooth'; o.frequency.setValueAtTime(95, t); o.frequency.linearRampToValueAtTime(70, t + len); const lfo = c.createOscillator(); lfo.frequency.value = 23; const lg = c.createGain(); lg.gain.value = 18; lfo.connect(lg); lg.connect(o.frequency); const f = c.createBiquadFilter(); f.type = 'lowpass'; f.frequency.value = 700; f.Q.value = 6; const g = c.createGain(); g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(0.22, t + 0.08); g.gain.exponentialRampToValueAtTime(0.0001, t + len); o.connect(f); f.connect(g); g.connect(this.sfx); o.start(t); lfo.start(t); o.stop(t + len + 0.1); lfo.stop(t + len + 0.1); this.puff('bandpass', 400, 200, len * 0.8, 0.18, 3); } // procedural music: 132 bpm, a bright i-VI-III-VII loop with bass, plucks and hats that come in with speed schedule() { const c = this.ctx; if (!c || c.state !== 'running') return; const spb = 60 / 132 / 2; // eighth notes if (this.nextT < c.currentTime) this.nextT = c.currentTime + 0.05; const prog = [[57, 60, 64], [53, 57, 60], [60, 64, 67], [55, 59, 62]]; // Am F C G while (this.nextT < c.currentTime + 0.25) { const b = this.beat, bar = Math.floor(b / 8) % 4, step = b % 8; const ch = prog[bar]; const hz = (m) => 440 * Math.pow(2, (m - 69) / 12); const at = this.nextT - c.currentTime; if (this.music && this.on) { if (step % 2 === 0) this.tone('triangle', hz(ch[0] - 24), hz(ch[0] - 24), spb * 1.8, 0.16, at, this.mus); const arp = [0, 1, 2, 1, 2, 0, 1, 2][step]; this.tone('square', hz(ch[arp] + 12), hz(ch[arp] + 12), spb * 0.8, 0.035 + this.intensity * 0.02, at, this.mus); if (this.playing && this.intensity > 0.15) this.hat(at, step % 2 ? 0.03 : 0.05); if (this.playing && step % 4 === 0) this.kick(at); if (step === 0 && bar === 0) this.tone('sine', hz(ch[2] + 24), hz(ch[2] + 24), spb * 6, 0.025, at, this.mus); } this.beat++; this.nextT += spb; } } hat(at, v) { const c = this.ctx; const t = c.currentTime + at; const s = c.createBufferSource(); s.buffer = this.noise; const f = c.createBiquadFilter(); f.type = 'highpass'; f.frequency.value = 7000; const g = c.createGain(); g.gain.setValueAtTime(v, t); g.gain.exponentialRampToValueAtTime(0.0001, t + 0.05); s.connect(f); f.connect(g); g.connect(this.mus); s.start(t, Math.random()); s.stop(t + 0.07); } kick(at) { this.tone('sine', 140, 45, 0.18, 0.35, at, this.mus); } }