prosperon/scripts/sound.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-02-25 17:31:48 -06:00
var audio = {};
audio.sound = {
2023-11-27 14:29:55 -06:00
bus: {},
2023-11-27 17:04:04 -06:00
samplerate() { return cmd(198); },
sounds: [], /* array of loaded sound files */
2023-11-27 14:29:55 -06:00
play(file, bus) {
2024-02-25 17:31:48 -06:00
if (!io.exists(file)) {
console.error(`Cannot play sound ${file}: does not exist.`);
2023-10-09 13:03:12 -05:00
return;
}
2024-02-25 17:31:48 -06:00
var src = audio.dsp.source(file);
bus ??= sound.bus.master;
2024-02-01 10:11:09 -06:00
// src.plugin(bus);
2023-11-27 14:29:55 -06:00
return src;
},
2024-02-25 17:31:48 -06:00
doc: {
play: "Play the given file once.",
volume: "Set the volume. 0 is no sound and 100 is loudest."
},
2023-11-27 14:29:55 -06:00
};
2024-02-25 17:31:48 -06:00
audio.dsp = {
2023-11-27 14:29:55 -06:00
mix(to) {
2024-01-14 10:24:31 -06:00
var n = cmd(181);
2023-11-27 14:29:55 -06:00
if (to) n.plugin(to);
return n;
},
source(path) {
2024-01-14 10:24:31 -06:00
return cmd(182,path);
2023-11-27 14:29:55 -06:00
},
delay(secs,decay) {
2024-01-14 10:24:31 -06:00
return cmd(185, secs, decay);
2023-11-27 14:29:55 -06:00
},
2023-11-28 22:48:32 -06:00
fwd_delay(secs, decay) {
2024-01-14 10:24:31 -06:00
return cmd(207,secs,decay);
2023-11-28 22:48:32 -06:00
},
allpass(secs, decay) {
2023-12-04 13:38:37 -06:00
var composite = {};
2024-02-25 17:31:48 -06:00
var fwd = audio.dsp.fwd_delay(secs,-decay);
var fbk = audio.dsp.delay(secs,decay);
2023-12-04 13:38:37 -06:00
composite.id = fwd.id;
composite.plugin = composite.plugin.bind(fbk);
composite.unplug = dsp_node.unplug.bind(fbk);
2023-11-28 22:48:32 -06:00
fwd.plugin(fbk);
2023-12-04 13:38:37 -06:00
return composite;
2023-11-28 22:48:32 -06:00
},
2023-11-27 14:29:55 -06:00
lpf(f) {
2024-01-14 10:24:31 -06:00
return cmd(186,f);
2023-11-27 14:29:55 -06:00
},
hpf(f) {
2024-01-14 10:24:31 -06:00
return cmd(187,f);
2023-11-27 14:29:55 -06:00
},
mod(path) {
2024-01-14 10:24:31 -06:00
return cmd(188,path);
2023-11-27 14:29:55 -06:00
},
2023-11-28 22:48:32 -06:00
midi(midi,sf) {
2024-01-14 10:24:31 -06:00
return cmd(206,midi,sf);
2023-11-28 22:48:32 -06:00
},
2023-11-27 14:29:55 -06:00
crush(rate, depth) {
2024-01-14 10:24:31 -06:00
return cmd(189,rate,depth);
2023-11-27 14:29:55 -06:00
},
compressor() {
2024-01-14 10:24:31 -06:00
return cmd(190);
2023-11-27 14:29:55 -06:00
},
limiter(ceil) {
2024-01-14 10:24:31 -06:00
return cmd(191,ceil);
2023-11-27 14:29:55 -06:00
},
noise_gate(floor) {
2024-01-14 10:24:31 -06:00
return cmd(192,floor);
},
2023-11-28 22:48:32 -06:00
pitchshift(octaves) {
2024-01-14 10:24:31 -06:00
return cmd(200,octaves);
2023-11-28 22:48:32 -06:00
},
noise() {
2024-01-14 10:24:31 -06:00
return cmd(203);
2023-11-28 22:48:32 -06:00
},
pink() {
2024-01-14 10:24:31 -06:00
return cmd(204);
2023-11-28 22:48:32 -06:00
},
red() {
2024-01-14 10:24:31 -06:00
return cmd(205);
2023-11-28 22:48:32 -06:00
},
};
2024-02-25 17:31:48 -06:00
audio.dsp.doc = {
2024-01-14 10:24:31 -06:00
delay: "Delays the input by secs, multiplied by decay",
fwd_delay: "Forward feedback delays the input by secs, multiplied by decay",
allpass: "Composite node of a delay and fwd_delay",
lpf: "Low pass filter at a given frequency",
hpf: "High pass filter at a given frequency",
midi: "A source node for a midi file with a given soundfont file",
crush: "Bitcrush the input to a given rate and bit depth",
limiter: "Limit audio to ceil with pleasent rolloff",
noise_gate: "Do not pass audio below the given floor",
pitchshift: "Shift sound by octaves",
noise: "Plain randon noise",
pink: "Pink noise",
red: "Red noise"
};
Object.mixin(cmd(180).__proto__, {
get db() { return 20*Math.log10(Math.abs(this.volume)); },
set db(x) { x = Math.clamp(x,-100,0); this.volume = Math.pow(10, x/20); },
get volume() { return this.gain; },
set volume(x) { this.gain = x; },
});
2024-02-25 17:31:48 -06:00
/*Object.mixin(audio.dsp.source().__proto__, {
2024-01-14 10:24:31 -06:00
frames() { return cmd(197,this); },
2024-02-25 17:31:48 -06:00
length() { return this.frames()/sound.samplerate(); },
time() { return this.frame/sound.samplerate(); },
2024-01-14 10:24:31 -06:00
pct() { return this.time()/this.length(); },
});
2024-01-31 02:42:15 -06:00
*/
return {audio};