# IIRFilters Just Seem to Mute the audio

**URL:** <https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862>\
**Category:** Audio Plugins\
**Created:** [March 6, 2016, 2:33pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862 "2016-03-06T14:33:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Dunn](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Scott\_Dunn](https://forum.juce.com/u/Scott_Dunn)\
**Post date:** [March 6, 2016, 2:33pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/1 "2016-03-06T14:33:09Z")

</div>

I've been trying to implement some filters in my VST Plugin but they just seem to mute the signal.

Below is the code I used to implement it in the prepareToPlay() function.

m\_kickCoeff.makePeakFilter(sampleRate,m\_kFreqValue,(m\_kFreqValue\*m\_kWidthValue)/100,2);  
&nbsp;&nbsp; &nbsp;m\_kickFilter.setCoefficients(m\_kickCoeff);

And below is the processBlock() function.

void HitDetectorAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)

{  
&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp; &nbsp;for (int i = getNumInputChannels(); i \< getNumOutputChannels(); ++i)  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buffer.clear (i, 0, buffer.getNumSamples());  
&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp; &nbsp;for (int channel = 0; channel \< getNumInputChannels(); ++channel)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float\* channelData = buffer.getWritePointer (channel);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;m\_kickFilter.processSamples(channelData, buffer.getNumSamples());  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp; &nbsp;  
}

m\_kFreqValue is an integer with a value ranging from 20-20000 (100 default) , and m\_kWidthValue is an integer ranging from 0 to 100 (10 default).

Does anyone have any idea what I'm doing wrong?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 6, 2016, 2:58pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/2 "2016-03-06T14:58:00Z")

</div>

makePeakFilter is a static method, so it doesn't alter the m\_kickCoeff variable.

Try instead:

```
m_kickCoeff = IIRCoefficients::makePeakFilter(sampleRate,m_kFreqValue,(m_kFreqValue*m_kWidthValue)/100,2);
```

Good luck.

---

<div class="post-metadata">

**Author:** ![Scott\_Dunn](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Scott\_Dunn](https://forum.juce.com/u/Scott_Dunn)\
**Post date:** [March 6, 2016, 3:31pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/3 "2016-03-06T15:31:30Z")

</div>

Thanks, but unfortunately it hasn't made any difference. I'm sure the "IIRCoefficients::" part is irrelevant as I declared m\_kickCoeff as:

IIRCoefficients m\_kickCoeff;

in the header.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 6, 2016, 5:25pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/4 "2016-03-06T17:25:00Z")

</div>

> I'm sure the "IIRCoefficients::" part is irrelevant

It's not, because a static method has no access to member variables. So calling makePeakFilter does not set any coefficients. You can check that using the debugger or print out the coefficients before and after the makePeakFilter call, if you doubt that.

Instead the makePeakFilter method returns a new IIRCoefficients instance with the required coefficients. You see that in the source code:

https://github.com/julianstorer/JUCE/blob/master/modules/juce\_audio\_basics/effects/juce\_IIRFilter.cpp#L142

The change is not the IIRCoefficients:: namespace, but to set the return value of the static method to m\_kickCoeff.

```
**m\_kickCoeff =** IIRCoefficients::makePeakFilter(sampleRate,m_kFreqValue,(m_kFreqValue*m_kWidthValue)/100,2);
```

The bold part is the important change.

Good luck

N.B. from the docs of the IIRCoefficients:

> IIRCoefficients::IIRCoefficients ( )
> 
> Creates a null set of coefficients (which will produce silence).

&nbsp;

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [March 7, 2016, 7:10am UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/5 "2016-03-07T07:10:52Z")

</div>

... and the big (but common) mistake is using the same filter object for multiple channels. You need an array of IIRCoefficients objects and use a different one for each channel.

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [April 9, 2016, 6:54pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/6 "2016-04-09T18:54:29Z")

</div>

I’m having the same problem (can’t hear any output) what’s wrong with my code?

[code]

void LpfilterAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)  
{  
setLeftCoeffs = IIRCoefficients::makeLowPass(sampleRate, cutoffFrequency);  
setRightCoeffs = IIRCoefficients::makeLowPass(sampleRate, cutoffFrequency);

```
lowPassFilterL.setCoefficients(setLeftCoeffs);
    lowPassFilterR.setCoefficients(setRightCoeffs);

```

}

void LpfilterAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)  
{

```
float* channelDataLeft = buffer.getWritePointer(0);
    float* channelDataRight = buffer.getWritePointer(1);

    int totalNumSamples = buffer.getNumSamples();

    lowPassFilterL.processSamples(channelDataLeft, totalNumSamples);
    lowPassFilterR.processSamples(channelDataRight, totalNumSamples);

```

} [/code]

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [April 9, 2016, 7:22pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/7 "2016-04-09T19:22:23Z")

</div>

