fix sound deletion and unplugging

This commit is contained in:
John Alanbrook 2024-09-15 17:49:35 -05:00
parent 00fa902e1f
commit b77281b311
4 changed files with 49 additions and 14 deletions

View file

@ -1,12 +1,13 @@
/* This file runs after the audio system is initiated */
var cries = {};
Object.readonly(audio, 'samplerate');
Object.readonly(audio, 'channels');
Object.readonly(audio, 'buffer_frames');
var sources = [];
audio.play = function(file,bus = audio.bus.master) {
var filename = file;
file = Resources.find_sound(file);
if (!file) {
console.error(`Cannot play sound ${file}: does not exist.`);
@ -15,6 +16,9 @@ audio.play = function(file,bus = audio.bus.master) {
var src = audio.dsp.source(file);
src.plugin(bus);
src.guid = prosperon.guid();
src.name = file;
src.type = "source";
sources.push(src);
return src;
}
audio.bus = {};
@ -22,6 +26,32 @@ audio.bus.master = dspsound.master();
audio.dsp = {};
audio.dsp = dspsound;
audio.bus.master.__proto__.type = "bus";
audio.bus.master.name = "master";
var plugin_node = audio.bus.master.plugin;
audio.bus.master.__proto__.plugin = function(to)
{
this.tos ??= [];
this.tos.push(to);
to.ins ??= [];
to.ins.push(this);
plugin_node.call(this, to);
}
var unplug_node = audio.bus.master.unplug;
audio.bus.master.__proto__.unplug = function()
{
if (this.tos) {
for (var node of this.tos)
node.ins.remove(this);
this.tos = [];
}
unplug_node.call(this);
}
audio.dsp.mix().__proto__.imgui = function()
{
imgui.pushid(this.memid());
@ -35,20 +65,18 @@ audio.cry = function(file, bus = audio.bus.sfx)
file = Resources.find_sound(file);
var player = audio.play(file, bus);
if (!player) return;
player.guid = prosperon.guid();
cries[player.guid] = player;
player.ended = function() { delete cries[player.guid]; player = undefined; }
player.ended = function() { player.unplug(); player = undefined; }
return player.ended;
}
// This function is called when every audio source is finished
var killer = Register.appupdate.register(function() {
for (var i in cries) {
var cry = cries[i];
if (!cry.ended) continue;
if (cry.frame < cry.lastframe || cry.frame === cry.frames())
cry.ended();
cry.lastframe = cry.frame;
for (var src of sources) {
if (!src.loop && (src.frame < src.lastframe || src.frame === src.frames())) {
src.unplug();
src.ended?.();
}
src.lastframe = src.frame;
}
});
@ -88,9 +116,11 @@ audio.music = function(file, fade = 0.5) {
audio.bus.music = audio.dsp.mix();
audio.bus.music.plugin(audio.bus.master);
audio.bus.music.name = "music";
audio.bus.sfx = audio.dsp.mix();
audio.bus.sfx.plugin(audio.bus.master);
audio.bus.sfx.name = "sfx";
audio.dsp.allpass = function(secs, decay) {
var composite = {};

View file

@ -525,6 +525,10 @@ JSC_SCALL(imgui_invisiblebutton,
ImGui::InvisibleButton(str, js2imvec2(argv[1]));
)
JSC_CCALL(imgui_width,
ImGui::PushItemWidth(js2number(argv[0]));
)
static const JSCFunctionListEntry js_imgui_funcs[] = {
MIST_FUNC_DEF(imgui, window, 2),
MIST_FUNC_DEF(imgui, menu, 2),
@ -592,6 +596,7 @@ static const JSCFunctionListEntry js_imgui_funcs[] = {
MIST_FUNC_DEF(imgui, contentregionavail, 0),
MIST_FUNC_DEF(imgui, dummy, 1),
MIST_FUNC_DEF(imgui, invisiblebutton, 2),
MIST_FUNC_DEF(imgui, width, 1),
};
static int started = 0;

View file

@ -161,7 +161,7 @@ void node_free(dsp_node *node)
return; /* Simple check to not delete the masterbus */
}
pthread_mutex_lock(&soundrun);
unplug_node(node);
// unplug_node(node);
if (node->data) {
if (node->data_free)
node->data_free(node->data);

View file

@ -31,7 +31,7 @@ soundbyte *dsp_node_out(dsp_node *node);
void dsp_node_run(dsp_node *node);
dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int samples), void (*fr)(void *data));
void plugin_node(dsp_node *from, dsp_node *to);
void unplug_node(dsp_node *node);
void unplug_node(dsp_node *from);
void node_free(dsp_node *node);
void dsp_node_free(dsp_node *node);
void filter_iir(struct dsp_iir *iir, soundbyte *buffer, int frames);