| 95 | } |
| 96 | |
| 97 | function tone(ctx: AudioContext, freq: number, start: number, duration: number, peak: number): void { |
| 98 | const osc = ctx.createOscillator(); |
| 99 | const gain = ctx.createGain(); |
| 100 | osc.type = 'sine'; |
| 101 | osc.frequency.value = freq; |
| 102 | osc.connect(gain); |
| 103 | gain.connect(ctx.destination); |
| 104 | const t0 = ctx.currentTime + start; |
| 105 | // Exponential ramps can't target 0, so use a tiny floor to fade in/out |
| 106 | // without the click you get from an abrupt start/stop. |
| 107 | gain.gain.setValueAtTime(0.0001, t0); |
| 108 | gain.gain.exponentialRampToValueAtTime(peak, t0 + 0.01); |
| 109 | gain.gain.exponentialRampToValueAtTime(0.0001, t0 + duration); |
| 110 | osc.start(t0); |
| 111 | osc.stop(t0 + duration + 0.02); |
| 112 | } |
| 113 | |
| 114 | function playChime(): void { |
| 115 | const ctx = getAudioContext(); |