2022-08-25 12:56:53 -07:00
|
|
|
/** Soapbox audio clip. */
|
|
|
|
type Sound = {
|
2023-02-15 13:26:27 -08:00
|
|
|
src: string
|
|
|
|
type: string
|
2022-08-25 12:56:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export type Sounds = 'boop' | 'chat'
|
|
|
|
|
|
|
|
/** Produce HTML5 audio from sound data. */
|
|
|
|
const createAudio = (sources: Sound[]): HTMLAudioElement => {
|
|
|
|
const audio = new Audio();
|
|
|
|
sources.forEach(({ type, src }) => {
|
|
|
|
const source = document.createElement('source');
|
|
|
|
source.type = type;
|
|
|
|
source.src = src;
|
|
|
|
audio.appendChild(source);
|
|
|
|
});
|
|
|
|
return audio;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Play HTML5 sound. */
|
|
|
|
const play = (audio: HTMLAudioElement): void => {
|
|
|
|
if (!audio.paused) {
|
|
|
|
audio.pause();
|
|
|
|
if (typeof audio.fastSeek === 'function') {
|
|
|
|
audio.fastSeek(0);
|
|
|
|
} else {
|
|
|
|
audio.currentTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
audio.play();
|
|
|
|
};
|
|
|
|
|
|
|
|
const soundCache: Record<Sounds, HTMLAudioElement> = {
|
|
|
|
boop: createAudio([
|
|
|
|
{
|
2022-11-17 07:58:34 -08:00
|
|
|
src: require('../../assets/sounds/boop.ogg'),
|
2022-08-25 12:56:53 -07:00
|
|
|
type: 'audio/ogg',
|
|
|
|
},
|
|
|
|
{
|
2022-11-17 07:58:34 -08:00
|
|
|
src: require('../../assets/sounds/boop.mp3'),
|
2022-08-25 12:56:53 -07:00
|
|
|
type: 'audio/mpeg',
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
chat: createAudio([
|
|
|
|
{
|
2022-11-17 07:58:34 -08:00
|
|
|
src: require('../../assets/sounds/chat.oga'),
|
2022-08-25 12:56:53 -07:00
|
|
|
type: 'audio/ogg',
|
|
|
|
},
|
|
|
|
{
|
2022-11-17 07:58:34 -08:00
|
|
|
src: require('../../assets/sounds/chat.mp3'),
|
2022-08-25 12:56:53 -07:00
|
|
|
type: 'audio/mpeg',
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
|
|
|
|
export { soundCache, play };
|