// Web Audio API Synthesizer for Retro RPG Dialogue, Chimes, and Fanfares let audioCtx: AudioContext | null = null; type CustomWindow = Window & typeof globalThis & { webkitAudioContext?: typeof AudioContext; }; function getAudioContext(): AudioContext | null { if (typeof window === 'undefined') return null; if (!audioCtx) { const customWin = window as CustomWindow; const AudioContextClass = window.AudioContext || customWin.webkitAudioContext; if (AudioContextClass) { audioCtx = new AudioContextClass(); } } if (audioCtx && audioCtx.state === 'suspended') { audioCtx.resume().catch(() => {}); } return audioCtx; } export function isAudioMuted(): boolean { try { const saved = localStorage.getItem('companion_sound_enabled'); return saved === 'false'; } catch { return false; } } export function setAudioMuted(muted: boolean): void { try { localStorage.setItem('companion_sound_enabled', muted ? 'false' : 'true'); } catch { // Graceful fallback if localStorage is disabled } } /** * Typewriter dialogue character blip (disabled) */ export function playTypewriterBlip(_pitchVariation: number = 0): void { // Disabled: typewriter blip sound removed } /** * Playful RPG character pop-up chime (C5 -> E5 -> G5 -> C6) */ export function playPopupChime(): void { if (isAudioMuted()) return; const ctx = getAudioContext(); if (!ctx) return; try { const notes = [523.25, 659.25, 783.99, 1046.5]; // C5, E5, G5, C6 const startTime = ctx.currentTime; notes.forEach((freq, idx) => { const osc = ctx.createOscillator(); const gain = ctx.createGain(); osc.type = 'sine'; osc.frequency.setValueAtTime(freq, startTime + idx * 0.06); gain.gain.setValueAtTime(0.06, startTime + idx * 0.06); gain.gain.exponentialRampToValueAtTime(0.001, startTime + idx * 0.06 + 0.18); osc.connect(gain); gain.connect(ctx.destination); osc.start(startTime + idx * 0.06); osc.stop(startTime + idx * 0.06 + 0.2); }); } catch { // Ignore audio errors gracefully } } /** * Gentle break reminder tone (Warm harp-like interval) */ export function playBreakReminderTone(): void { if (isAudioMuted()) return; const ctx = getAudioContext(); if (!ctx) return; try { const notes = [440.0, 554.37, 659.25, 880.0]; // A4, C#5, E5, A5 const startTime = ctx.currentTime; notes.forEach((freq, idx) => { const osc = ctx.createOscillator(); const gain = ctx.createGain(); osc.type = 'triangle'; osc.frequency.setValueAtTime(freq, startTime + idx * 0.09); gain.gain.setValueAtTime(0.07, startTime + idx * 0.09); gain.gain.exponentialRampToValueAtTime(0.0001, startTime + idx * 0.09 + 0.35); osc.connect(gain); gain.connect(ctx.destination); osc.start(startTime + idx * 0.09); osc.stop(startTime + idx * 0.09 + 0.4); }); } catch { // Ignore audio errors gracefully } } /** * Tower floor milestone level-up fanfare */ export function playLevelUpFanfare(): void { if (isAudioMuted()) return; const ctx = getAudioContext(); if (!ctx) return; try { const melody = [ { freq: 523.25, time: 0.0, dur: 0.1 }, // C5 { freq: 659.25, time: 0.1, dur: 0.1 }, // E5 { freq: 783.99, time: 0.2, dur: 0.1 }, // G5 { freq: 1046.5, time: 0.3, dur: 0.25 }, // C6 { freq: 880.0, time: 0.55, dur: 0.12 }, // A5 { freq: 1046.5, time: 0.7, dur: 0.45 }, // C6 hold ]; const startTime = ctx.currentTime; melody.forEach((item) => { const osc = ctx.createOscillator(); const gain = ctx.createGain(); osc.type = 'triangle'; osc.frequency.setValueAtTime(item.freq, startTime + item.time); gain.gain.setValueAtTime(0.09, startTime + item.time); gain.gain.exponentialRampToValueAtTime(0.0001, startTime + item.time + item.dur); osc.connect(gain); gain.connect(ctx.destination); osc.start(startTime + item.time); osc.stop(startTime + item.time + item.dur + 0.05); }); } catch { // Ignore audio errors gracefully } }