Accessing project sample rate in separate class (very simple!)

Hi JUCErs, total noob here with a hopefully painfully simple question.

I have a class in a separate .cpp file where I need to access the project sample rate. I wanna make sure I do this properly. (I have no trouble doing this in PluginProcessor.cpp) I tried using getSampleRate(), but the function is undefined in my other class.

Thanks for reading!

Have a setter method and call it in prepareToPlay.

Gotcha, I have this now in prepareToPlay

m_sampleRate.store(static_cast<int>(sampleRate));

I’m still iffy on how to access this in my separate .cpp file. (Sorry, I’m sure this is painfully basic!)

You may want to read up a bit more on C++ before delving into a plugin project.

You have to expose the interface to your new class to the rest of the code, if it’s in its own .cpp file that means you need a corresponding header for the class declaration. The setter method I mentioned would be a method for the new class, again exposed through its header, that you call on the class instance.

There are a few other ways to do this, but tl;dr, keeping a member variable around that holds the current sample rate and is updated only in prepareToPlay is good enough. Just don’t pretend it’s global or constant.

Thanks for the response, you helped me get it. In prepareToPlay, I now do…

objectName.className.store(static_cast<int>(sampleRate));

Cheers!