Line data Source code
1 : /** 2 : * \file SecondOrderSVFFilter.h 3 : * Inspired by http://www.cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf 4 : */ 5 : 6 : #ifndef ATK_EQ_SECONDORDERSVFFILTER_H 7 : #define ATK_EQ_SECONDORDERSVFFILTER_H 8 : 9 : #include <ATK/EQ/config.h> 10 : #include <ATK/Core/TypedBaseFilter.h> 11 : 12 : namespace ATK 13 : { 14 : /// Second order SVF main filter class 15 : template<typename SVFCoefficients> 16 36 : class ATK_EQ_EXPORT SecondOrderSVFFilter final: public SVFCoefficients 17 : { 18 : protected: 19 : using Parent = SVFCoefficients; 20 : using typename Parent::AlignedScalarVector; 21 : using typename Parent::DataType; 22 : using typename Parent::CoeffDataType; 23 : using Parent::converted_inputs; 24 : using Parent::outputs; 25 : using Parent::nb_input_ports; 26 : using Parent::nb_output_ports; 27 : using Parent::a1; 28 : using Parent::a2; 29 : using Parent::a3; 30 : using Parent::m0; 31 : using Parent::m1; 32 : using Parent::m2; 33 : 34 : public: 35 : explicit SecondOrderSVFFilter(gsl::index nb_channels = 1); 36 : ~SecondOrderSVFFilter() override; 37 : 38 : void full_setup() final; 39 : protected: 40 : void process_impl(gsl::index size) const final; 41 : 42 : class SVFState; 43 : std::unique_ptr<SVFState[]> state; 44 : }; 45 : 46 : /// Second order SVF base coefficient class 47 : template<typename DataType_> 48 : class SecondOrderSVFBaseCoefficients : public TypedBaseFilter<DataType_> 49 : { 50 : public: 51 : using Parent = TypedBaseFilter<DataType_>; 52 : using typename Parent::DataType; 53 : using CoeffDataType = typename TypeTraits<DataType>::Scalar; 54 : using Parent::setup; 55 : protected: 56 : CoeffDataType cut_frequency{0}; 57 : CoeffDataType Q{1}; 58 : 59 : CoeffDataType a1{0}; 60 : CoeffDataType a2{0}; 61 : CoeffDataType a3{0}; 62 : CoeffDataType m0{0}; 63 : CoeffDataType m1{0}; 64 : CoeffDataType m2{0}; 65 : 66 : public: 67 : explicit SecondOrderSVFBaseCoefficients(gsl::index nb_channels); 68 : 69 : /// Sets the cut or central frequency of the filter 70 : void set_cut_frequency(CoeffDataType cut_frequency); 71 : /// Returns the cut or central frequency 72 : CoeffDataType get_cut_frequency() const; 73 : /// Sets the Q factor, must be strictly positive 74 : /*! 75 : * A smaller Q will lead to a bigger bandwidth, a bigger Q will lead to a smaller bandwidth 76 : */ 77 : void set_Q(CoeffDataType Q); 78 : /// Returns the Q factor 79 : CoeffDataType get_Q() const; 80 : }; 81 : 82 : /// Coefficients for a second order SVF low-pass filter 83 : template<typename DataType_> 84 : class SecondOrderSVFLowPassCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 85 : { 86 : public: 87 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 88 : using typename Parent::DataType; 89 : using typename Parent::CoeffDataType; 90 : using Parent::setup; 91 : using Parent::a1; 92 : using Parent::a2; 93 : using Parent::a3; 94 : using Parent::m0; 95 : using Parent::m1; 96 : using Parent::m2; 97 : using Parent::cut_frequency; 98 : using Parent::Q; 99 : using Parent::input_sampling_rate; 100 : 101 : explicit SecondOrderSVFLowPassCoefficients(gsl::index nb_channels); 102 : 103 : protected: 104 : void setup() override; 105 : }; 106 : 107 : /// Coefficients for a second order SVF band-pass filter 108 : template<typename DataType_> 109 : class SecondOrderSVFBandPassCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 110 : { 111 : public: 112 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 113 : using typename Parent::DataType; 114 : using typename Parent::CoeffDataType; 115 : using Parent::setup; 116 : using Parent::a1; 117 : using Parent::a2; 118 : using Parent::a3; 119 : using Parent::m0; 120 : using Parent::m1; 121 : using Parent::m2; 122 : using Parent::cut_frequency; 123 : using Parent::Q; 124 : using Parent::input_sampling_rate; 125 : 126 : explicit SecondOrderSVFBandPassCoefficients(gsl::index nb_channels); 127 : 128 : protected: 129 : void setup() override; 130 : }; 131 : 132 : /// Coefficients for a second order SVF high-pass filter 133 : template<typename DataType_> 134 : class SecondOrderSVFHighPassCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 135 : { 136 : public: 137 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 138 : using typename Parent::DataType; 139 : using typename Parent::CoeffDataType; 140 : using Parent::setup; 141 : using Parent::a1; 142 : using Parent::a2; 143 : using Parent::a3; 144 : using Parent::m0; 145 : using Parent::m1; 146 : using Parent::m2; 147 : using Parent::cut_frequency; 148 : using Parent::Q; 149 : using Parent::input_sampling_rate; 150 : 151 : explicit SecondOrderSVFHighPassCoefficients(gsl::index nb_channels); 152 : 153 : protected: 154 : void setup() override; 155 : }; 156 : 157 : /// Coefficients for a second order SVF notch filter 158 : template<typename DataType_> 159 : class SecondOrderSVFNotchCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 160 : { 161 : public: 162 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 163 : using typename Parent::DataType; 164 : using typename Parent::CoeffDataType; 165 : using Parent::setup; 166 : using Parent::a1; 167 : using Parent::a2; 168 : using Parent::a3; 169 : using Parent::m0; 170 : using Parent::m1; 171 : using Parent::m2; 172 : using Parent::cut_frequency; 173 : using Parent::Q; 174 : using Parent::input_sampling_rate; 175 : 176 : explicit SecondOrderSVFNotchCoefficients(gsl::index nb_channels); 177 : 178 : protected: 179 : void setup() override; 180 : }; 181 : 182 : /// Coefficients for a second order SVF peak filter 183 : template<typename DataType_> 184 : class SecondOrderSVFPeakCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 185 : { 186 : public: 187 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 188 : using typename Parent::DataType; 189 : using typename Parent::CoeffDataType; 190 : using Parent::setup; 191 : using Parent::a1; 192 : using Parent::a2; 193 : using Parent::a3; 194 : using Parent::m0; 195 : using Parent::m1; 196 : using Parent::m2; 197 : using Parent::cut_frequency; 198 : using Parent::Q; 199 : using Parent::input_sampling_rate; 200 : 201 : explicit SecondOrderSVFPeakCoefficients(gsl::index nb_channels); 202 : 203 : protected: 204 : void setup() override; 205 : }; 206 : 207 : /// Coefficients for a second order SVF bell filter 208 : template<typename DataType_> 209 : class SecondOrderSVFBellCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 210 : { 211 : public: 212 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 213 : using typename Parent::DataType; 214 : using typename Parent::CoeffDataType; 215 : using Parent::setup; 216 : using Parent::a1; 217 : using Parent::a2; 218 : using Parent::a3; 219 : using Parent::m0; 220 : using Parent::m1; 221 : using Parent::m2; 222 : using Parent::cut_frequency; 223 : using Parent::Q; 224 : using Parent::input_sampling_rate; 225 : 226 : explicit SecondOrderSVFBellCoefficients(gsl::index nb_channels); 227 : 228 : /// Sets the gain of the bell 229 : void set_gain(CoeffDataType gain); 230 : /// Returns the gain for the bell 231 : CoeffDataType get_gain() const; 232 : protected: 233 : void setup() override; 234 : 235 : CoeffDataType gain{1}; 236 : }; 237 : 238 : /// Coefficients for a second order SVF low-pass shelving filter 239 : template<typename DataType_> 240 : class SecondOrderSVFLowShelfCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 241 : { 242 : public: 243 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 244 : using typename Parent::DataType; 245 : using typename Parent::CoeffDataType; 246 : using Parent::setup; 247 : using Parent::a1; 248 : using Parent::a2; 249 : using Parent::a3; 250 : using Parent::m0; 251 : using Parent::m1; 252 : using Parent::m2; 253 : using Parent::cut_frequency; 254 : using Parent::Q; 255 : using Parent::input_sampling_rate; 256 : 257 : explicit SecondOrderSVFLowShelfCoefficients(gsl::index nb_channels); 258 : 259 : /// Sets the gain of the shelf 260 : void set_gain(CoeffDataType gain); 261 : /// Returns the gain for the shelf 262 : CoeffDataType get_gain() const; 263 : protected: 264 : void setup() override; 265 : 266 : CoeffDataType gain{1}; 267 : }; 268 : 269 : /// Coefficients for a second order SVF high-pass shelving filter 270 : template<typename DataType_> 271 : class SecondOrderSVFHighShelfCoefficients : public SecondOrderSVFBaseCoefficients<DataType_> 272 : { 273 : public: 274 : using Parent = SecondOrderSVFBaseCoefficients<DataType_>; 275 : using typename Parent::DataType; 276 : using typename Parent::CoeffDataType; 277 : using Parent::setup; 278 : using Parent::a1; 279 : using Parent::a2; 280 : using Parent::a3; 281 : using Parent::m0; 282 : using Parent::m1; 283 : using Parent::m2; 284 : using Parent::cut_frequency; 285 : using Parent::Q; 286 : using Parent::input_sampling_rate; 287 : 288 : explicit SecondOrderSVFHighShelfCoefficients(gsl::index nb_channels); 289 : 290 : /// Sets the gain of the shelf 291 : void set_gain(CoeffDataType gain); 292 : /// Returns the gain for the shelf 293 : CoeffDataType get_gain() const; 294 : 295 : protected: 296 : void setup() override; 297 : 298 : CoeffDataType gain{1}; 299 : }; 300 : } 301 : 302 : #endif