-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageNotifySound.ts
More file actions
37 lines (33 loc) · 1.05 KB
/
Copy pathmessageNotifySound.ts
File metadata and controls
37 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let sharedAudioContext: AudioContext | null = null
export async function playMessageNotifySound(): Promise<void> {
if (!import.meta.client) {
return
}
try {
const audioContext = sharedAudioContext ?? new AudioContext()
sharedAudioContext = audioContext
if (audioContext.state === 'suspended') {
await audioContext.resume()
}
const startTime = audioContext.currentTime
const gainNode = audioContext.createGain()
gainNode.gain.value = 0.08
gainNode.connect(audioContext.destination)
const playTone = (
frequency: number,
offsetSeconds: number,
durationSeconds: number,
) => {
const oscillator = audioContext.createOscillator()
oscillator.type = 'sine'
oscillator.frequency.value = frequency
oscillator.connect(gainNode)
oscillator.start(startTime + offsetSeconds)
oscillator.stop(startTime + offsetSeconds + durationSeconds)
}
playTone(880, 0, 0.08)
playTone(660, 0.1, 0.12)
} catch {
// Autoplay policy or unsupported audio — ignore.
}
}