Best time to run parameter calculations code

I have a lot of pre-calculations to make based on parameters that may have changed and I'm trying to figure out the best way/time to make those calcs.

It seems that I have two choices, but they both have downfalls. 

A) At the beginning of processBlock, but regulated to around 30hz:

void prepareToPlay (double sampleRate, int samplesPerBlock)
{
    triggerHz = 30.0;
    triggerCounter = (int) ((sampleRate / samplesPerBlock) / triggerHz);      
}

void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    if(counter++ % triggerCounter == 0)      // keep it at 30hz
    {
        doParameterCalculations();
    }

    // now, process all the audio
}

Or, B) create a timer to do the pre-calculations:

void prepareToPlay (double sampleRate, int samplesPerBlock)
{
    startTimerHz(30);
} 

void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    // process all the audio
}

void timerCallback()
{
    doParameterCalculations();
}

It seems that if I go with A, I'm essentially using up precious time during the processBlock call, but I'm thread safe.

But if I go with B, I'm very likely changing variables at random times while processBlock is already running. Thread safety seems compromised, but processBlock is tight as possible.

Is there a better way? Thanks.

 

 

 

Sounds like a good time to use the old pass-a-new-block-of-data-using-an-atomic-reference-counted-pointer trick.

Bit complex to describe here but Timur did a good talk about this at CppCon last year describing how it works. We should probably try to find a way to add it to the library as a utility somehow.

Are you referring to this:

https://www.youtube.com/watch?v=boPEO2auJj4  ?

 

Hmmm...this looks like it's gonna hurt...

 

Why aren't the JUCE Summit talks online somewhere??????

Some are: https://www.youtube.com/channel/UCaF6fKdDrSmPDmiZcl9KLnQ

...and more of them will be, please be patient.