bigbuffet-rw/app/soapbox/middleware/sounds.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
'use strict';
2021-09-04 10:59:28 -07:00
import { join } from 'path';
import { FE_BASE_PATH } from 'soapbox/build_config';
2020-03-27 13:59:38 -07:00
const createAudio = sources => {
const audio = new Audio();
sources.forEach(({ type, src }) => {
const source = document.createElement('source');
source.type = type;
source.src = src;
audio.appendChild(source);
});
return audio;
};
const play = audio => {
if (!audio.paused) {
audio.pause();
if (typeof audio.fastSeek === 'function') {
audio.fastSeek(0);
} else {
audio.currentTime = 0;
}
}
audio.play();
2020-03-27 13:59:38 -07:00
};
export default function soundsMiddleware() {
const soundCache = {
boop: createAudio([
{
2021-09-04 10:59:28 -07:00
src: join(FE_BASE_PATH, '/sounds/boop.ogg'),
2020-03-27 13:59:38 -07:00
type: 'audio/ogg',
},
{
2021-09-04 10:59:28 -07:00
src: join(FE_BASE_PATH, '/sounds/boop.mp3'),
2020-03-27 13:59:38 -07:00
type: 'audio/mpeg',
},
]),
chat: createAudio([
{
2021-09-04 10:59:28 -07:00
src: join(FE_BASE_PATH, '/sounds/chat.oga'),
type: 'audio/ogg',
},
{
2021-09-04 10:59:28 -07:00
src: join(FE_BASE_PATH, '/sounds/chat.mp3'),
type: 'audio/mpeg',
},
]),
2020-03-27 13:59:38 -07:00
};
return () => next => action => {
if (action.meta && action.meta.sound && soundCache[action.meta.sound]) {
play(soundCache[action.meta.sound]);
}
return next(action);
};
2021-08-03 12:22:51 -07:00
}