# JUCE filter has popping/clip sound

**URL:** <https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377>\
**Category:** General JUCE discussion\
**Created:** [January 4, 2024, 11:16am UTC](https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377 "2024-01-04T11:16:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangdaole](https://avatars.discourse-cdn.com/v4/letter/z/ecb155/32.png) [@zhangdaole](https://forum.juce.com/u/zhangdaole)\
**Post date:** [January 4, 2024, 11:16am UTC](https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377/1 "2024-01-04T11:16:16Z")

</div>

I chose to use my own specified coefficients to create an iir filter, but there was always noise, and I don’t know what problem I overlooked? (This coefficient is not a problem as it does not produce any popping sound, which I have verified in Python)

```auto
void FilterAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    juce::ScopedNoDenormals noDenormals;
    auto totalNumInputChannels = getTotalNumInputChannels();
    auto totalNumOutputChannels = getTotalNumOutputChannels();

    for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
        buffer.clear (i, 0, buffer.getNumSamples());
    
     float* channel1Data = buffer.getWritePointer(0);
     float* channel2Data = buffer.getWritePointer(1);

    juce::dsp::IIR::Coefficients<float>coefs(std::array<float, 10>{ 0.00117571, 0. , -0.00235143, 0. , 0.00117571,1. , -3.9201592 , 5.76585759, -3.77123379, 0.92553541 });

    juce::dsp::IIR::Filter<float>filter;
    //filter.reset();
    juce::ReferenceCountedObjectPtr<juce::dsp::IIR::Coefficients<float>> coefsPtr = new juce::dsp::IIR::Coefficients<float>(coefs);
    filter.coefficients = coefsPtr;
    filter.reset();
    for (int i = 0; i < buffer.getNumSamples(); ++i)
       {
        channel1Data[i] = filter.processSample(channel1Data[i]);
        channel1Data[i] = filter.processSample(channel2Data[i]);
       }

}

```

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 4, 2024, 11:20am UTC](https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377/2 "2024-01-04T11:20:29Z")

</div>

> [@zhangdaole](#):
>
> ```auto
> for (int i = 0; i < buffer.getNumSamples(); ++i)
> {
> channel1Data[i] = filter.processSample(channel1Data[i]);
> channel1Data[i] = filter.processSample(channel2Data[i]);
> }
> 
> ```

- the result of filtering channel 2 is lost, because it’s assigned to channel 1
- the same filter is used for both channels, which will lead to discontinuities - instead, keep a separate state/filter per channel

There’s more info here:

> [@\#1 most common programming mistake that we see on the forum](https://forum.juce.com/t/1-most-common-programming-mistake-that-we-see-on-the-forum/26013):
>
> This post is here so that we have something we can link to when this same issue comes up, because the JUCE team is getting very bored of spending time replying again and again to the same damned problem! This isn’t a JUCE bug - there’s nothing we can do in our codebase that would stop people making it, which makes it painful for us to watch beginner after beginner come along and fall headfirst into the same trap! It’s also understandable that people will post a new topic when they hit it, beca…

---

<div class="post-metadata">

**Author:** ![zhangdaole](https://avatars.discourse-cdn.com/v4/letter/z/ecb155/32.png) [@zhangdaole](https://forum.juce.com/u/zhangdaole)\
**Post date:** [January 4, 2024, 2:42pm UTC](https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377/3 "2024-01-04T14:42:07Z")

</div>

I see that the solution you shared is to save an intermediate variable, how to initialize this intermediate variable A and B? I use float A = 0.0f; Deinitialize the two values and the problem is not solved

```auto
    for (int i = 0; i < buffer.getNumSamples(); ++i)
       {
        channel1Data[i] = filter.processSample(A);
        A = channel1Data[i];
        channel1Data[i] = filter.processSample(B);
        B = channe2Data[i]
       }

```

---

<div class="post-metadata">

**Author:** ![hugoderwolf](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hugoderwolf/32/14039_2.png) [@hugoderwolf](https://forum.juce.com/u/hugoderwolf)\
**Post date:** [January 4, 2024, 2:44pm UTC](https://forum.juce.com/t/juce-filter-has-popping-clip-sound/59377/4 "2024-01-04T14:44:04Z")

</div>

Also you’re creating a new filter each time, so you’ll lose the filter state in each process block. The filters (and since they’re constant also the coefficients) should be a member of your processor that you initialize in prepareToPlay().
