This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioPlayer.ts
355 lines (300 loc) · 8.84 KB
/
AudioPlayer.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
abstract class AudioPlayer extends Logger {
private static _singlePoolVolumeSlider: VolumeSlider;
private static _multiPoolVolumeSlider: VolumeSlider;
private static _audioStore: AudioStoreManager = new AudioStoreManager();
private static _$audioDevicesSelect: JQuery<HTMLInputElement>;
private static _audioDevices: MediaDeviceInfo[];
public static get audioDevices(): MediaDeviceInfo[] {
return this._audioDevices;
}
private static _currentAudioDevice: MediaDeviceInfo;
private static _audioDevicesInitialized: boolean = false;
private static _$playToggleButtonIcon: JQuery<HTMLElement>;
private static _playToggleButtonIconInterval: NodeJS.Timer;
private static _playToggleButtonIconIntervalLocked: boolean;
public static async initializeAudioDevices(): Promise<void> {
this._$audioDevicesSelect = $("#audio-output-select");
// Fill device array and dropdown, and set audio device
await this.refreshAudioDevices();
this._$audioDevicesSelect.on("change", () => {
let audioIndex = +this._$audioDevicesSelect.val();
this.logDebug(
"(audio device dropdown change)",
"Main output dropdown triggered change\n",
`ID: ${audioIndex}\n`,
`Option label: '${this._$audioDevicesSelect
.children("option:selected")
.text()}'`
);
// const audioIndex = this._$audioDevicesSelect.prop("selectedIndex") as number;
this.setAudioDevice(audioIndex);
});
// FIXME: Remove this hardcoded thing
// Temporarily hardcoded "Virtual Audio Cable" output as default for main output
this.hardCodeVirtualAudioCableAsDefault();
// Update audio devices dropdown and array on device change event
navigator.mediaDevices.ondevicechange = async (_e) => {
await this.refreshAudioDevices();
this.hardCodeVirtualAudioCableAsDefault();
// TODO: update output and try keeping the previous one
// also, check if the audio changed came from audio output or not
console.log("DEVICE CHANGE EVENT ", _e);
};
this._audioDevicesInitialized = true;
this.logInfo(
this.initializeAudioDevices,
"Devices initialized!\n",
this._audioDevices
);
}
/**
* Refreshes devices array and dropdown
*/
private static async refreshAudioDevices(): Promise<void> {
await this.refreshAudioDevicesArray();
this.refreshAudioDevicesDropdown();
}
private static refreshAudioDevicesDropdown(): void {
this._$audioDevicesSelect.empty();
// Append audio devices to dropdown (if the OS has at least one)
if (this._audioDevices.length > 0) {
this._audioDevices.forEach((device, i) => {
this._$audioDevicesSelect.append(
$("<option>", {
value: i,
text: device.label,
})
);
});
} else {
this._$audioDevicesSelect.append(
$("<option>", {
value: -1,
text: "(None)",
})
);
}
}
private static async refreshAudioDevicesArray(): Promise<void> {
// Get audio output devices
const devices = await navigator.mediaDevices.enumerateDevices();
this._audioDevices = devices.filter(({ kind }) => kind === "audiooutput");
}
private static hardCodeVirtualAudioCableAsDefault(): void {
let virtualAudioCableIndex = -1;
if (
this._audioDevices.some((e, i) => {
if (e.label === "Virtual Audio Cable (VB-Audio Virtual Cable)") {
virtualAudioCableIndex = i;
return true;
}
return false;
})
) {
$(`#audio-output-select>option:eq(${virtualAudioCableIndex})`).prop(
"selected",
true
);
this._$audioDevicesSelect.trigger("change");
}
}
private static setAudioDevice(device: number | MediaDeviceInfo): void {
// If the OS has at least one output
if (this._audioDevices.length > 0) {
if (typeof device != "number") {
this._audioStore.setAudioDevice(device);
this._currentAudioDevice = device;
this.logInfo(
this.setAudioDevice,
`Audio device changed to '${device.label}'\n`,
device
);
} else if (
device >= 0 &&
StringUtilities.isDefined(this._audioDevices[device])
) {
this._audioStore.setAudioDevice(this._audioDevices[device]);
this._currentAudioDevice = this._audioDevices[device];
this.logInfo(
this.setAudioDevice,
`Audio device changed to '${this._audioDevices[device].label}'\n`,
this._audioDevices[device]
);
}
}
// TODO: add `else`s
}
public static setAudioButtons(
$playToggleButton: JQuery<HTMLButtonElement>,
$stopButton: JQuery<HTMLButtonElement>
): typeof AudioPlayer {
// Icon inside the play toggle button
this._$playToggleButtonIcon = $playToggleButton.children("i.fa-play");
// Buttons events
$playToggleButton
// On play toggle click
.on("click", () => {
if (this._audioStore.isPlaying) {
AudioPlayer.pause();
} else {
AudioPlayer.play();
}
});
$stopButton.on("click", () => {
this.logInfo(this.setAudioButtons, "Stop audio button clicked");
AudioPlayer.stop();
});
// Play toggle button icon updater
// FIXME: Use an array / a semaphore system to check when to change the icon instead of an interval
let iconUpdater = setInterval(() => {
if (this._playToggleButtonIconIntervalLocked) {
return;
}
this.updatePlayToggleButton();
}, 10);
Main.addInterval(iconUpdater);
this._playToggleButtonIconInterval = iconUpdater;
return this;
}
private static updatePlayToggleButton() {
this._playToggleButtonIconIntervalLocked = true;
let isPlaying = this._audioStore.isPlaying;
this._$playToggleButtonIcon
.toggleClass("fa-pause", isPlaying)
.toggleClass("fa-play", !isPlaying);
this._playToggleButtonIconIntervalLocked = false;
}
public static initVolumeSliders(
$singlePoolSlider: JQuery<HTMLInputElement>,
$multiPoolSlider: JQuery<HTMLInputElement>
): typeof AudioPlayer {
const decimals = 4;
const exponentialBase = 100;
this._singlePoolVolumeSlider = new VolumeSlider(
$singlePoolSlider,
() => {
// Update existing audio volume
this._audioStore.setSinglePoolVolume(this._singlePoolVolumeSlider.value);
// Log new volume
this.logDebug(
"(single pool volume slider change)",
"Volume:",
this._singlePoolVolumeSlider.value
);
},
decimals,
exponentialBase
);
this._multiPoolVolumeSlider = new VolumeSlider(
$multiPoolSlider,
() => {
// Update existing audio volume
this._audioStore.setMultiPoolVolume(this._multiPoolVolumeSlider.value);
// Log new volume
this.logDebug(
"(multi pool volume slider change)",
"Volume:",
this._multiPoolVolumeSlider.value
);
},
decimals,
exponentialBase
);
this._singlePoolVolumeSlider.$slider.trigger("input");
this._multiPoolVolumeSlider.$slider.trigger("input");
this.logInfo(
this.initVolumeSliders,
"Sliders set!\n",
$singlePoolSlider,
"\n",
$multiPoolSlider
);
return this;
}
public static addAudio(
path: string,
time: AudioTimings,
useMultiPool: boolean = false
): void {
this.logInfo(
this.addAudio,
`Using path "%c${path}%c"%s`,
"color: #03fc98;",
"",
"\n- Start time:",
time.start,
"(ms)\n- End time:",
time.end,
`(ms)\n- Condition: "${time.condition}"`
);
// TODO: clamp time? (e.g. -1000ms = 0ms)
this.createAndPlayAudio(path, time, useMultiPool);
}
private static createAndPlayAudio(
path: string,
time: AudioTimings,
useMultiPool: boolean
): void {
if (!useMultiPool) {
this._audioStore.addToSinglePool(path, time);
return;
}
// Limited sounds to prevent memory or human ear issues
if (this._audioStore.isLimitReached) {
this.logWarn(this.createAndPlayAudio, "Pool limit exceeded.");
return;
}
let mainAudio = new Audio(path);
$(mainAudio)
.one("canplay", (e) => {
this.logInfo(
this.createAndPlayAudio,
"Audio file created. Duration: " + e.target.duration + " seconds"
);
this.storeAudio(e.target, time);
})
.one("error", (e) => {
this.logError(
this.createAndPlayAudio,
"Error loading audio\n",
`(Code ${e.target.error.code}) "${e.target.error.message}"\n`,
e
);
});
}
private static async storeAudio(
mainAudio: HTMLAudioElement,
time: AudioTimings
): Promise<void> {
// Create multi audio pool group
const main = mainAudio;
const playback = mainAudio.cloneNode() as HTMLAudioElement;
const group: AudioPoolGroup = {
main,
playback,
$all: $(main).add($(playback)),
all: (func) => {
func(main);
func(playback);
},
ended: false,
forcedStop: false,
};
main.volume = playback.volume = this._multiPoolVolumeSlider.value;
main.currentTime = playback.currentTime = time.start / 1000;
main.setSinkId(this._currentAudioDevice.deviceId);
this._audioStore.addToMultiPool(group);
}
public static async play(): Promise<void> {
await this._audioStore.play();
}
public static pause(): void {
this._audioStore.pause();
}
public static stop(): void {
this._audioStore.stop();
}
public static get isPlaying(): boolean {
return this._audioStore.isPlaying;
}
}