Files
musicmouse/web/src/lib/pop.ts
2026-08-27 12:32:20 +02:00

23 lines
830 B
TypeScript

/** The mockup's click feedback: a short rising blip per interaction. */
let context: AudioContext | null = null;
export function playPop(frequency: number): void {
try {
context ??= new AudioContext();
const now = context.currentTime;
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.type = "sine";
oscillator.frequency.setValueAtTime(frequency, now);
oscillator.frequency.exponentialRampToValueAtTime(frequency * 1.8, now + 0.08);
gain.gain.setValueAtTime(0.15, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.15);
oscillator.connect(gain).connect(context.destination);
oscillator.start();
oscillator.stop(now + 0.16);
} catch {
// No audio context before the first user gesture, and on some browsers never.
}
}