SquidgeDSP/core/Clipper.cpp

42 lines
1.1 KiB
C++
Raw Normal View History

2026-08-02 20:03:04 +01:00
#include "Clipper.hpp"
2026-08-02 22:58:56 +01:00
#include <cmath>
2026-08-02 20:03:04 +01:00
2026-08-05 13:46:38 +01:00
Clipper::Clipper(float initClip, ClipperMode initMode)
: threshold_(initClip), mode_(initMode) {}
2026-08-02 20:03:04 +01:00
void Clipper::process(const float *in, float *out, uint32_t frames) {
for (uint32_t i = 0; i < frames; ++i) {
2026-08-02 22:58:56 +01:00
out[i] = clip(in[i]);
2026-08-02 20:03:04 +01:00
}
}
void Clipper::processStereo(const float *inLeft, const float *inRight,
float *outLeft, float *outRight, uint32_t frames) {
for (uint32_t i = 0; i < frames; ++i) {
outLeft[i] = clip(inLeft[i]);
outRight[i] = clip(inRight[i]);
2026-08-02 20:03:04 +01:00
}
}
float Clipper::clip(const float in) {
2026-08-02 22:58:56 +01:00
switch (mode_) {
case kClipperModeHard: {
return std::fmin(std::fmax(in, -threshold_), threshold_);
2026-08-02 22:58:56 +01:00
}
default:
2026-08-02 22:58:56 +01:00
case kClipperModeSoft: {
float normalized = (in / threshold_) * 0.75f;
return std::tanh(normalized) * threshold_;
2026-08-02 22:58:56 +01:00
}
2026-08-02 23:13:42 +01:00
case kClipperModeFold: {
float x = in;
while (x > threshold_ || x < -threshold_) {
if (x > threshold_) {
x = 2.0f * threshold_ - x;
} else if (x < -threshold_) {
x = -2.0f * threshold_ - x;
2026-08-02 23:13:42 +01:00
}
}
return x;
}
}
}