refactor: sound

This commit is contained in:
cha0s 2021-02-04 16:29:52 -06:00
parent e45bf84a19
commit 61f63179ca
3 changed files with 35 additions and 44 deletions

View File

@ -23,7 +23,7 @@
"@latus/core": "^2.0.0",
"autoprefixer": "^9.8.6",
"debug": "4.3.1",
"howler": "2.1.2"
"lru-cache": "^6.0.0"
},
"devDependencies": {
"@neutrinojs/airbnb": "^9.4.0",

View File

@ -1,53 +1,49 @@
import {join} from 'path';
import {JsonResource, Resource} from '@avocado/resource';
import {Howl} from 'howler';
import LRU from 'lru-cache';
const cache = new LRU({
dispose: ([url]) => URL.revokeObjectURL(url),
max: 32,
maxAge: Infinity,
});
export default () => class Sound extends JsonResource {
constructor() {
super();
this.handle = undefined;
this.timestamp = 0;
}
#audio;
destroy() {
this.sound?.unload();
}
#objectUrl;
async load(json = {}) {
await super.load(json);
const {interval = 0, volume = 1} = json;
this.interval = interval;
this.originalVolume = volume;
if (json.src && json.src.length > 0) {
const mappedSources = json.src.map((src) => join(Resource.root, src));
this.sound = new Howl({src: mappedSources});
this.sound.volume(volume);
// await new Promise((resolve, reject) => {
// this.sound.once('load', () => {
// resolve();
// });
// this.sound.once('loaderror', reject);
// });
const {volume = 1} = json;
const src = json.src || json.uri.replace('.sound.json', '.wav');
if (cache.has(src)) {
[this.#objectUrl, this.#audio] = await cache.get(src);
return;
}
const promise = new Promise((resolve, reject) => {
Resource.read(src).then((resource) => {
const url = URL.createObjectURL(new Blob([resource]));
const audio = new Audio(url);
audio.volume = volume;
const onCanPlay = () => {
audio.removeEventListener('canplay', onCanPlay);
resolve([url, audio]);
};
const onError = (error) => {
audio.removeEventListener('error', onError);
reject(error);
};
audio.addEventListener('canplay', onCanPlay);
audio.addEventListener('error', onError);
}).catch(reject);
});
cache.set(src, promise);
[this.#objectUrl, this.#audio] = await promise;
}
play() {
if (this.sound && this.interval > 0 && this.timestamp > 0) {
if ((Date.now() - this.timestamp) < this.interval) {
const volume = this.sound.volume(this.handle);
this.sound.volume(
Math.min(1, Math.min(volume, this.originalVolume * 20)),
this.handle,
);
return;
}
}
this.handle = this.sound.play();
if (this.interval > 0) {
this.timestamp = Date.now();
}
this.#audio.play();
}
};

View File

@ -4310,11 +4310,6 @@ hosted-git-info@^2.1.4:
resolved "http://npm.cha0sdev/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"
integrity sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==
howler@2.1.2:
version "2.1.2"
resolved "http://npm.cha0sdev/howler/-/howler-2.1.2.tgz#8433a09d8fe84132a3e726e05cb2bd352ef8bd49"
integrity sha512-oKrTFaVXsDRoB/jik7cEpWKTj7VieoiuzMYJ7E/EU5ayvmpRhumCv3YQ3823zi9VTJkSWAhbryHnlZAionGAJg==
hpack.js@^2.1.6:
version "2.1.6"
resolved "http://npm.cha0sdev/hpack.js/-/hpack.js-2.1.6.tgz#87774c0949e513f42e84575b3c45681fade2a0b2"