From 6110595259972c8b7771cb33e0c7b51da537ee6c Mon Sep 17 00:00:00 2001 From: kevinstadler Date: Wed, 20 Mar 2024 22:30:01 +0100 Subject: [PATCH] Keep some stopped SoundObjects in synth network (closes #95) Keep stopped SoundObjects in the synth network as long as they're still connected to some non-output unit (typically an analyzer) (This can re-introduce some memory leakage.) --- src/processing/sound/Engine.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/processing/sound/Engine.java b/src/processing/sound/Engine.java index 5cab3d9..ce45fb0 100644 --- a/src/processing/sound/Engine.java +++ b/src/processing/sound/Engine.java @@ -527,6 +527,15 @@ protected void disconnectFromOutput(int channel, UnitSource source, int part) { source.getOutput().disconnect(part, this.volume[channel].inputA, 0); } + protected void disconnectFromOutput(UnitSource source) { + for (Multiply o : this.volume) { + // keep it generic: disconnect all parts from all outputs + for (int i = 0; i < source.getOutput().getNumParts(); i++) { + source.getOutput().disconnect(i, o.inputA, 0); + } + } + } + protected void play(UnitSource source) { // add unit to synth UnitGenerator generator = source.getUnitGenerator(); @@ -544,13 +553,12 @@ protected void play(UnitSource source) { protected void stop(UnitSource source) { if (this.addedUnits.contains(source.getUnitGenerator())) { - // this is usually just the two-part output of a JSynCircuit, but let's - // keep it generic just in case - for (int i : IntStream.range(0, source.getOutput().getNumParts()).toArray()) { - source.getOutput().disconnectAll(i); + // disconnect from any and all outputs + this.disconnectFromOutput(source); + // don't remove if it's still connected (typically to an analyzer) + if (!source.getOutput().isConnected()) { + this.remove(source.getUnitGenerator()); } - // removal happens inside - this.remove(source.getUnitGenerator()); } }