-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardClipDistortion.cpp
More file actions
50 lines (40 loc) · 1.84 KB
/
HardClipDistortion.cpp
File metadata and controls
50 lines (40 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2024 JDSherbert. All rights reserved.
#include "HardClipDistortion.h"
#include <algorithm>
#include <cassert>
// ======================================================================= //
Sherbert::HardClipDistortion::HardClipDistortion(float distortionAmount, float threshold)
: Distortion(distortionAmount)
, threshold(threshold)
{
assert(threshold > 0.0f && threshold <= 1.0f);
}
// ======================================================================= //
float Sherbert::HardClipDistortion::ProcessSample(float input) const
{
// ==== HOW HARD CLIPPING WORKS ========================================
//
// The input is first amplified by distortionAmount (increasing drive),
// then clamped hard to the range [-threshold, +threshold].
//
// Any part of the signal that exceeds the threshold is simply cut flat.
// This abrupt transition introduces strong odd harmonics (3rd, 5th, 7th)
// which give hard clipping its characteristic harsh, buzzy character.
//
// At low drive: only the loudest peaks are clipped — subtle effect.
// At high drive: most of the waveform is clamped, approaching a square
// wave with hard edges — very aggressive.
//
// Lowering the threshold clips earlier, increasing aggression
// independently of the drive amount.
// =====================================================================
const float driven = distortionAmount * input;
return std::clamp(driven, -threshold, threshold);
}
// ======================================================================= //
void Sherbert::HardClipDistortion::setThreshold(float newThreshold)
{
assert(newThreshold > 0.0f && newThreshold <= 1.0f);
threshold = newThreshold;
}
// ======================================================================= //