1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 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 GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#ifndef __FILTER_H__ |
24 |
#define __FILTER_H__ |
25 |
|
26 |
// TODO: we should split this file into two when we restructure the source tree for multi engine support |
27 |
|
28 |
|
29 |
// ######################################################################### |
30 |
// # Generic filter code |
31 |
// # (independent of used engine) |
32 |
|
33 |
|
34 |
/// ln(2) / 2 |
35 |
#define LN_2_2 0.34657359f |
36 |
|
37 |
#ifndef LIMIT |
38 |
# define LIMIT(v,l,u) (v < l ? l : (v > u ? u : v)) |
39 |
#endif |
40 |
|
41 |
typedef float bq_t; |
42 |
|
43 |
/** |
44 |
* Bi-quadratic filter |
45 |
* (adapted from lisp code by Eli Brandt, http://www.cs.cmu.edu/~eli/) |
46 |
*/ |
47 |
class BiquadFilter { |
48 |
protected: |
49 |
bq_t a1; |
50 |
bq_t a2; |
51 |
bq_t b0; |
52 |
bq_t b1; |
53 |
bq_t b2; |
54 |
bq_t x1; |
55 |
bq_t x2; |
56 |
bq_t y1; |
57 |
bq_t y2; |
58 |
|
59 |
/** |
60 |
* Prevent \a f from going into denormal mode which would slow down |
61 |
* subsequent floating point calculations, we achieve that by setting |
62 |
* \a f to zero when it falls under the denormal threshold value. |
63 |
*/ |
64 |
inline void KillDenormal(float& f) { |
65 |
// TODO: this is a generic solution for 32bit floats, should be replaced by CPU specific asm code |
66 |
f += 1e-18f; |
67 |
f -= 1e-18f; |
68 |
} |
69 |
public: |
70 |
inline BiquadFilter() { |
71 |
x1 = 0.0f; |
72 |
x2 = 0.0f; |
73 |
y1 = 0.0f; |
74 |
y2 = 0.0f; |
75 |
} |
76 |
|
77 |
inline bq_t Apply(const bq_t x) { |
78 |
bq_t y; |
79 |
|
80 |
y = this->b0 * x + this->b1 * this->x1 + this->b2 * this->x2 + |
81 |
this->a1 * this->y1 + this->a2 * this->y2; |
82 |
KillDenormal(y); |
83 |
this->x2 = this->x1; |
84 |
this->x1 = x; |
85 |
this->y2 = this->y1; |
86 |
this->y1 = y; |
87 |
|
88 |
return y; |
89 |
} |
90 |
|
91 |
inline bq_t ApplyFB(bq_t x, const bq_t fb) { |
92 |
bq_t y; |
93 |
|
94 |
x += this->y1 * fb * 0.98; |
95 |
y = this->b0 * x + this->b1 * this->x1 + this->b2 * this->x2 + |
96 |
this->a1 * this->y1 + this->a2 * this->y2; |
97 |
KillDenormal(y); |
98 |
this->x2 = this->x1; |
99 |
this->x1 = x; |
100 |
this->y2 = this->y1; |
101 |
this->y1 = y; |
102 |
|
103 |
return y; |
104 |
} |
105 |
}; |
106 |
|
107 |
class LowpassFilter : public BiquadFilter { |
108 |
public: |
109 |
inline LowpassFilter() : BiquadFilter() {} |
110 |
|
111 |
inline void SetParameters(bq_t fc, bq_t bw, bq_t fs) { |
112 |
bq_t omega = 2.0 * M_PI * fc / fs; |
113 |
bq_t sn = sin(omega); |
114 |
bq_t cs = cos(omega); |
115 |
bq_t alpha = sn * sinh(M_LN2 / 2.0 * bw * omega / sn); |
116 |
|
117 |
const float a0r = 1.0 / (1.0 + alpha); |
118 |
this->b0 = a0r * (1.0 - cs) * 0.5; |
119 |
this->b1 = a0r * (1.0 - cs); |
120 |
this->b2 = a0r * (1.0 - cs) * 0.5; |
121 |
this->a1 = a0r * (2.0 * cs); |
122 |
this->a2 = a0r * (alpha - 1.0); |
123 |
} |
124 |
}; |
125 |
|
126 |
class BandpassFilter : public BiquadFilter { |
127 |
public: |
128 |
inline BandpassFilter() : BiquadFilter() {} |
129 |
|
130 |
inline void SetParameters(bq_t fc, bq_t bw, bq_t fs) { |
131 |
bq_t omega = 2.0 * M_PI * fc / fs; |
132 |
bq_t sn = sin(omega); |
133 |
bq_t cs = cos(omega); |
134 |
bq_t alpha = sn * sinh(M_LN2 / 2.0 * bw * omega / sn); |
135 |
|
136 |
const float a0r = 1.0 / (1.0 + alpha); |
137 |
this->b0 = a0r * alpha; |
138 |
this->b1 = 0.0; |
139 |
this->b2 = a0r * -alpha; |
140 |
this->a1 = a0r * (2.0 * cs); |
141 |
this->a2 = a0r * (alpha - 1.0); |
142 |
} |
143 |
}; |
144 |
|
145 |
class HighpassFilter : public BiquadFilter { |
146 |
public: |
147 |
inline HighpassFilter() : BiquadFilter() {} |
148 |
|
149 |
inline void SetParameters(bq_t fc, bq_t bw, bq_t fs) { |
150 |
bq_t omega = 2.0 * M_PI * fc / fs; |
151 |
bq_t sn = sin(omega); |
152 |
bq_t cs = cos(omega); |
153 |
bq_t alpha = sn * sinh(M_LN2 / 2.0 * bw * omega / sn); |
154 |
|
155 |
const float a0r = 1.0 / (1.0 + alpha); |
156 |
this->b0 = a0r * (1.0 + cs) * 0.5; |
157 |
this->b1 = a0r * -(1.0 + cs); |
158 |
this->b2 = a0r * (1.0 + cs) * 0.5; |
159 |
this->a1 = a0r * (2.0 * cs); |
160 |
this->a2 = a0r * (alpha - 1.0); |
161 |
} |
162 |
}; |
163 |
|
164 |
|
165 |
|
166 |
// ######################################################################### |
167 |
// # Gigasampler filter code |
168 |
// # (only Gigasampler engine specific part) |
169 |
|
170 |
|
171 |
// TODO: Gigasampler's "Turbo Lowpass" and "Bandreject" filters not implemented yet |
172 |
|
173 |
#include "gig.h" |
174 |
|
175 |
#define LSF_BW 0.9 |
176 |
#define LSF_FB 0.9f |
177 |
|
178 |
class GigFilter { |
179 |
protected: |
180 |
BandpassFilter BasicBPFilter; |
181 |
HighpassFilter HPFilter; |
182 |
BandpassFilter BPFilter; |
183 |
LowpassFilter LPFilter; |
184 |
BiquadFilter* pFilter; |
185 |
bq_t scale; |
186 |
bq_t resonance; |
187 |
bq_t cutoff; |
188 |
gig::vcf_type_t Type; |
189 |
public: |
190 |
bool Enabled; |
191 |
|
192 |
inline GigFilter() { |
193 |
// set filter type to 'lowpass' by default |
194 |
pFilter = &LPFilter; |
195 |
Type = gig::vcf_type_lowpass; |
196 |
} |
197 |
|
198 |
inline bq_t Cutoff() { return cutoff; } |
199 |
|
200 |
inline bq_t Resonance() { return resonance; } |
201 |
|
202 |
inline void SetType(gig::vcf_type_t FilterType) { |
203 |
switch (FilterType) { |
204 |
case gig::vcf_type_highpass: |
205 |
pFilter = &HPFilter; |
206 |
break; |
207 |
case gig::vcf_type_bandreject: //TODO: not implemented yet |
208 |
Type = gig::vcf_type_bandpass; |
209 |
case gig::vcf_type_bandpass: |
210 |
pFilter = &BPFilter; |
211 |
break; |
212 |
case gig::vcf_type_lowpassturbo: //TODO: not implemented yet |
213 |
default: |
214 |
Type = gig::vcf_type_lowpass; |
215 |
case gig::vcf_type_lowpass: |
216 |
pFilter = &LPFilter; |
217 |
|
218 |
} |
219 |
Type = FilterType; |
220 |
} |
221 |
|
222 |
inline void SetParameters(bq_t cutoff, bq_t resonance, bq_t fs) { |
223 |
BasicBPFilter.SetParameters(cutoff, 0.7, fs); |
224 |
switch (Type) { |
225 |
case gig::vcf_type_highpass: |
226 |
HPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
227 |
break; |
228 |
case gig::vcf_type_bandpass: |
229 |
BPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
230 |
break; |
231 |
case gig::vcf_type_lowpass: |
232 |
LPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
233 |
break; |
234 |
} |
235 |
this->scale = 1.0f - resonance * 0.7f; |
236 |
this->resonance = resonance; |
237 |
this->cutoff = cutoff; |
238 |
} |
239 |
|
240 |
inline bq_t Apply(const bq_t in) { |
241 |
return (Enabled) ? pFilter->Apply(in) * this->scale + |
242 |
BasicBPFilter.ApplyFB(in, this->resonance * LSF_FB) * this->resonance |
243 |
: in; |
244 |
} |
245 |
}; |
246 |
|
247 |
#endif // __FILTER_H__ |