You should add a cookbook-section to the documentation on how to do typical use cases properly.
In an experimentation-friendly enviroment like sc people only read documentation when something goes wrong and there is a lot of cargo-cult programming where you copy some snippet from somewhere that seems to work and so you assume that this is the way to do it when in reality it is just a hack that works for reasons you don’t even understand.
But the more I understand about the problem the more I come to appreciate that the interface is actually quite well thought out.
Actually, VSTPluginController.schelp has a bunch of examples for typical use cases at the end.
The second sentence of the help file literally says:
Have a look at the examples at the bottom!
I guess I should add a link to the section… It would stick out visually and also make it easier to navigate.
In an experimentation-friendly enviroment like sc people only read documentation when something goes wrong and there is a lot of cargo-cult programming where you copy some snippet from somewhere that seems to work and so you assume that this is the way to do it when in reality it is just a hack that works for reasons you don’t even understand.
So what happens if the open fails in the above example with the CondVar?
Does cv,wait then block forever?
I was afraid you would ask this Yes, in this case it would block forever. (Note that you would still get an error message in the console, so you at least know that something has been going wrong.)
Here’s the version with error handling:
fork {
var cv = CondVar(), done = false;
~fx = VSTPluginController(~synth).open("foo", action: { done = true; cv.signalOne });
cv.wait { done }; // note the boolean test!
if (~fx.isOpen) {
// now you can use the plugin
~fx.editor;
~fx.loadPreset("baz");
} {
// error handling
}
}
Or with the older Condition (which does not require to check a separate variable):
fork {
var cond = Condition(true);
~fx = VSTPluginController(~synth).open("foo", action: { cond.signal });
cond.wait;
if (~fx.isOpen) {
// now you can use the plugin
~fx.editor;
~fx.loadPreset("baz");
} {
// error handling
}
}
Is ~fx still useful in the error handling branch (maybe to get more details on the error condition) or is at this point all you know that the open was not successful and ~fx can no longer be used?
You wouldn’t get any details, no. The reason for the failure is posted to the console though.
and ~fx can no longer be used?
You can try another plugin
I have to admit that I personally don’t really do any error handling when opening plugins. It’s not like plugins will just refuse to open randomly. Once the plugin has been probed successfully, I can just assume it will always work. If something goes wrong for some unexpected reason, the console will tell me anyway.
Could be that you can’t initialize the Condition with true and instead set test to true in the action function. TBH, I always forget how Condition works…
Personally I tend to use CondVar because the semantics are more clear to me. Just don’t forget the boolean check in the call to wait!
I have tried to do away with the boolean check in wait as I don’t understand why it is needed - but it definitely seems to be necessary - can you explain why that is?
So why is it not sufficient to have the action signal the condvar which should wake up the thread sitting on wait (without a predicate) which can then check if the plugin is open. But when I try it like that it seems wait block forever - but why is that?
You are talking about CondVar and not Condition, right? Classic condition variables always need a predicate because they do not ‘know’ that they have been signalled. If the async operation is very fast, the callback may be scheduled before the main routine calls wait. In this case, signalOne has no effect because the routine isn’t waiting yet. Without the predicate, the subsequent wait call would block forever because nobody will signal it. With the predicate, it will just return immediately without waiting.
Yeah, that’s how Condition is supposed to be used. I got a bit confused there.
I think, though, that without a timeout, it’s impossible to advance beyond the wait-with-predicate e.g. cv.wait { done } until the predicate is true…? So the error handling branch would never be reached.
CondVar can:
wait without a predicate – always advances when signaled, even if the async op was unsuccessful;
wait with a predicate, and no timeout – no way past the barrier except success (failure means the thread blocks forever);
wait with a predicate and timeout – the thread can advance right away on success, or after the timeout on failure.