-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaturation.cpp
More file actions
59 lines (48 loc) · 1.67 KB
/
saturation.cpp
File metadata and controls
59 lines (48 loc) · 1.67 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
51
52
53
54
55
56
57
58
59
#include <cmath>
#include <algorithm>
#include "saturation.h"
namespace MKAudio
{
Saturation::Saturation(double alphaPlus, double alphaMinus,
double betaPlus, double betaMinus,
double deltaBias, bool flip)
{
// 1. Set Alpha (Knee/Drive)
driveAlphaPlus = std::max(alphaPlus, 1e-4);
driveAlphaMinus = std::max(alphaMinus, 1e-4);
// 2. Set Beta (Compression/Gain)
compressionBetaPlus = betaPlus;
compressionBetaMinus = betaMinus;
// 3. Set Bias (Delta)
biasDelta = deltaBias;
// 4. Set Polarity (Gamma - Boolean)
flipPolarity = flip;
// 5. Recalculate Normalization Factors
normFactorPlus = 1.0 / std::log2(1.0 + driveAlphaPlus);
normFactorMinus = 1.0 / std::log2(1.0 + driveAlphaMinus);
}
double Saturation::process(double inputSample)
{
double outputValue;
// 1. Calculate Saturated Output (S(x, δ))
if (inputSample >= biasDelta)
{
// Processing Positive side (x >= δ)
double relativeInput = inputSample - biasDelta;
double logOut = std::log2(1.0 + (driveAlphaPlus * relativeInput));
outputValue = compressionBetaPlus * (logOut * normFactorPlus);
}
else
{
// Processing Negative side (x < δ)
double relativeInput = biasDelta - inputSample;
double logOut = std::log2(1.0 + (driveAlphaMinus * relativeInput));
// Output must be negative before final flip
outputValue = -1.0 * compressionBetaMinus * (logOut * normFactorMinus);
}
outputValue += biasDelta;
// 2. Apply Boolean Polarity Flip (γ)
if (flipPolarity) return -outputValue;
return outputValue;
}
} // namespace MKAudio