# Compute dynamically a signal from a bus

Is there a way to compute dynamically a signal from a bus? In practice, I would like to add or to subtract values under condition applied directly to a signal. Thanks for any help.

I’d say that’s what buses are designed for.

Possible so far but I don’t understand what you mean with the continuation of the sentence:

Could you explain a bit further what you mean with this ? How comes the condition into play and what should it cause ?

To be clear, before the signal goes to the DAC I want to modify the current value if this value reaches a threshold, a bit like a compressor on the fly but with my own conditions and my own functions.

You can write a dedicated SynthDef for this and pipe the signal through a Synth of that kind. Busses and order of execution have to be considered, but this would be a normal fx chain and should be rather straight.
If you are thinking about using the ‘if’ ugen for conditions, see this first:

https://supercollider.github.io/tutorials/If-statements-in-a-SynthDef.html

I want to work with the Ugen In
and the condition(s) is/are applied on the signal value,
something like:
if i>0.3 then add 0.1 else subtract 0.3

IIRC in the server (i>0.3) gives 0.0 or 1.0 so you could write

sig=In.ar(0);
sig=(sig > 0.3) * 0.1+ ( (sig < 0.3) * (-0.3) ) + sig;

Yes (though really it should be ( (sig <= 0.3) * (-0.3) ) because otherwise it doesn’t handle sig == 0.3 exactly).

Or, to avoid repeating the comparison:

((true - false) * condition + false) + sig
= (0.4 * (i > 0.3) - 0.3) + sig


hjh

This does not work for me.
Let this snippet be the testing code:

    (
SynthDef("help-PinkNoise", { arg out=0;
Out.ar(out, PinkNoise.ar(0.1))

SynthDef("help-In", { arg out=0, in=0;
var sig;
sig = In.ar(in, 1).poll;
sig=(sig > 0.3) * 0.1+ ( (sig < 0.3) * (-0.3) ) + sig;
// the idea is to work with the value you can post with poll ....
Out.ar(out, sig);

)

//play noise on the right channel
x = Synth("help-PinkNoise", [\out, 1]);

//read the input and play it out on the left channel
Synth.after(x, "help-In", [\out, 0, \in, 1]);


PinkNoise.ar outputs -1 to +1.

The argument 0.1 is a multiplier, so PinkNoise.ar(0.1) outputs -0.1 to +0.1.

The maximum output value, 0.1, is below the threshold 0.3. So all possible values are below the threshold.

So the expected discontinuity at the threshold will never be triggered.

hjh

Yes, it seems that it works.
I have to test a little bit more…

I got it.
Thanks for the trick.

One useful takeaway from this thread is that it’s almost always useful to think in terms of signal ranges, e.g. SinOsc.ar(100, 0, mul: 50, add: 100) isn’t just a sinewave. It’s rather:

SinOsc.ar(100, 0)  // -1 .. +1
* 50     // -50 .. +50
+ 100    // +50 .. +150


Having this in your mind for every signal makes it a lot clearer to reason about the behavior of UGens and math ops.

hjh