Is there a pattern to storing states that are accessible by Synth?

I have a synthdef that I’d like to access a state variable. Using a global variable ~a, and setting it within the Synth doesn’t work. Can a Synth do a self.set(\var,23); ? Or can I write to a looped control bus and have that change an arg variable?

Basically I’d like a Synth to be able to alter it’s own state.

No.

The language can send messages to the server. /n_set is one of these messages.

The server can send messages to the language (SendReply or the older SendTrig), received in the language by OSCFunc or OSCdef.

The server cannot send messages to itself.

If you have mapped the arg to the control bus, then yes. Whatever you write to the control bus will feed back into the synth, one control block later.

hjh

It turns out that doing the looping bus to store state,

a=Bus.control;
a.set(0.1);
SynthDef.new(\ts,{ arg bus; var sig; 
 sig=In.kr(bus);
sig.poll;
Out.kr(bus,sig);
}).add;

b=Synth(\ts,[\bus:a]);

will give 0.2 and not 0.1.

No, this gives 0.1. Try s.newBusAllocators and run again. Something to be aware of when using busses is that busses are referenced by number not by name in a synth. To illustrate try this:

(
s.newBusAllocators;
s.waitForBoot{
	
	SynthDef(\readBus, {
		var sig = In.kr(\bus.kr());
		sig.poll(1);
	}).add;
	s.sync;
	a = Bus.control;
	b = Bus.control;
	a.set(0.5);
	x = Synth(\readBus, [\bus, a]);
}
)
// outputs the value 0.5 every second to the postwindow. Check the bus number:

a // Bus(control, 0, 1, localhost) = bus number 0

// Now lets suppose we free the bus and create a new one:

a.free;
b = Bus.control;

// still outputting 0.5

// Check the bus number:
b //  Bus(control, 0, 1, localhost). Now 'b' is bus number 0

b.set(0.2);

// Synth now outputs 0.2 since 'b' has the same busnumber (0) as 'a' had when the synth was initiated

There’s a set value 0.1.

Then Out.kr mixes in another 0.1.

I kinda suspect the requirement here might not be well defined. “I want to set the bus sometimes, and have the synth control the bus sometimes” is in general difficult if not impossible, because you have to be able to tell the computer what is the difference between you setting the bus vs the synth controlling it. (It’s easy and common if you have the main synth only reading from the bus – not writing to it – and then the bus either gets set by a command or by a second synth. This works because you can choose to run or not run the kr synth. But if you want to fold that into the main synth and choose after the fact whether to use the in-synth LFO or not… seems complicated. I would avoid this.)

It is probably better to have a control that you set, and a kr signal in the synth, and use some math to combine them (similar to an analog module that has a frequency knob = set control and a CV input for frequency = modulation by a signal).

hjh