Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
svf.hpp
Go to the documentation of this file.
1/**
2 * @file svf.hpp
3 * @brief A state-variable filter: low-pass, band-pass, high-pass, notch and all-pass from one section
4 */
5
6#ifndef SBL_DSP_FILTERS_SVF_HPP_
7#define SBL_DSP_FILTERS_SVF_HPP_
8
9#include <cstdint>
10#include <iterator>
11
15#include <sbl/math/clamp.hpp>
16#include <sbl/math/finite.hpp>
17
18namespace sbl::dsp {
19
20/**
21 * @brief A trapezoidal state-variable filter with five responses from one section
22 *
23 * The trapezoidal (TPT) state-variable filter of Zavalishin, "The Art of VA
24 * Filter Design": two integrators and one divide per frame, giving the
25 * low-pass, band-pass and high-pass of one second-order section from one
26 * call. With the integrator gain g = tan(pi fc) and the damping k = 1 / Q,
27 *
28 * @code
29 * hp = (x - (g + k) s1 - s2) / (1 + g (g + k))
30 * bp = g hp + s1, s1 <- g hp + bp
31 * lp = g bp + s2, s2 <- g bp + lp
32 * @endcode
33 *
34 * which is the analog prototype under the bilinear transform with its
35 * center pre-warped: the response at fc is the prototype's at any fc below
36 * Nyquist, and the bandwidth is Q's in the audio EQ cookbook's sense. The
37 * band-pass peaks at Q; bp / Q peaks at 1.
38 *
39 * The responses. The taps sum to the input as x = hp + k bp + lp, so the
40 * notch is x - k bp and the all-pass x - 2 k bp; response() gives any of
41 * the five from a frame's taps. The band-pass response is k bp, which
42 * peaks at 1 whatever the Q.
43 *
44 * The all-pass mix. A mix m of the all-pass with the input,
45 * (1 - m) x + m AP(x), is one tap, y = x - 2 m k bp: allpass_mix(). Its gain
46 * at fc is 1 - 2 m, and 1 at DC and at Nyquist, for every m: 0 is the input,
47 * 1/2 a notch, 1 the all-pass, -1 a peak of 3 (+9.54 dB). It is the
48 * all-pass response through a dry/wet mix (dynamics/dry_wet.hpp), which is
49 * how the buffer form's users get it. On a pulse the phase shift raises the
50 * peak as well: a pulse between -1 and 1 stays under 4.1 at m = 1 and
51 * m = -1 (test_svf.hpp).
52 *
53 * The frame call takes its coefficients, as OnePole's takes one: the owner
54 * works them with integrator_gain_for() and damping_for() at its own rate
55 * and decides how they move between. The filter is stable under any
56 * trajectory of g > 0 and k > 0, at audio rate too, and a constant input
57 * settles to the same output whatever the coefficients do (Wishnick,
58 * DAFx-14). Moving the center shifts the phase near fc as it goes: phase
59 * distortion whose sidebands are not band-limited.
60 *
61 * The frame call is below the signal transport: the center is in cycles per
62 * frame, kept in [CUTOFF_MIN, CUTOFF_MAX], and Q in [Q_MIN, Q_MAX]; it holds
63 * no sample rate and ramps nothing. A value that is not a number counts
64 * as the low end of its range. integrator_gain_for() uses math::fast_tan_pif,
65 * which puts the center within CUTOFF_ERROR_CENTS of the one asked for. The
66 * audio input is not tested, as OnePole's and DcBlock's are not: a
67 * non-finite input stays in the state until reset(), and its owner keeps it
68 * out. The filter adds no latency.
69 *
70 * The buffer form is on the transport: it renders the response chosen by
71 * set_response() with the center and Q as ports, each a block or a scalar.
72 * A block is used as given, its coefficient worked each frame. A scalar's
73 * coefficient is worked once a call and moves to it over RAMP_FRAMES, the
74 * first one at once, so a center set from a control does not step.
75 */
76class Svf {
77public:
78 static constexpr float CUTOFF_MIN = 1.0e-5f; /**< the lowest center, in cycles per frame: 0.48 Hz at 48 kHz */
79 static constexpr float CUTOFF_MAX = 0.45f; /**< the highest center, in cycles per frame: 0.9 of Nyquist */
80 static constexpr float CUTOFF_ERROR_CENTS = 0.01f; /**< the most fast_tan_pif moves the center, at CUTOFF_MAX */
81 static constexpr float Q_MIN = 0.5f; /**< two real poles at the center */
82 static constexpr float Q_MAX = 32.0f; /**< the highest Q */
83 static constexpr uint16_t RAMP_FRAMES = 64; /**< the buffer form's scalar ports arrive after this many frames */
84
85 /** @brief What the buffer form renders. */
86 enum class Response : uint8_t {
87 LowPass, /**< lp */
88 BandPass, /**< k bp: a peak of 1 at the center */
89 HighPass, /**< hp */
90 Notch, /**< x - k bp */
91 AllPass, /**< x - 2 k bp */
92 };
93
94 /** @brief Integrators at rest; the buffer form renders the low-pass and ramps its scalar ports over RAMP_FRAMES. */
95 Svf() {
97 damping_.set_length(RAMP_FRAMES);
98 }
99
100 /** @brief One frame's taps. bp peaks at Q; damping * bp peaks at 1. */
101 struct Taps {
102 float lp; /**< low-pass */
103 float bp; /**< band-pass, with a peak of Q */
104 float hp; /**< high-pass */
105 };
106
107 /**
108 * @brief The integrator gain g = tan(pi cutoff) for a center.
109 *
110 * @param cutoff_cycles_per_frame The center in cycles per frame, kept in [CUTOFF_MIN, CUTOFF_MAX]; a NaN gives CUTOFF_MIN
111 * @return g, above 0
112 */
113 static float integrator_gain_for(float cutoff_cycles_per_frame) {
114 return math::fast_tan_pif(sbl::math::clamp(cutoff_cycles_per_frame, CUTOFF_MIN, CUTOFF_MAX));
115 }
116
117 /**
118 * @brief The damping k = 1 / Q.
119 *
120 * @param q Q, kept in [Q_MIN, Q_MAX]; a NaN gives Q_MIN
121 * @return k, in [1 / Q_MAX, 1 / Q_MIN]
122 */
123 static float damping_for(float q) { return 1.0f / sbl::math::clamp(q, Q_MIN, Q_MAX); }
124
125 /**
126 * @brief One frame.
127 *
128 * @param x The input sample
129 * @param g The integrator gain, from integrator_gain_for()
130 * @param damping 1 / Q, from damping_for()
131 * @return The frame's taps
132 * @context Audio thread; real-time safe.
133 */
134 Taps process(float x, float g, float damping) {
135 const float hp = (x - (g + damping) * s1_ - s2_) / (1.0f + g * (g + damping));
136 const float bp = g * hp + s1_;
137 s1_ = g * hp + bp;
138 const float lp = g * bp + s2_;
139 s2_ = g * bp + lp;
140 return {lp, bp, hp};
141 }
142
143 /**
144 * @brief The all-pass mix of a frame: x - 2 mix damping bp.
145 *
146 * @param x The frame's input
147 * @param bp The frame's band-pass tap
148 * @param damping The damping the frame ran at
149 * @param mix 0 the input, 1/2 a notch, 1 the all-pass, -1 a peak; used as given
150 * @return The mixed sample
151 */
152 static float allpass_mix(float x, float bp, float damping, float mix) { return x - 2.0f * mix * damping * bp; }
153
154 /**
155 * @brief A response from a frame: its input, its taps and the damping it ran at.
156 *
157 * @param which The response
158 * @param x The frame's input
159 * @param taps The frame's taps
160 * @param damping The damping the frame ran at
161 * @return The response's sample
162 */
163 static float response(Response which, float x, const Taps& taps, float damping) {
164 switch (which) {
165 case Response::LowPass: return taps.lp;
166 case Response::BandPass: return damping * taps.bp;
167 case Response::HighPass: return taps.hp;
168 case Response::Notch: return x - damping * taps.bp;
169 case Response::AllPass: return x - 2.0f * damping * taps.bp;
170 }
171 return x;
172 }
173
174 /**
175 * @brief Parameter: the response the buffer form renders. The low-pass until set.
176 *
177 * @param which The response
178 * @context Same thread as process(); real-time safe.
179 */
180 void set_response(Response which) { response_ = which; }
181 /** @brief The response the buffer form renders. */
182 Response response() const { return response_; }
183
184 /**
185 * @brief set_response() by number, for a tool's flag.
186 *
187 * @param index 0 the low-pass to 4 the all-pass, in Response's order; clamped and rounded, a NaN gives 0
188 * @context Same thread as process(); real-time safe.
189 */
190 void set_response_index(float index) {
191 const float i = sbl::math::clamp(index, 0.0f, static_cast<float>(Response::AllPass));
192 // NOLINTNEXTLINE(bugprone-incorrect-roundings): i is non-negative
193 response_ = static_cast<Response>(static_cast<uint8_t>(i + 0.5f));
194 }
195
196 /** @brief The ports of the buffer form, in the order it takes them. */
197 static constexpr transport::Port PORTS[] = {
199 {"q"},
200 };
201
202 /** @brief The parameters, for tools. */
206
207 /**
208 * @brief The chosen response over a buffer, the ports in PORTS' order, both given
209 *
210 * Each port is a block, one value per frame, or a scalar. A scalar's
211 * coefficient moves to it over RAMP_FRAMES; the first scalar a port is
212 * given is arrived at at once, and after a block a later scalar moves
213 * from where the block ended. The center and Q are kept in their
214 * ranges. In place is fine: out may be in.
215 *
216 * @tparam Ports A block (a float pointer) or an arithmetic scalar for each of PORTS
217 * @param in Input buffer, `frames` long
218 * @param[out] out Output buffer, `frames` long
219 * @param frames Frame count
220 * @param ports The center in cycles per frame, then Q
221 * @context Audio thread; real-time safe.
222 */
223 template<class... Ports>
224 void process(const float* in, float* out, uint16_t frames, Ports... ports) {
225 static_assert(sizeof...(Ports) == std::size(PORTS) && transport::fits_v<Svf, Ports...>,
226 "SBL ERROR: Svf's buffer form takes cutoff and q, each a block or a scalar");
227 const auto cutoff = transport::arg<0>(ports...);
228 const auto q = transport::arg<1>(ports...);
229 if constexpr (transport::is_scalar_v<decltype(cutoff)>) {
230 g_.head(integrator_gain_for(static_cast<float>(cutoff)));
231 }
232 if constexpr (transport::is_scalar_v<decltype(q)>) {
233 damping_.head(damping_for(static_cast<float>(q)));
234 }
235 const Response which = response_;
236 float g = 0.0f;
237 float damping = 0.0f;
238 for (uint16_t i = 0; i < frames; ++i) {
239 if constexpr (transport::is_block_v<decltype(cutoff)>) {
240 g = integrator_gain_for(cutoff[i]);
241 } else {
242 g = g_.step();
243 }
244 if constexpr (transport::is_block_v<decltype(q)>) {
245 damping = damping_for(q[i]);
246 } else {
247 damping = damping_.step();
248 }
249 const float x = in[i];
250 out[i] = response(which, x, process(x, g, damping), damping);
251 }
252 if (frames > 0) {
253 if constexpr (transport::is_block_v<decltype(cutoff)>) {
254 g_.jump(g);
255 }
256 if constexpr (transport::is_block_v<decltype(q)>) {
257 damping_.jump(damping);
258 }
259 }
260 }
261
262 /** @brief Both integrators at rest, and the buffer form's coefficients at their targets. */
263 void reset() {
264 s1_ = 0.0f;
265 s2_ = 0.0f;
266 g_.jump(g_.target());
267 damping_.jump(damping_.target());
268 }
269
270private:
271 float s1_ = 0.0f; /**< the first integrator's state: the band-pass side */
272 float s2_ = 0.0f; /**< the second integrator's state: the low-pass side */
273 Response response_ = Response::LowPass; /**< what the buffer form renders */
274 Ramp g_{0.0f}; /**< the buffer form's integrator gain under a scalar cutoff */
275 Ramp damping_{1.0f}; /**< and its damping under a scalar Q */
276};
277
278} // namespace sbl::dsp
279
280#endif // SBL_DSP_FILTERS_SVF_HPP_
Clamping.
A linear ramp to a target over a fixed number of frames.
Definition ramp.hpp:25
float step()
One frame.
Definition ramp.hpp:90
float target() const
The value the ramp heads for.
Definition ramp.hpp:103
void head(float target)
set(), except that the first value given since construction, by head() or jump(), is arrived at at on...
Definition ramp.hpp:64
void jump(float value)
Arrive at a value at once.
Definition ramp.hpp:77
void set_length(uint16_t frames)
Set how many frames a ramp takes.
Definition ramp.hpp:39
A trapezoidal state-variable filter with five responses from one section.
Definition svf.hpp:76
Response
What the buffer form renders.
Definition svf.hpp:86
static float response(Response which, float x, const Taps &taps, float damping)
A response from a frame: its input, its taps and the damping it ran at.
Definition svf.hpp:163
void set_response(Response which)
Parameter: the response the buffer form renders. The low-pass until set.
Definition svf.hpp:180
static constexpr float Q_MIN
Definition svf.hpp:81
void set_response_index(float index)
set_response() by number, for a tool's flag.
Definition svf.hpp:190
static constexpr float CUTOFF_MIN
Definition svf.hpp:78
static constexpr transport::Parameter< Svf > PARAMETERS[]
The parameters, for tools.
Definition svf.hpp:203
static float damping_for(float q)
The damping k = 1 / Q.
Definition svf.hpp:123
static constexpr float Q_MAX
Definition svf.hpp:82
Response response() const
The response the buffer form renders.
Definition svf.hpp:182
Svf()
Integrators at rest; the buffer form renders the low-pass and ramps its scalar ports over RAMP_FRAMES...
Definition svf.hpp:95
static float allpass_mix(float x, float bp, float damping, float mix)
The all-pass mix of a frame: x - 2 mix damping bp.
Definition svf.hpp:152
static constexpr uint16_t RAMP_FRAMES
Definition svf.hpp:83
static constexpr transport::Port PORTS[]
The ports of the buffer form, in the order it takes them.
Definition svf.hpp:197
static float integrator_gain_for(float cutoff_cycles_per_frame)
The integrator gain g = tan(pi cutoff) for a center.
Definition svf.hpp:113
static constexpr float CUTOFF_MAX
Definition svf.hpp:79
void process(const float *in, float *out, uint16_t frames, Ports... ports)
The chosen response over a buffer, the ports in PORTS' order, both given.
Definition svf.hpp:224
static constexpr float CUTOFF_ERROR_CENTS
Definition svf.hpp:80
Taps process(float x, float g, float damping)
One frame.
Definition svf.hpp:134
void reset()
Both integrators at rest, and the buffer form's coefficients at their targets.
Definition svf.hpp:263
Fast analytical approximations of sin, tan, 2^x and tanh.
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
float fast_tan_pif(float f)
Fast tan(pi f) for normalized frequency f in [0, 0.497].
Definition fast_math.hpp:73
The audio stack.
Definition dry_wet.hpp:15
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
Definition clamp.hpp:24
constexpr bool fits_v
A linear ramp to a target over a fixed number of frames.
One frame's taps. bp peaks at Q; damping * bp peaks at 1.
Definition svf.hpp:101
The processing call's shape as a checked contract.