27 lines
851 B
TypeScript
27 lines
851 B
TypeScript
let ctx: AudioContext | null = null;
|
|
|
|
/** Create or resume the audio context. Call from a user gesture (the Start camera click). */
|
|
export function unlockAudio(): void {
|
|
try {
|
|
ctx ??= new AudioContext();
|
|
if (ctx.state === 'suspended') void ctx.resume();
|
|
} catch { /* audio unavailable; stay silent */ }
|
|
}
|
|
|
|
function tone(freq: number, ms: number): void {
|
|
if (!ctx) return;
|
|
try {
|
|
const osc = ctx.createOscillator();
|
|
const gain = ctx.createGain();
|
|
osc.frequency.value = freq;
|
|
gain.gain.setValueAtTime(0.2, ctx.currentTime);
|
|
gain.gain.setValueAtTime(0.0001, ctx.currentTime + ms / 1000);
|
|
osc.connect(gain).connect(ctx.destination);
|
|
osc.start();
|
|
osc.stop(ctx.currentTime + ms / 1000);
|
|
} catch { /* ignore */ }
|
|
}
|
|
|
|
export const beepAccept = () => tone(880, 120);
|
|
export const beepLow = () => tone(220, 200);
|