|
1 | | -// Subtle Web Audio chimes for the TV scoreboard (no audio files or external requests). |
2 | | -// Only plays when the user explicitly enables sound in the TV header. |
| 1 | +// Loud, broadcast-style Web Audio fanfares for the TV scoreboard (no external audio files). |
| 2 | +// Built with a dynamics compressor + bright brass/chime harmonics so alerts cut through a loud |
| 3 | +// TV and Thanksgiving living room. Only plays when sound is enabled in the TV header. |
3 | 4 |
|
4 | | -export type ChimeKind = 'banked' | 'final' | 'confirm'; |
| 5 | +export type ChimeKind = 'confirm' | 'swing' | 'scoring' | 'banked' | 'final'; |
| 6 | + |
| 7 | +export interface ChimeOptions { |
| 8 | + /** Adds a turkey gobble + enthusiastic "BOOM! Turducken!" callout via Web Speech. */ |
| 9 | + turducken?: boolean; |
| 10 | +} |
5 | 11 |
|
6 | 12 | let sharedCtx: AudioContext | null = null; |
| 13 | +let masterBus: AudioNode | null = null; |
7 | 14 |
|
8 | | -function audioContext(): AudioContext | null { |
| 15 | +function audioGraph(): { ctx: AudioContext; out: AudioNode } | null { |
9 | 16 | if (typeof window === 'undefined') return null; |
10 | 17 | const Ctor = window.AudioContext ?? (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext; |
11 | 18 | if (!Ctor) return null; |
12 | 19 | try { |
13 | | - if (!sharedCtx) sharedCtx = new Ctor(); |
| 20 | + if (!sharedCtx) { |
| 21 | + sharedCtx = new Ctor(); |
| 22 | + const comp = sharedCtx.createDynamicsCompressor(); |
| 23 | + comp.threshold.value = -14; |
| 24 | + comp.knee.value = 10; |
| 25 | + comp.ratio.value = 8; |
| 26 | + comp.attack.value = 0.003; |
| 27 | + comp.release.value = 0.14; |
| 28 | + |
| 29 | + const master = sharedCtx.createGain(); |
| 30 | + master.gain.value = 0.92; |
| 31 | + comp.connect(master); |
| 32 | + master.connect(sharedCtx.destination); |
| 33 | + masterBus = comp; |
| 34 | + } |
14 | 35 | if (sharedCtx.state === 'suspended') void sharedCtx.resume().catch(() => {}); |
15 | | - return sharedCtx; |
| 36 | + return { ctx: sharedCtx, out: masterBus! }; |
16 | 37 | } catch { |
17 | 38 | return null; |
18 | 39 | } |
19 | 40 | } |
20 | 41 |
|
21 | | -const NOTES: Record<ChimeKind, { freq: number; delay: number; duration: number; gain: number }[]> = { |
22 | | - // Soft single-note click confirmation when unmuting. |
23 | | - confirm: [{ freq: 587.33, delay: 0, duration: 0.16, gain: 0.08 }], // D5 |
24 | | - // Crisp two-note upward fifth when points get banked mid-game. |
25 | | - banked: [ |
26 | | - { freq: 523.25, delay: 0, duration: 0.22, gain: 0.1 }, // C5 |
27 | | - { freq: 783.99, delay: 0.11, duration: 0.34, gain: 0.11 }, // G5 |
28 | | - ], |
29 | | - // Warm three-note major triad when a game goes final. |
30 | | - final: [ |
31 | | - { freq: 523.25, delay: 0, duration: 0.22, gain: 0.1 }, // C5 |
32 | | - { freq: 659.25, delay: 0.11, duration: 0.24, gain: 0.1 }, // E5 |
33 | | - { freq: 783.99, delay: 0.22, duration: 0.44, gain: 0.12 }, // G5 |
34 | | - ], |
35 | | -}; |
36 | | - |
37 | | -/** Plays a soft chime; safe no-op when audio is unsupported or blocked. */ |
38 | | -export function playChime(kind: ChimeKind): void { |
39 | | - const ctx = audioContext(); |
40 | | - if (!ctx) return; |
| 42 | +/** Punchy NFL-broadcast-style brass horn note (detuned saws + sub square through a bright filter sweep). */ |
| 43 | +function brassNote(ctx: AudioContext, out: AudioNode, freq: number, start: number, duration: number, level = 0.55): void { |
| 44 | + const filter = ctx.createBiquadFilter(); |
| 45 | + filter.type = 'lowpass'; |
| 46 | + filter.Q.value = 2.4; |
| 47 | + filter.frequency.setValueAtTime(900, start); |
| 48 | + filter.frequency.exponentialRampToValueAtTime(3800, start + 0.045); |
| 49 | + filter.frequency.exponentialRampToValueAtTime(2100, start + duration); |
| 50 | + |
| 51 | + const env = ctx.createGain(); |
| 52 | + env.gain.setValueAtTime(0.0001, start); |
| 53 | + env.gain.linearRampToValueAtTime(level, start + 0.018); |
| 54 | + env.gain.setValueAtTime(level * 0.88, start + Math.max(0.03, duration * 0.65)); |
| 55 | + env.gain.exponentialRampToValueAtTime(0.0001, start + duration); |
| 56 | + |
| 57 | + filter.connect(env); |
| 58 | + env.connect(out); |
| 59 | + |
| 60 | + const voices: { type: OscillatorType; mult: number; detune: number; gain: number }[] = [ |
| 61 | + { type: 'sawtooth', mult: 1, detune: -7, gain: 0.45 }, |
| 62 | + { type: 'sawtooth', mult: 1, detune: 7, gain: 0.45 }, |
| 63 | + { type: 'square', mult: 0.5, detune: 0, gain: 0.35 }, |
| 64 | + ]; |
| 65 | + |
| 66 | + for (const v of voices) { |
| 67 | + const osc = ctx.createOscillator(); |
| 68 | + const vg = ctx.createGain(); |
| 69 | + osc.type = v.type; |
| 70 | + osc.frequency.setValueAtTime(freq * v.mult, start); |
| 71 | + osc.detune.setValueAtTime(v.detune, start); |
| 72 | + vg.gain.value = v.gain; |
| 73 | + osc.connect(vg); |
| 74 | + vg.connect(filter); |
| 75 | + osc.start(start); |
| 76 | + osc.stop(start + duration + 0.03); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** Crisp high-presence broadcast chime / bell hit that cuts cleanly over room chatter. */ |
| 81 | +function bellNote(ctx: AudioContext, out: AudioNode, freq: number, start: number, duration: number, level = 0.42): void { |
| 82 | + const partials: { mult: number; gain: number; decay: number }[] = [ |
| 83 | + { mult: 1, gain: 1.0, decay: duration }, |
| 84 | + { mult: 2, gain: 0.48, decay: duration * 0.72 }, |
| 85 | + { mult: 2.756, gain: 0.25, decay: duration * 0.45 }, |
| 86 | + ]; |
| 87 | + for (const p of partials) { |
| 88 | + const osc = ctx.createOscillator(); |
| 89 | + const env = ctx.createGain(); |
| 90 | + osc.type = p.mult === 1 ? 'triangle' : 'sine'; |
| 91 | + osc.frequency.setValueAtTime(freq * p.mult, start); |
| 92 | + env.gain.setValueAtTime(0.0001, start); |
| 93 | + env.gain.linearRampToValueAtTime(level * p.gain, start + 0.008); |
| 94 | + env.gain.exponentialRampToValueAtTime(0.0001, start + p.decay); |
| 95 | + osc.connect(env); |
| 96 | + env.connect(out); |
| 97 | + osc.start(start); |
| 98 | + osc.stop(start + p.decay + 0.02); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** Synthesized loud turkey gobble (rapid FM wobble through a nasal vocal formant filter). */ |
| 103 | +function playGobble(ctx: AudioContext, out: AudioNode, start: number): void { |
| 104 | + const duration = 0.58; |
| 105 | + const carrier = ctx.createOscillator(); |
| 106 | + carrier.type = 'sawtooth'; |
| 107 | + carrier.frequency.setValueAtTime(780, start); |
| 108 | + carrier.frequency.exponentialRampToValueAtTime(520, start + duration); |
| 109 | + |
| 110 | + const lfo = ctx.createOscillator(); |
| 111 | + lfo.type = 'sine'; |
| 112 | + lfo.frequency.setValueAtTime(17, start); |
| 113 | + const lfoDepth = ctx.createGain(); |
| 114 | + lfoDepth.gain.setValueAtTime(145, start); |
| 115 | + lfo.connect(lfoDepth); |
| 116 | + lfoDepth.connect(carrier.frequency); |
| 117 | + |
| 118 | + const formant = ctx.createBiquadFilter(); |
| 119 | + formant.type = 'bandpass'; |
| 120 | + formant.frequency.setValueAtTime(1250, start); |
| 121 | + formant.Q.value = 3.2; |
| 122 | + |
| 123 | + const env = ctx.createGain(); |
| 124 | + env.gain.setValueAtTime(0.0001, start); |
| 125 | + env.gain.linearRampToValueAtTime(0.75, start + 0.03); |
| 126 | + env.gain.setValueAtTime(0.68, start + duration * 0.72); |
| 127 | + env.gain.exponentialRampToValueAtTime(0.0001, start + duration); |
| 128 | + |
| 129 | + carrier.connect(formant); |
| 130 | + formant.connect(env); |
| 131 | + env.connect(out); |
| 132 | + |
| 133 | + carrier.start(start); |
| 134 | + lfo.start(start); |
| 135 | + carrier.stop(start + duration + 0.03); |
| 136 | + lfo.stop(start + duration + 0.03); |
| 137 | +} |
| 138 | + |
| 139 | +/** Enthusiastic Web Speech shout for Turducken mode (safe no-op if unsupported). */ |
| 140 | +function speakTurducken(kind: ChimeKind): void { |
| 141 | + if (typeof window === 'undefined' || typeof window.speechSynthesis === 'undefined') return; |
| 142 | + try { |
| 143 | + const phrase = |
| 144 | + kind === 'final' |
| 145 | + ? 'Boom! Game over! Turducken!' |
| 146 | + : kind === 'banked' || kind === 'scoring' || kind === 'confirm' |
| 147 | + ? 'Boom! Turducken!' |
| 148 | + : 'Turducken!'; |
| 149 | + window.speechSynthesis.cancel(); |
| 150 | + const utter = new SpeechSynthesisUtterance(phrase); |
| 151 | + utter.volume = 1; |
| 152 | + utter.rate = 1.05; |
| 153 | + utter.pitch = 0.85; |
| 154 | + window.speechSynthesis.speak(utter); |
| 155 | + } catch { |
| 156 | + // Ignore speech synthesis errors on restricted browsers. |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** Maps a TV alert to its sound cue. */ |
| 161 | +export function alertChimeKind(alert: { kind: 'final' | 'banked' | 'scoring' | 'swing'; banked: readonly unknown[] }): ChimeKind { |
| 162 | + if (alert.kind === 'final') return 'final'; |
| 163 | + if (alert.kind === 'banked' || alert.banked.length > 0) return 'banked'; |
| 164 | + if (alert.kind === 'scoring') return 'scoring'; |
| 165 | + return 'swing'; |
| 166 | +} |
| 167 | + |
| 168 | +/** Plays a prominent stadium broadcast alert; safe no-op when audio is unsupported or blocked. */ |
| 169 | +export function playChime(kind: ChimeKind, opts: ChimeOptions = {}): void { |
| 170 | + const graph = audioGraph(); |
| 171 | + if (!graph) return; |
| 172 | + const { ctx, out } = graph; |
41 | 173 | try { |
42 | | - const t0 = ctx.currentTime + 0.01; |
43 | | - for (const note of NOTES[kind]) { |
44 | | - const osc = ctx.createOscillator(); |
45 | | - const gain = ctx.createGain(); |
46 | | - osc.type = 'triangle'; |
47 | | - osc.frequency.setValueAtTime(note.freq, t0 + note.delay); |
48 | | - gain.gain.setValueAtTime(0.0001, t0 + note.delay); |
49 | | - gain.gain.linearRampToValueAtTime(note.gain, t0 + note.delay + 0.02); |
50 | | - gain.gain.exponentialRampToValueAtTime(0.0001, t0 + note.delay + note.duration); |
51 | | - osc.connect(gain); |
52 | | - gain.connect(ctx.destination); |
53 | | - osc.start(t0 + note.delay); |
54 | | - osc.stop(t0 + note.delay + note.duration + 0.02); |
| 174 | + const t0 = ctx.currentTime + 0.015; |
| 175 | + switch (kind) { |
| 176 | + case 'confirm': |
| 177 | + // Punchy 2-hit stadium test sting so you can set TV volume right away. |
| 178 | + brassNote(ctx, out, 392.0, t0, 0.18, 0.62); // G4 |
| 179 | + bellNote(ctx, out, 783.99, t0, 0.24, 0.45); // G5 |
| 180 | + brassNote(ctx, out, 523.25, t0 + 0.16, 0.36, 0.72); // C5 |
| 181 | + bellNote(ctx, out, 1046.5, t0 + 0.16, 0.48, 0.5); // C6 |
| 182 | + break; |
| 183 | + |
| 184 | + case 'swing': |
| 185 | + // Bright triple broadcast alert bell + brass stab (turnovers, 3-pt swings, 1st overall). |
| 186 | + brassNote(ctx, out, 392.0, t0, 0.22, 0.6); // G4 |
| 187 | + bellNote(ctx, out, 783.99, t0, 0.26, 0.55); // G5 |
| 188 | + bellNote(ctx, out, 987.77, t0 + 0.13, 0.26, 0.58); // B5 |
| 189 | + brassNote(ctx, out, 587.33, t0 + 0.26, 0.42, 0.68); // D5 |
| 190 | + bellNote(ctx, out, 1174.66, t0 + 0.26, 0.56, 0.6); // D6 |
| 191 | + break; |
| 192 | + |
| 193 | + case 'scoring': |
| 194 | + // Bold 4-note NFL-style scoring brass fanfare (TDs, FGs, safeties). |
| 195 | + brassNote(ctx, out, 392.0, t0, 0.16, 0.65); // G4 |
| 196 | + brassNote(ctx, out, 523.25, t0 + 0.14, 0.16, 0.7); // C5 |
| 197 | + brassNote(ctx, out, 659.25, t0 + 0.28, 0.18, 0.72); // E5 |
| 198 | + brassNote(ctx, out, 783.99, t0 + 0.44, 0.58, 0.8); // G5 |
| 199 | + brassNote(ctx, out, 523.25, t0 + 0.44, 0.58, 0.5); // C5 harmony |
| 200 | + bellNote(ctx, out, 1567.98, t0 + 0.44, 0.72, 0.52); // G6 shimmer |
| 201 | + break; |
| 202 | + |
| 203 | + case 'banked': |
| 204 | + // Celebratory rising stadium horn fanfare + chime chord when points lock in! |
| 205 | + brassNote(ctx, out, 523.25, t0, 0.15, 0.68); // C5 |
| 206 | + bellNote(ctx, out, 1046.5, t0, 0.22, 0.5); // C6 |
| 207 | + brassNote(ctx, out, 659.25, t0 + 0.14, 0.15, 0.72); // E5 |
| 208 | + bellNote(ctx, out, 1318.51, t0 + 0.14, 0.22, 0.52); // E6 |
| 209 | + brassNote(ctx, out, 783.99, t0 + 0.28, 0.18, 0.76); // G5 |
| 210 | + bellNote(ctx, out, 1567.98, t0 + 0.28, 0.28, 0.55); // G6 |
| 211 | + brassNote(ctx, out, 1046.5, t0 + 0.46, 0.68, 0.82); // C6 |
| 212 | + brassNote(ctx, out, 523.25, t0 + 0.46, 0.68, 0.55); // C5 |
| 213 | + bellNote(ctx, out, 2093.0, t0 + 0.46, 0.85, 0.52); // C7 |
| 214 | + break; |
| 215 | + |
| 216 | + case 'final': |
| 217 | + // Big full victory brass + bell fanfare on Game Final. |
| 218 | + brassNote(ctx, out, 392.0, t0, 0.14, 0.68); // G4 |
| 219 | + brassNote(ctx, out, 523.25, t0 + 0.13, 0.14, 0.72); // C5 |
| 220 | + brassNote(ctx, out, 659.25, t0 + 0.26, 0.14, 0.74); // E5 |
| 221 | + brassNote(ctx, out, 783.99, t0 + 0.39, 0.22, 0.78); // G5 |
| 222 | + brassNote(ctx, out, 659.25, t0 + 0.62, 0.16, 0.72); // E5 |
| 223 | + brassNote(ctx, out, 783.99, t0 + 0.78, 0.82, 0.85); // G5 |
| 224 | + brassNote(ctx, out, 523.25, t0 + 0.78, 0.82, 0.65); // C5 |
| 225 | + brassNote(ctx, out, 1046.5, t0 + 0.78, 0.82, 0.72); // C6 |
| 226 | + bellNote(ctx, out, 1567.98, t0 + 0.78, 1.0, 0.55); // G6 |
| 227 | + bellNote(ctx, out, 2093.0, t0 + 0.78, 1.0, 0.45); // C7 |
| 228 | + break; |
| 229 | + } |
| 230 | + |
| 231 | + if (opts.turducken) { |
| 232 | + const gobbleDelay = kind === 'confirm' ? 0.22 : kind === 'swing' ? 0.35 : 0.52; |
| 233 | + playGobble(ctx, out, t0 + gobbleDelay); |
| 234 | + speakTurducken(kind); |
55 | 235 | } |
56 | 236 | } catch { |
57 | 237 | // Ignore autoplay / hardware audio failures. |
|
0 commit comments