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
slew.hpp
Go to the documentation of this file.
1/**
2 * @file slew.hpp
3 * @brief One-pole slew toward a target, with separate rise and fall time constants
4 */
5
6#ifndef SBL_DSP_SMOOTHING_SLEW_HPP_
7#define SBL_DSP_SMOOTHING_SLEW_HPP_
8
9#include <cmath>
10#include <cstdint>
11
12namespace sbl::dsp {
13
14/**
15 * @brief One-pole slew toward a target, with separate rise and fall time constants
16 *
17 * y += k (target - y) per frame, k the rise coefficient when the target is
18 * above y and the fall coefficient when below. Below the signal transport:
19 * the time constants are in frames, set by the component or app that owns
20 * it. Until a time constant is set, its coefficient is 1 and the output
21 * jumps to the target.
22 */
23class Slew {
24public:
25 /**
26 * @brief The coefficient for a time constant in frames: 1 - e^(-1 / tau).
27 *
28 * The step response reaches 1 - 1/e after tau frames.
29 *
30 * @param time_constant_frames tau, in frames
31 * @return k in [0, 1]: 1 (instant) for a tau of 0 or less, or NaN; 0 for +inf, where the output holds
32 */
33 static float coefficient_for(float time_constant_frames) {
34 if (!(time_constant_frames > 0.0f)) {
35 return 1.0f;
36 }
37 return 1.0f - std::exp(-1.0f / time_constant_frames);
38 }
39
40 /**
41 * @brief Set the rise time constant.
42 *
43 * @param time_constant_frames tau in frames, as coefficient_for() takes it
44 * @context Same thread as process(); real-time safe.
45 */
46 void set_rise(float time_constant_frames) { rise_ = coefficient_for(time_constant_frames); }
47
48 /**
49 * @brief Set the fall time constant.
50 *
51 * @param time_constant_frames tau in frames, as coefficient_for() takes it
52 * @context Same thread as process(); real-time safe.
53 */
54 void set_fall(float time_constant_frames) { fall_ = coefficient_for(time_constant_frames); }
55
56 /**
57 * @brief Set where the output heads.
58 *
59 * @param target The target; a non-finite value counts as 0
60 * @context Same thread as process(); real-time safe.
61 */
62 void set_target(float target) { target_ = std::isfinite(target) ? target : 0.0f; }
63
64 /**
65 * @brief One frame.
66 *
67 * @return The output after the step
68 * @context Audio thread; real-time safe.
69 */
70 float process() {
71 const float k = target_ > y_ ? rise_ : fall_;
72 y_ += k * (target_ - y_);
73 return y_;
74 }
75
76 /**
77 * @brief A buffer of frames.
78 *
79 * @param[out] out Output buffer, `frames` long
80 * @param frames Frame count
81 * @context Audio thread; real-time safe.
82 */
83 void process(float* out, uint16_t frames) {
84 for (uint16_t i = 0; i < frames; ++i) {
85 out[i] = process();
86 }
87 }
88
89 /**
90 * @brief The output and the target jump to a value.
91 *
92 * @param value New output and target; a non-finite value gives 0
93 */
94 void reset(float value = 0.0f) {
95 y_ = std::isfinite(value) ? value : 0.0f;
96 target_ = y_;
97 }
98
99 /** @brief The current output. */
100 float value() const { return y_; }
101 /** @brief The current target. */
102 float target() const { return target_; }
103
104private:
105 float rise_ = 1.0f;
106 float fall_ = 1.0f;
107 float target_ = 0.0f;
108 float y_ = 0.0f;
109};
110
111} // namespace sbl::dsp
112
113#endif // SBL_DSP_SMOOTHING_SLEW_HPP_
One-pole slew toward a target, with separate rise and fall time constants.
Definition slew.hpp:23
float target() const
The current target.
Definition slew.hpp:102
float value() const
The current output.
Definition slew.hpp:100
void set_fall(float time_constant_frames)
Set the fall time constant.
Definition slew.hpp:54
void set_target(float target)
Set where the output heads.
Definition slew.hpp:62
void set_rise(float time_constant_frames)
Set the rise time constant.
Definition slew.hpp:46
void reset(float value=0.0f)
The output and the target jump to a value.
Definition slew.hpp:94
float process()
One frame.
Definition slew.hpp:70
static float coefficient_for(float time_constant_frames)
The coefficient for a time constant in frames: 1 - e^(-1 / tau).
Definition slew.hpp:33
void process(float *out, uint16_t frames)
A buffer of frames.
Definition slew.hpp:83
The audio stack.
Definition dry_wet.hpp:15