AnalogTapeModel

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

DegradeFilter.h (1716B)


      1 #ifndef DEGRADEFILTER_H_INCLUDED
      2 #define DEGRADEFILTER_H_INCLUDED
      3 
      4 #include "JuceHeader.h"
      5 
      6 /** Lowpass filter for tape degrade effect */
      7 class DegradeFilter
      8 {
      9 public:
     10     DegradeFilter() { freq.reset (numSteps); }
     11     DegradeFilter (DegradeFilter&&) noexcept = default;
     12 
     13     void reset (float sampleRate, int steps = 0)
     14     {
     15         fs = sampleRate;
     16         for (int n = 0; n < 2; ++n)
     17             z[n] = 0.0f;
     18 
     19         if (steps > 0)
     20             freq.reset (steps);
     21 
     22         freq.setCurrentAndTargetValue (freq.getTargetValue());
     23         calcCoefs (freq.getCurrentValue());
     24     }
     25 
     26     inline void calcCoefs (float fc)
     27     {
     28         float wc = MathConstants<float>::twoPi * fc / fs;
     29         float c = 1.0f / dsp::FastMathApproximations::tan (wc / 2.0f);
     30         float a0 = c + 1.0f;
     31 
     32         b[0] = 1 / a0;
     33         b[1] = b[0];
     34         a[1] = (1.0f - c) / a0;
     35     }
     36 
     37     inline void process (float* buffer, int numSamples)
     38     {
     39         for (int n = 0; n < numSamples; ++n)
     40         {
     41             if (freq.isSmoothing())
     42                 calcCoefs (freq.getNextValue());
     43 
     44             buffer[n] = processSample (buffer[n]);
     45         }
     46     }
     47 
     48     inline float processSample (float x)
     49     {
     50         float y = z[1] + x * b[0];
     51         z[1] = x * b[1] - y * a[1];
     52         return y;
     53     }
     54 
     55     void setFreq (float newFreq)
     56     {
     57         freq.setTargetValue (newFreq);
     58     }
     59 
     60 private:
     61     SmoothedValue<float, ValueSmoothingTypes::Multiplicative> freq = 20000.0f;
     62     float fs = 44100.0f;
     63     const int numSteps = 200;
     64 
     65     float a[2] = { 1.0f, 0.0f };
     66     float b[2] = { 1.0f, 0.0f };
     67     float z[2] = { 1.0f, 0.0f };
     68 
     69     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DegradeFilter)
     70 };
     71 
     72 #endif // DEGRADEFILTER_H_INCLUDED