# Glitchy sound issue with IIR filter

**URL:** <https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575>\
**Category:** Audio Plugins\
**Created:** [May 23, 2022, 8:08am UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575 "2022-05-23T08:08:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![geliang87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/geliang87/32/7861_2.png) [@geliang87](https://forum.juce.com/u/geliang87)\
**Post date:** [May 23, 2022, 8:08am UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575/1 "2022-05-23T08:08:41Z")

</div>

Hi JUCE folks,

I’m developing a synth plugin based on sampling instrument. In my customized synthesiserVoice::renderNextBlock method, I put a low pass filter at the end. When I turn it off, the playback sound is fine. My problem is that when I turn the filter on, the output sound is glitchy instantly when noteOff occurs.

My code roughly looks like below:

```auto
cutoffFreq = 1000.0f;

dsp::IIR::Filter<float>::CoefficientsPtr cutoffCoefficients = dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), cutoffFreq);

updateCoefficients (leftCutoff.coefficients, cutoffCoefficients);
updateCoefficients (rightCutoff.coefficients, cutoffCoefficients);

auto leftBlock = block.getSingleChannelBlock(0);
auto rightBlock = block.getSingleChannelBlock(1);

juce::dsp::ProcessContextReplacing<float> leftContext (leftBlock);
juce::dsp::ProcessContextReplacing<float> rightContext (rightBlock);

leftCutoff.process (leftContext);
rightCutoff.process (rightContext);

```

I tested my code with sine wave input. The glitchy sound at noteOff point looks like this:

 ![glitch sound](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/1/1ad6718c0faaccf8b26ec30d1965fa016d2ab41f.jpeg)

It seems that some discontinuity happened at that particular point. Any idea on how to fix it? Thanks.

Liang

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [May 23, 2022, 2:49pm UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575/2 "2022-05-23T14:49:40Z")

</div>

> [@geliang87](#):
>
> `updateCoefficients`

Just a shot in the dark, but what happens in that function, does clearing filter states introduce some disconuity? Do you update the filter coefficients with every block? If the cutoff frequency is fixed, it’s usually just fine to set the coefficients once in prepare and then leave them as they are.

And as a sidenote, you can use the [juce::dsp::ProcessorDuplicator](https://docs.juce.com/master/structdsp_1_1ProcessorDuplicator.html) if you want a multichannel capable filter, then you don’t need the single channel blocks and separate context and filter instances etc. But this is not related to the issue that you are seeing

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 23, 2022, 2:58pm UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575/3 "2022-05-23T14:58:02Z")

</div>

In the SynthesiserVoice rendering code, you are not supposed to replace the contents of the buffer passed in, which you are apparently doing at the moment. You need to _sum_ the voice audio into the passed in buffer. You also need to properly take into account the startSample and numSamples arguments given for the renderNextBlock method.

---

<div class="post-metadata">

**Author:** ![geliang87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/geliang87/32/7861_2.png) [@geliang87](https://forum.juce.com/u/geliang87)\
**Post date:** [May 23, 2022, 3:29pm UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575/4 "2022-05-23T15:29:38Z")

</div>

Thanks, xenakios.

Your comments are very right. I just observed that when noteOff happens, the startSample with numSamples arguments only covers a portion of the output buffer. But I mistakenly processed the entire output buffer. That’s why the glitchy sound occurs all the time. I just changed the _process_ method to _processSample_. The glitchy sound goes away now.

---

<div class="post-metadata">

**Author:** ![geliang87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/geliang87/32/7861_2.png) [@geliang87](https://forum.juce.com/u/geliang87)\
**Post date:** [May 23, 2022, 3:37pm UTC](https://forum.juce.com/t/glitchy-sound-issue-with-iir-filter/51575/5 "2022-05-23T15:37:55Z")

</div>

Thanks for your reply.

The cutoff frequency is actually controlled by some other UI inputs. For simplicity, I fixed it to a constant for demonstration.

Yeah I agree the juce::dsp::ProcessorDuplicator is a better way. I don’t need to write a lot of similar and low efficient code. I will definitely consider the change. Thanks for your advice again.
