/src/mozilla-central/dom/media/webaudio/blink/DynamicsCompressorKernel.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011 Google Inc. All rights reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions |
6 | | * are met: |
7 | | * |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
14 | | * its contributors may be used to endorse or promote products derived |
15 | | * from this software without specific prior written permission. |
16 | | * |
17 | | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #include "DynamicsCompressorKernel.h" |
30 | | |
31 | | #include "DenormalDisabler.h" |
32 | | #include <algorithm> |
33 | | #include <cmath> |
34 | | |
35 | | #include "mozilla/FloatingPoint.h" |
36 | | #include "WebAudioUtils.h" |
37 | | |
38 | | using namespace std; |
39 | | |
40 | | using namespace mozilla::dom; // for WebAudioUtils |
41 | | using mozilla::IsInfinite; |
42 | | using mozilla::PositiveInfinity; |
43 | | using mozilla::IsNaN; |
44 | | using mozilla::MakeUnique; |
45 | | |
46 | | namespace WebCore { |
47 | | |
48 | | |
49 | | // Metering hits peaks instantly, but releases this fast (in seconds). |
50 | | const float meteringReleaseTimeConstant = 0.325f; |
51 | | |
52 | | const float uninitializedValue = -1; |
53 | | |
54 | | DynamicsCompressorKernel::DynamicsCompressorKernel(float sampleRate, unsigned numberOfChannels) |
55 | | : m_sampleRate(sampleRate) |
56 | | , m_lastPreDelayFrames(DefaultPreDelayFrames) |
57 | | , m_preDelayReadIndex(0) |
58 | | , m_preDelayWriteIndex(DefaultPreDelayFrames) |
59 | | , m_ratio(uninitializedValue) |
60 | | , m_slope(uninitializedValue) |
61 | | , m_linearThreshold(uninitializedValue) |
62 | | , m_dbThreshold(uninitializedValue) |
63 | | , m_dbKnee(uninitializedValue) |
64 | | , m_kneeThreshold(uninitializedValue) |
65 | | , m_kneeThresholdDb(uninitializedValue) |
66 | | , m_ykneeThresholdDb(uninitializedValue) |
67 | | , m_K(uninitializedValue) |
68 | 0 | { |
69 | 0 | setNumberOfChannels(numberOfChannels); |
70 | 0 |
|
71 | 0 | // Initializes most member variables |
72 | 0 | reset(); |
73 | 0 |
|
74 | 0 | m_meteringReleaseK = |
75 | 0 | static_cast<float>(WebAudioUtils::DiscreteTimeConstantForSampleRate(meteringReleaseTimeConstant, sampleRate)); |
76 | 0 | } |
77 | | |
78 | | size_t DynamicsCompressorKernel::sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
79 | 0 | { |
80 | 0 | size_t amount = 0; |
81 | 0 | amount += m_preDelayBuffers.ShallowSizeOfExcludingThis(aMallocSizeOf); |
82 | 0 | for (size_t i = 0; i < m_preDelayBuffers.Length(); i++) { |
83 | 0 | amount += aMallocSizeOf(m_preDelayBuffers[i].get()); |
84 | 0 | } |
85 | 0 |
|
86 | 0 | return amount; |
87 | 0 | } |
88 | | |
89 | | void DynamicsCompressorKernel::setNumberOfChannels(unsigned numberOfChannels) |
90 | 0 | { |
91 | 0 | if (m_preDelayBuffers.Length() == numberOfChannels) |
92 | 0 | return; |
93 | 0 | |
94 | 0 | m_preDelayBuffers.Clear(); |
95 | 0 | for (unsigned i = 0; i < numberOfChannels; ++i) |
96 | 0 | m_preDelayBuffers.AppendElement(MakeUnique<float[]>(MaxPreDelayFrames)); |
97 | 0 | } |
98 | | |
99 | | void DynamicsCompressorKernel::setPreDelayTime(float preDelayTime) |
100 | 0 | { |
101 | 0 | // Re-configure look-ahead section pre-delay if delay time has changed. |
102 | 0 | unsigned preDelayFrames = preDelayTime * sampleRate(); |
103 | 0 | if (preDelayFrames > MaxPreDelayFrames - 1) |
104 | 0 | preDelayFrames = MaxPreDelayFrames - 1; |
105 | 0 |
|
106 | 0 | if (m_lastPreDelayFrames != preDelayFrames) { |
107 | 0 | m_lastPreDelayFrames = preDelayFrames; |
108 | 0 | for (unsigned i = 0; i < m_preDelayBuffers.Length(); ++i) |
109 | 0 | memset(m_preDelayBuffers[i].get(), 0, sizeof(float) * MaxPreDelayFrames); |
110 | 0 |
|
111 | 0 | m_preDelayReadIndex = 0; |
112 | 0 | m_preDelayWriteIndex = preDelayFrames; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | // Exponential curve for the knee. |
117 | | // It is 1st derivative matched at m_linearThreshold and asymptotically approaches the value m_linearThreshold + 1 / k. |
118 | | float DynamicsCompressorKernel::kneeCurve(float x, float k) |
119 | 0 | { |
120 | 0 | // Linear up to threshold. |
121 | 0 | if (x < m_linearThreshold) |
122 | 0 | return x; |
123 | 0 | |
124 | 0 | return m_linearThreshold + (1 - expf(-k * (x - m_linearThreshold))) / k; |
125 | 0 | } |
126 | | |
127 | | // Full compression curve with constant ratio after knee. |
128 | | float DynamicsCompressorKernel::saturate(float x, float k) |
129 | 0 | { |
130 | 0 | float y; |
131 | 0 |
|
132 | 0 | if (x < m_kneeThreshold) |
133 | 0 | y = kneeCurve(x, k); |
134 | 0 | else { |
135 | 0 | // Constant ratio after knee. |
136 | 0 | float xDb = WebAudioUtils::ConvertLinearToDecibels(x, -1000.0f); |
137 | 0 | float yDb = m_ykneeThresholdDb + m_slope * (xDb - m_kneeThresholdDb); |
138 | 0 |
|
139 | 0 | y = WebAudioUtils::ConvertDecibelsToLinear(yDb); |
140 | 0 | } |
141 | 0 |
|
142 | 0 | return y; |
143 | 0 | } |
144 | | |
145 | | // Approximate 1st derivative with input and output expressed in dB. |
146 | | // This slope is equal to the inverse of the compression "ratio". |
147 | | // In other words, a compression ratio of 20 would be a slope of 1/20. |
148 | | float DynamicsCompressorKernel::slopeAt(float x, float k) |
149 | 0 | { |
150 | 0 | if (x < m_linearThreshold) |
151 | 0 | return 1; |
152 | 0 | |
153 | 0 | float x2 = x * 1.001; |
154 | 0 |
|
155 | 0 | float xDb = WebAudioUtils::ConvertLinearToDecibels(x, -1000.0f); |
156 | 0 | float x2Db = WebAudioUtils::ConvertLinearToDecibels(x2, -1000.0f); |
157 | 0 |
|
158 | 0 | float yDb = WebAudioUtils::ConvertLinearToDecibels(kneeCurve(x, k), -1000.0f); |
159 | 0 | float y2Db = WebAudioUtils::ConvertLinearToDecibels(kneeCurve(x2, k), -1000.0f); |
160 | 0 |
|
161 | 0 | float m = (y2Db - yDb) / (x2Db - xDb); |
162 | 0 |
|
163 | 0 | return m; |
164 | 0 | } |
165 | | |
166 | | float DynamicsCompressorKernel::kAtSlope(float desiredSlope) |
167 | 0 | { |
168 | 0 | float xDb = m_dbThreshold + m_dbKnee; |
169 | 0 | float x = WebAudioUtils::ConvertDecibelsToLinear(xDb); |
170 | 0 |
|
171 | 0 | // Approximate k given initial values. |
172 | 0 | float minK = 0.1f; |
173 | 0 | float maxK = 10000; |
174 | 0 | float k = 5; |
175 | 0 |
|
176 | 0 | for (int i = 0; i < 15; ++i) { |
177 | 0 | // A high value for k will more quickly asymptotically approach a slope of 0. |
178 | 0 | float slope = slopeAt(x, k); |
179 | 0 |
|
180 | 0 | if (slope < desiredSlope) { |
181 | 0 | // k is too high. |
182 | 0 | maxK = k; |
183 | 0 | } else { |
184 | 0 | // k is too low. |
185 | 0 | minK = k; |
186 | 0 | } |
187 | 0 |
|
188 | 0 | // Re-calculate based on geometric mean. |
189 | 0 | k = sqrtf(minK * maxK); |
190 | 0 | } |
191 | 0 |
|
192 | 0 | return k; |
193 | 0 | } |
194 | | |
195 | | float DynamicsCompressorKernel::updateStaticCurveParameters(float dbThreshold, float dbKnee, float ratio) |
196 | 0 | { |
197 | 0 | if (dbThreshold != m_dbThreshold || dbKnee != m_dbKnee || ratio != m_ratio) { |
198 | 0 | // Threshold and knee. |
199 | 0 | m_dbThreshold = dbThreshold; |
200 | 0 | m_linearThreshold = WebAudioUtils::ConvertDecibelsToLinear(dbThreshold); |
201 | 0 | m_dbKnee = dbKnee; |
202 | 0 |
|
203 | 0 | // Compute knee parameters. |
204 | 0 | m_ratio = ratio; |
205 | 0 | m_slope = 1 / m_ratio; |
206 | 0 |
|
207 | 0 | float k = kAtSlope(1 / m_ratio); |
208 | 0 |
|
209 | 0 | m_kneeThresholdDb = dbThreshold + dbKnee; |
210 | 0 | m_kneeThreshold = WebAudioUtils::ConvertDecibelsToLinear(m_kneeThresholdDb); |
211 | 0 |
|
212 | 0 | m_ykneeThresholdDb = WebAudioUtils::ConvertLinearToDecibels(kneeCurve(m_kneeThreshold, k), -1000.0f); |
213 | 0 |
|
214 | 0 | m_K = k; |
215 | 0 | } |
216 | 0 | return m_K; |
217 | 0 | } |
218 | | |
219 | | void DynamicsCompressorKernel::process(float* sourceChannels[], |
220 | | float* destinationChannels[], |
221 | | unsigned numberOfChannels, |
222 | | unsigned framesToProcess, |
223 | | |
224 | | float dbThreshold, |
225 | | float dbKnee, |
226 | | float ratio, |
227 | | float attackTime, |
228 | | float releaseTime, |
229 | | float preDelayTime, |
230 | | float dbPostGain, |
231 | | float effectBlend, /* equal power crossfade */ |
232 | | |
233 | | float releaseZone1, |
234 | | float releaseZone2, |
235 | | float releaseZone3, |
236 | | float releaseZone4 |
237 | | ) |
238 | 0 | { |
239 | 0 | MOZ_ASSERT(m_preDelayBuffers.Length() == numberOfChannels); |
240 | 0 |
|
241 | 0 | float sampleRate = this->sampleRate(); |
242 | 0 |
|
243 | 0 | float dryMix = 1 - effectBlend; |
244 | 0 | float wetMix = effectBlend; |
245 | 0 |
|
246 | 0 | float k = updateStaticCurveParameters(dbThreshold, dbKnee, ratio); |
247 | 0 |
|
248 | 0 | // Makeup gain. |
249 | 0 | float fullRangeGain = saturate(1, k); |
250 | 0 | float fullRangeMakeupGain = 1 / fullRangeGain; |
251 | 0 |
|
252 | 0 | // Empirical/perceptual tuning. |
253 | 0 | fullRangeMakeupGain = powf(fullRangeMakeupGain, 0.6f); |
254 | 0 |
|
255 | 0 | float masterLinearGain = WebAudioUtils::ConvertDecibelsToLinear(dbPostGain) * fullRangeMakeupGain; |
256 | 0 |
|
257 | 0 | // Attack parameters. |
258 | 0 | attackTime = max(0.001f, attackTime); |
259 | 0 | float attackFrames = attackTime * sampleRate; |
260 | 0 |
|
261 | 0 | // Release parameters. |
262 | 0 | float releaseFrames = sampleRate * releaseTime; |
263 | 0 |
|
264 | 0 | // Detector release time. |
265 | 0 | float satReleaseTime = 0.0025f; |
266 | 0 | float satReleaseFrames = satReleaseTime * sampleRate; |
267 | 0 |
|
268 | 0 | // Create a smooth function which passes through four points. |
269 | 0 |
|
270 | 0 | // Polynomial of the form |
271 | 0 | // y = a + b*x + c*x^2 + d*x^3 + e*x^4; |
272 | 0 |
|
273 | 0 | float y1 = releaseFrames * releaseZone1; |
274 | 0 | float y2 = releaseFrames * releaseZone2; |
275 | 0 | float y3 = releaseFrames * releaseZone3; |
276 | 0 | float y4 = releaseFrames * releaseZone4; |
277 | 0 |
|
278 | 0 | // All of these coefficients were derived for 4th order polynomial curve fitting where the y values |
279 | 0 | // match the evenly spaced x values as follows: (y1 : x == 0, y2 : x == 1, y3 : x == 2, y4 : x == 3) |
280 | 0 | float kA = 0.9999999999999998f*y1 + 1.8432219684323923e-16f*y2 - 1.9373394351676423e-16f*y3 + 8.824516011816245e-18f*y4; |
281 | 0 | float kB = -1.5788320352845888f*y1 + 2.3305837032074286f*y2 - 0.9141194204840429f*y3 + 0.1623677525612032f*y4; |
282 | 0 | float kC = 0.5334142869106424f*y1 - 1.272736789213631f*y2 + 0.9258856042207512f*y3 - 0.18656310191776226f*y4; |
283 | 0 | float kD = 0.08783463138207234f*y1 - 0.1694162967925622f*y2 + 0.08588057951595272f*y3 - 0.00429891410546283f*y4; |
284 | 0 | float kE = -0.042416883008123074f*y1 + 0.1115693827987602f*y2 - 0.09764676325265872f*y3 + 0.028494263462021576f*y4; |
285 | 0 |
|
286 | 0 | // x ranges from 0 -> 3 0 1 2 3 |
287 | 0 | // -15 -10 -5 0db |
288 | 0 |
|
289 | 0 | // y calculates adaptive release frames depending on the amount of compression. |
290 | 0 |
|
291 | 0 | setPreDelayTime(preDelayTime); |
292 | 0 |
|
293 | 0 | const int nDivisionFrames = 32; |
294 | 0 |
|
295 | 0 | const int nDivisions = framesToProcess / nDivisionFrames; |
296 | 0 |
|
297 | 0 | unsigned frameIndex = 0; |
298 | 0 | for (int i = 0; i < nDivisions; ++i) { |
299 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
300 | 0 | // Calculate desired gain |
301 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
302 | 0 |
|
303 | 0 | // Fix gremlins. |
304 | 0 | if (IsNaN(m_detectorAverage)) |
305 | 0 | m_detectorAverage = 1; |
306 | 0 | if (IsInfinite(m_detectorAverage)) |
307 | 0 | m_detectorAverage = 1; |
308 | 0 |
|
309 | 0 | float desiredGain = m_detectorAverage; |
310 | 0 |
|
311 | 0 | // Pre-warp so we get desiredGain after sin() warp below. |
312 | 0 | float scaledDesiredGain = asinf(desiredGain) / (0.5f * M_PI); |
313 | 0 |
|
314 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
315 | 0 | // Deal with envelopes |
316 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
317 | 0 |
|
318 | 0 | // envelopeRate is the rate we slew from current compressor level to the desired level. |
319 | 0 | // The exact rate depends on if we're attacking or releasing and by how much. |
320 | 0 | float envelopeRate; |
321 | 0 |
|
322 | 0 | bool isReleasing = scaledDesiredGain > m_compressorGain; |
323 | 0 |
|
324 | 0 | // compressionDiffDb is the difference between current compression level and the desired level. |
325 | 0 | float compressionDiffDb; |
326 | 0 | if (scaledDesiredGain == 0.0) { |
327 | 0 | compressionDiffDb = PositiveInfinity<float>(); |
328 | 0 | } else { |
329 | 0 | compressionDiffDb = WebAudioUtils::ConvertLinearToDecibels(m_compressorGain / scaledDesiredGain, -1000.0f); |
330 | 0 | } |
331 | 0 |
|
332 | 0 |
|
333 | 0 | if (isReleasing) { |
334 | 0 | // Release mode - compressionDiffDb should be negative dB |
335 | 0 | m_maxAttackCompressionDiffDb = -1; |
336 | 0 |
|
337 | 0 | // Fix gremlins. |
338 | 0 | if (IsNaN(compressionDiffDb)) |
339 | 0 | compressionDiffDb = -1; |
340 | 0 | if (IsInfinite(compressionDiffDb)) |
341 | 0 | compressionDiffDb = -1; |
342 | 0 |
|
343 | 0 | // Adaptive release - higher compression (lower compressionDiffDb) releases faster. |
344 | 0 |
|
345 | 0 | // Contain within range: -12 -> 0 then scale to go from 0 -> 3 |
346 | 0 | float x = compressionDiffDb; |
347 | 0 | x = max(-12.0f, x); |
348 | 0 | x = min(0.0f, x); |
349 | 0 | x = 0.25f * (x + 12); |
350 | 0 |
|
351 | 0 | // Compute adaptive release curve using 4th order polynomial. |
352 | 0 | // Normal values for the polynomial coefficients would create a monotonically increasing function. |
353 | 0 | float x2 = x * x; |
354 | 0 | float x3 = x2 * x; |
355 | 0 | float x4 = x2 * x2; |
356 | 0 | float releaseFrames = kA + kB * x + kC * x2 + kD * x3 + kE * x4; |
357 | 0 |
|
358 | 0 | #define kSpacingDb 5 |
359 | 0 | float dbPerFrame = kSpacingDb / releaseFrames; |
360 | 0 |
|
361 | 0 | envelopeRate = WebAudioUtils::ConvertDecibelsToLinear(dbPerFrame); |
362 | 0 | } else { |
363 | 0 | // Attack mode - compressionDiffDb should be positive dB |
364 | 0 |
|
365 | 0 | // Fix gremlins. |
366 | 0 | if (IsNaN(compressionDiffDb)) |
367 | 0 | compressionDiffDb = 1; |
368 | 0 | if (IsInfinite(compressionDiffDb)) |
369 | 0 | compressionDiffDb = 1; |
370 | 0 |
|
371 | 0 | // As long as we're still in attack mode, use a rate based off |
372 | 0 | // the largest compressionDiffDb we've encountered so far. |
373 | 0 | if (m_maxAttackCompressionDiffDb == -1 || m_maxAttackCompressionDiffDb < compressionDiffDb) |
374 | 0 | m_maxAttackCompressionDiffDb = compressionDiffDb; |
375 | 0 |
|
376 | 0 | float effAttenDiffDb = max(0.5f, m_maxAttackCompressionDiffDb); |
377 | 0 |
|
378 | 0 | float x = 0.25f / effAttenDiffDb; |
379 | 0 | envelopeRate = 1 - powf(x, 1 / attackFrames); |
380 | 0 | } |
381 | 0 |
|
382 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
383 | 0 | // Inner loop - calculate shaped power average - apply compression. |
384 | 0 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
385 | 0 |
|
386 | 0 | { |
387 | 0 | int preDelayReadIndex = m_preDelayReadIndex; |
388 | 0 | int preDelayWriteIndex = m_preDelayWriteIndex; |
389 | 0 | float detectorAverage = m_detectorAverage; |
390 | 0 | float compressorGain = m_compressorGain; |
391 | 0 |
|
392 | 0 | int loopFrames = nDivisionFrames; |
393 | 0 | while (loopFrames--) { |
394 | 0 | float compressorInput = 0; |
395 | 0 |
|
396 | 0 | // Predelay signal, computing compression amount from un-delayed version. |
397 | 0 | for (unsigned i = 0; i < numberOfChannels; ++i) { |
398 | 0 | float* delayBuffer = m_preDelayBuffers[i].get(); |
399 | 0 | float undelayedSource = sourceChannels[i][frameIndex]; |
400 | 0 | delayBuffer[preDelayWriteIndex] = undelayedSource; |
401 | 0 |
|
402 | 0 | float absUndelayedSource = undelayedSource > 0 ? undelayedSource : -undelayedSource; |
403 | 0 | if (compressorInput < absUndelayedSource) |
404 | 0 | compressorInput = absUndelayedSource; |
405 | 0 | } |
406 | 0 |
|
407 | 0 | // Calculate shaped power on undelayed input. |
408 | 0 |
|
409 | 0 | float scaledInput = compressorInput; |
410 | 0 | float absInput = scaledInput > 0 ? scaledInput : -scaledInput; |
411 | 0 |
|
412 | 0 | // Put through shaping curve. |
413 | 0 | // This is linear up to the threshold, then enters a "knee" portion followed by the "ratio" portion. |
414 | 0 | // The transition from the threshold to the knee is smooth (1st derivative matched). |
415 | 0 | // The transition from the knee to the ratio portion is smooth (1st derivative matched). |
416 | 0 | float shapedInput = saturate(absInput, k); |
417 | 0 |
|
418 | 0 | float attenuation = absInput <= 0.0001f ? 1 : shapedInput / absInput; |
419 | 0 |
|
420 | 0 | float attenuationDb = -WebAudioUtils::ConvertLinearToDecibels(attenuation, -1000.0f); |
421 | 0 | attenuationDb = max(2.0f, attenuationDb); |
422 | 0 |
|
423 | 0 | float dbPerFrame = attenuationDb / satReleaseFrames; |
424 | 0 |
|
425 | 0 | float satReleaseRate = WebAudioUtils::ConvertDecibelsToLinear(dbPerFrame) - 1; |
426 | 0 |
|
427 | 0 | bool isRelease = (attenuation > detectorAverage); |
428 | 0 | float rate = isRelease ? satReleaseRate : 1; |
429 | 0 |
|
430 | 0 | detectorAverage += (attenuation - detectorAverage) * rate; |
431 | 0 | detectorAverage = min(1.0f, detectorAverage); |
432 | 0 |
|
433 | 0 | // Fix gremlins. |
434 | 0 | if (IsNaN(detectorAverage)) |
435 | 0 | detectorAverage = 1; |
436 | 0 | if (IsInfinite(detectorAverage)) |
437 | 0 | detectorAverage = 1; |
438 | 0 |
|
439 | 0 | // Exponential approach to desired gain. |
440 | 0 | if (envelopeRate < 1) { |
441 | 0 | // Attack - reduce gain to desired. |
442 | 0 | compressorGain += (scaledDesiredGain - compressorGain) * envelopeRate; |
443 | 0 | } else { |
444 | 0 | // Release - exponentially increase gain to 1.0 |
445 | 0 | compressorGain *= envelopeRate; |
446 | 0 | compressorGain = min(1.0f, compressorGain); |
447 | 0 | } |
448 | 0 |
|
449 | 0 | // Warp pre-compression gain to smooth out sharp exponential transition points. |
450 | 0 | float postWarpCompressorGain = sinf(0.5f * M_PI * compressorGain); |
451 | 0 |
|
452 | 0 | // Calculate total gain using master gain and effect blend. |
453 | 0 | float totalGain = dryMix + wetMix * masterLinearGain * postWarpCompressorGain; |
454 | 0 |
|
455 | 0 | // Calculate metering. |
456 | 0 | float dbRealGain = 20 * log10(postWarpCompressorGain); |
457 | 0 | if (dbRealGain < m_meteringGain) |
458 | 0 | m_meteringGain = dbRealGain; |
459 | 0 | else |
460 | 0 | m_meteringGain += (dbRealGain - m_meteringGain) * m_meteringReleaseK; |
461 | 0 |
|
462 | 0 | // Apply final gain. |
463 | 0 | for (unsigned i = 0; i < numberOfChannels; ++i) { |
464 | 0 | float* delayBuffer = m_preDelayBuffers[i].get(); |
465 | 0 | destinationChannels[i][frameIndex] = delayBuffer[preDelayReadIndex] * totalGain; |
466 | 0 | } |
467 | 0 |
|
468 | 0 | frameIndex++; |
469 | 0 | preDelayReadIndex = (preDelayReadIndex + 1) & MaxPreDelayFramesMask; |
470 | 0 | preDelayWriteIndex = (preDelayWriteIndex + 1) & MaxPreDelayFramesMask; |
471 | 0 | } |
472 | 0 |
|
473 | 0 | // Locals back to member variables. |
474 | 0 | m_preDelayReadIndex = preDelayReadIndex; |
475 | 0 | m_preDelayWriteIndex = preDelayWriteIndex; |
476 | 0 | m_detectorAverage = DenormalDisabler::flushDenormalFloatToZero(detectorAverage); |
477 | 0 | m_compressorGain = DenormalDisabler::flushDenormalFloatToZero(compressorGain); |
478 | 0 | } |
479 | 0 | } |
480 | 0 | } |
481 | | |
482 | | void DynamicsCompressorKernel::reset() |
483 | 0 | { |
484 | 0 | m_detectorAverage = 0; |
485 | 0 | m_compressorGain = 1; |
486 | 0 | m_meteringGain = 1; |
487 | 0 |
|
488 | 0 | // Predelay section. |
489 | 0 | for (unsigned i = 0; i < m_preDelayBuffers.Length(); ++i) |
490 | 0 | memset(m_preDelayBuffers[i].get(), 0, sizeof(float) * MaxPreDelayFrames); |
491 | 0 |
|
492 | 0 | m_preDelayReadIndex = 0; |
493 | 0 | m_preDelayWriteIndex = DefaultPreDelayFrames; |
494 | 0 |
|
495 | 0 | m_maxAttackCompressionDiffDb = -1; // uninitialized state |
496 | 0 | } |
497 | | |
498 | | } // namespace WebCore |