Detecting Change in Voltage

Is there a built-in method for detecting when the voltage into a port changes? I found event::Change but I’m not sure how it works… could someone explain and maybe provide an example?

I just use:

static float lastVoltage;

float voltage = inputs[INPUT].getVoltage();

if(voltage != lastVoltage) {} // Voltage has changed

lastVoltage = voltage;

Thanks, that’s exactly what I had been doing. I just wondered whether the API provides a ‘nicer’ way to do so

I‘d be careful with that. There is (in almost all cases) no reason to use static and it will most probably cause side-effects if multiple instances of the module are used.

1 Like

Can you elaborate Ben? How would you do this?

The static float would be in process() (as would all of the above), so would just persist across calls, not be visible across all instances? I think, still relearning…

Yes! How many bugs have we seen from use of static? It even happened to me once, although I was mortified. Also, be careful how you define a “change”. Should there be some threshold? Hysteresis? Minimum slope to trigger?

1 Like

To be honest, I’m not sure right now…

The static should instead be an instance variable in the module object. And then Will not need to be static.

The api is thankfully agnostic to what the signal is. It delivers a sample to you every process call, but there is no api to register for events in processed audio. It’s all up to you. So in this case do it yourself change detector is the way to go. There are a lot of helpers in the sdk, and one turns voltage changes into triggers, but it’s probably not exactly what you want here, it’s more for getting clean clocks from cv inputs.

I can see how that would work. What would that guard against that a static in process() wouldn’t?

What I meant I am not sure about: Is a static variable declared inside a function the same across all instances of an object. My C++ experience is not that long yet. My guess is „yes“ as static within a function just changes the visibility but it’s still static.

Not as far as I’m aware, a static member is shared across instances.

There will be a unique one for each module, so that if you insert two they can be independent. They will still hold their values between step() invocations, but each module will have its own.

I disagree, I think a static in a function will be shared.

I think so too, it has just a different visibility scope.

What specifically are you trying to do?

logic would dictate if x > 0 it is no longer equal to 0

https://www.cprogramming.com/tutorial/statickeyword.html

It is shared between the object instances. It’s generally a very bad idea to have class methods that have local static variables in them. There are some exceptions but for example detecting the voltage change certainly isn’t one of them. The last voltage should be a class member variable. (Non-static, obviously. :wink: )

Right, I’m off to find a nice red shirt to match my face and get my name changed to Major Misunderstanding :upside_down_face:

(and change some code here & there)

Static in process will still be shared by all instances of your plugin. In VCV plugins there are very, very few places you want to use static, and those should be for cases where you really do want to share data between instances of your plugin. For example, our plugin has a static ObjectCache that is used to share things like lookup tables between instances.

Too few to mention?