2024-02-25 17:31:48 -06:00
|
|
|
var audio = {};
|
2024-04-04 17:28:11 -05:00
|
|
|
var cries = {};
|
2024-03-13 03:51:44 -05:00
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
audio.samplerate = dspsound.samplerate();
|
2024-04-07 13:16:54 -05:00
|
|
|
audio.play = function(file,bus = audio.bus.master) {
|
2024-03-19 17:00:49 -05:00
|
|
|
file = Resources.find_sound(file);
|
|
|
|
if (!file) {
|
|
|
|
console.error(`Cannot play sound ${file}: does not exist.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var src = audio.dsp.source(file);
|
|
|
|
src.plugin(bus);
|
2024-04-04 17:28:11 -05:00
|
|
|
src.guid = prosperon.guid();
|
2024-03-19 17:00:49 -05:00
|
|
|
return src;
|
|
|
|
}
|
|
|
|
audio.bus = {};
|
|
|
|
audio.bus.master = dspsound.master();
|
2024-03-19 14:39:19 -05:00
|
|
|
audio.dsp = {};
|
2024-03-19 17:00:49 -05:00
|
|
|
audio.dsp = dspsound;
|
2024-03-19 14:39:19 -05:00
|
|
|
|
2024-04-01 08:13:57 -05:00
|
|
|
audio.cry = function(file)
|
|
|
|
{
|
|
|
|
var player = audio.play(file);
|
2024-04-04 17:28:11 -05:00
|
|
|
player.guid = prosperon.guid();
|
|
|
|
cries[player.guid] = player;
|
|
|
|
player.ended = function() { delete cries[player.guid]; player = undefined; }
|
|
|
|
return player.ended;
|
2024-04-01 08:13:57 -05:00
|
|
|
}
|
|
|
|
|
2024-04-04 17:28:11 -05:00
|
|
|
var killer = Register.appupdate.register(function() {
|
|
|
|
for (var i in cries) {
|
|
|
|
var cry = cries[i];
|
|
|
|
if (!cry.ended) continue;
|
2024-04-05 23:03:10 -05:00
|
|
|
if (cry.frame < cry.lastframe || cry.frame === cry.frames())
|
|
|
|
cry.ended();
|
2024-04-04 17:28:11 -05:00
|
|
|
cry.lastframe = cry.frame;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-01 08:13:57 -05:00
|
|
|
var song;
|
|
|
|
|
2024-04-07 13:16:54 -05:00
|
|
|
audio.music = function(file, fade = 0) {
|
2024-04-01 08:13:57 -05:00
|
|
|
if (!fade) {
|
|
|
|
song = audio.play(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var temp = audio.play(file);
|
|
|
|
temp.volume = 0;
|
|
|
|
var temp2 = song;
|
|
|
|
tween(temp, 'volume', 1, fade);
|
|
|
|
tween(temp2, 'volume', 0, fade);
|
|
|
|
song = temp;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
audio.dsp.mix = function(to) {
|
|
|
|
var n = audio.dsp.mix();
|
|
|
|
if (to) n.plugin(to);
|
|
|
|
return n;
|
2023-09-07 16:46:35 -05:00
|
|
|
};
|
2023-10-23 08:08:11 -05:00
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
audio.dsp.allpass = function(secs, decay) {
|
|
|
|
var composite = {};
|
|
|
|
var fwd = audio.dsp.fwd_delay(secs,-decay);
|
|
|
|
var fbk = audio.dsp.delay(secs,decay);
|
|
|
|
composite.id = fwd.id;
|
|
|
|
composite.plugin = composite.plugin.bind(fbk);
|
|
|
|
composite.unplug = dsp_node.unplug.bind(fbk);
|
|
|
|
fwd.plugin(fbk);
|
|
|
|
return composite;
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
2024-03-19 17:00:49 -05:00
|
|
|
Object.mixin(audio.bus.master.__proto__, {
|
2024-01-14 10:24:31 -06:00
|
|
|
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-03-19 17:00:49 -05:00
|
|
|
length() { return this.frames()/audio.samplerate(); },
|
2024-02-25 17:31:48 -06:00
|
|
|
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
|
|
|
*/
|
2024-02-29 13:54:33 -06:00
|
|
|
|
|
|
|
return {audio};
|