Updating plugin user interface from processor

#1

I know this topic has been talked about many times, but I have a specific case that is failing and I want to understand why. I gather there are a few ways to update the UI from the processor - you can setup a timer in the UI component to pull the value from the processor, or you can use an ActionBroadcaster or ChangeBroadcaster. In this case, I have a UI component that displays a value that is a processor “parameter” (e.g. AudioProcessorParameter). I figured I would try to use AudioProcessorParameter::Listener in lieu of the above “tried and true” methods. Here is the idea:

class MyComponent :
public Component,
public AudioProcessorParameter::Listener
{
public:
    MyComponent(AudioProcessorParameter *parameter)
    {
        x = 0;
        parameter->addListener(this);
    }
    
    void setX(float newX)
    {
        x = newX;
        MessageManager::callAsync([this]() { repaint(); });
    }
    
private:
    void paint(Graphics& g) override
    {
        <uses x>
    }

    void parameterValueChanged(int index, float normValue) override
    {
        setX(normValue);
    }
    
    void parameterGestureChanged(int index, bool gestureIsStarting) override
    {
    }

    float x;
};

I thought I was being clever with the callAsync. What appears to be happening is that parameterValueChanged is being called near the time that this Component is being created. When it is called, the code fails down inside repaint() somewhere near where it says “don’t call repaint from anywhere except the MessageManager thread”. The value of x does not appear to be initialized at this time. It feels like a partially constructed object when repaint is called. But, this component is not a listener until the end of the constructor (?). Any ideas what is going wrong?

0 Likes

#2

Quickfix: you could fetch the parameter’s value in the constructor with getValue(), so it’s initialized with the current value, and later updated each time the value changes within your parameterValueChanged() callback.

Edit: I see, that problem is not the uninitialized value -.-

0 Likes