AnalogTapeModel

Physical modelling signal processing for analog tape recording
Log | Files | Refs | Submodules | README | LICENSE

DCBlocker.h (1648B)


      1 #ifndef DCBLOCKER_H_INCLUDED
      2 #define DCBLOCKER_H_INCLUDED
      3 
      4 #include <JuceHeader.h>
      5 
      6 /** DC blocking filter */
      7 class DCBlocker
      8 {
      9 public:
     10     DCBlocker() = default;
     11     DCBlocker (DCBlocker&&) noexcept = default;
     12     DCBlocker& operator= (DCBlocker&&) noexcept = default;
     13 
     14     /** Prepare the DC blocker to process samples
     15       *
     16       * @param dcFreq: the cutoff frequency of the highpass filter.
     17       */
     18     void prepare (double sampleRate, float dcFreq)
     19     {
     20         for (auto& filt : hpf)
     21             filt.reset();
     22 
     23         fs = (float) sampleRate;
     24 
     25         calcCoefs (dcFreq);
     26     }
     27 
     28     void calcCoefs (float fc)
     29     {
     30         const static auto butterQs = chowdsp::QValCalcs::butterworth_Qs<float, 2 * NFilt>();
     31 
     32         float wc = MathConstants<float>::twoPi * fc / fs;
     33         float c = 1.0f / dsp::FastMathApproximations::tan (wc / 2.0f);
     34         float phi = c * c;
     35 
     36         float b[3];
     37         float a[3];
     38         for (int i = 0; i < NFilt; ++i)
     39         {
     40             float K = c / butterQs[i];
     41             float a0 = phi + K + 1.0f;
     42 
     43             b[0] = phi / a0;
     44             b[1] = -2.0f * b[0];
     45             b[2] = b[0];
     46             a[1] = 2.0f * (1.0f - phi) / a0;
     47             a[2] = (phi - K + 1.0f) / a0;
     48 
     49             hpf[i].setCoefs (b, a);
     50         }
     51     }
     52 
     53     void processBlock (float* buffer, const int numSamples)
     54     {
     55         for (auto& filt : hpf)
     56             filt.processBlock (buffer, numSamples);
     57     }
     58 
     59 private:
     60     static constexpr int NFilt = 2;
     61     std::array<chowdsp::IIRFilter<2>, NFilt> hpf;
     62 
     63     float fs = 44100.0f;
     64 
     65     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DCBlocker)
     66 };
     67 
     68 #endif // DCBLOCKER_H_INCLUDED