# Routing Matrix Problem

Sorry for posting this a second time - I realized I posted in the wrong area of the forum before.

I’m attempting to make a simple routing matrix. My thinking was the following code would work… but it seems that there is a problem starting and stopping a particular instance of a Synth within a toggle (g.free doesn’t seem to work)… Maybe I’m missing something obvious?

``````SynthDef(\routing,
{|busNum, inPort|
Out.ar(busNum, SoundIn.ar(inPort));

({|inNum=64, outNum=64|

win = Window("", Rect(100, 200, 600, 600)).front;

h = StaticText(win, Rect(100, 0, 200, 30))
.string_("input to bus");

o = Array.fill(24, {|x| StaticText(win, Rect(10, 35+(20*x), 15, 15))
.string_(x)});

m=	Array.fill2D(24, 24, {|x, y| Button(win, Rect(30+(20*x), 30+(20*y), 20, 20))
.states_([ ["", Color.black, Color.white], ["x", Color.white, Color.black]])
.action_({|v| var g;
if (v.value==1, {
g = Synth("routing", [x, y]);}, {g.free;});
});
});

e = Array.fill(24, {|x| StaticText(win, Rect(30+(20*x), 510, 25, 15))
.string_("m_"++x)});
s.plotTree;
}.value);
``````

`g` is a local variable. Local variables do not persist once the function returns:

``````(
f = { |input = 0|
var g;
if(input > 0) {
g = input;
};
"g is %\n".postf(g);
};

f.(1);
g is 1

f.value;
g is nil
``````

In the second call, g was not reassigned, and its value was forgotten between calls.

``````(
var g;

f = { |input = 0|
if(input > 0) {
g = input;
};
"g is %\n".postf(g);
};

f.(1);
g is 1

f.value;
g is 1
``````

Now `g` exists outside of `f` so it is not forgotten between calls.

But the bigger problem is: suppose you move `var g` outside the button action, but inside the Array2D loop. Once you create these groups, their variables will not be accessible outside this small block, so you won’t be able to put anything into them.

I find SC GUIs work best, or are easier to manage, when the code logic works 100% by code commands (without relying on any GUIs for the core functionality). Then you can put a GUI shell on top of a structure that already works. Part of your problem here is that you’re “cheating” by trying to fold the storage for your groups into the GUI. I think the storage and the logic needs to exist fully outside the GUI, and then the GUI is just an interface.

hjh

2 Likes