Sound Byte Libs 29c5ff3
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// sbl/dsp/slew.hpp — Slew rate limiter
2//
3// Per-sample rate limiting with independent rise and fall rates.
4// Used for portamento, parameter smoothing, and envelope shapes.
5//
6// Rate is specified as maximum change per sample (in sample units).
7// Higher rate = faster slew. Rate of 0 = no change (hold).
8//
9// Usage:
10// sbl::dsp::SlewLimiter slew;
11// slew.set_rise_rate(1000);
12// slew.set_fall_rate(500);
13// Sample out = slew.process(target); // Per-sample rate limiting
14
15#pragma once
16
17#include <cstdint>
18
19#include <sbl/dsp/types.hpp>
20
21namespace sbl::dsp {
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(uint32_t rate) { rise_rate_ = rate; }
29
30 /** @brief Set maximum decrease per sample */
31 void set_fall_rate(uint32_t rate) { fall_rate_ = rate; }
32
33 /** @brief Set both rise and fall rates to the same value */
34 void set_rate(uint32_t 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 */
42 int32_t delta = target - state_;
43
44 if (delta > 0) {
45 // Rising
46 if (static_cast<uint32_t>(delta) > rise_rate_) {
47 state_ += static_cast<int32_t>(rise_rate_);
48 } else {
49 state_ = target;
50 }
51 } else if (delta < 0) {
52 // Falling
53 uint32_t abs_delta = static_cast<uint32_t>(-delta);
54 if (abs_delta > fall_rate_) {
55 state_ -= static_cast<int32_t>(fall_rate_);
56 } else {
57 state_ = target;
58 }
59 }
60
61 return state_;
62 }
63
64 /** @brief Current output value */
65 Sample value() const { return state_; }
66
67 /** @brief Reset to zero */
68 void reset() { state_ = 0; }
69
70 /** @brief Reset to a specific value */
71 void reset(Sample initial) { state_ = initial; }
72
73private:
74 uint32_t rise_rate_ = 0;
75 uint32_t fall_rate_ = 0;
76 Sample state_ = 0;
77};
78
79} // namespace sbl::dsp
void set_fall_rate(uint32_t rate)
Set maximum decrease per sample.
Definition slew.hpp:31
void reset()
Reset to zero.
Definition slew.hpp:68
Sample process(Sample target)
Process one sample toward target with rate limiting.
Definition slew.hpp:41
void set_rate(uint32_t rate)
Set both rise and fall rates to the same value.
Definition slew.hpp:34
Sample value() const
Current output value.
Definition slew.hpp:65
void set_rise_rate(uint32_t rate)
Set maximum increase per sample.
Definition slew.hpp:28
void reset(Sample initial)
Reset to a specific value.
Definition slew.hpp:71
DSP atoms for audio signal processing.
Definition allpass.hpp:22
int32_t Sample
Definition types.hpp:11