"use client"; import { getProgress } from "./progress"; let ctx: AudioContext | null = null; function tone(freq: number, start: number, dur: number, type: OscillatorType = "sine", vol = 0.15) { if (!ctx) return; const o = ctx.createOscillator(); const g = ctx.createGain(); o.type = type; o.frequency.value = freq; g.gain.setValueAtTime(0, ctx.currentTime + start); g.gain.linearRampToValueAtTime(vol, ctx.currentTime + start + 0.01); g.gain.exponentialRampToValueAtTime(0.0001, ctx.currentTime + start + dur); o.connect(g).connect(ctx.destination); o.start(ctx.currentTime + start); o.stop(ctx.currentTime + start + dur + 0.05); } function ready(): boolean { if (typeof window === "undefined" || !getProgress().sound) return false; try { if (!ctx) ctx = new (window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext)(); if (ctx.state === "suspended") void ctx.resume(); return true; } catch { return false; } } export const sfx = { correct() { if (!ready()) return; tone(660, 0, 0.12, "triangle"); tone(990, 0.09, 0.18, "triangle"); }, wrong() { if (!ready()) return; tone(220, 0, 0.18, "sawtooth", 0.08); tone(180, 0.12, 0.22, "sawtooth", 0.08); }, flip() { if (!ready()) return; tone(520, 0, 0.06, "sine", 0.08); }, win() { if (!ready()) return; [523, 659, 784, 1047].forEach((f, i) => tone(f, i * 0.11, 0.25, "triangle")); }, };