Calling multiple functions with delay on a Knob's Callback
-
You can do this with a timer, but in this case function two and three might not be called if you move the knob within the 80ms timeframe (take a look at the console output and you'll see that two and three are skipped).
function functOne() { Console.print("ONE"); } function functTwo() { Console.print("TWO"); } function functThree() { Console.print("THREE"); } const var TIMER = Engine.createTimerObject(); reg STATE = 0; TIMER.setTimerCallback(function() { if(STATE == 0) { STATE = 1; functTwo(); this.startTimer(50); } if(STATE == 1) { STATE = 0; functThree(); this.stopTimer(); } }); const var Knob1 = Content.getComponent("Knob1"); inline function onKnob1Control(component, value) { functOne(); STATE = 0; TIMER.startTimer(30); }; Content.getComponent("Knob1").setControlCallback(onKnob1Control); ``
-
@Christoph-Hart Thanks! Actually the above snipped is very simplified, so I will have to use lots of timers (with also skipped ones) with this case which will be no good.
I see
callAfterDelay
API, is there any alternative solution similar to this (without using timers) or broadcasters maybe? -
No, but maybe the broadcaster's callAfterDelay function is a bit simpler to use (and to scale). You can also put the timer logic into the functions you call which might help a bit. This should be simple enough to be used multiple times with a bit of clever thinking:
const var Knob1 = Content.getComponent("Knob1"); inline function createDelayBroadcaster(knob) { return Engine.createBroadcaster({ "id": knob.get("id") + " Delayer", "args": [ "unused" ] }); } const var Knob1Broadcaster = createDelayBroadcaster(Knob1); function functOne() { Console.print("ONE"); // You need to call it with a new argument (random number) or // it won't fire... Knob1Broadcaster.callWithDelay(30, [Math.random()], functTwo); } function functTwo() { Console.print("TWO"); Knob1Broadcaster.callWithDelay(50, [Math.random()], functThree); } function functThree() { Console.print("THREE"); } inline function onKnob1Control(component, value) { functOne(); }; Content.getComponent("Knob1").setControlCallback(onKnob1Control);
-
@Christoph-Hart I'll look at it thanks!
-
@resonant Oh and if you want to deactivate the delayed calls, you can use Broadcaster.setBypassed() for this - if it's bypassed it will not call the delayed functions.
-
@resonant said in Calling multiple functions with delay on a Knob's Callback:
- Firstly run functOne()
- Then wait for 30 ms and run functTwo()
- Then wait for 50 ms and run functThree()
Just out of curiosity, why would you need this function workflow?
-
As you see in the snippet, on each knob I am using multiple functions and each function contains lots of
setAttribute
APIs. Also there are lots of knobs, so this means tons of setAttribute APIs.I've just discovered that the more setAttributes are assigned to the knobs/buttons, the more crashes happen while loading DAW Projects with multiple plugin instances.
These crashes happen on Windows and it's a memory corruption error. Because while the DAW project is loading, all of the parameters are trying to load the setAttribute functions and this causes a bottleneck that causes the crash.
I think delaying these functions will prevent crashes because the functions aren't being loaded simultaneously.
Or this might be a bug, I am not sure. But these crashes are happening with multiple plugin instances, on Windows and while loading the DAW project.
-
@resonant So if this work around succeeds it's a good alternative, but it might also mean we're lacking of a possible Hise optimisation so this bottleneck doesn't happen... @Christoph-Hart?
-
Yes I agree. If you manage to make a minimal example that recreates this I can take a look. This shouldn‘t be a reason to hack around with timers.
-
I've pushed an experimental build flag that might help with the issue. Please pull HISE and export your plugin with the
HI_DONT_SEND_ATTRIBUTE_UPDATES
flag set in your Extradefinitions. This should deactivate the internal messaging and hopefully fix the crash. Let me know if that helped. -
@Christoph-Hart Thank you for the update.
Unfortunately, this update is not a solution for the crash. The plugin seems a little bit relaxed but the crash still happens.
I sent you a minimal project of this FX plugin privately.
We tried this crash problem on different users and different computers. My advice to replicate the problem there is if possible, on Windows in
Cubase 9/10/11
orStudio One 5/6
create 3 individual DAW projects (Using this FX plugin on the channels of a real song project will give a much more realistic result):- 3 plugins
- 8 plugins
- 20 plugins
Some computers crash at 3 plugins, some at 20.
I also discovered that, if I insert only a ShapeFX module at the begining of the project (no setAttribute or parameter usage for this module), it reduces the crash because the loading time increases and this prevent crash. But the plugin seems still unstable.
I think the real cause of the crash is all of the parameter settings, UI elements...etc. are being called simultaneously at the same time on DAW project loadings. A little bit delayed calls can solve this issue.
-
@resonant So the issue doesn't happen in a single plugin instance but when multiple are used?
-
@d-healey said in Calling multiple functions with delay on a Knob's Callback:
@resonant So the issue doesn't happen in a single plugin instance but when multiple are used?
Yes exactly.
-
@resonant Are you doing realtime stuff in the UI thread?
-
@Christoph-Hart Any progress for this issue?
-
No, I need to test this. Can you give me a list of all host / OS combinations. I know Cubase is very critical when it comes to UI CPU pressure but I can‘t est on Cubase at the moment.
-
@Christoph-Hart I understand. We've tested on 11 different Windows 10 machines.
- 8 crashes are coming from Cubase version 9 or 10 or 11 unfortunately.
- 2 crashes from Studio One 5 and 6.
- 1 crash from Reaper 6.
For other DAWs and macOS, no known crashes for now. But I can definitely say that, if the plugin works in Cubase, it will work on everywhere.
For the NFR License, as a Developer you can write to Steinberg and ask from them, they are giving NFR Licenses to the Developers for free. If you get version 11, you can rest assured because you will reproduce the issue in there.
-
@resonant yeah I wrote them for an NFR but never heard back, then I stopped caring.
-
@Christoph-Hart Have you used up your free trial?
-
@Christoph-Hart said in Calling multiple functions with delay on a Knob's Callback:
@resonant yeah I wrote them for an NFR but never heard back, then I stopped caring.
They have changed their NFR License section lately. The new system is very cool and easy to use/update, their response is now quick. Maybe giving a one more shot would be useful.