1 /*
2   ZynAddSubFX - a software synthesizer
3 
4   Analog Filter.h - Several analog filters (lowpass, highpass...)
5   Copyright (C) 2002-2005 Nasca Octavian Paul
6   Copyright (C) 2010-2010 Mark McCurry
7   Author: Nasca Octavian Paul
8           Mark McCurry
9 
10   This program is free software; you can redistribute it and/or
11   modify it under the terms of the GNU General Public License
12   as published by the Free Software Foundation; either version 2
13   of the License, or (at your option) any later version.
14 */
15 
16 #ifndef ANALOG_FILTER_H
17 #define ANALOG_FILTER_H
18 
19 #include "../globals.h"
20 #include "Filter.h"
21 #include "Value_Smoothing_Filter.h"
22 
23 namespace zyn {
24 
25 /**Implementation of Several analog filters (lowpass, highpass...)
26  * Implemented with IIR filters
27  * Coefficients generated with "Cookbook formulae for audio EQ"*/
28 class AnalogFilter:public Filter
29 {
30     public:
31         AnalogFilter(unsigned char Ftype, float Ffreq, float Fq,
32                      unsigned char Fstages, unsigned int srate, int bufsize);
33         ~AnalogFilter();
34         void filterout(float *smp);
35         void setfreq(float frequency);
36         void setfreq_and_q(float frequency, float q_);
37         void setq(float q_);
38 
39         void settype(int type_);
40         void setgain(float dBgain);
41         void setstages(int stages_);
42         void cleanup();
43 
44         float H(float freq); //Obtains the response for a given frequency
45 
46 
47         struct Coeff {
48             float c[3], //Feed Forward
49                   d[3];    //Feed Back
50         } coeff, oldCoeff;
51 
52         static Coeff computeCoeff(int type, float cutoff, float q, int stages,
53                 float gain, float fs, int &order);
54 
55     private:
56         struct fstage {
57             float x1, x2; //Input History
58             float y1, y2; //Output History
59         } history[MAX_FILTER_STAGES + 1], oldHistory[MAX_FILTER_STAGES + 1];
60 
61         //old coeffs are used for interpolation when parameters change quickly
62 
63         //Apply IIR filter to Samples, with coefficients, and past history
64     void singlefilterout(float *smp, fstage &hist, float f, unsigned int bufsize);// const Coeff &coeff);
65         //Update coeff and order
66     void computefiltercoefs(float freq, float q);
67 
68         int   type;   //The type of the filter (LPF1,HPF1,LPF2,HPF2...)
69         int   stages; //how many times the filter is applied (0->1,1->2,etc.)
70         float freq;   //Frequency given in Hz
71         float q;      //Q factor (resonance or Q factor)
72         float gain;   //the gain of the filter (if are shelf/peak) filters
73         bool recompute; // need to recompute coeff.
74         int order; //the order of the filter (number of poles)
75 
76         int freqbufsize;
77         Value_Smoothing_Filter freq_smoothing; /* for smoothing freq modulations to avoid zipper effect */
78         bool beforeFirstTick; // reset the smoothing at first Tick
79 };
80 
81 }
82 
83 #endif
84