getSampleRate gives 0

Hi,

I’m new to Juce and I’m very happy with it.
I want to get the sample rate of the DAW in the constructor of the plugin.
I want my main processor class (class MyFirstAudioPluginAudioProcessor : public AudioProcessor) to use the getSampleRate() function in the constructor of the class.
But the function returns 0.
I have tested the plugin in the DAW’s Studio One and in Mainstage(which has basically the same DSP as Logic, I think).

Who can help me?

Hi jellebakker,
the samplerate is not known in the constructor. The method getSampleRate retrieves it from the wrapper. But the sampleRate is set in the callback AudioProcessor::prepareToPlay()
It is the best to do allocations etc. in this callback.
There is a reason, why it is not set during the construction, that is, that the constructor is also called e.g. for scanning for available plugins etc.

Hope that helps…

1 Like

in addition to @daniel’s correct answer – you can set a default sample rate for your processor… so you can initialize graphs, etc… but you also need to handle AudioProcessor::prepareToPlay() to change that sample rate to whatever the host’s sample rate is.

Rail

1 Like

Great!
Thank you guys.

…nice idea, I never thought about that…

You can also call getSampleRate() in your Editor constructor if you need it to initialize graphs. Works fine!

Do you mean an onscreen graph (plot) or an AudioProcessorGraph? If the latter, I don’t think it’s a good idea to init it in your Editor… since your Editor can come and go during the lifetime of your plug-in.

Rail

sorry for not being clear, I meant a plot of course!