Web frontend
This commit is contained in:
22
web/src/lib/pop.ts
Normal file
22
web/src/lib/pop.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/** 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.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user