[SOLVED ] Max/Msp Past object in C++ (increment a value when a threshold is passed)

Hi everyone,
In Max/Msp which got an object called “past”, which increment a variable when a threshold is exceeded. (like a trigger)

I’d like to do the same in openframeworkq, but I don’t know how to…

For exemple, I’d like to make x a variable and y a counter : when x>100, y=y+1;

I’ve tried this, but it doesn’t work :
(in Update)

cout << "x = " << x << endl;
if (x>100){bang=1;}
cout << "bang = " << bang << endl;
if(bang == !bang){y++;}
cout << "y = " << y << endl;
if (y > 0){cout << "done" << endl; }

Thanks !

i think this should work for you.

if (x>100) {
    y = y+1;

Of if you want to explore ternary operations …

y += ( x > 100 ? 1 : 0);

This is is probably not as efficient as @genekogan’s answer since it always has an addition operation …

Thank @genekogan, but it doesn’t work for me because my input source is a microphone (which goes from 0 to 1), and I’d like to trig something, every time it goes over 0.5.

For exemple, if I say :

   if (sound>0.5)
   y = y+1;

If my sound is equal 0.6 then 0.61 then 0.65 my effect is always triggered.
I just want to trig my effect if my sound passes over 0.5, not if it is over 0.5…

@bakercp : I’ll try it right now !

EDIT: @bakercp, just tried, but my Y variable is still incrementing even if my sound > 0.5.
I’ve tried if sound=0.5, but as it’s sound, and depending on my fps, sometimes my app doesn’t get 0.5 but 0.51 for exemple so Y doesn’t increment…

Are you taking the RMS of the signal to get the volume?

I take the sound directly from and Ableton track (which is sent via Osc). So I guess no ?

Yeah you need to take rms, then do

if (rms>0.5)
   y = y+1;

@Cyril, I take it you are looking for something that triggers when a value exceeds a threshold, but that then the value must drop below the threshold before another event can be triggered?

You need to add a little extra to the if statements to get that behavior. You need to track whether or not the threshold has been exceeded/reset. That would look something like this:

bool hasThresholdBeenExceeded = false;
float threshold = 0.5;
float value;

void update() {
	// Grab the mic level from OSC and store it in the variable called value

	if (!hasThresholdBeenExceeded && value >= threshold) {
	   hasThresholdBeenExceeded = true;
	   cout << "Threshold exceeded.  It needs to be reset before it can be triggered again" << endl;
	else if (hasThresholdBeenExceeded && value < threshold) {
	    hasThresholdBeenExceeded = false;   
	    cout << "Threshold is now reset and ready to be triggered again" << endl;

1 Like

Thanks @mikewesthad, it works !

1 Like

A little by the way remark that may or may not be useful. Sometimes it’s good to have two slightly different threshold values (one for triggering the increment and one for resetting the hasThresholdbeenExceeded) so that if whatever you are thresholding is just on the edge of the threshold, it won’t keep triggering the increment.

For instance if your signal is more or less constant at 0.5 but actually oscillating between 0.499 and 0.5001 you may have tons of unwanted increment that could be avoided if your reset threshold was at 0.4 rather than 0.5.
(of course you actually pick values that are making sense for your situation)

Yep, in this situation i just take a snare drum via a “physic” trigger on it.
Then the sound is routed to my OF project and I’ve assigned a Mackie CC to the Threshold, so I can modify the amount of my visual effect. (If my threshold is low, everything is reacting via the kick drum etc… otherwise, if it high, the effect is really “jerky”. It’s now more like a DJ which controls the tension of the sound… but for the visuals :smiley: