libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/synth/filter.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) Juan Pedro Bolivar Puente 2007                          *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_FILTER_H
00024 #define PSYNTH_FILTER_H
00025 
00026 #include <psynth/synth/audio_info.hpp>
00027 #include <cmath>
00028 
00029 namespace psynth
00030 {
00031 
00032 class filter_values
00033 {
00034     friend class filter;
00035     
00036 public:
00037     enum type {
00038         LOWPASS,
00039         HIPASS,
00040         BANDPASS_CSG,
00041         BANDPASS_CZPG,
00042         NOTCH,
00043         MOOG
00044     };
00045 
00046 private:
00047     float m_b0a0, m_b1a0, m_b2a0, m_a1a0, m_a2a0; // filter coeffs
00048     float m_r, m_p, m_k; // coeffs for moog-filter
00049 
00050     type m_type;
00051     float m_freq;
00052     float m_res;
00053     float m_srate;
00054     
00055 public:
00056     filter_values (type type = LOWPASS,
00057                    float freq = 220.0f,
00058                    float res = 0.1f,
00059                    float srate = 44100.0f) {
00060         calculate (type, freq, res, srate);
00061     }
00062     
00063     void calculate (type type, float freq, float res, float srate);
00064     void calculate (float freq);
00065     void calculate ();
00066     
00067     type get_type () {
00068         return m_type;
00069     };
00070 
00071     float get_frequency () {
00072         return m_freq;
00073     }
00074 
00075     float get_resonance () {
00076         return m_res;
00077     }
00078 };
00079 
00080 class filter
00081 {
00082     filter_values* m_coef;
00083     bool m_local_coef;
00084     /*
00085       in/out history
00086     */
00087     sample m_ou1, m_ou2, m_in1, m_in2;
00088     
00089     /*
00090       in/out history for moog-filter
00091     */
00092     sample m_y1, m_y2, m_y3, m_y4, m_oldx, m_oldy1, m_oldy2, m_oldy3;
00093 
00094 public:
00095     filter(filter_values* coef = 0) :
00096         m_coef(coef), m_local_coef(false),
00097         m_ou1(0), m_ou2(0),
00098         m_in1(0), m_in2(0),
00099         m_y1(0), m_y2(0),
00100         m_y3(0), m_y4(0),
00101         m_oldx(0),
00102         m_oldy1(0), m_oldy2(0), m_oldy3(0) {
00103         if (!m_coef) {
00104             m_coef = new filter_values;
00105             m_local_coef = true;
00106         }
00107     }
00108 
00109     filter(const filter& f) :
00110         m_coef(f.m_coef), m_local_coef(f.m_local_coef),
00111         m_ou1(f.m_ou1), m_ou2(f.m_ou2),
00112         m_in1(f.m_in1), m_in2(f.m_in2),
00113         m_y1(f.m_y1), m_y2(f.m_y2),
00114         m_y3(f.m_y3), m_y4(f.m_y4),
00115         m_oldx(f.m_oldx),
00116         m_oldy1(f.m_oldy1), m_oldy2(f.m_oldy2), m_oldy3(f.m_oldy3) {
00117         if (f.m_local_coef) {
00118             m_coef = new filter_values;
00119             *m_coef = *f.m_coef;
00120         }
00121     }
00122 
00123     filter& operator= (const filter& f) {
00124         if (this != &f) {
00125             m_coef = f.m_coef;
00126             m_local_coef = f.m_local_coef;
00127             m_ou1 = f.m_ou1;
00128             m_ou2 = f.m_ou2;
00129             m_in1 = f.m_in1;
00130             m_in2 = f.m_in2;
00131             m_y1 = f.m_y1;
00132             m_y2 = f.m_y2;
00133             m_y3 = f.m_y3;
00134             m_y4 = f.m_y4;
00135             m_oldx = f.m_oldx;
00136             m_oldy1 = f.m_oldy1;
00137             m_oldy2 = f.m_oldy2;
00138             m_oldy3 = f.m_oldy3;
00139 
00140             if (f.m_local_coef) {
00141                 m_coef = new filter_values;
00142                 *m_coef = *f.m_coef;
00143             }       
00144         }
00145 
00146         return *this;
00147     }
00148     
00149     ~filter() {
00150         if (m_local_coef)
00151             delete m_coef;
00152     }
00153     
00154     filter_values* get_values () {
00155         return m_coef;
00156     }
00157 
00158     /*void set_values (filter_values* coef) {
00159         m_coef = coef;
00160         }*/
00161     
00162     sample update (sample x);
00163 };
00164 
00165 } /* namespace psynth */
00166 
00167 #endif /* PSYNTH_FILTER_H */