99 removalScratch_.assign(
static_cast<size_t>(std::min(numChannels_, kMaxChannels)),
100 std::vector<T>(
static_cast<size_t>(std::max(1, spec.
maxBlockSize)), T(0)));
101 mixScratch_.assign(
static_cast<size_t>(kMaxChannels),
102 std::vector<T>(
static_cast<size_t>(std::max(1, spec.
maxBlockSize)), T(0)));
103 mixWeights_ = mixScratch_;
104 endDesigned_ =
false;
105 updateEnvelopeCoeffs();
106 coefSmoothCoeff_ =
static_cast<T
>(1.0 - std::exp(-1.0 / (sampleRate_ * 0.001)));
107 autoFall_ = std::pow(10.0, -kAutoRefFallDb / (20.0 * sampleRate_));
111 filterParamsDirty_.store(
true, std::memory_order_release);
112 updateFiltersIfDirty();
121 if (removalListen_.load(std::memory_order_relaxed) && !removalScratch_.empty())
125 const int nCh = std::min(buffer.getNumChannels(),
static_cast<int>(removalScratch_.size()));
126 const int step =
static_cast<int>(removalScratch_[0].size());
127 for (
int off = 0; off < buffer.getNumSamples(); off += step)
129 const int m = std::min(step, buffer.getNumSamples() - off);
130 auto part = buffer.getSubView(off, m);
131 for (
int ch = 0; ch < nCh; ++ch) std::copy_n(part.getChannel(ch), m, removalScratch_[
static_cast<size_t>(ch)].data());
133 for (
int ch = 0; ch < nCh; ++ch)
135 T* d = part.getChannel(ch);
136 const T* in = removalScratch_[
static_cast<size_t>(ch)].data();
137 for (
int i = 0; i < m; ++i) d[i] = in[i] - d[i];
149 void setRemovalListen(
bool on)
noexcept { removalListen_.store(on, std::memory_order_relaxed); }
151 [[nodiscard]]
bool isRemovalListen() const noexcept {
return removalListen_.load(std::memory_order_relaxed); }
173 structure_.store(
static_cast<Structure>(std::clamp(
static_cast<int>(structure), 0, 1)), std::memory_order_relaxed);
208 void setMixedBell(
bool on)
noexcept { mixedWanted_.store(on, std::memory_order_relaxed); }
211 [[nodiscard]]
bool isMixedBell() const noexcept {
return mixedWanted_.load(std::memory_order_relaxed); }
216 int numCh = std::min(buffer.getNumChannels(), std::min(numChannels_, kMaxChannels));
217 int numSamples = buffer.getNumSamples();
218 if (numSamples == 0 || numCh == 0)
return;
221 updateFiltersIfDirty();
224 const bool ladder = structure_.load(std::memory_order_relaxed) ==
Structure::Ladder;
225 if (ladder != ladderActive_)
227 ladderActive_ = ladder;
228 for (
int ch = 0; ch < kMaxChannels; ++ch)
230 reduction_[ch].
reset();
232 ladder_[ch].
setCoeffs(reduction_[ch].getCoeffs());
236 if (envCoefsDirty_.exchange(
false, std::memory_order_acquire))
237 updateEnvelopeCoeffs();
239 T thresh = threshold_.load(std::memory_order_relaxed);
240 T maxRed = maxReduction_.load(std::memory_order_relaxed);
241 auto mode = detectionMode_.load(std::memory_order_relaxed);
242 relative_ = autoThreshold_.load(std::memory_order_relaxed);
244 constexpr int kCoefRefreshInterval = 16;
245 const T coefSmooth = coefSmoothCoeff_;
249 const bool mixed = mixedWanted_.load(std::memory_order_relaxed) && !mixScratch_.empty();
250 if (mixed != mixedActive_)
252 mixedActive_ = mixed;
254 for (
int ch = 0; ch < kMaxChannels; ++ch)
256 reduction_[ch].
reset();
263 designEnd(
static_cast<double>(maxRed));
264 const int step =
static_cast<int>(mixScratch_[0].size());
265 for (
int off = 0; off < numSamples; off += step)
267 const int m = std::min(step, numSamples - off);
268 auto part = buffer.getSubView(off, m);
270 processInternal<true, true>(part, numCh, m, thresh, maxRed, coefSmooth, kCoefRefreshInterval, blockMaxGr);
272 processInternal<false, true>(part, numCh, m, thresh, maxRed, coefSmooth, kCoefRefreshInterval, blockMaxGr);
273 mixEnd(part, numCh, m);
279 processInternal<true>(buffer, numCh, numSamples, thresh, maxRed, coefSmooth, kCoefRefreshInterval, blockMaxGr);
283 processInternal<false>(buffer, numCh, numSamples, thresh, maxRed, coefSmooth, kCoefRefreshInterval, blockMaxGr);
286 gainReduction_.store(blockMaxGr, std::memory_order_relaxed);
293 for (
int ch = 0; ch < kMaxChannels; ++ch)
295 detector_[ch].
reset();
296 reduction_[ch].
reset();
298 smoothedGrDb_[ch] = T(0);
299 derivShift_[ch].fill(T(0));
303 autoRef_ = kAutoRefFloor;
305 gainReduction_.store(T(0), std::memory_order_relaxed);
313 if (!std::isfinite(ms))
return;
314 attackMs_.store(std::clamp(ms, T(0.1), T(20)), std::memory_order_relaxed);
315 envCoefsDirty_.store(
true, std::memory_order_release);
321 if (!std::isfinite(ms))
return;
322 releaseMs_.store(std::clamp(ms, T(1), T(500)), std::memory_order_relaxed);
323 envCoefsDirty_.store(
true, std::memory_order_release);
334 if (!std::isfinite(hz))
return;
335 frequency_.store(hz, std::memory_order_relaxed);
336 filterParamsDirty_.store(
true, std::memory_order_release);
342 if (!std::isfinite(octaves))
return;
343 T bw = std::max(octaves, T(0.1));
344 T q = T(1) / (T(2) * std::sinh(T(0.34657359) * bw));
345 bandwidth_.store(q, std::memory_order_relaxed);
346 filterParamsDirty_.store(
true, std::memory_order_release);
352 if (!std::isfinite(db))
return;
353 threshold_.store(db, std::memory_order_relaxed);
368 void setAutoThreshold(
bool on)
noexcept { autoThreshold_.store(on, std::memory_order_relaxed); }
371 [[nodiscard]]
bool isAutoThreshold() const noexcept {
return autoThreshold_.load(std::memory_order_relaxed); }
376 if (!std::isfinite(db))
return;
377 maxReduction_.store(std::abs(db), std::memory_order_relaxed);
383 const int v = std::clamp(
static_cast<int>(mode),
386 detectionMode_.store(
static_cast<DetectionMode>(v), std::memory_order_relaxed);
390 [[nodiscard]] T
getGainReductionDb() const noexcept {
return gainReduction_.load(std::memory_order_relaxed); }
391 [[nodiscard]] T
getFrequency() const noexcept {
return frequency_.load(std::memory_order_relaxed); }
392 [[nodiscard]] T
getThreshold() const noexcept {
return threshold_.load(std::memory_order_relaxed); }
394 [[nodiscard]] T
getAttack() const noexcept {
return attackMs_.load(std::memory_order_relaxed); }
395 [[nodiscard]] T
getRelease() const noexcept {
return releaseMs_.load(std::memory_order_relaxed); }
400 return static_cast<T
>(std::asinh(1.0 / (2.0 *
static_cast<double>(
401 bandwidth_.load(std::memory_order_relaxed)))) / 0.34657359);
405 [[nodiscard]] T
getReduction() const noexcept {
return maxReduction_.load(std::memory_order_relaxed); }
408 [[nodiscard]] std::vector<uint8_t>
getState()
const
413 w.
write(
"frequency",
static_cast<float>(frequency_.load(std::memory_order_relaxed)));
416 w.
write(
"threshold",
static_cast<float>(threshold_.load(std::memory_order_relaxed)));
417 w.
write(
"reduction",
static_cast<float>(maxReduction_.load(std::memory_order_relaxed)));
418 w.
write(
"attack",
static_cast<float>(attackMs_.load(std::memory_order_relaxed)));
419 w.
write(
"release",
static_cast<float>(releaseMs_.load(std::memory_order_relaxed)));
420 w.
write(
"detection",
static_cast<int32_t
>(detectionMode_.load(std::memory_order_relaxed)));
422 w.
write(
"autoThreshold", autoThreshold_.load(std::memory_order_relaxed));
448 template <
bool IsDerivative,
bool Mixed = false>
450 T thresh, T maxRed, T coefSmooth,
int refreshInterval, T& blockMaxGr)
noexcept
452 for (
int i = 0; i < numSamples; ++i)
458 for (
int ch = 0; ch < numCh; ++ch)
460 T dataIn = buffer.getChannel(ch)[i];
461 if (!std::isfinite(dataIn)) dataIn = T(0);
462 maxInput = std::max(maxInput, std::abs(dataIn));
465 if constexpr (IsDerivative)
467 auto& sr = derivShift_[ch];
468 for (
int k = kDerivLen - 1; k > 0; --k) sr[k] = sr[k - 1];
472 for (
int k = 0; k < kDerivLen - 1; ++k)
474 sumDiffs += std::abs(sr[k] - sr[k + 1]);
476 level = sumDiffs / T(kDerivLen - 1);
480 level = std::abs(detector_[ch].processSample(dataIn, 0));
483 if (level > maxLevel) maxLevel = level;
487 T coeff = (maxLevel > envelope_) ? attackCoeff_ : releaseCoeff_;
488 envelope_ += coeff * (maxLevel - envelope_);
492 T threshNow = thresh;
497 autoRef_ = std::max({
static_cast<double>(maxInput), autoRef_ * autoFall_, kAutoRefFloor });
500 T overDb = envDb - threshNow;
501 T grDb = (overDb > T(0)) ? -std::min(overDb, maxRed) : T(0);
503 if (-grDb > blockMaxGr) blockMaxGr = -grDb;
512 (void)refreshInterval;
513 for (
int ch = 0; ch < numCh; ++ch)
515 T& smoothedGr = smoothedGrDb_[ch];
516 smoothedGr += coefSmooth * (grDb - smoothedGr);
518 if (smoothedGr < T(0) && endShare_ > 0.0)
519 w = std::clamp((1.0 - std::pow(10.0,
static_cast<double>(smoothedGr) / 20.0)) / endShare_, 0.0, 1.0);
520 mixWeights_[
static_cast<size_t>(ch)][
static_cast<size_t>(i)] =
static_cast<T
>(w);
525 const bool refresh = (refreshCounter_ &
static_cast<unsigned>(refreshInterval - 1)) == 0u;
527 for (
int ch = 0; ch < numCh; ++ch)
529 T& smoothedGr = smoothedGrDb_[ch];
530 smoothedGr += coefSmooth * (grDb - smoothedGr);
535 if (ch > 0 && smoothedGr == smoothedGrDb_[0])
538 if (ladderActive_) ladder_[ch].
setLadder(ladder_[0].getLadder());
541 updateDynamicPeakCoeffs(ch, smoothedGr);
544 T* channelData = buffer.getChannel(ch);
545 const T x = channelData[i];
547 if (!std::isfinite(x))
continue;
548 channelData[i] = ladderActive_ ?
static_cast<T
>(ladder_[ch].
processSample(
static_cast<double>(x), 0))
549 : reduction_[ch].processSample(x, 0);
559 void updateFiltersIfDirty() noexcept
561 if (!filterParamsDirty_.exchange(
false, std::memory_order_acquire))
564 T freq = frequency_.load(std::memory_order_relaxed);
565 T bw = bandwidth_.load(std::memory_order_relaxed);
571 const double fEff = std::clamp(
static_cast<double>(freq), 1.0,
572 std::max(1.0, sampleRate_ * 0.499));
581 for (
int ch = 0; ch < kMaxChannels; ++ch)
582 detector_[ch].setCoeffsNow(c);
585 peakQ_ =
static_cast<double>(bw);
593 void updateDynamicPeakCoeffs(
int ch, T gainDb)
noexcept
599 if (ladderActive_) ladder_[ch].
setCoeffs(c);
610 void designEnd(
double rangeDb)
noexcept
612 if (endDesigned_ && peakFreq_ == endFreq_ && peakQ_ == endQ_ && rangeDb == endRange_)
return;
614 const double reach = 1.0 + std::pow(10.0, -rangeDb / 40.0);
615 BiquadCoeffs end = half;
616 end.b0 = 1.0 + reach * (half.b0 - 1.0);
617 end.b1 = half.a1 + reach * (half.b1 - half.a1);
618 end.b2 = half.a2 + reach * (half.b2 - half.a2);
619 if (endDesigned_) end_.
rampCoeffs(end,
static_cast<int>(std::lround(kMixedRampMs * 0.001 * sampleRate_)));
622 endFreq_ = peakFreq_;
625 endShare_ = rangeDb > 0.0 ? 1.0 - std::pow(10.0, -rangeDb / 20.0) : 0.0;
633 void mixEnd(AudioBufferView<T>& part,
int numCh,
int n)
noexcept
635 T* copies[kMaxChannels] = {};
636 for (
int ch = 0; ch < numCh; ++ch)
638 const T* x = part.getChannel(ch);
639 T* c = mixScratch_[
static_cast<size_t>(ch)].data();
640 for (
int i = 0; i < n; ++i) c[i] = std::isfinite(x[i]) ? x[i] : T(0);
643 end_.
processBlock(AudioBufferView<T>(copies, numCh, n));
644 for (
int ch = 0; ch < numCh; ++ch)
646 T* y = part.getChannel(ch);
647 const T* e = copies[ch];
648 const T* w = mixWeights_[
static_cast<size_t>(ch)].data();
649 for (
int i = 0; i < n; ++i)
650 if (w[i] > T(0) && std::isfinite(y[i])) y[i] += w[i] * (e[i] - y[i]);
654 void updateEnvelopeCoeffs() noexcept
656 if (sampleRate_ <= 0.0)
return;
657 const double atkSec =
static_cast<double>(attackMs_.load(std::memory_order_relaxed)) * 0.001;
658 const double relSec =
static_cast<double>(releaseMs_.load(std::memory_order_relaxed)) * 0.001;
659 attackCoeff_ =
static_cast<T
>(1.0 - std::exp(-1.0 / (sampleRate_ * std::max(atkSec, 1e-6))));
660 releaseCoeff_ =
static_cast<T
>(1.0 - std::exp(-1.0 / (sampleRate_ * std::max(relSec, 1e-6))));
663 static constexpr int kMaxChannels = 2;
664 static constexpr int kDerivLen = 8;
666 double sampleRate_ = 44100.0;
667 int numChannels_ = 0;
669 std::atomic<T> frequency_ { T(7000) };
670 std::atomic<T> bandwidth_ { T(2) };
671 std::atomic<T> threshold_ { T(-20) };
672 std::atomic<T> maxReduction_ { T(12) };
673 std::atomic<T> attackMs_ { T(0.5) };
674 std::atomic<T> releaseMs_ { T(20) };
677 std::atomic<bool> envCoefsDirty_ {
false };
678 std::atomic<bool> filterParamsDirty_ {
true };
680 T attackCoeff_ = T(0);
681 T releaseCoeff_ = T(0);
682 T coefSmoothCoeff_ = T(0);
683 std::atomic<T> gainReduction_ { T(0) };
685 Biquad<T, 1> detector_[kMaxChannels]{};
686 Biquad<T, 1> reduction_[kMaxChannels]{};
687 NormalizedLadder<T, 1> ladder_[kMaxChannels]{};
689 bool ladderActive_ =
false;
692 static constexpr double kMixedRampMs = 20.0;
693 std::atomic<bool> mixedWanted_ {
false };
694 bool mixedActive_ =
false;
695 NormalizedLadder<T, kMaxChannels> end_;
696 std::vector<std::vector<T>> mixScratch_;
697 std::vector<std::vector<T>> mixWeights_;
698 bool endDesigned_ =
false;
699 double endFreq_ = 0.0, endQ_ = 0.0, endRange_ = 0.0;
700 double endShare_ = 0.0;
703 static constexpr double kAutoRefFallDb = 1.0;
704 static constexpr double kAutoRefFloor = 1e-5;
705 std::atomic<bool> autoThreshold_ {
false };
706 std::atomic<bool> removalListen_ {
false };
707 std::vector<std::vector<T>> removalScratch_;
708 bool relative_ =
false;
709 double autoRef_ = kAutoRefFloor;
710 double autoFall_ = 1.0;
713 T smoothedGrDb_[kMaxChannels]{};
715 std::array<std::array<T, kDerivLen>, kMaxChannels> derivShift_ {};
718 double peakFreq_ = 7000.0;
720 unsigned refreshCounter_ = 0;