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

View file

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

View file

@ -161,7 +161,7 @@ void node_free(dsp_node *node)
return; /* Simple check to not delete the masterbus */ return; /* Simple check to not delete the masterbus */
} }
pthread_mutex_lock(&soundrun); pthread_mutex_lock(&soundrun);
unplug_node(node); // unplug_node(node);
if (node->data) { if (node->data) {
if (node->data_free) if (node->data_free)
node->data_free(node->data); 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); 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)); 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 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 node_free(dsp_node *node);
void dsp_node_free(dsp_node *node); void dsp_node_free(dsp_node *node);
void filter_iir(struct dsp_iir *iir, soundbyte *buffer, int frames); void filter_iir(struct dsp_iir *iir, soundbyte *buffer, int frames);