chenlh
2026-03-20 f761b6198e2e98f02adb84c83dbf59529e51b2ef
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "Compressor.h" 
 
template <typename SampleType>
Compressor<SampleType>::Compressor()
{
 
}
 
template <typename SampleType>
Compressor<SampleType>::~Compressor()
{
 
}
 
template <typename SampleType>
void Compressor<SampleType>::prepare(SampleType sampleRate)
{
    this->detector.prepare(sampleRate);
    this->detector.setOutputIndB(true);
}
 
template <typename SampleType>
SampleType Compressor<SampleType>::processSample(SampleType sample)
{
    SampleType xn = sample;
    SampleType detect_input = 0.0;
 
    detect_input = this->detector.processSample(xn);
 
    //Gain Reduction Calculation
    SampleType gainReduction = calaculateGain(detect_input);
 
    //Make-up Gain
    //SampleType muGain = GainUtilities<SampleType>::decibelsToGain(makeupGain);
 
    //return xn * gainReduction * muGain;
    return gainReduction;
}
 
template <typename SampleType>
void Compressor<SampleType>::process(SampleType* data, int startSample, int endSample)
{
    for(int sample = startSample; sample < endSample; ++sample)
        data[sample] = processSample(data[sample]);
}
 
template <typename SampleType>
void Compressor<SampleType>::setThreshold(SampleType newThreshold)
{
    threshold = newThreshold;
 
    //Bounds Check
    if (threshold > 0.0)
        threshold = 0.0;
    if (threshold < -80.0)
        threshold = -80.0;
}
 
template <typename SampleType>
void Compressor<SampleType>::setRatio(SampleType newRatio)
{
    ratio = newRatio;
 
    //Bounds Check
    if (ratio > 100.0)
        ratio = 100.0;
    if (ratio < 1.0)
        ratio = 1.0;
}
 
template <typename SampleType>
void Compressor<SampleType>::setAttack(SampleType newAttack)
{
    attack = newAttack;
 
    //Bounds Check
    if (attack > 100.0)
        attack = 100.0;
    if (attack < 1.0)
        attack = 1.0;
    
    this->detector.setAttackTime(attack);
}
 
template <typename SampleType>
void Compressor<SampleType>::setRelease(SampleType newRelease)
{
    release = newRelease;
 
    //Bounds Check
    if (release > 1000.0)
        release = 1000.0;
    if (release < 10.0)
        release = 10.0;
 
    this->detector.setReleaseTime(release);
}
 
template <typename SampleType>
void Compressor<SampleType>::setGain(SampleType newGain)
{
    makeupGain = newGain;
}
 
template <typename SampleType>
void Compressor<SampleType>::setParameter(int index, SampleType newValue)
{
    switch (index)
    {
    case Parameters::Threshold:
        setThreshold(newValue);
        break;
    case Parameters::Ratio:
        setRatio(newValue);
        break;
    case Parameters::Attack:    
        setAttack(newValue);
        break;
    case Parameters::Release:
        setRelease(newValue);
        break;
    case Parameters::MakeupGain:
        setGain(newValue);
        break;
    }
}
 
template <typename SampleType>
SampleType Compressor<SampleType>::getParameter(int index)
{
    switch (index)
    {
    case Parameters::Threshold:
        return threshold;
        break;
    case Parameters::Ratio:
        return ratio;
        break;
    case Parameters::Attack:
        return attack;
        break;
    case Parameters::Release:
        return release;
        break;
    case Parameters::MakeupGain:
        return makeupGain;
        break;
    }
}
 
template <typename SampleType>
void Compressor<SampleType>::setKneeType(int index)
{
    switch (index)
    {
    case KneeTypes::Soft:
        kneeType = KneeTypes::Soft;
        break;
    case KneeTypes::Hard:
        kneeType = KneeTypes::Hard;
        break;
    }
}
 
template <typename SampleType>
int Compressor<SampleType>::getKneeType()
{
    return kneeType;
}
 
template <typename SampleType>
SampleType Compressor<SampleType>::calaculateGain(SampleType input)
{
    SampleType output = 0.0;
 
    switch (kneeType)
    {
    case KneeTypes::Soft:
 
        if (2.0 * (input - threshold) < -(kneeWidth))
            output = input;
        else if (2.0 * (std::fabs(input - threshold)) <= kneeWidth)
            output = input + (((1.0 / ratio) - 1.0) * std::pow((input - threshold + (kneeWidth/ 2.0)), 2.0)) / (2.0 * kneeWidth);
        else if (2.0 * (input - threshold) > kneeWidth)
            output = threshold + (input - threshold) / ratio;
 
        break;
    case KneeTypes::Hard:
 
        if (input <= threshold)
            output = input;
        else
            output = threshold + (input - threshold) / ratio;
 
        break;
    }
 
    SampleType gainReduction_dB = output - input;
    SampleType gainReduction_Linear = std::pow(10.0, (gainReduction_dB) / 20.0);
 
    return gainReduction_Linear;
}
 
template class Compressor<float>;
template class Compressor<double>;