From a381581ab1aca72bcff12dd9f6406a41f759b75b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 24 Feb 2024 11:38:52 +0100 Subject: [PATCH] audio: add clipping functions for audio samples --- include/lol/private/audio/stream.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/lol/private/audio/stream.h b/include/lol/private/audio/stream.h index ff823f44..4262ec69 100644 --- a/include/lol/private/audio/stream.h +++ b/include/lol/private/audio/stream.h @@ -127,6 +127,35 @@ public: return T(x ^ y) < 0 ? T(ret) : x >= 0 ? T(std::min(ret, umax)) : T(std::max(ret, umin)); } } + + // Clipping for samples + template + static inline T clip(T x) + { + if constexpr (std::is_floating_point_v) + { + return std::min(T(1), std::max(T(-1), x)); + } + else + { + // Clipping is only relevant for floating point types + return x; + } + } + + template + static inline T softclip(T x) + { + if constexpr (std::is_floating_point_v) + { + return std::tanh(x); + } + else + { + // Clipping is only relevant for floating point types + return x; + } + } }; template