Caching a parameter pointer: is it safe?

I’d like to cache the indexes of parameters, I don’t see why I’d have to search for the params everytime.
Is this safe?

 // I'll get the pointer once at the start of the session
 std::atomic<float>* myParamPointer = state.getRawParameterValue ("myParam");
 // Now i'll use the value durign the whole of the session
 doSomeThingWith( *myParamPointer )

I will ofcourse not write to *myParamPointer, it is just for faster reading in the audiothread

I recently learned this extra step, which is better, as you then also get the built in rounding functions for parameters that aren’t meant to be floats:


delta = dynamic_cast<juce::AudioParameterBool*>(apvts.getParameter(ID::PARAM::delta));

2 Likes

thanks!
personally I’m happy with all unrounded floats.
My question is whether my caching is correct, meaning that the param adress is solid during a session. You suggest that it is allright then?

Yes, the pointers will remain the same. That also means Juce does not currently support dynamically adding/removing parameters of the plugin, which you might want to keep in mind if you are planning to do more complicated plugins.

1 Like

To be fair, most plugin formats don’t support that either.

1 Like

ok guys, thanks!
all clear to me now

all my parameters are created with their corresponding atomic* and I always use them in the processor. only in the editor i use get and set.

Although as a precautionary measure, before processing the audio buffer it copied the values to local variables. That is, I do not want the values to change in the middle of the process. Apart from preventing them from being read hundreds of times in a loop.

1 Like