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
one_pole.hpp
Go to the documentation of this file.
1/**
2 * @file one_pole.hpp
3 * @brief A one-pole low-pass
4 */
5
6#ifndef SBL_DSP_FILTERS_ONE_POLE_HPP_
7#define SBL_DSP_FILTERS_ONE_POLE_HPP_
8
9#include <cmath>
10#include <cstdint>
11
14
15namespace sbl::dsp {
16
17/**
18 * @brief A one-pole low-pass, exact for an input that holds through the frame
19 *
20 * y += k (x - y) per frame, with k = 1 - e^(-2 pi fc) for a cutoff fc in
21 * cycles per frame: the low-pass whose response to an input that holds
22 * through the frame is exact, y(t) = x + (y(0) - x) e^(-2 pi fc t). The same
23 * form holds over any time, so coefficient_for(cutoff, frames) gives the
24 * coefficient for a part of a frame or for several, and process(x,
25 * coefficient) advances by that time toward a held input. An owner that
26 * knows when its input stepped within a frame can advance to the step and
27 * on from it, and so follow the input itself instead of samples of it.
28 *
29 * Below the signal transport: the cutoff is in cycles per frame, set by the
30 * component or app that owns it; it holds no sample rate. It declares no
31 * ports; the buffer call takes none, and PARAMETERS names the cutoff so a
32 * tool can run it alone. A
33 * non-finite or negative cutoff or time gives a coefficient of 0, where the
34 * output holds. The input is not tested, as DcBlock's is not: a non-finite
35 * input stays in the state until reset(), and its owner keeps it out.
36 * Until a cutoff is set the coefficient is 1, and the output follows the input.
37 */
38class OnePole {
39public:
40 /**
41 * @brief The coefficient for a cutoff over a time: 1 - e^(-2 pi cutoff frames), in [0, 1).
42 *
43 * @param cutoff_cycles_per_frame The corner, in cycles per frame
44 * @param frames The time one process() call covers, in frames; 1 by default
45 * @return The coefficient; 0 when cutoff * frames is not finite or not above 0
46 */
47 static float coefficient_for(float cutoff_cycles_per_frame, float frames = 1.0f) {
48 const float turns = cutoff_cycles_per_frame * frames;
49 if (!(turns > 0.0f) || !std::isfinite(turns)) {
50 return 0.0f;
51 }
52 return 1.0f - std::exp(-sbl::math::TWO_PI * turns);
53 }
54
55 /**
56 * @brief Set the cutoff for the one-frame calls.
57 *
58 * @param cycles_per_frame Cutoff in cycles per frame; a non-finite or non-positive value holds the output
59 * @context Same thread as process(); real-time safe.
60 */
61 void set_cutoff(float cycles_per_frame) { k_ = coefficient_for(cycles_per_frame); }
62
63 /**
64 * @brief One frame.
65 *
66 * @param x Input sample
67 * @return Output sample
68 * @context Audio thread; real-time safe.
69 */
70 float process(float x) { return process(x, k_); }
71
72 /**
73 * @brief An advance by the time `coefficient` was worked for, toward an input that holds through it.
74 *
75 * @param x Input, held through the advance
76 * @param coefficient A coefficient from coefficient_for(), in [0, 1)
77 * @return Output after the advance
78 * @context Audio thread; real-time safe.
79 */
80 float process(float x, float coefficient) {
81 y_ += coefficient * (x - y_);
82 return y_;
83 }
84
85 /** @brief The parameters, for tools. */
89
90 /**
91 * @brief A buffer of frames. In place is fine: out may be in.
92 *
93 * @param in Input buffer, `frames` long
94 * @param[out] out Output buffer, `frames` long
95 * @param frames Frame count
96 * @context Audio thread; real-time safe.
97 */
98 void process(const float* in, float* out, uint16_t frames) {
99 for (uint16_t i = 0; i < frames; ++i) {
100 out[i] = process(in[i]);
101 }
102 }
103
104 /**
105 * @brief The output jumps to a value.
106 *
107 * @param value New output; a non-finite value gives 0
108 */
109 void reset(float value = 0.0f) { y_ = std::isfinite(value) ? value : 0.0f; }
110
111 /** @brief The current output. */
112 float value() const { return y_; }
113 /** @brief The coefficient the one-frame calls use. */
114 float coefficient() const { return k_; }
115
116private:
117 float k_ = 1.0f; /**< no smoothing until a cutoff is set */
118 float y_ = 0.0f;
119};
120
121} // namespace sbl::dsp
122
123#endif // SBL_DSP_FILTERS_ONE_POLE_HPP_
A one-pole low-pass, exact for an input that holds through the frame.
Definition one_pole.hpp:38
void set_cutoff(float cycles_per_frame)
Set the cutoff for the one-frame calls.
Definition one_pole.hpp:61
float value() const
The current output.
Definition one_pole.hpp:112
static float coefficient_for(float cutoff_cycles_per_frame, float frames=1.0f)
The coefficient for a cutoff over a time: 1 - e^(-2 pi cutoff frames), in [0, 1).
Definition one_pole.hpp:47
void process(const float *in, float *out, uint16_t frames)
A buffer of frames. In place is fine: out may be in.
Definition one_pole.hpp:98
static constexpr transport::Parameter< OnePole > PARAMETERS[]
The parameters, for tools.
Definition one_pole.hpp:86
float process(float x)
One frame.
Definition one_pole.hpp:70
float coefficient() const
The coefficient the one-frame calls use.
Definition one_pole.hpp:114
float process(float x, float coefficient)
An advance by the time coefficient was worked for, toward an input that holds through it.
Definition one_pole.hpp:80
void reset(float value=0.0f)
The output jumps to a value.
Definition one_pole.hpp:109
The numbers every layer reaches for.
The audio stack.
Definition dry_wet.hpp:15
constexpr float TWO_PI
Definition constants.hpp:18
The processing call's shape as a checked contract.