Import fmit upstream version 0.97.6
[fmit.git] / libs / Music / Filter.h
1 // Copyright 2007 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #ifndef _Filter_h_
21 #define _Filter_h_
22
23 #include <assert.h>
24 #include <vector>
25 #include <deque>
26
27 namespace Music
28 {
29         std::vector<double> fir1_lowpass(int n, double cutoff);
30         std::vector<double> fir1_highpass(int n, double cutoff);
31         std::vector<double> fir1_bandpass(int n, double low_cutoff, double high_cutoff);
32
33         /* Real-Time Filter
34         */
35         class RTFilter
36         {
37           public:
38                 virtual double operator()(double v)=0;
39
40                 virtual int getLength()=0;
41
42                 virtual ~RTFilter(){}
43         };
44
45         class FIRRTFilter : public RTFilter
46         {
47                 std::vector<double> m_imp_res;
48                 std::deque<double> m_to_filter;
49
50           public:
51                 FIRRTFilter(){m_imp_res=std::vector<double>(1, 1.0);}
52                 FIRRTFilter(std::vector<double>& imp_res);
53                 void setImpulseResponse(std::vector<double>& imp_res)   {m_imp_res=imp_res;}
54
55                 virtual int getLength()                                                                 {return m_imp_res.size();}
56
57                 virtual double operator()(double v);
58         };
59
60         class RectangularHighPassRTFilter : public RTFilter
61         {
62                 int m_N;
63                 std::deque<double> m_summed_values;
64                 double m_sum;
65
66           public:
67                 RectangularHighPassRTFilter(int N=0);
68
69                 void reset(int N);
70                 virtual int getLength()                                                                 {return m_N;}
71
72                 virtual double operator()(double v);
73         };
74         class RectangularLowPassRTFilter : public RTFilter
75         {
76           public:
77                 virtual double operator()(double v);
78         };
79         class RectangularBandPassRTFilter : public RTFilter
80         {
81           public:
82                 virtual double operator()(double v);
83         };
84
85         class DummyRTFilter : public RTFilter
86         {
87           public:
88                 DummyRTFilter() {}
89
90                 virtual int getLength()                                                                 {return 1;}
91
92                 virtual double operator()(double v)     {return v;}
93         };
94 }
95
96 #endif // _Filter_h_