Hmm, I figured out why it was muted. The filter is only being modified/set when prepareToPlay is called. How do I manipulate the cutoff frequency while audio is actually playing? I have set up a slider to control my cutoffFrequency member variable in AudioProcessor. I can only hear the cutoff change after prepareToPlay has been called… I tried calling setCoefficients in processBlock but that didn’t seem to work either. I’ve also tried calling it in sliderValueChanged which does not seem to work again.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 9, 2016, 7:28pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/8 "2016-04-09T19:28:10Z")

</div>

Are you also calling setCoefficients on the filter? You don’t need to store the coefficients extra, you can call directly:  
`lowPassFilterL.setCoefficients (IIRCoefficients::makeLowPass (sampleRate, cutoffFrequency));`  
Only changing your setLeftCoeffs variable doesn’t change the filter…

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [April 9, 2016, 7:44pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/9 "2016-04-09T19:44:19Z")

</div>

When I call directly like that, firstly I get screaming feedback (I am clearing the buffer in processBlock to attempt to avoid this), then when I force a call to prepareToPlay after changing the cutoff slider I get the same behaviour as previously described. Should I be calling setCoefficients in processBlock? I’m only calling setCoefficients in prepareToPlay currently…

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [April 9, 2016, 7:50pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/10 "2016-04-09T19:50:49Z")

</div>

Tried calling in processBlock, now it actually works but I still get crazy feedback when I first instantiate my plugin and play audio.

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [April 9, 2016, 7:57pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/11 "2016-04-09T19:57:11Z")

</div>

OK, I fixed it by storing the coeffs in setLeftCoeffs in prepareToPlay and calling setCoefficients directly on the filter in processBlock. Seems to be ok now.

---

<div class="post-metadata">

**Author:** ![erteash](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@erteash](https://forum.juce.com/u/erteash)\
**Post date:** [January 9, 2017, 6:23am UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/12 "2017-01-09T06:23:49Z")

</div>

> [@daniel](#):
>
> IIRCoefficients::makeLowPass

I was having the same problem, this solved it for me too. I don’t get it why…  
This isn’t working in process block:

> LPCoef1.makeLowPass(getSampleRate(), 200);  
> LP1.setCoefficients(LPCoef1)

But this works:

> LP1.setCoefficients(IIRCoefficients::makeLowPass(getSampleRate(), 200));

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 9, 2017, 9:04am UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/13 "2017-01-09T09:04:33Z")

</div>

> [@erteash](#):
>
> I don’t get it why…

Because [IIRCoefficients::makeLowPass()](https://www.juce.com/doc/classIIRCoefficients#a789e5863ae22b5a1c817c7156e406874) is a static member. It cannot change the instance (i.e. set coefficients inside the instance), even if you are calling it with an instance, see e.g.

> [static members - cppreference.com](http://en.cppreference.com/w/cpp/language/static)  
> Static member functions:  
> Static member functions are not associated with any object. When called, they have no this pointer.

---

<div class="post-metadata">

**Author:** ![rob-walton](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rob-walton](https://forum.juce.com/u/rob-walton)\
**Post date:** [January 29, 2017, 1:53pm UTC](https://forum.juce.com/t/iirfilters-just-seem-to-mute-the-audio/16862/14 "2017-01-29T13:53:32Z")

</div>

I modified my code significantly since this was posted. I figured I only need to set the coefficients when something changes, so I created a processor.setFilterProperties() method and set the coefficients for the filter here. This method is called by the editor in sliderValueChanged() when I change the slider.

```
void MyAudioProcessor::setFilterProperties()
{
    switch (filterType)
    {
	case shelf:
	    myFilter.setCoefficients(IIRCoefficients::makeLowShelf(sampleRate, cutoffFreq.getNextValue(), resonance.getNextValue(), gainLevel.getNextValue()));
	    break;
	case lowPass:
	    myFilter.setCoefficients(IIRCoefficients::makeLowPass(sampleRate, cutoffFreq.getNextValue()));
	    break;
	case highPass:
	    myFilter.setCoefficients(IIRCoefficients::makeHighPass(sampleRate, cutoffFreq.getNextValue()));
            break;
	case peak:
	    myFilter.setCoefficients(IIRCoefficients::makePeakFilter(sampleRate, cutoffFreq.getNextValue(), resonance.getNextValue(), gainLevel.getNextValue()));
	    break;
    }
}

```

So now prepareToPlay() looks something like this:

```
 void MyAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
 {
     myFilter.reset();
 }

```

processBlock() looks something like this:

```
void MyAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    // In case we have more outputs than inputs, this code clears any output
    // channels that didn't contain input data.   
	for (int i = getNumInputChannels(); i < getNumOutputChannels(); ++i)
		buffer.clear(i, 0, buffer.getNumSamples());
	
	for (int i = 0; i < numChannels; i++)
	{
	     myFilter.processSamples(buffer.getWritePointer(channel), buffer.getNumSamples());
	}	
}

```

This worked for me. I’ve extended it to a multi-band EQ plug-in.
