1 /*
2  * Copyright 2007-2017 by Erik Hofman.
3  * Copyright 2009-2017 by Adalin B.V.
4  *
5  * This file is part of SimGear
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the Lesser GNU General Public License as published
9  *  by the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the Lesser GNU General Public License
18  *  along with this program;
19  *  if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  */
22 
23 #ifndef _SIMGEAR_FREQUENCY_FILTER_HXX
24 #define _SIMGEAR_FREQUENCY_FILTER_HXX
25 
26 #include <cstdint>
27 
28 namespace simgear {
29 
30 // Every stage is a 2nd order filter
31 // Four stages therefore equals to an 8th order filter with a 48dB/oct slope.
32 #define SG_FREQFILTER_MAX_STAGES	4
33 
34 class FreqFilter {
35 
36 private:
37 
38     float fs, Q, gain;
39     float coeff[4*SG_FREQFILTER_MAX_STAGES];
40     float hist[2*SG_FREQFILTER_MAX_STAGES];
41     unsigned char no_stages;
42 
43     void butterworth_compute(float fc);
44     void bilinear(float a0, float a1, float a2,
45                   float b0, float b1, float b2,
46                   float *k, int stage);
47     void bilinear_s2z(float *a0, float *a1, float *a2,
48                       float *b0, float *b1, float *b2,
49                       float fc, float fs, float *k, int stage);
50 
51 public:
52 
53     FreqFilter(int order, float fs, float cutoff, float Qfactor = 1.0f);
54     ~FreqFilter();
55 
56     void update( int16_t *data, unsigned int num );
57 };
58 
59 
60 
61 class BitCrusher {
62 
63 private:
64     float factor, devider;
65 
66 public:
67 
68     // level ranges from 0.0f (all muted) to 1.0f (no change)
69     BitCrusher(float level);
70     ~BitCrusher();
71 
72     void update( int16_t *data, unsigned int num );
73 };
74 
75 }; // namespace simgear
76 
77 #endif // _SIMGEAR_FREQUENCY_FILTER_HXX
78