LMMS
Loading...
Searching...
No Matches
SVF.h
Go to the documentation of this file.
1/*
2 dsp/SVF.h
3
4 Copyright 2002-4 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 ladder filter in Chamberlin topology. supports largely independent
9 f and Q adjustments and sweeps.
10
11*/
12/*
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2
16 of the License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA or point your web browser to http://www.gnu.org.
27*/
28/*
29 inspired by this music-dsp entry:
30
31 State Variable Filter (Double Sampled, Stable)
32 Type : 2 Pole Low, High, Band, Notch and Peaking
33 References :Posted by Andrew Simper
34
35 Notes :
36 Thanks to Laurent de Soras for the stability limit
37 and Steffan Diedrichsen for the correct notch output.
38
39 Code :
40 input = input buffer;
41 output = output buffer;
42 fs = sampling frequency;
43 fc = cutoff frequency normally something like:
44 440.0*pow(2.0, (midi_note - 69.0)/12.0);
45 res = resonance 0 to 1;
46 drive = internal distortion 0 to 0.1
47 freq = MIN(0.25, 2.0*sin(PI*fc/(fs*2))); // the fs*2 is because it's double sampled
48 damp = MIN(2.0*(1.0 - pow(res, 0.25)), MIN(2.0, 2.0/freq - freq*0.5));
49 notch = notch output
50 low = low pass output
51 high = high pass output
52 band = band pass output
53 peak = peaking output = low - high
54 --
55 double sampled svf loop:
56 for (i=0; i<numSamples; i++)
57 {
58 in = input[i];
59 notch = in - damp*band;
60 low = low + freq*band;
61 high = notch - low;
62 band = freq*high + band - drive*band*band*band;
63 out = 0.5*(notch or low or high or band or peak);
64 notch = in - damp*band;
65 low = low + freq*band;
66 high = notch - low;
67 band = freq*high + band - drive*band*band*band;
68 out += 0.5*(same out as above);
69 output[i] = out;
70 }
71*/
72
73#ifndef _DSP_SVF_H_
74#define _DSP_SVF_H_
75
76namespace DSP {
77
78template <int OVERSAMPLE>
79class SVF
80{
81 protected:
82 /* loop parameters */
84
85 /* outputs (peak and notch left out) */
88
89 public:
90 /* the type of filtering to do. */
91 enum {
92 Low = 0,
93 Band = 1,
94 High = 2
95 };
96
98 {
99 set_out (Low);
100 set_f_Q (.1, .1);
101 }
102
103 void reset()
104 {
105 hi = band = lo = 0;
106 }
107
108 void set_f_Q (double fc, double Q)
109 {
110 /* this is a very tight limit */
111 f = min (.25, 2 * sin (M_PI * fc / OVERSAMPLE));
112
113 q = 2 * cos (pow (Q, .1) * M_PI * .5);
114 q = min (q, min (2., 2 / f - f * .5));
115 qnorm = sqrt (fabs (q) / 2. + .001);
116 }
117
118 void set_out (int o)
119 {
120 if (o == Low)
121 out = &lo;
122 else if (o == Band)
123 out = &band;
124 else
125 out = &hi;
126 }
127
128 void one_cycle (sample_t * s, int frames)
129 {
130 for (int i = 0; i < frames; ++i)
131 s[i] = process (s[i]);
132 }
133
135 {
136 x = qnorm * x;
137
138 for (int pass = 0; pass < OVERSAMPLE; ++pass)
139 {
140 hi = x - lo - q * band;
141 band += f * hi;
142 lo += f * band;
143
144 /* zero-padding, not 0th order holding. */
145 x = 0;
146 }
147
148 /* peak and notch outputs don't belong in the loop, put them
149 * here (best in a template) if needed. */
150
151 return *out;
152 }
153};
154
155template <int STACKED, int OVERSAMPLE>
157{
158 public:
160
161 void reset()
162 {
163 for (int i = 0; i < STACKED; ++i)
164 svf[i].reset();
165 }
166
167 void set_out (int out)
168 {
169 for (int i = 0; i < STACKED; ++i)
170 svf[i].set_out (out);
171 }
172
173 void set_f_Q (double f, double Q)
174 {
175 for (int i = 0; i < STACKED; ++i)
176 svf[i].set_f_Q (f, Q);
177 }
178
180 {
181 for (int i = 0; i < STACKED; ++i)
182 x = svf[i].process (x);
183
184 return x;
185 }
186};
187
188} /* namespace DSP */
189
190#endif /* _DSP_SVF_H_ */
LADSPA_Data sample_t
Definition basics.h:100
Definition SVF.h:80
sample_t q
Definition SVF.h:83
sample_t qnorm
Definition SVF.h:83
sample_t f
Definition SVF.h:83
sample_t lo
Definition SVF.h:86
sample_t hi
Definition SVF.h:86
void set_out(int o)
Definition SVF.h:118
SVF()
Definition SVF.h:97
sample_t process(sample_t x)
Definition SVF.h:134
@ Band
Definition SVF.h:93
@ Low
Definition SVF.h:92
@ High
Definition SVF.h:94
sample_t band
Definition SVF.h:86
void set_f_Q(double fc, double Q)
Definition SVF.h:108
sample_t * out
Definition SVF.h:87
void reset()
Definition SVF.h:103
void one_cycle(sample_t *s, int frames)
Definition SVF.h:128
Definition SVF.h:157
void set_out(int out)
Definition SVF.h:167
sample_t process(sample_t x)
Definition SVF.h:179
SVF< OVERSAMPLE > svf[STACKED]
Definition SVF.h:159
void reset()
Definition SVF.h:161
void set_f_Q(double f, double Q)
Definition SVF.h:173
#define M_PI
Definition compat.h:149
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
unsigned f
Definition inflate.c:1572
float out
Definition lilv_test.c:1461
Definition BiQuad.h:31
#define min(x, y)
Definition os.h:74