Getting AudioProcessorValueTreeState

Hey,
I just wanted to make sure I’m not doing something I shouldn’t.
Seems weird to me calling it like this but it’s working

AudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{

    auto drive = apvts.getRawParameterValue("DRIVE");
    auto range = apvts.getRawParameterValue("RANGE");
    
    float driver = drive->load();
    float ranger = range->load();
   
`//now comes the channal and the buffer loop, just saved you code
        for(int sample = 0; sample < buffer.getNumSamples(); sample ++)
        {

            channelData[sample] *= (driver * ranger); //bla bla

Is there a better/cleaner way of loading the APVTS? and if so what are the disadvantages of my code?

No need to be creating variables for the raw parameters in the processBlock(). Make them class members.

auto drive = apvts.getRawParameterValue("DRIVE");
auto range = apvts.getRawParameterValue("RANGE");

Then, you should move your raw parameter variables to prepareToPlay(), or even the constructor for the processor.

1 Like

Thanks, not sure on how to pass them from the prepareToPlay(), as I’m just starting with c++,
as I understand it they are already class members of APVTS no?
any guidance would be highly appreciated at this point

BTW I declared them as floats - because otherwise I’m getting an error - Cannot initialize a parameter of type 'float' with an lvalue of type 'std::__1::atomic<float>

The best approach is to use the parameter itself as member. They are members of the APVTS, that’s correct. But the getRawParameterValue() and the getParameter() as well do a lookup that is avoidable by having a member variable that is set only once in the constructor.

class MyAudioProcessor : public juce::AudioProcessor
{
    // ...your other code...
private:
    juce::AudioParameterFloat* drive = nullptr;
    juce::AudioParameterFloat* range = nullptr;
};

// in the constructor:
MyAudioProcessor::MyAudioProcessor()
  : // all the stuff
{
    // ... all your other initialisations ...

    drive = dynamic_cast<juce::AudioParameterFloat*>(apvts.getParameter ("DRIVE"));
    jassert (drive);  // if this fires you don't have a parameter of that type with that ID

    range = dynamic_cast<juce::AudioParameterFloat*>(apvts.getParameter ("RANGE"));
    jassert (range);  // if this fires you don't have a parameter of that type with that ID
}

// in processBlock or wherever you need it:

auto driveValue = drive->get();  // returns the current float value
auto rangeValue = range->get();  // returns the current float value

Hope that helps

1 Like

Daniel always has good and helpful ideas. So you can study his example.

I was going to illustrate my way of doing it, but I think if you can understand Daniel’s approach you will be able to get things working as you require.

1 Like