dsp::IIR::Filter inside dsp::ProcessorDuplicator: updating coefficients inside processBlock() not working

Hello, this might be a silly problem but I’m a bit puzzled right now.

I’m trying t to filter the input signal in the same way for the left and right channel using a dsp::IIR::Filter so instead of creating 2 filters manually (for l/r channels) I’m trying using a ProcessorDuplicator.

The issue is this: apparently setting the filter coefficients only works once, when I do it inside prepareToPlay. After that, it’s not possible to change the filter coefficients anymore from the processBlock(). Let’s say if I do something like this:

void AudioPluginAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
...
    stereoFilter.reset();
    stereoFilter.prepare (spec);

    // this shouldn't be here... 
    stereoFilter.state = *juce::dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), 500, 0.7);
}

(where stereoFilter is defined as an instance as juce::dsp::ProcessorDuplicator<juce::dsp::IIR::Filter<float>, juce::dsp::IIR::Coefficients<float>> stereoFilter;)

this will set the frequency to 500hz and so far it works fine (filter does the job).

However if I try to change the frequency in processBlock (based on a parameter value) that seems to be ignored…

void processBlock (...)
{
    ...
    currentFreqSmoothed = // read frequency from the parameter
    
     // this is NOT doing anything!! the filter's frequency is the one set in prepareToPlay!!
    stereoFilter.state = *juce::dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), currentFreqSmoothed, 0.7);

    juce::dsp::AudioBlock<float> block (buffer);
    juce::dsp::ProcessContextReplacing context (block);
    stereoFilter.process (context);
    ...
}

Am I doing something wrong? I’m not super familiar with the DSP module classes so I might as well be missing something easy. Thanks!

I have never used juce::dsp::IIR::Coefficients before because it allocates memory. Does the following work?

*stereoFilter.state = juce::dsp::IIR::ArrayCoefficients<float>::makeLowPass (getSampleRate(), currentFreqSmoothed, 0.7);
1 Like

Ah right, yep, this works! thanks!