@excaliburjs/plugin-jsfxr
Version:
excalibur-jsfxr provides sound effect generation utilizing a wrapper around jsfxr
62 lines • 1.83 kB
JavaScript
export class JsfxrResource {
sounds = {};
jsfxrModule;
jsfxr;
async init() {
this.jsfxrModule = await import("./sfxr.mjs");
this.jsfxr = this.jsfxrModule.default;
}
loadSoundConfig(name, config) {
this.sounds[name] = config;
}
rangeValue(min, max) {
return Math.random() * (max - min) + min;
}
deleteSoundConfig(name) {
delete this.sounds[name];
}
playConfig(config) {
if (this.jsfxr === undefined)
return;
if (this.jsfxrModule === undefined)
return;
const resolvedConfig = this.resolveValues(config);
const a = this.jsfxr.toAudio(resolvedConfig);
a.play();
}
resolveValues(config) {
const newConfig = {};
for (const key in config) {
if (key === "oldParams")
continue;
const value = config[key];
if (typeof value === "number") {
newConfig[key] = value;
}
else {
const min = value.min;
const max = value.max;
newConfig[key] = this.rangeValue(min, max);
console.log("set ", key, " to ", newConfig[key], " from ", min, " to ", max);
}
}
return newConfig;
}
playSound(name) {
if (this.jsfxr === undefined)
return;
if (this.jsfxrModule === undefined)
return;
const config = this.sounds[name];
if (!config) {
throw new Error(`Sound ${name} not found`);
}
const resolvedConfig = this.resolveValues(config);
const a = this.jsfxr.toAudio(resolvedConfig);
a.play();
}
getConfigs() {
return this.sounds;
}
}
//# sourceMappingURL=excalibur-jsfxr.js.map