CPU usage in standalone plugin

I’d like to get the CPU usage of my plugin and I assume that I can only do that when I’m running the standalone version of my plugin. And to get the CPU usage, I need to get the AudioDeviceManager from my processor.

Any ideas how to do that?

The only way I know of is to use the StandalonePluginHolder class. You’ll have to include the header file since it’s not included with the audio_plugin_client module by default:

#include <juce_audio_plugin_client/Standalone/juce_StandaloneFilterWindow.h>

if (auto* holder = juce::StandalonePluginHolder::getInstance())
{
    auto cpuUsage = holder->deviceManager.getCpuUsage();
    // ...
}
2 Likes

The above solution totally works.

But - you can also init one of those on the stack so you if you have multiple processors and want to measure just one:

//in your class somewhere:
juce::AudioProcessLoadMeasurer measurer;

void prepareToPlay(double sr, int blockSize) override
{
    measurer.reset(sr, blockSize);
}

void processBlock(AudioBuffer<float>&, MidiBuffer&) override
{
    juce::AudioProcessLoadMeasurer::ScopedTimer s(measurer);
    //processing you want to measure
}
1 Like

Wonderful. I didn’t even know that class existed. Works perfectly.

I fleshed out your example to be a little more complete. Thanks again.

//in your processor class somewhere:
juce::AudioProcessLoadMeasurer measurer;
float cpuLoad; 

void prepareToPlay(double sr, int blockSize) override
{
    measurer.reset(sr, blockSize);
}

void processBlock(AudioBuffer<float>&, MidiBuffer&) override
{
    // I added the inner brackets to be clear exactly what was being measured
    {
        juce::AudioProcessLoadMeasurer::ScopedTimer s(measurer);

        //processing you want to measure
    }
    cpuLoad = measurer.getLoadAsPercentage();
}

// probably called on a timer in your editor
float getCPULoad()
{
    return cpuLoad;
}

Great! You probably want to make cpuLoad an std::atomic<float>.