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
clamp.hpp
Go to the documentation of this file.
1// sbl/dsp/math/clamp.hpp — Clamping (Cross-cutting — Math)
2//
3// One clamp for the library. A NaN lands on the low bound, so a bad signal
4// is a quiet one, not a loud one; every component that clamps a port or a
5// parameter uses this rather than its own ternary (AP-041 Phase 1).
6
7#ifndef SBL_DSP_MATH_CLAMP_HPP_
8#define SBL_DSP_MATH_CLAMP_HPP_
9
10namespace sbl::dsp::math {
11
12/// x held to [lo, hi]; NaN → lo.
13inline constexpr float clamp(float x, float lo, float hi) {
14 return (x > lo) ? (x < hi ? x : hi) : lo;
15}
16
17/// x held to [0, 1]; NaN → 0.
18inline constexpr float clamp01(float x) { return clamp(x, 0.0f, 1.0f); }
19
20} // namespace sbl::dsp::math
21
22#endif // SBL_DSP_MATH_CLAMP_HPP_
Cross-cutting math used at every audio layer.
Definition clamp.hpp:10
constexpr float clamp01(float x)
x held to [0, 1]; NaN → 0.
Definition clamp.hpp:18
constexpr float clamp(float x, float lo, float hi)
x held to [lo, hi]; NaN → lo.
Definition clamp.hpp:13