softSet, softVol_ set a proxy param value or vol conditionally



NodeProxy.softSet, Ndef.softSet 

sets a nodeproxy param value only  in two conditions: 

* if the new value is close enough to the current param value;

*or  if the setting controller knows the previous value and sends it 

along. the idea here is that if the controller knows it did the last 

value change, it has authority to jump now.

softSet(param, val, within = 0.025, mapped=false, lastVal)

param - the name of the parameter to set

val - the value to set the param to (can be mapped or unmapped)

within  the normalized range within which the set is accepted - default is 0.025

mapped a flag whether val is mapped (within the range of param.asSpec) or unmapped (0.0-1.0)

lastVal the previous value that a control has set - see last examples.



Typical uses would be switching to a preset, then using a faderbox, 

and avoiding sudden controller jumps that could occur (soft takeover).



Ndef(\test, { |freq=200| SinOsc.ar(freq * [0.99, 1.01])  });

Ndef(\test).play(vol: 0.1);


NodeProxyEditor(Ndef(\test), 4);


Ndef(\test).set(\freq, 300);


// 3 midi steps == 0.02362;

Ndef(\test).softSet(\freq, 320, 0.03, mapped: true); // ok

Ndef(\test).softSet(\freq, 280, 0.03, mapped: true); // ok

Ndef(\test).softSet(\freq, 200, 0.03, mapped: true); // returns false if too big jump


Ndef(\test).set(\freq, \freq.asSpec.map(0.5)); 



// use unmapped, i.e. normalized values to set. 

// this is the default behaviour.

Ndef(\test).softSet(\freq, 0.5299, 0.03); // ok

Ndef(\test).softSet(\freq, 0.45, 0.03); // no, too big



// softSet can be risky - one can lose a parameter when moving a controller

// too quickly. So, if we know the last value (e.g. because the same controller 

// knows it has set to that value),  it is OK to jump:

Ndef(\test).softSet(\freq, 0.45, 0.03, lastVal: 0.53); // 



// example of softSet, softSet which knows lastVal,

// softVol_ and softVol_ which knows lastVol:

(

w = Window("softSet, softVol", Rect(500, 200, 360, 240)).front;

w.view.addFlowLayout;

NodeProxyEditor(Ndef(\test), 2, win: w);


// can lose control if wiggled too fast

EZSlider(w, 340@30, \softSet, nil, { |sl| 

Ndef(\test).softSet(\freq, sl.value, 0.05)

});


// knows it was in control

EZSlider(w, 340@30, \knowsLast, nil, Routine { |sl| 

var newVal, lastVal; 

loop { 

newVal = sl.value;

Ndef(\test).softSet(\freq, newVal, 0.05, lastVal: lastVal);

lastVal = newVal;

\dummy.yield;

}

});


// same for volume - not too safe

EZSlider(w, 340@30, \softVol, \amp, { |sl| 

Ndef(\test).softVol_(sl.value, 0.05)

});

// safer

EZSlider(w, 340@30, \knowLastV, \amp, Routine { |sl| 

var newVal, lastVal; 

loop { 

newVal = sl.value;

Ndef(\test).softVol_(sl.value, 0.05, lastVal: lastVal);

lastVal = newVal;

\dummy.yield;

}

});

)



// Maybe rewrite as SoftSet(what, key, val, within, mapped, oldVal); // ?