Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
slew_limiter.hpp
Go to the documentation of this file.
1// sbl/dsp/primitives/slew_limiter.hpp — Slew rate limiter (float)
2//
3// Per-sample rate limiting with independent rise and fall rates.
4// Used for portamento, parameter smoothing, tape motor time slew,
5// and envelope shapes.
6//
7// Rate is specified as maximum change per sample (in float units).
8// Higher rate = faster slew. Rate of 0 = no change (hold).
9//
10// Usage:
11// sbl::dsp::SlewLimiter slew;
12// slew.set_rise_rate(0.01f);
13// slew.set_fall_rate(0.005f);
14// float out = slew.process(target); // Per-sample rate limiting
15
16#ifndef SBL_DSP_PRIMITIVES_SLEW_LIMITER_HPP_
17#define SBL_DSP_PRIMITIVES_SLEW_LIMITER_HPP_
18
19#include <cstdint>
20
21namespace sbl::dsp::primitives {
22
24public:
25 /// @note All public methods are ISR-safe — bounded computation, no I/O.
26
27 /** @brief Set maximum increase per sample */
28 void set_rise_rate(float rate) { rise_rate_ = rate; }
29
30 /** @brief Set maximum decrease per sample */
31 void set_fall_rate(float rate) { fall_rate_ = rate; }
32
33 /** @brief Set both rise and fall rates to the same value */
34 void set_rate(float rate) { rise_rate_ = rate; fall_rate_ = rate; }
35
36 /**
37 * @brief Process one sample toward target with rate limiting
38 * @param target Target value
39 * @return Rate-limited output
40 */
41 float process(float target) {
42 float diff = target - state_;
43
44 if (diff > 0.0f) {
45 if (diff > rise_rate_) {
46 state_ += rise_rate_;
47 } else {
48 state_ = target;
49 }
50 } else if (diff < 0.0f) {
51 if (-diff > fall_rate_) {
52 state_ -= fall_rate_;
53 } else {
54 state_ = target;
55 }
56 }
57
58 // Track remaining distance for consumers (e.g., modulation scaling)
59 delta_ = (target >= state_) ? (target - state_) : (state_ - target);
60
61 return state_;
62 }
63
64 /** @brief Current output value */
65 float value() const { return state_; }
66
67 /** @brief Distance from current state to last processed target (absolute) */
68 float delta() const { return delta_; }
69
70 /** @brief Reset to zero */
71 void reset() { state_ = 0.0f; delta_ = 0.0f; }
72
73 /** @brief Reset to a specific value */
74 void reset(float initial) { state_ = initial; delta_ = 0.0f; }
75
76private:
77 float rise_rate_ = 0.0f;
78 float fall_rate_ = 0.0f;
79 float state_ = 0.0f;
80 float delta_ = 0.0f; // remaining distance after last process()
81};
82
83} // namespace sbl::dsp::primitives
84
85#endif // SBL_DSP_PRIMITIVES_SLEW_LIMITER_HPP_
void set_rate(float rate)
Set both rise and fall rates to the same value.
void set_fall_rate(float rate)
Set maximum decrease per sample.
float value() const
Current output value.
void set_rise_rate(float rate)
Set maximum increase per sample.
float process(float target)
Process one sample toward target with rate limiting.
float delta() const
Distance from current state to last processed target (absolute)
void reset(float initial)
Reset to a specific value.
Stateful, single-concern building blocks.