33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
/* Krótki alarm dźwiękowy (Web Audio, bez plików audio) — trzy beepy.
|
|
Używany m.in. przez timer przerwy w sesji treningowej. */
|
|
let ctx: AudioContext | null = null;
|
|
|
|
function getCtx(): AudioContext {
|
|
if (!ctx) ctx = new AudioContext();
|
|
return ctx;
|
|
}
|
|
|
|
function beepAt(c: AudioContext, t: number, freq: number, duration: number) {
|
|
const osc = c.createOscillator();
|
|
const gain = c.createGain();
|
|
osc.type = 'sine';
|
|
osc.frequency.value = freq;
|
|
gain.gain.setValueAtTime(0.0001, t);
|
|
gain.gain.exponentialRampToValueAtTime(0.3, t + 0.02);
|
|
gain.gain.exponentialRampToValueAtTime(0.0001, t + duration);
|
|
osc.connect(gain).connect(c.destination);
|
|
osc.start(t);
|
|
osc.stop(t + duration + 0.05);
|
|
}
|
|
|
|
export function playAlarm() {
|
|
try {
|
|
const c = getCtx();
|
|
if (c.state === 'suspended') void c.resume();
|
|
const t0 = c.currentTime;
|
|
for (let i = 0; i < 3; i++) beepAt(c, t0 + i * 0.25, 880, 0.18);
|
|
} catch {
|
|
// brak AudioContext (np. środowisko testowe) lub polityka autoplay — ignoruj
|
|
}
|
|
}
|