Search code examples
c#naudiolowpass-filter

C# - How to make the BiQuadFilter Code work


I have to following C# code which passes my WAV file through a low pass filter. I am using NAudio's BiQuadFilter for this. However there is no changes in the sound and it is still the same.

My code:

    public void setValues(ISampleProvider sourceProvider,int cutOffFreq)
    {
        this.sourceProvider = sourceProvider;
        this.cutOffFreq = cutOffFreq;

        filter_LowPass();
    }

    private void filter_LowPass()
    {
        channels = sourceProvider.WaveFormat.Channels;
        filters = new BiQuadFilter[channels];

        for (int n = 0; n < channels; n++)
            if (filters[n] == null)
                filters[n] = BiQuadFilter.LowPassFilter(44100, cutOffFreq, 1);
            else
                filters[n].SetLowPassFilter(44100, cutOffFreq, 1);
    }

    public WaveFormat WaveFormat { get { return sourceProvider.WaveFormat; } }

    public int Read(float[] buffer, int offset, int count)
    {
        int samplesRead = sourceProvider.Read(buffer, offset, count);

        for (int i = 0; i < samplesRead; i++)
            buffer[offset + i] = filters[(i % channels)].Transform(buffer[offset + i]);

        return samplesRead;
    }

This is in a class called MyFilter and I am calling it this way:

        audioFileReader = new AudioFileReader("a.wav");

        myFilter.setValues(audioFileReader, currentCutOff);

        waveOut.Init(audioFileReader);

        waveOut.Play();

The cut off values being passed (in this order only) are: 3000,2500,2000,1500 and 1000, however, there is no change being felt when cut off changes.


Solution

  • You need to pass your filter into waveOut.Init rather than audioFileReader, so that the audio from the file is pulled through the filter.

    waveOut.Init(myFilter);