In my main process loop, I iterate over the buttons to see if any have been clicked on using:
for (unsigned int button_id = 0; button_id < NUMBER_OF_BUTTONS; button_id++)
{
if (button_schmitt_triggers[button_id].process(params[BUTTONS + button_id].getValue()))
{
// do stuff
}
}
It’s not vitally important that button processing happens immediately. Users wouldn’t notice a very small delay.
Should I consider creating a custom button class which overrides onClick, and handling the “stuff” that happens within the onClick event instead of polling it from within the module’s main process() loop?
The Schmitt trigger logic is extremely efficient. Unless you have hundreds of buttons, I doubt this matters much. Is your goal to cut down on CPU usage in your module? I would start by commenting out different parts of your process function (even if it breaks functionality) and measure CPU usage each time. Use experimental data to identify your bottleneck. Even the best experts are often surprised when they find out what’s really slowing down their code.
Possibly the easiest (and probably laziest) thing to do, without knowing anything about the “stuff” that gets handled, why don’t you just start by polling the buttons less regularly and seeing how this affects CPU usage? I would start with something “extreme”, like polling them only once per second, and see if that makes any difference with regards to your CPU usage. Then you could increase the frequency to see if you can get it to a point where you’re happy with the CPU usage and polling frequency.
I have always done this kind of polling at every four process calls, sometime less. It’s the single easiest thing you can do to make your stuff faster. Usually.
but really don’t call anything every sample except things which need to be sample accurate. let me echo the advice above that if your user won’t mind a 0.01 second delay in the button responding you can process or at least respond every 100 samples easily.
one trick i’ve used is to run the schmidt trigger every sample but || it onto a bool
that way if your sample rate is > the open/close time you won’t miss (which is the risk if, say, you sample every second and only run the trigger every second)
so if you have something super low frequency you may want to cache the trigger or what not.
all sorts of strategies. But basically as squinty’s doc and lots of the performing modules show: don’t do stuff every sample if you don’t have to.
Like others have said, don’t do everything every sample. A long time ago (in a vst synth) I did stuff every 8 or 16 samples… now I’ve changed it to be based on time (instead of samples).
So I’ll have some stuff fill up a buffer and deal with it when buffer is full. I’ve tried using a whole millisecond or a quarter of a millisecond for the buffer.
For triggers they are expected to be on for a whole ms. (from VCV Manual - Voltage Standards) " Trigger sources should produce 10 V with a duration of 1 ms ." So you don’t have to sample triggers every frame…