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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "DPFrequency"
//#define LOG_NDEBUG 0
#include <log/log.h>
#include "DPFrequency.h"
#include <algorithm>
#include <sys/param.h>
namespace dp_fx {
using Eigen::MatrixXd;
#define MAX_BLOCKSIZE 16384 //For this implementation
#define MIN_BLOCKSIZE 8
#define CIRCULAR_BUFFER_UPSAMPLE 4 //4 times buffer size
static constexpr float MIN_ENVELOPE = 1e-6f; //-120 dB
static constexpr float EPSILON = 0.0000001f;
static inline bool isZero(float f) {
return fabs(f) <= EPSILON;
}
template <class T>
bool compareEquality(T a, T b) {
return (a == b);
}
template <> bool compareEquality<float>(float a, float b) {
return isZero(a - b);
}
//TODO: avoid using macro for estimating change and assignment.
#define IS_CHANGED(c, a, b) { c |= !compareEquality(a,b); \
(a) = (b); }
//ChannelBuffers helper
void ChannelBuffer::initBuffers(unsigned int blockSize, unsigned int overlapSize,
unsigned int halfFftSize, unsigned int samplingRate, DPBase &dpBase) {
ALOGV("ChannelBuffer::initBuffers blockSize %d, overlap %d, halfFft %d",
blockSize, overlapSize, halfFftSize);
mSamplingRate = samplingRate;
mBlockSize = blockSize;
cBInput.resize(mBlockSize * CIRCULAR_BUFFER_UPSAMPLE);
cBOutput.resize(mBlockSize * CIRCULAR_BUFFER_UPSAMPLE);
//temp vectors
input.resize(mBlockSize);
output.resize(mBlockSize);
outTail.resize(overlapSize);
//module vectors
mPreEqFactorVector.resize(halfFftSize, 1.0);
mPostEqFactorVector.resize(halfFftSize, 1.0);
mPreEqBands.resize(dpBase.getPreEqBandCount());
mMbcBands.resize(dpBase.getMbcBandCount());
mPostEqBands.resize(dpBase.getPostEqBandCount());
ALOGV("mPreEqBands %zu, mMbcBands %zu, mPostEqBands %zu",mPreEqBands.size(),
mMbcBands.size(), mPostEqBands.size());
DPChannel *pChannel = dpBase.getChannel(0);
if (pChannel != nullptr) {
mPreEqInUse = pChannel->getPreEq()->isInUse();
mMbcInUse = pChannel->getMbc()->isInUse();
mPostEqInUse = pChannel->getPostEq()->isInUse();
mLimiterInUse = pChannel->getLimiter()->isInUse();
}
mLimiterParams.linkGroup = -1; //no group.
}
void ChannelBuffer::computeBinStartStop(BandParams &bp, size_t binStart) {
bp.binStart = binStart;
bp.binStop = (int)(0.5 + bp.freqCutoffHz * mBlockSize / mSamplingRate);
}
//== LinkedLimiters Helper
void LinkedLimiters::reset() {
mGroupsMap.clear();
}
void LinkedLimiters::update(int32_t group, int index) {
mGroupsMap[group].push_back(index);
}
void LinkedLimiters::remove(int index) {
//check all groups and if index is found, remove it.
//if group is empty afterwards, remove it.
for (auto it = mGroupsMap.begin(); it != mGroupsMap.end(); ) {
for (auto itIndex = it->second.begin(); itIndex != it->second.end(); ) {
if (*itIndex == index) {
itIndex = it->second.erase(itIndex);
} else {
++itIndex;
}
}
if (it->second.size() == 0) {
it = mGroupsMap.erase(it);
} else {
++it;
}
}
}
//== DPFrequency
void DPFrequency::reset() {
}
size_t DPFrequency::getMinBockSize() {
return MIN_BLOCKSIZE;
}
size_t DPFrequency::getMaxBockSize() {
return MAX_BLOCKSIZE;
}
void DPFrequency::configure(size_t blockSize, size_t overlapSize,
size_t samplingRate) {
ALOGV("configure");
mBlockSize = blockSize;
if (mBlockSize > MAX_BLOCKSIZE) {
mBlockSize = MAX_BLOCKSIZE;
} else if (mBlockSize < MIN_BLOCKSIZE) {
mBlockSize = MIN_BLOCKSIZE;
} else {
if (!powerof2(blockSize)) {
//find next highest power of 2.
mBlockSize = 1 << (32 - __builtin_clz(blockSize));
}
}
mHalfFFTSize = 1 + mBlockSize / 2; //including Nyquist bin
mOverlapSize = std::min(overlapSize, mBlockSize/2);
int channelcount = getChannelCount();
mSamplingRate = samplingRate;
mChannelBuffers.resize(channelcount);
for (int ch = 0; ch < channelcount; ch++) {
mChannelBuffers[ch].initBuffers(mBlockSize, mOverlapSize, mHalfFFTSize,
mSamplingRate, *this);
}
//effective number of frames processed per second
mBlocksPerSecond = (float)mSamplingRate / (mBlockSize - mOverlapSize);
fill_window(mVWindow, RDSP_WINDOW_HANNING_FLAT_TOP, mBlockSize, mOverlapSize);
//split window into analysis and synthesis. Both are the sqrt() of original
//window
Eigen::Map<Eigen::VectorXf> eWindow(&mVWindow[0], mVWindow.size());
eWindow = eWindow.array().sqrt();
//compute window rms for energy compensation
mWindowRms = 0;
for (size_t i = 0; i < mVWindow.size(); i++) {
mWindowRms += mVWindow[i] * mVWindow[i];
}
//Making sure window rms is not zero.
mWindowRms = std::max(sqrt(mWindowRms / mVWindow.size()), MIN_ENVELOPE);
}
void DPFrequency::updateParameters(ChannelBuffer &cb, int channelIndex) {
DPChannel *pChannel = getChannel(channelIndex);
if (pChannel == nullptr) {
ALOGE("Error: updateParameters null DPChannel %d", channelIndex);
return;
}
//===Input Gain and preEq
{
bool changed = false;
IS_CHANGED(changed, cb.inputGainDb, pChannel->getInputGain());
//===EqPre
if (cb.mPreEqInUse) {
DPEq *pPreEq = pChannel->getPreEq();
if (pPreEq == nullptr) {
ALOGE("Error: updateParameters null PreEq for channel: %d", channelIndex);
return;
}
IS_CHANGED(changed, cb.mPreEqEnabled, pPreEq->isEnabled());
if (cb.mPreEqEnabled) {
for (unsigned int b = 0; b < getPreEqBandCount(); b++) {
DPEqBand *pEqBand = pPreEq->getBand(b);
if (pEqBand == nullptr) {
ALOGE("Error: updateParameters null PreEqBand for band %d", b);
return; //failed.
}
ChannelBuffer::EqBandParams *pEqBandParams = &cb.mPreEqBands[b];
IS_CHANGED(changed, pEqBandParams->enabled, pEqBand->isEnabled());
IS_CHANGED(changed, pEqBandParams->freqCutoffHz,
pEqBand->getCutoffFrequency());
IS_CHANGED(changed, pEqBandParams->gainDb, pEqBand->getGain());
}
}
}
if (changed) {
float inputGainFactor = dBtoLinear(cb.inputGainDb);
if (cb.mPreEqInUse && cb.mPreEqEnabled) {
ALOGV("preEq changed, recomputing! channel %d", channelIndex);
size_t binNext = 0;
for (unsigned int b = 0; b < getPreEqBandCount(); b++) {
ChannelBuffer::EqBandParams *pEqBandParams = &cb.mPreEqBands[b];
//frequency translation
cb.computeBinStartStop(*pEqBandParams, binNext);
binNext = pEqBandParams->binStop + 1;
float factor = dBtoLinear(pEqBandParams->gainDb);
if (!pEqBandParams->enabled) {
factor = inputGainFactor;
}
for (size_t k = pEqBandParams->binStart;
k <= pEqBandParams->binStop && k < mHalfFFTSize; k++) {
cb.mPreEqFactorVector[k] = factor * inputGainFactor;
}
}
} else {
ALOGV("only input gain changed, recomputing!");
//populate PreEq factor with input gain factor.
for (size_t k = 0; k < mHalfFFTSize; k++) {
cb.mPreEqFactorVector[k] = inputGainFactor;
}
}
}
} //inputGain and preEq
//===EqPost
if (cb.mPostEqInUse) {
bool changed = false;
DPEq *pPostEq = pChannel->getPostEq();
if (pPostEq == nullptr) {
ALOGE("Error: updateParameters null postEq for channel: %d", channelIndex);
return; //failed.
}
IS_CHANGED(changed, cb.mPostEqEnabled, pPostEq->isEnabled());
if (cb.mPostEqEnabled) {
for (unsigned int b = 0; b < getPostEqBandCount(); b++) {
DPEqBand *pEqBand = pPostEq->getBand(b);
if (pEqBand == nullptr) {
ALOGE("Error: updateParameters PostEqBand NULL for band %d", b);
return; //failed.
}
ChannelBuffer::EqBandParams *pEqBandParams = &cb.mPostEqBands[b];
IS_CHANGED(changed, pEqBandParams->enabled, pEqBand->isEnabled());
IS_CHANGED(changed, pEqBandParams->freqCutoffHz,
pEqBand->getCutoffFrequency());
IS_CHANGED(changed, pEqBandParams->gainDb, pEqBand->getGain());
}
if (changed) {
ALOGV("postEq changed, recomputing! channel %d", channelIndex);
size_t binNext = 0;
for (unsigned int b = 0; b < getPostEqBandCount(); b++) {
ChannelBuffer::EqBandParams *pEqBandParams = &cb.mPostEqBands[b];
//frequency translation
cb.computeBinStartStop(*pEqBandParams, binNext);
binNext = pEqBandParams->binStop + 1;
float factor = dBtoLinear(pEqBandParams->gainDb);
if (!pEqBandParams->enabled) {
factor = 1.0;
}
for (size_t k = pEqBandParams->binStart;
k <= pEqBandParams->binStop && k < mHalfFFTSize; k++) {
cb.mPostEqFactorVector[k] = factor;
}
}
}
} //enabled
}
//===MBC
if (cb.mMbcInUse) {
DPMbc *pMbc = pChannel->getMbc();
if (pMbc == nullptr) {
ALOGE("Error: updateParameters Mbc NULL for channel: %d", channelIndex);
return;
}
cb.mMbcEnabled = pMbc->isEnabled();
if (cb.mMbcEnabled) {
bool changed = false;
for (unsigned int b = 0; b < getMbcBandCount(); b++) {
DPMbcBand *pMbcBand = pMbc->getBand(b);
if (pMbcBand == nullptr) {
ALOGE("Error: updateParameters MbcBand NULL for band %d", b);
return; //failed.
}
ChannelBuffer::MbcBandParams *pMbcBandParams = &cb.mMbcBands[b];
pMbcBandParams->enabled = pMbcBand->isEnabled();
IS_CHANGED(changed, pMbcBandParams->freqCutoffHz,
pMbcBand->getCutoffFrequency());
pMbcBandParams->gainPreDb = pMbcBand->getPreGain();
pMbcBandParams->gainPostDb = pMbcBand->getPostGain();
pMbcBandParams->attackTimeMs = pMbcBand->getAttackTime();
pMbcBandParams->releaseTimeMs = pMbcBand->getReleaseTime();
pMbcBandParams->ratio = pMbcBand->getRatio();
pMbcBandParams->thresholdDb = pMbcBand->getThreshold();
pMbcBandParams->kneeWidthDb = pMbcBand->getKneeWidth();
pMbcBandParams->noiseGateThresholdDb = pMbcBand->getNoiseGateThreshold();
pMbcBandParams->expanderRatio = pMbcBand->getExpanderRatio();
}
if (changed) {
ALOGV("mbc changed, recomputing! channel %d", channelIndex);
size_t binNext= 0;
for (unsigned int b = 0; b < getMbcBandCount(); b++) {
ChannelBuffer::MbcBandParams *pMbcBandParams = &cb.mMbcBands[b];
pMbcBandParams->previousEnvelope = 0;
//frequency translation
cb.computeBinStartStop(*pMbcBandParams, binNext);
binNext = pMbcBandParams->binStop + 1;
}
}
}
}
//===Limiter
if (cb.mLimiterInUse) {
bool changed = false;
DPLimiter *pLimiter = pChannel->getLimiter();
if (pLimiter == nullptr) {
ALOGE("Error: updateParameters Limiter NULL for channel: %d", channelIndex);
return;
}
cb.mLimiterEnabled = pLimiter->isEnabled();
if (cb.mLimiterEnabled) {
IS_CHANGED(changed, cb.mLimiterParams.linkGroup ,
(int32_t)pLimiter->getLinkGroup());
cb.mLimiterParams.attackTimeMs = pLimiter->getAttackTime();
cb.mLimiterParams.releaseTimeMs = pLimiter->getReleaseTime();
cb.mLimiterParams.ratio = pLimiter->getRatio();
cb.mLimiterParams.thresholdDb = pLimiter->getThreshold();
cb.mLimiterParams.postGainDb = pLimiter->getPostGain();
}
if (changed) {
ALOGV("limiter changed, recomputing linkGroups for %d", channelIndex);
mLinkedLimiters.remove(channelIndex); //in case it was already there.
mLinkedLimiters.update(cb.mLimiterParams.linkGroup, channelIndex);
}
}
//=== Output Gain
cb.outputGainDb = pChannel->getOutputGain();
}
size_t DPFrequency::processSamples(const float *in, float *out, size_t samples) {
const float *pIn = in;
float *pOut = out;
int channelCount = mChannelBuffers.size();
if (channelCount < 1) {
ALOGW("warning: no Channels ready for processing");
return 0;
}
//**Check if parameters have changed and update
for (int ch = 0; ch < channelCount; ch++) {
updateParameters(mChannelBuffers[ch], ch);
}
//**separate into channels
for (size_t k = 0; k < samples; k += channelCount) {
for (int ch = 0; ch < channelCount; ch++) {
mChannelBuffers[ch].cBInput.write(*pIn++);
}
}
//**process all channelBuffers
processChannelBuffers(mChannelBuffers);
//** estimate how much data is available in ALL channels
size_t available = mChannelBuffers[0].cBOutput.availableToRead();
for (int ch = 1; ch < channelCount; ch++) {
available = std::min(available, mChannelBuffers[ch].cBOutput.availableToRead());
}
//** make sure to output just what the buffer can handle
if (available > samples/channelCount) {
available = samples/channelCount;
}
//**Prepend zeroes if necessary
size_t fill = samples - (channelCount * available);
for (size_t k = 0; k < fill; k++) {
*pOut++ = 0;
}
//**interleave channels
for (size_t k = 0; k < available; k++) {
for (int ch = 0; ch < channelCount; ch++) {
*pOut++ = mChannelBuffers[ch].cBOutput.read();
}
}
return samples;
}
size_t DPFrequency::processChannelBuffers(CBufferVector &channelBuffers) {
const int channelCount = channelBuffers.size();
size_t processedSamples = 0;
size_t processFrames = mBlockSize - mOverlapSize;
size_t available = channelBuffers[0].cBInput.availableToRead();
for (int ch = 1; ch < channelCount; ch++) {
available = std::min(available, channelBuffers[ch].cBInput.availableToRead());
}
while (available >= processFrames) {
//First pass
for (int ch = 0; ch < channelCount; ch++) {
ChannelBuffer * pCb = &channelBuffers[ch];
//move tail of previous
std::copy(pCb->input.begin() + processFrames,
pCb->input.end(),
pCb->input.begin());
//read new available data
for (unsigned int k = 0; k < processFrames; k++) {
pCb->input[mOverlapSize + k] = pCb->cBInput.read();
}
//first stages: fft, preEq, mbc, postEq and start of Limiter
processedSamples += processFirstStages(*pCb);
}
//**compute linked limiters and update levels if needed
processLinkedLimiters(channelBuffers);
//final pass.
for (int ch = 0; ch < channelCount; ch++) {
ChannelBuffer * pCb = &channelBuffers[ch];
//linked limiter and ifft
processLastStages(*pCb);
//mix tail (and capture new tail
for (unsigned int k = 0; k < mOverlapSize; k++) {
pCb->output[k] += pCb->outTail[k];
pCb->outTail[k] = pCb->output[processFrames + k]; //new tail
}
//output data
for (unsigned int k = 0; k < processFrames; k++) {
pCb->cBOutput.write(pCb->output[k]);
}
}
available -= processFrames;
}
return processedSamples;
}
size_t DPFrequency::processFirstStages(ChannelBuffer &cb) {
//##apply window
Eigen::Map<Eigen::VectorXf> eWindow(&mVWindow[0], mVWindow.size());
Eigen::Map<Eigen::VectorXf> eInput(&cb.input[0], cb.input.size());
Eigen::VectorXf eWin = eInput.cwiseProduct(eWindow); //apply window
//##fft
//Note: we are using eigen with the default scaling, which ensures that
// IFFT( FFT(x) ) = x.
// TODO: optimize by using the noscale option, and compensate with dB scale offsets
mFftServer.fwd(cb.complexTemp, eWin);
size_t cSize = cb.complexTemp.size();
size_t maxBin = std::min(cSize/2, mHalfFFTSize);
//== EqPre (always runs)
for (size_t k = 0; k < maxBin; k++) {
cb.complexTemp[k] *= cb.mPreEqFactorVector[k];
}
//== MBC
if (cb.mMbcInUse && cb.mMbcEnabled) {
for (size_t band = 0; band < cb.mMbcBands.size(); band++) {
ChannelBuffer::MbcBandParams *pMbcBandParams = &cb.mMbcBands[band];
float fEnergySum = 0;
//apply pre gain.
float preGainFactor = dBtoLinear(pMbcBandParams->gainPreDb);
float preGainSquared = preGainFactor * preGainFactor;
for (size_t k = pMbcBandParams->binStart; k <= pMbcBandParams->binStop; k++) {
fEnergySum += std::norm(cb.complexTemp[k]) * preGainSquared; //mag squared
}
//Eigen FFT is full spectrum, even if the source was real data.
// Each half spectrum has half the energy. This is taken into account with the * 2
// factor in the energy computations.
// energy = sqrt(sum_components_squared) number_points
// in here, the fEnergySum is duplicated to account for the second half spectrum,
// and the windowRms is used to normalize by the expected energy reduction
// caused by the window used (expected for steady state signals)
fEnergySum = sqrt(fEnergySum * 2) / (mBlockSize * mWindowRms);
// updates computed per frame advance.
float fTheta = 0.0;
float fFAttSec = pMbcBandParams->attackTimeMs / 1000; //in seconds
float fFRelSec = pMbcBandParams->releaseTimeMs / 1000; //in seconds
if (fEnergySum > pMbcBandParams->previousEnvelope) {
fTheta = exp(-1.0 / (fFAttSec * mBlocksPerSecond));
} else {
fTheta = exp(-1.0 / (fFRelSec * mBlocksPerSecond));
}
float fEnv = (1.0 - fTheta) * fEnergySum + fTheta * pMbcBandParams->previousEnvelope;
//preserve for next iteration
pMbcBandParams->previousEnvelope = fEnv;
if (fEnv < MIN_ENVELOPE) {
fEnv = MIN_ENVELOPE;
}
const float envDb = linearToDb(fEnv);
float newLevelDb = envDb;
//using shorter variables for code clarity
const float thresholdDb = pMbcBandParams->thresholdDb;
const float ratio = pMbcBandParams->ratio;
const float kneeWidthDbHalf = pMbcBandParams->kneeWidthDb / 2;
const float noiseGateThresholdDb = pMbcBandParams->noiseGateThresholdDb;
const float expanderRatio = pMbcBandParams->expanderRatio;
//find segment
if (envDb > thresholdDb + kneeWidthDbHalf) {
//compression segment
newLevelDb = envDb + ((1 / ratio) - 1) * (envDb - thresholdDb);
} else if (envDb > thresholdDb - kneeWidthDbHalf) {
//knee-compression segment
float temp = (envDb - thresholdDb + kneeWidthDbHalf);
newLevelDb = envDb + ((1 / ratio) - 1) *
temp * temp / (kneeWidthDbHalf * 4);
} else if (envDb < noiseGateThresholdDb) {
//expander segment
newLevelDb = noiseGateThresholdDb -
expanderRatio * (noiseGateThresholdDb - envDb);
}
float newFactor = dBtoLinear(newLevelDb - envDb);
//apply post gain.
newFactor *= dBtoLinear(pMbcBandParams->gainPostDb);
//apply to this band
for (size_t k = pMbcBandParams->binStart; k <= pMbcBandParams->binStop; k++) {
cb.complexTemp[k] *= newFactor;
}
} //end per band process
} //end MBC
//== EqPost
if (cb.mPostEqInUse && cb.mPostEqEnabled) {
for (size_t k = 0; k < maxBin; k++) {
cb.complexTemp[k] *= cb.mPostEqFactorVector[k];
}
}
//== Limiter. First Pass
if (cb.mLimiterInUse && cb.mLimiterEnabled) {
float fEnergySum = 0;
for (size_t k = 0; k < maxBin; k++) {
fEnergySum += std::norm(cb.complexTemp[k]);
}
//see explanation above for energy computation logic
fEnergySum = sqrt(fEnergySum * 2) / (mBlockSize * mWindowRms);
float fTheta = 0.0;
float fFAttSec = cb.mLimiterParams.attackTimeMs / 1000; //in seconds
float fFRelSec = cb.mLimiterParams.releaseTimeMs / 1000; //in seconds
if (fEnergySum > cb.mLimiterParams.previousEnvelope) {
fTheta = exp(-1.0 / (fFAttSec * mBlocksPerSecond));
} else {
fTheta = exp(-1.0 / (fFRelSec * mBlocksPerSecond));
}
float fEnv = (1.0 - fTheta) * fEnergySum + fTheta * cb.mLimiterParams.previousEnvelope;
//preserve for next iteration
cb.mLimiterParams.previousEnvelope = fEnv;
const float envDb = linearToDb(fEnv);
float newFactorDb = 0;
//using shorter variables for code clarity
const float thresholdDb = cb.mLimiterParams.thresholdDb;
const float ratio = cb.mLimiterParams.ratio;
if (envDb > thresholdDb) {
//limiter segment
newFactorDb = ((1 / ratio) - 1) * (envDb - thresholdDb);
}
float newFactor = dBtoLinear(newFactorDb);
cb.mLimiterParams.newFactor = newFactor;
} //end Limiter
return mBlockSize;
}
void DPFrequency::processLinkedLimiters(CBufferVector &channelBuffers) {
const int channelCount = channelBuffers.size();
for (auto &groupPair : mLinkedLimiters.mGroupsMap) {
float minFactor = 1.0;
//estimate minfactor for all linked
for(int index : groupPair.second) {
if (index >= 0 && index < channelCount) {
minFactor = std::min(channelBuffers[index].mLimiterParams.newFactor, minFactor);
}
}
//apply minFactor
for(int index : groupPair.second) {
if (index >= 0 && index < channelCount) {
channelBuffers[index].mLimiterParams.linkFactor = minFactor;
}
}
}
}
size_t DPFrequency::processLastStages(ChannelBuffer &cb) {
float outputGainFactor = dBtoLinear(cb.outputGainDb);
//== Limiter. last Pass
if (cb.mLimiterInUse && cb.mLimiterEnabled) {
//compute factor, with post-gain
float factor = cb.mLimiterParams.linkFactor * dBtoLinear(cb.mLimiterParams.postGainDb);
outputGainFactor *= factor;
}
//apply to all if != 1.0
if (!compareEquality(outputGainFactor, 1.0f)) {
size_t cSize = cb.complexTemp.size();
size_t maxBin = std::min(cSize/2, mHalfFFTSize);
for (size_t k = 0; k < maxBin; k++) {
cb.complexTemp[k] *= outputGainFactor;
}
}
//##ifft directly to output.
Eigen::Map<Eigen::VectorXf> eOutput(&cb.output[0], cb.output.size());
mFftServer.inv(eOutput, cb.complexTemp);
//apply rest of window for resynthesis
Eigen::Map<Eigen::VectorXf> eWindow(&mVWindow[0], mVWindow.size());
eOutput = eOutput.cwiseProduct(eWindow);
return mBlockSize;
}
} //namespace dp_fx
|