Problem with Knob Update

UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Problem with Knob Update

Post by UrbanCyborg »

Can someone tell me why the following code fails to update the knob in the gui when the value is changed by mouse-dragging the knob? It works if you edit the value, but not if you changed it with the mouse. In that case, the value is correctly changed, but the gui doesn't update past where the mouse left it. I tried inserting code on several other hooks, including in ProcessSample(), but no joy.

Code: Select all

                if(component == knob1) {
                    if(doubleValue < 0.0)
                        knob1.SetValue(-1.0, true);
                }
This is from a simple test case I wrote to see what was going on with a slightly more involved module, to reduce things to the minimum necessary to replicate the problem. Off the top of my head, I don't know of any modules that try to do this, but I'm betting there probably are some; it would be necessary to automate a mixer, for instance.
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: Problem with Knob Update

Post by honki-bobo »

It looks like you've created an infinite loop. If the knobs value is below 0 you set the value to -1, which is below 0 and causes a notification, where you check if the value is below zero, which it is, and then you set it to -1 again, which creates a notification and so on and so forth. The loop can only be broken by setting a value greater than 0, which is what you do by editing the value.
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Problem with Knob Update

Post by UrbanCyborg »

I was afraid I might have some obscure recursion going on. However, in my testing, I hand-entered values below zero, and it correctly set the value and the graphic to -1. Interestingly, the one value it won't respond to is -1; I suspect there's an optimization in the knob handler whereby it exits early if you try to set the value to what it already is.

I tried using knob.SetValueNoNotification(), but that didn't help.

The basic question I have is why it works correctly with hand-entered values over the entire range of the knob, but not with mouse-dragged values.
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: Problem with Knob Update

Post by honki-bobo »

I can't verify this right now, but my assumption is this:

when you turn the knob away from the -1.0 value, the first value that is read is still below 0.0 and this gets reset to -1.0. Using SetValueNoNotification() breaks the notify-loop, but the value is still -1.0, because the newly read value is still below 0.0 and therefore the knob does not move. Maybe it jitters a bit when dragging it, but that would be it.
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Problem with Knob Update

Post by UrbanCyborg »

I don't think it's a recursion problem; I think it's a problem with mouse handler updating. Changing the code to this:

Code: Select all

                if(component == knob1) {
                    if(doubleValue < 0.0)
                        knob1.SetValue(0.0, true);
                }
Doesn't make any difference. Nor does trying to change it to any other value, positive or negative, and using SetValueNoNotificationI() doesn't matter, either. The knob visually updates when setting the value from its editor, but not when setting it by mouse dragging. I'm tempted to call this a bug, and put in a report.
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Problem with Knob Update

Post by seal58 »

What does the boolean parameter do? I never used it.
In GUI-Code popup I see this:
Knob_SetValue.jpg
Knob_SetValue.jpg (20.33 KiB) Viewed 2471 times
There isn't any other parameter than newValue.
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: Problem with Knob Update

Post by honki-bobo »

UrbanCyborg wrote: Mon Feb 14, 2022 11:30 pm I don't think it's a recursion problem; I think it's a problem with mouse handler updating.
That's what I was trying to say. When you drag the knob with the mouse it updates the knob and calls Notify() which then resets the knobs value. I don't think it's a bug.
I don't know what you are trying to achieve, but you could circumvent this problem by storing the knobs value into a variable, i.e.

Code: Select all

double knob1Value = 0.0;
and in Notify()

Code: Select all

if (component == knob1) {
    knob1Value = Math.max(0.0, doubleValue);
}
seal58 wrote: Tue Feb 15, 2022 8:16 am What does the boolean parameter do?
From the API documentation:

Code: Select all

public void SetValue​(double newValue,
                     boolean bUpdateGUI)
Sets the value of the knob.

Overrides:
    SetValue in class VoltageComponent
Parameters:
    newValue - the new value of the knob.
    bUpdateGUI - should the gui be updated? 
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Problem with Knob Update

Post by UrbanCyborg »

honki-bobo wrote:
That's what I was trying to say. When you drag the knob with the mouse it updates the knob and calls Notify() which then resets the knobs value. I don't think it's a bug.
I don't know what you are trying to achieve, but you could circumvent this problem by storing the knobs value into a variable, i.e.
What I'm trying to do is to prevent the user from dragging a control to a value that breaks a module invariant. The invariant takes the form of one control which must never be less than the value of another control. So if the user drags to a value that would break the invariant, the knob handler fixes it by resetting the value on the fly to a reasonable alternative value. I could have done this by mangling the controls' min and max values on the fly, so that the user couldn't drag to an invalid location, but doing that runs into recursion problems almost immediately, and making the visual travel on a knob be consistent with both its new limits and the old would be an awful mess. One of those instances where getting it to look pretty would come to dominate the code.

As for saving the value in a variable, it looks to me like that would just push the problem down a meta-level. The problem isn't that the control's value isn't getting reset (it is), but that the control's appearance doesn't update to match the value. But only if the updating was done via mouse-drag.
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: Problem with Knob Update

Post by honki-bobo »

Ok, I see. You can try this:

Set up 2 variables for storing the knob values:

Code: Select all

double knob1Val = 0.5; // knob default value
double knob2Val = 0.5; // knob default value
Then, in the case Knob_Changed in Notify():

Code: Select all

if (component == knob1) {
    knob2.SetRange(
        Math.max(doubleValue, 0.0), // 0.0 := button min value
        1.0,
        Math.max(doubleValue, 0.5), // 0.5 := button default value
        false,
        0);
    knob2.SetValue(Math.max(doubleValue, knob2Val));
    knob1Val = doubleValue;
} else if (component == knob2) {
    knob2Val = doubleValue;
}
In this code the value and range of knob2 depend on the value of knob1. Knob2 gets updated according to the value of knob1 and its position gets updated when you drag knob1. Maybe this helps.
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Problem with Knob Update

Post by seal58 »

Wouldn't it be possible to set knob min and max values just during designing? Or is it really necessary to change min and max even during runtime?
Post Reply

Return to “Module Designer”