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
soft_limiter.hpp
Go to the documentation of this file.
1/**
2 * @file soft_limiter.hpp
3 * @brief A soft-knee limiter: the input under a knee, a tanh bend under a ceiling above it
4 */
5
6#ifndef SBL_DSP_DYNAMICS_SOFT_LIMITER_HPP_
7#define SBL_DSP_DYNAMICS_SOFT_LIMITER_HPP_
8
9#include <cmath>
10#include <cstdint>
11
14
15namespace sbl::dsp {
16
17/**
18 * @brief A soft-knee limiter: the input under a knee, a tanh bend under a ceiling above it
19 *
20 * Memoryless. A sample whose magnitude is at or under the knee passes
21 * unchanged, bit for bit. Above it the excess is bent by a tanh scaled to the
22 * room between the knee and the ceiling, so the curve leaves the knee at a
23 * slope of 1 and approaches the ceiling without reaching past it. A NaN
24 * comes out as 0. Below the signal transport (signal-transport section 5.6):
25 * the knee and the ceiling are in the signal's own scale, 0 < knee <
26 * ceiling, fixed at construction, and it declares no ports.
27 *
28 * While a signal is above the knee the bend adds harmonics, which are not
29 * band-limited; set the knee above the level the signal has in normal use.
30 *
31 * @code
32 * constexpr sbl::dsp::SoftLimiter limiter{0.92f, 0.98f};
33 * out[i] = limiter.limit(in[i] * level);
34 * @endcode
35 */
37public:
38 /**
39 * @brief A limiter with a knee and a ceiling, 0 < knee < ceiling.
40 *
41 * @param knee The magnitude up to which the input passes unchanged
42 * @param ceiling The magnitude the output does not exceed
43 */
44 constexpr SoftLimiter(float knee, float ceiling) : knee_(knee), ceiling_(ceiling) {}
45
46 /** @brief The knee. */
47 constexpr float knee() const { return knee_; }
48 /** @brief The ceiling. */
49 constexpr float ceiling() const { return ceiling_; }
50
51 /**
52 * @brief Whether limit() changes x: its magnitude is above the knee, or it is not a number.
53 *
54 * @param x Input sample
55 * @return True when limit(x) differs from x
56 */
57 bool acts_on(float x) const { return sbl::math::magnitude_bits(x) > sbl::math::magnitude_bits(knee_); }
58
59 /**
60 * @brief One sample through the limiter.
61 *
62 * @param x Input sample, any value
63 * @return x at or under the knee; above it, a magnitude in (knee, ceiling]; 0 for a NaN
64 * @context Audio thread; real-time safe.
65 */
66 float limit(float x) const {
67 if (!acts_on(x)) {
68 return x;
69 }
70 if (x != x) {
71 return 0.0f;
72 }
73 const float room = ceiling_ - knee_;
74 const float over = (std::fabs(x) - knee_) / room;
75 return std::copysign(knee_ + room * sbl::dsp::math::fast_tanhf(over), x);
76 }
77
78 /**
79 * @brief A block through the limiter; `out` may be `in`.
80 *
81 * @param in Input buffer, `frames` long
82 * @param[out] out Output buffer, `frames` long
83 * @param frames Frame count
84 * @context Audio thread; real-time safe.
85 */
86 void process(const float* in, float* out, uint16_t frames) const {
87 for (uint16_t i = 0; i < frames; ++i) {
88 out[i] = limit(in[i]);
89 }
90 }
91
92private:
93 float knee_;
94 float ceiling_;
95};
96
97} // namespace sbl::dsp
98
99#endif // SBL_DSP_DYNAMICS_SOFT_LIMITER_HPP_
A soft-knee limiter: the input under a knee, a tanh bend under a ceiling above it.
constexpr float knee() const
The knee.
bool acts_on(float x) const
Whether limit() changes x: its magnitude is above the knee, or it is not a number.
constexpr SoftLimiter(float knee, float ceiling)
A limiter with a knee and a ceiling, 0 < knee < ceiling.
float limit(float x) const
One sample through the limiter.
void process(const float *in, float *out, uint16_t frames) const
A block through the limiter; out may be in.
constexpr float ceiling() const
The ceiling.
Fast analytical approximations of sin, tan, 2^x and tanh.
A float's magnitude as its bit pattern, for compares without the FPU.
float fast_tanhf(float x)
Fast tanh approximation using a [3,2] Pade approximant.
The audio stack.
Definition dry_wet.hpp:15
uint32_t magnitude_bits(float x)
x's bits with the sign cleared.
Definition magnitude.hpp:29