2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
export type SoundOpts = {
|
|
|
|
loop?: boolean;
|
|
|
|
src: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class Sound {
|
|
|
|
static sounds = new Map();
|
|
|
|
|
2023-02-08 15:05:50 +00:00
|
|
|
private static context: AudioContext | undefined;
|
2020-09-14 21:56:35 +00:00
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
private readonly loop: boolean;
|
2020-09-14 21:56:35 +00:00
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
private node?: AudioBufferSourceNode;
|
2020-09-14 21:56:35 +00:00
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
private readonly src: string;
|
|
|
|
|
|
|
|
constructor(options: SoundOpts) {
|
|
|
|
this.loop = Boolean(options.loop);
|
|
|
|
this.src = options.src;
|
|
|
|
}
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
async play(): Promise<void> {
|
2020-06-04 18:16:19 +00:00
|
|
|
if (!Sound.sounds.has(this.src)) {
|
|
|
|
try {
|
|
|
|
const buffer = await Sound.loadSoundFile(this.src);
|
|
|
|
const decodedBuffer = await this.context.decodeAudioData(buffer);
|
|
|
|
Sound.sounds.set(this.src, decodedBuffer);
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(`Sound error: ${err}`);
|
2020-06-04 18:16:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const soundBuffer = Sound.sounds.get(this.src);
|
|
|
|
|
|
|
|
const soundNode = this.context.createBufferSource();
|
|
|
|
soundNode.buffer = soundBuffer;
|
|
|
|
|
|
|
|
const volumeNode = this.context.createGain();
|
|
|
|
soundNode.connect(volumeNode);
|
|
|
|
volumeNode.connect(this.context.destination);
|
|
|
|
|
|
|
|
soundNode.loop = this.loop;
|
|
|
|
|
|
|
|
soundNode.start(0, 0);
|
|
|
|
|
|
|
|
this.node = soundNode;
|
|
|
|
}
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
stop(): void {
|
2020-06-04 18:16:19 +00:00
|
|
|
if (this.node) {
|
|
|
|
this.node.stop(0);
|
|
|
|
this.node = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:05:50 +00:00
|
|
|
private get context(): AudioContext {
|
|
|
|
if (!Sound.context) {
|
|
|
|
Sound.context = new AudioContext();
|
|
|
|
}
|
|
|
|
return Sound.context;
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
static async loadSoundFile(src: string): Promise<ArrayBuffer> {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', src, true);
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
resolve(xhr.response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reject(new Error(`Request failed: ${xhr.statusText}`));
|
|
|
|
};
|
|
|
|
xhr.onerror = () => {
|
|
|
|
reject(new Error(`Request failed, most likely file not found: ${src}`));
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|