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.
