From 2d5d2d4d2c9c43032a2c57e5ce4eba33901f88b3 Mon Sep 17 00:00:00 2001 From: tomcombriat Date: Sat, 2 Oct 2021 23:02:26 +0200 Subject: [PATCH] Initial commit --- SoftClip.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 SoftClip.h diff --git a/SoftClip.h b/SoftClip.h new file mode 100644 index 000000000..69858c8d5 --- /dev/null +++ b/SoftClip.h @@ -0,0 +1,50 @@ +#ifndef SOFT_CLIP_H +#define SOFT_CLIP_H + +#include "AudioOutput.h" + +template +class SoftClip +{ + private: + const unsigned int max_threshold = (unsigned int) SMOOTHING * PI / 2; + T lookup_table[(unsigned int) (SMOOTHING * PI / 2)]; + T max_value; + + public: + SoftClip(T max_val) + { + max_value = max_val; + for (unsigned int i = 0; i < max_threshold; i++) + { + lookup_table[i] = SMOOTHING * sin(1.*i / SMOOTHING); + } + } + + inline T next(T in) + { + if (abs(in) < max_value - SMOOTHING) return in; + else if (in > 0) + { + if (in < (T) (max_value - SMOOTHING + max_threshold)) return lookup_table[in - max_value + SMOOTHING] + max_value - SMOOTHING; + else return max_value; + } + else + { + if (-in < (T) (max_value - SMOOTHING + max_threshold)) return -lookup_table[-in - max_value + SMOOTHING] - ( max_value - SMOOTHING); + else return -max_value; + } + } + void setMaxValue(T max_val) { + max_value = max_val; + } + + T getMaxValue() { + return max_value; + } + +}; + + + +#endif