|
| 1 | +import React, { |
| 2 | + createContext, |
| 3 | + useCallback, |
| 4 | + useEffect, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | +} from 'react' |
| 9 | +import { useSettings } from 'src/contexts/SettingsContext' |
| 10 | + |
| 11 | +type SoundType = 'move' | 'capture' |
| 12 | + |
| 13 | +export interface SoundContextValue { |
| 14 | + playMoveSound: (isCapture?: boolean) => void |
| 15 | + ready: boolean |
| 16 | +} |
| 17 | + |
| 18 | +export const SoundContext = createContext<SoundContextValue | undefined>( |
| 19 | + undefined, |
| 20 | +) |
| 21 | + |
| 22 | +interface Props { |
| 23 | + children: React.ReactNode |
| 24 | +} |
| 25 | + |
| 26 | +export const SoundProvider: React.FC<Props> = ({ children }) => { |
| 27 | + const { settings } = useSettings() |
| 28 | + |
| 29 | + const ctxRef = useRef<AudioContext | null>(null) |
| 30 | + const gainRef = useRef<GainNode | null>(null) |
| 31 | + const buffersRef = useRef<Map<SoundType, AudioBuffer>>(new Map()) |
| 32 | + const lastTypeRef = useRef<SoundType | null>(null) |
| 33 | + const lastPlayedAtMsRef = useRef(0) |
| 34 | + const initializedRef = useRef(false) |
| 35 | + |
| 36 | + const [ready, setReady] = useState(false) |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (initializedRef.current || typeof window === 'undefined') return |
| 40 | + initializedRef.current = true |
| 41 | + |
| 42 | + let canceled = false |
| 43 | + |
| 44 | + const init = async () => { |
| 45 | + try { |
| 46 | + const AudioCtx: typeof AudioContext | undefined = |
| 47 | + (window as any).AudioContext || (window as any).webkitAudioContext |
| 48 | + if (!AudioCtx) { |
| 49 | + console.warn('Web Audio API not supported; sounds disabled') |
| 50 | + setReady(false) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + const ctx = new AudioCtx() |
| 55 | + const gain = ctx.createGain() |
| 56 | + gain.connect(ctx.destination) |
| 57 | + |
| 58 | + ctxRef.current = ctx |
| 59 | + gainRef.current = gain |
| 60 | + |
| 61 | + const loadBuffer = async (url: string): Promise<AudioBuffer | null> => { |
| 62 | + try { |
| 63 | + const res = await fetch(url) |
| 64 | + const arr = await res.arrayBuffer() |
| 65 | + const buf = await ctx.decodeAudioData(arr) |
| 66 | + return buf |
| 67 | + } catch (e) { |
| 68 | + console.warn('Failed to load sound:', url, e) |
| 69 | + return null |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const [moveBuf, captureBuf] = await Promise.all([ |
| 74 | + loadBuffer('/assets/sound/move.mp3'), |
| 75 | + loadBuffer('/assets/sound/capture.mp3'), |
| 76 | + ]) |
| 77 | + |
| 78 | + if (moveBuf) buffersRef.current.set('move', moveBuf) |
| 79 | + if (captureBuf) buffersRef.current.set('capture', captureBuf) |
| 80 | + |
| 81 | + if (!canceled) setReady(buffersRef.current.size > 0) |
| 82 | + } catch (error) { |
| 83 | + console.warn('Failed to initialize sound:', error) |
| 84 | + if (!canceled) setReady(false) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void init() |
| 89 | + |
| 90 | + return () => { |
| 91 | + canceled = true |
| 92 | + const gain = gainRef.current |
| 93 | + const ctx = ctxRef.current |
| 94 | + try { |
| 95 | + if (gain) gain.disconnect() |
| 96 | + } catch { |
| 97 | + /* noop */ |
| 98 | + } |
| 99 | + gainRef.current = null |
| 100 | + |
| 101 | + try { |
| 102 | + if (ctx) void ctx.close() |
| 103 | + } catch { |
| 104 | + /* noop */ |
| 105 | + } |
| 106 | + ctxRef.current = null |
| 107 | + buffersRef.current.clear() |
| 108 | + setReady(false) |
| 109 | + initializedRef.current = false |
| 110 | + } |
| 111 | + }, []) |
| 112 | + |
| 113 | + const playMoveSound = useCallback( |
| 114 | + (isCapture = false) => { |
| 115 | + if (!settings.soundEnabled) return |
| 116 | + const ctx = ctxRef.current |
| 117 | + const gain = gainRef.current |
| 118 | + if (!ctx || !gain) return |
| 119 | + |
| 120 | + if (ctx.state !== 'running') { |
| 121 | + void ctx.resume() |
| 122 | + } |
| 123 | + |
| 124 | + const type: SoundType = isCapture ? 'capture' : 'move' |
| 125 | + const buffer = buffersRef.current.get(type) |
| 126 | + if (!buffer) return |
| 127 | + |
| 128 | + const nowMs = ctx.currentTime * 1000 |
| 129 | + if ( |
| 130 | + lastTypeRef.current === type && |
| 131 | + nowMs - lastPlayedAtMsRef.current < 30 |
| 132 | + ) { |
| 133 | + return |
| 134 | + } |
| 135 | + lastTypeRef.current = type |
| 136 | + lastPlayedAtMsRef.current = nowMs |
| 137 | + |
| 138 | + try { |
| 139 | + const src = ctx.createBufferSource() |
| 140 | + src.buffer = buffer |
| 141 | + src.connect(gain) |
| 142 | + src.start(0) |
| 143 | + } catch (e) { |
| 144 | + console.warn('Failed to play sound:', type, e) |
| 145 | + } |
| 146 | + }, |
| 147 | + [settings.soundEnabled], |
| 148 | + ) |
| 149 | + |
| 150 | + const value = useMemo<SoundContextValue>( |
| 151 | + () => ({ playMoveSound, ready }), |
| 152 | + [playMoveSound, ready], |
| 153 | + ) |
| 154 | + |
| 155 | + return <SoundContext.Provider value={value}>{children}</SoundContext.Provider> |
| 156 | +} |
0 commit comments