diff --git a/web/src/utils/sounds.ts b/web/src/utils/sounds.ts index 6760711..841f7d9 100644 --- a/web/src/utils/sounds.ts +++ b/web/src/utils/sounds.ts @@ -4,17 +4,20 @@ function getAudioContext(): AudioContext { if (!audioCtx) { audioCtx = new AudioContext(); } + if (audioCtx.state === 'suspended') { + audioCtx.resume(); + } return audioCtx; } function playTone( + ctx: AudioContext, frequency: number, startTime: number, duration: number, type: OscillatorType = 'sine', volume = 0.3, ) { - const ctx = getAudioContext(); const osc = ctx.createOscillator(); const gain = ctx.createGain(); @@ -30,27 +33,33 @@ function playTone( } export function playCorrectSound() { - const ctx = getAudioContext(); - const now = ctx.currentTime; - // Cheerful ascending two-note ding - playTone(523.25, now, 0.15, 'sine', 0.25); // C5 - playTone(659.25, now + 0.12, 0.2, 'sine', 0.25); // E5 + try { + const ctx = getAudioContext(); + const now = ctx.currentTime; + // Cheerful ascending two-note ding + playTone(ctx, 523.25, now, 0.15, 'sine', 0.25); // C5 + playTone(ctx, 659.25, now + 0.12, 0.2, 'sine', 0.25); // E5 + } catch { /* audio unavailable */ } } export function playIncorrectSound() { - const ctx = getAudioContext(); - const now = ctx.currentTime; - // Gentle descending two-note - playTone(330, now, 0.15, 'triangle', 0.2); // E4 - playTone(262, now + 0.12, 0.25, 'triangle', 0.2); // C4 + try { + const ctx = getAudioContext(); + const now = ctx.currentTime; + // Gentle descending two-note + playTone(ctx, 330, now, 0.15, 'triangle', 0.2); // E4 + playTone(ctx, 262, now + 0.12, 0.25, 'triangle', 0.2); // C4 + } catch { /* audio unavailable */ } } export function playCompletionSound() { - const ctx = getAudioContext(); - const now = ctx.currentTime; - // Ascending fanfare - playTone(523.25, now, 0.15, 'sine', 0.2); // C5 - playTone(659.25, now + 0.1, 0.15, 'sine', 0.2); // E5 - playTone(783.99, now + 0.2, 0.15, 'sine', 0.2); // G5 - playTone(1046.5, now + 0.3, 0.35, 'sine', 0.25); // C6 + try { + const ctx = getAudioContext(); + const now = ctx.currentTime; + // Ascending fanfare + playTone(ctx, 523.25, now, 0.15, 'sine', 0.2); // C5 + playTone(ctx, 659.25, now + 0.1, 0.15, 'sine', 0.2); // E5 + playTone(ctx, 783.99, now + 0.2, 0.15, 'sine', 0.2); // G5 + playTone(ctx, 1046.5, now + 0.3, 0.35, 'sine', 0.25); // C6 + } catch { /* audio unavailable */ } }