-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressor.cpp
More file actions
215 lines (181 loc) · 5.77 KB
/
Compressor.cpp
File metadata and controls
215 lines (181 loc) · 5.77 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
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
206
207
208
209
210
211
212
213
214
#include "Compressor.h"
//==============================================================================
Compressor::Compressor()
{
gainReductionSmoothed.setCurrentAndTargetValue(0.0f);
}
Compressor::~Compressor()
{
releaseResources();
}
void Compressor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
this->sampleRate = sampleRate;
this->blockSize = samplesPerBlock;
// Initialize RMS buffer
rmsBuffer.setSize(2, rmsWindowSize);
rmsBuffer.clear();
rmsIndex = 0;
// Setup smoothed values
gainReductionSmoothed.reset(sampleRate, 0.01); // 10ms smoothing
// Update coefficients
updateCoefficients();
}
float Compressor::processBlock(juce::dsp::AudioBlock<float>& audioBlock)
{
const int numChannels = static_cast<int>(audioBlock.getNumChannels());
const int numSamples = static_cast<int>(audioBlock.getNumSamples());
// Get RMS level of input
float rmsLevel = getRMSLevel(audioBlock);
// Convert to dB
float rmsLevelDb = rmsLevel > 0.0f ? juce::Decibels::gainToDecibels(rmsLevel) : -100.0f;
// Calculate required gain reduction
float targetGainReduction = calculateGainReduction(rmsLevelDb);
// Apply envelope follower
if (targetGainReduction > envelope)
{
// Attack phase
envelope = targetGainReduction + (envelope - targetGainReduction) * attackCoeff;
}
else
{
// Release phase
envelope = targetGainReduction + (envelope - targetGainReduction) * releaseCoeff;
}
// Smooth the gain reduction for audio application
gainReductionSmoothed.setTargetValue(envelope);
// Apply gain reduction to audio
for (int sample = 0; sample < numSamples; ++sample)
{
float gainReduction = gainReductionSmoothed.getNextValue();
float gain = juce::Decibels::decibelsToGain(-gainReduction + makeupGain);
for (int channel = 0; channel < numChannels; ++channel)
{
auto* channelData = audioBlock.getChannelPointer(channel);
channelData[sample] *= gain;
}
}
currentGainReduction = envelope;
return currentGainReduction;
}
void Compressor::releaseResources()
{
rmsBuffer.clear();
}
//==============================================================================
void Compressor::setThreshold(float thresholdDb)
{
threshold = thresholdDb;
if (autoMakeupGain)
{
// Calculate automatic makeup gain
float compressionAmount = (threshold - (-60.0f)) / ratio;
makeupGain = compressionAmount * 0.5f; // Conservative makeup gain
}
}
void Compressor::setRatio(float ratio)
{
this->ratio = juce::jmax(1.0f, ratio);
if (autoMakeupGain)
{
float compressionAmount = (threshold - (-60.0f)) / this->ratio;
makeupGain = compressionAmount * 0.5f;
}
}
void Compressor::setAttack(float attackMs)
{
attack = juce::jmax(0.1f, attackMs);
updateCoefficients();
}
void Compressor::setRelease(float releaseMs)
{
release = juce::jmax(1.0f, releaseMs);
updateCoefficients();
}
void Compressor::setKnee(float kneeDb)
{
knee = juce::jmax(0.0f, kneeDb);
}
void Compressor::setMakeupGain(float gainDb)
{
makeupGain = gainDb;
autoMakeupGain = false;
}
void Compressor::setAutoMakeupGain(bool enabled)
{
autoMakeupGain = enabled;
if (enabled)
{
float compressionAmount = (threshold - (-60.0f)) / ratio;
makeupGain = compressionAmount * 0.5f;
}
}
//==============================================================================
void Compressor::updateCoefficients()
{
// Calculate attack and release coefficients
// Using exponential approach for smooth envelope following
attackCoeff = std::exp(-1.0f / (attack * 0.001f * static_cast<float>(sampleRate)));
releaseCoeff = std::exp(-1.0f / (release * 0.001f * static_cast<float>(sampleRate)));
}
float Compressor::calculateGainReduction(float inputLevelDb)
{
if (inputLevelDb <= threshold)
return 0.0f;
if (knee > 0.0f)
{
return softKneeCompression(inputLevelDb);
}
else
{
// Hard knee compression
float overThreshold = inputLevelDb - threshold;
return overThreshold - (overThreshold / ratio);
}
}
float Compressor::softKneeCompression(float inputLevelDb)
{
float kneeStart = threshold - (knee / 2.0f);
float kneeEnd = threshold + (knee / 2.0f);
if (inputLevelDb <= kneeStart)
{
return 0.0f;
}
else if (inputLevelDb >= kneeEnd)
{
// Above knee - full compression
float overThreshold = inputLevelDb - threshold;
return overThreshold - (overThreshold / ratio);
}
else
{
// In knee region - gradual compression
float kneeRatio = (inputLevelDb - kneeStart) / knee;
float currentRatio = 1.0f + (ratio - 1.0f) * kneeRatio;
float overThreshold = inputLevelDb - threshold;
return overThreshold - (overThreshold / currentRatio);
}
}
float Compressor::getRMSLevel(const juce::dsp::AudioBlock<float>& audioBlock)
{
const int numChannels = static_cast<int>(audioBlock.getNumChannels());
const int numSamples = static_cast<int>(audioBlock.getNumSamples());
float sumSquares = 0.0f;
int sampleCount = 0;
// Calculate RMS over the current block
for (int channel = 0; channel < numChannels; ++channel)
{
auto* channelData = audioBlock.getChannelPointer(channel);
for (int sample = 0; sample < numSamples; ++sample)
{
float sampleValue = channelData[sample];
sumSquares += sampleValue * sampleValue;
sampleCount++;
}
}
if (sampleCount > 0)
{
return std::sqrt(sumSquares / static_cast<float>(sampleCount));
}
return 0.0f;
}