-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplaySound.ts
More file actions
152 lines (127 loc) · 4.17 KB
/
playSound.ts
File metadata and controls
152 lines (127 loc) · 4.17 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { Settings } from '../types/settings';
export function linearToLogVolume(linearVolume: number): number {
const clamped = Math.max(0, Math.min(200, linearVolume));
if (clamped <= 100) {
const normalized = clamped / 100;
return Math.pow(normalized, 2);
} else {
return 1.0 + (clamped - 100) / 100;
}
}
export function playAudioWithGain(
audioElement: HTMLAudioElement,
volume: number
): void {
if (volume <= 1.0) {
audioElement.volume = volume;
audioElement.play().catch(() => {});
return;
}
const audioContext = new (window.AudioContext ||
(
window as Window &
typeof globalThis & { webkitAudioContext?: typeof AudioContext }
).webkitAudioContext!)();
const source = audioContext.createMediaElementSource(audioElement);
const gainNode = audioContext.createGain();
gainNode.gain.value = volume;
source.connect(gainNode);
gainNode.connect(audioContext.destination);
audioElement.volume = 1.0;
audioElement.play().catch(() => {});
}
export function playSound(
filepath: string,
volume: number = 0.7
): Promise<void> {
return new Promise((resolve, reject) => {
try {
const audio = new Audio(filepath);
audio.volume = Math.max(0, Math.min(1, volume));
audio.preload = 'auto';
const onCanPlay = () => {
audio.removeEventListener('canplay', onCanPlay);
audio.removeEventListener('error', onError);
resolve();
};
const onError = (error: Event) => {
audio.removeEventListener('canplay', onCanPlay);
audio.removeEventListener('error', onError);
console.warn('Could not play sound:', filepath, error);
reject(error);
};
audio.addEventListener('canplay', onCanPlay);
audio.addEventListener('error', onError);
audio.play().catch(onError);
} catch (error) {
console.warn('Could not create audio element for:', filepath, error);
reject(error);
}
});
}
export function playSoundWithSettings(
soundType: 'startupSound' | 'chatNotificationSound' | 'newStripSound',
userSettings: Settings,
fallbackVolume: number = 0.7
): Promise<void> {
const soundSettings = userSettings?.sounds?.[soundType];
if (!soundSettings?.enabled) {
return Promise.resolve();
}
const logVolume = linearToLogVolume(soundSettings.volume || 100);
const adjustedVolume = logVolume * fallbackVolume;
const soundMap = {
startupSound: SOUNDS.SESSION_STARTUP,
chatNotificationSound: SOUNDS.CHAT_NOTIFICATION,
newStripSound: SOUNDS.NEW_STRIP,
};
return new Promise((resolve, reject) => {
try {
const audio = new Audio(soundMap[soundType]);
audio.preload = 'auto';
const onCanPlay = () => {
audio.removeEventListener('canplay', onCanPlay);
audio.removeEventListener('error', onError);
playAudioWithGain(audio, adjustedVolume);
resolve();
};
const onError = (error: Event) => {
audio.removeEventListener('canplay', onCanPlay);
audio.removeEventListener('error', onError);
console.warn('Could not play sound:', soundMap[soundType], error);
reject(error);
};
audio.addEventListener('canplay', onCanPlay);
audio.addEventListener('error', onError);
audio.load();
} catch (error) {
console.warn(
'Could not create audio element for:',
soundMap[soundType],
error
);
reject(error);
}
});
}
export function preloadSound(filepath: string): HTMLAudioElement {
const audio = new Audio(filepath);
audio.preload = 'auto';
return audio;
}
export function playSounds(
sounds: Array<{ filepath: string; volume?: number }>
): Promise<void[]> {
return Promise.all(
sounds.map((sound) => playSound(sound.filepath, sound.volume))
);
}
export const SOUNDS = {
CHAT_NOTIFICATION: '/assets/app/sounds/chatNotification.wav',
SESSION_STARTUP: '/assets/app/sounds/startup.mp3',
NEW_STRIP: '/assets/app/sounds/newStrip.mp3',
ACARS_BEEP: '/assets/app/sounds/ACARSBeep.wav',
ACARS_CHAT_POP: '/assets/app/sounds/ACARSChatPop.mp3',
VC_CONNECT: '/assets/app/sounds/vcConnect.mp3',
VC_DISCONNECT: '/assets/app/sounds/vcDisconnect.mp3',
} as const;