Sync Widget with samplerate?

Hi all,

I’m building a “loading” widget: a simple svg image that will rotate in the time:

struct Loading : TransparentWidget {
    int mStep;
    std::shared_ptr<rack::Svg> pImage;

    Loading() : mStep(0) {
        pImage = APP->window->loadSvg(gPluginPath + "res/Loading.svg");
        box.size.x = pImage->handle->width;
        box.size.y = pImage->handle->height;
    }

    void setup(Vec pos) {
        box.pos = pos;
    }
    void draw(const DrawArgs &args) override {
        float angle = ((float)mStep / 50.0f) * g2PI;

        nvgSave(args.vg);
        
        // rotate stuff

        svgDraw(args.vg, pImage->handle);
        nvgRestore(args.vg);

        mStep++;
    }
};

But I’ve simply mStep / 50.0f. What if I’d like to do a whole rotation in (let say) 2 seconds? And keep this time absolute between machines/sample rates?

I think the only way is to sync the gui thread (widget) with samplerate, right? Is there any guaranted ratio between the two?

I wish to keep the Widget indipendent, separated from Audio part of the plugin, without having to create a mStep variable on audio thread and ++ it at every sample.

Thanks :slight_smile:

You can try APP->window->getLastFrameRate() the determine the time spent since the last frame or you use your own timer. There is no relation between sample rate and screen refresh rate, they can be set independently. You shouldn’t use the dsp thread for something like that.

1 Like

Add an angle or progress or mStep variable to your Module subclass and update it as your module is processing.

Yes, as I said. But I’d like to make the widget indipendent, so I don’t need to pass a pointer to that progress variable.

Nice catch, it seems a good way. Just a question: the number returned are not ms, neither samples. It seems that doing (mStep / 4410.0f) * g2PI is 1 seconds, even if I change the sample rate :open_mouth:

As far as I understand APP->window->getLastFrameRate() returns the current screen refresh rate in Hz. This should be the minimum of „View->Frame rate“ and how many frames your hardware can render.

1 Like

I see:

mProgress += 1.0f / APP->window->getLastFrameRate();

Seems to works :wink: