1 /* 2 * Copyright (C) 2002 - David W. Durham 3 * 4 * This file is part of ReZound, an audio editing application. 5 * 6 * ReZound is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published 8 * by the Free Software Foundation; either version 2 of the License, 9 * or (at your option) any later version. 10 * 11 * ReZound is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 19 */ 20 21 #ifndef __DSP_BiquadResFilters_H__ 22 #define __DSP_BiquadResFilters_H__ 23 24 #include "../../config/common.h" 25 26 #include <math.h> 27 28 /* --- CDSPBiquadResFilter ---------------------- 29 * Based on designs in Section 6 of "Game Programming 30 * Gems 3" 31 * 32 * The first template parameter specifies the type of the input samples (and is thus 33 * the type also of the output, the return value of processSample() ). And The second 34 * template parameter specifies the type of the coefficients used in the calculations. 35 */ 36 // ??? an version that uses an LFO would be nice.. and pretty easy... just recalc coefficients from the LFO each time 37 template<class sample_t,class coefficient_t> class TDSPBiquadResFilter 38 { 39 public: processSample(const sample_t inputSample)40 const sample_t processSample(const sample_t inputSample) 41 { 42 const coefficient_t ouputSample=a0*inputSample + a1*prevInputSample1 + a2*prevInputSample2 - b1*prevOutputSample1 - b2*prevOutputSample2; 43 prevOutputSample2=prevOutputSample1; 44 prevOutputSample1=ouputSample; 45 46 prevInputSample2=prevInputSample1; 47 prevInputSample1=inputSample; 48 49 return((sample_t)ouputSample); 50 } 51 52 protected: 53 const coefficient_t omega,sin_omega,cos_omega,alpha,scalar; 54 coefficient_t a0,a1,a2,b1,b2; 55 56 // the cut-off frequency is expressed in a fraction of the sampling rate [0,0.5] TDSPBiquadResFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance)57 TDSPBiquadResFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance) : 58 omega(2.0*M_PI*cutoffFreqFraction), 59 sin_omega(sin(omega)), 60 cos_omega(cos(omega)), 61 alpha(sin_omega/(2.0*resonance)), 62 scalar(1.0/(1.0+alpha)), 63 64 prevInputSample1(0), 65 prevInputSample2(0), 66 prevOutputSample1(0), 67 prevOutputSample2(0) 68 { 69 } 70 71 private: 72 coefficient_t prevInputSample1; 73 coefficient_t prevInputSample2; 74 coefficient_t prevOutputSample1; 75 coefficient_t prevOutputSample2; 76 }; 77 78 79 80 template<class sample_t,class coefficient_t=float> class TDSPBiquadResLowpassFilter : public TDSPBiquadResFilter<sample_t,coefficient_t> 81 { 82 public: 83 84 // the cut-off frequency is expressed in a fraction of the sampling rate [0,0.5] TDSPBiquadResLowpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance)85 TDSPBiquadResLowpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance) : 86 TDSPBiquadResFilter<sample_t,coefficient_t>(cutoffFreqFraction,resonance) 87 { 88 this->a0=0.5*(1.0-this->cos_omega)*this->scalar; 89 this->a1=(1.0-this->cos_omega)*this->scalar; 90 this->a2=this->a0; 91 this->b1=-2.0*this->cos_omega*this->scalar; 92 this->b2=(1.0-this->alpha)*this->scalar; 93 } 94 95 }; 96 97 template<class sample_t,class coefficient_t=float> class TDSPBiquadResHighpassFilter : public TDSPBiquadResFilter<sample_t,coefficient_t> 98 { 99 public: 100 101 // the cut-off frequency is expressed in a fraction of the sampling rate [0,0.5] TDSPBiquadResHighpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance)102 TDSPBiquadResHighpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance) : 103 TDSPBiquadResFilter<sample_t,coefficient_t>(cutoffFreqFraction,resonance) 104 { 105 this->a0=0.5*(1.0+this->cos_omega)*this->scalar; 106 this->a1=-(1.0+this->cos_omega)*this->scalar; 107 this->a2=this->a0; 108 this->b1=-2.0*this->cos_omega*this->scalar; 109 this->b2=(1.0-this->alpha)*this->scalar; 110 } 111 112 }; 113 114 template<class sample_t,class coefficient_t=float> class TDSPBiquadResBandpassFilter : public TDSPBiquadResFilter<sample_t,coefficient_t> 115 { 116 public: 117 118 // the cut-off frequency is expressed in a fraction of the sampling rate [0,0.5] TDSPBiquadResBandpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance)119 TDSPBiquadResBandpassFilter(const coefficient_t cutoffFreqFraction,const coefficient_t resonance) : 120 TDSPBiquadResFilter<sample_t,coefficient_t>(cutoffFreqFraction,resonance) 121 { 122 this->a0=this->alpha*this->scalar; 123 this->a1=0.0; 124 this->a2=-this->a0; 125 this->b1=-2.0*this->cos_omega*this->scalar; 126 this->b2=(1.0-this->alpha)*this->scalar; 127 } 128 129 }; 130 131 #endif 132