prosperon/scripts/sound.js

91 lines
2 KiB
JavaScript
Raw Normal View History

2023-11-27 14:29:55 -06:00
var dsp_node = {
make(id) {
var n = Object.create(this);
n.id = id;
return n;
},
id: undefined,
get volume() { return cmd(175, this.id); },
set volume(x) { cmd(176, this.id,x); },
get pan() { return cmd(178,this.id); },
set pan(x) { cmd(179,this.id,x); },
off(t) { cmd(183, this.id, t); }, /* True to turn off */
bypass(t) { cmd(184, this.id, t); }, /* True to bypass filter effect */
unplug() { cmd(164, this.id); },
plugin(to) { cmd(177, this.id, to.id); },
kill() {
if (this._dead) return;
this._dead = true;
cmd(193, this.id); },
};
2023-11-27 14:29:55 -06:00
var dsp_source = Object.copy(dsp_node,{
end(){},
get loop() { return cmd(194,this.id); },
set loop(x) { cmd(195,this.id, x);},
});
var Sound = {
2023-11-27 14:29:55 -06:00
bus: {},
sounds: [], /* array of loaded sound files */
2023-11-27 14:29:55 -06:00
play(file, bus) {
2023-10-09 13:03:12 -05:00
if (!IO.exists(file)) {
2023-10-11 17:22:41 -05:00
Log.error(`Cannot play sound ${file}: does not exist.`);
2023-10-09 13:03:12 -05:00
return;
}
2023-11-27 14:29:55 -06:00
var src = DSP.source(file);
bus ??= Sound.bus.master;
src.plugin(bus);
return src;
},
music(midi, sf) {
cmd(13, midi, sf);
},
2023-11-27 14:29:55 -06:00
};
2023-11-27 14:29:55 -06:00
var DSP = {
mix(to) {
var n = dsp_node.make(cmd(181));
if (to) n.plugin(to);
return n;
},
source(path) {
var src = Object.create(dsp_source);
src.id = cmd(182,path,src);
return src;
},
delay(secs,decay) {
return dsp_node.make(cmd(185, secs, decay));
},
lpf(f) {
return dsp_node.make(cmd(186,f));
},
hpf(f) {
return dsp_node.make(cmd(187,f));
},
mod(path) {
return dsp_node.make(cmd(188,path));
},
crush(rate, depth) {
return dsp_node.make(cmd(189,rate,depth));
},
compressor() {
return dsp_node.make(cmd(190));
},
limiter(ceil) {
return dsp_node.make(cmd(191,ceil));
},
noise_gate(floor) {
return dsp_node.make(cmd(192,floor));
},
};
2023-11-27 14:29:55 -06:00
Sound.bus.master = dsp_node.make(cmd(180));
Sound.master = Sound.bus.master;
Sound.play.doc = "Play the given file once.";
Sound.doc = {};
Sound.doc.volume = "Set the master volume. 0 is no sound and 100 is loudest.";