chenlh
2026-03-26 36e42207da4c088b5bfd96f2cfc8944f890440d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
 
#include <cmath>
#include <EffectProcessorBase.h>
 
#define PI 3.14159265358979323846
 
/**
 * IIR Filter Class
 * 
 */
template <typename SampleType>
class IIRFilter : public EffectProcessorBase<SampleType>
{
public:
 
    /**
     * @brief Constructor
     */
    IIRFilter();
 
    /**
     * @brief Destructor
     */
    ~IIRFilter();
 
    /**
     * @brief Prepares object for playback
     * 
     * @param sampleRate Current sampling rate
     */
    void prepare(SampleType sampleRate) override;
 
    /**
     * @brief Processes a single sample
     * 
     * @param input Input sample
     */
    SampleType processSample(SampleType input) override;
 
    /**
     * @brief Processes a memory block that holds audio samples
     * 
     * @param data Memory block start pointer 
     * @param startSample Sample index to start processing from
     * @param endSample Number of samples to process
     */
    void process(SampleType* data, int startSample, int endSample) override;
 
    /**
     * @brief Sets filter cutoff frequency
     * 
     * @param newValue New cutoff frequency value
     * 
     * When the filter mode is set to Parametric it sets the center frequency instead.
     */
    void setCutoff(SampleType newValue);
 
    /**
     * @brief Sets the filter type
     * 
     * @param filterType Filter type index
     */
    void setFilterType(int filterType);
 
    /**
     * @brief Sets the filter quality factor
     * 
     * @param newValue New filter quality factor
     */
    void setQ(SampleType newValue);
 
    /**
     * @brief Sets the amount of boost/attenuation of the parametric mode
     * 
     * @param newValue New boost/attention value in decibels
     */
    void setGain(SampleType newValue);
 
    /**
     * @brief Returns the cutoff/center frequency of the filter
     */
    SampleType getCutoff();
 
    /**
     * @brief Returns the quality factor of the filter
     */
    SampleType getQ();
 
    /**
     * @brief Returns the boost/cut value of the parametric filter mode
     */
    SampleType getGain();
 
    /**
     * @brief Returns the filter type index
     */
    int getFilterType();
    
    enum FilterTypes
    {
        LPF = 0,    // Low Pass Fitler
        HPF,        // High Pass Filter
        Parametric,
    };
 
 
private:
 
    SampleType a0, a1, a2, b1, b2, c0, d0, r, C;
 
    // Paremetric EQ Filter Variables
    SampleType K, V0, e0, D0, alpha, beta, gamma, delta, heta;
    SampleType theta_c, u, zeta;
 
    // Previous in/out values
    SampleType xn_1 = 0, xn_2 = 0, yn_1 = 0, yn_2 = 0;
 
    // User Parameters
    SampleType fc {500.0}, Q {1.0}, notchGain {0.0};
    int filterType {FilterTypes::LPF};
 
    void calculateCoeffs();
 
};