244 static_assert(std::is_trivially_copyable_v<BandConfig>,
"BandConfig must be trivially copyable for std::atomic");
245 static_assert(std::atomic<T>::is_always_lock_free,
246 "audio-thread stores must not lock");
250 for (
int i = 0; i < MaxBands; ++i) {
252 staged_[
static_cast<size_t>(i)].publish(masterConfigs_[i]);
253 paramsDirty_[i].store(
true, std::memory_order_relaxed);
264 isPrepared_.store(
false, std::memory_order_relaxed);
267 if (oversamplingAutomatic_)
270 if (oversamplingFactor_ > 1 && !linearWanted_)
275 oversampler_ = std::make_unique<Oversampling<T>>(oversamplingFactor_);
276 oversamplerSc_ = std::make_unique<Oversampling<T>>(oversamplingFactor_);
277 oversampler_ ->prepare(spec);
278 oversamplerSc_->prepare(spec);
282 oversampler_.reset();
283 oversamplerSc_.reset();
286 analytic_.assign(
static_cast<size_t>(MaxBands * kMaxChannels),
HilbertIIR<double> {});
290 mixedEngine_ = std::make_unique<MixedEngine>();
291 for (
auto& band : mixedEngine_->bands)
293 band.rest.setNumSections(1);
294 band.cut.setNumSections(1);
295 band.lift.setNumSections(1);
297 designBand_.
prepare(sampleRate_ * runFactor());
305 auto engine = std::make_unique<LinearEngine>();
306 LinearEngine& e = *engine;
307 e.kernel = resolveLinearTaps(spec.
sampleRate);
308 e.channels = std::min(spec.
numChannels, kMaxChannels);
309 e.bank.prepare(e.kernel, e.kernel / kLinearPartitions, e.channels + (e.channels >= 2 ? 2 : 0),
310 MaxBands * kLinearBranches);
311 e.latency = e.kernel / 2 + e.bank.getLatency();
312 e.designFft = std::make_unique<FFTReal<T>>(
static_cast<size_t>(e.kernel));
313 e.spectrum.assign(
static_cast<size_t>(e.kernel + 2), T(0));
314 e.time.assign(
static_cast<size_t>(e.kernel), T(0));
315 e.taps.assign(
static_cast<size_t>(e.kernel), T(0));
316 e.window.resize(
static_cast<size_t>(e.kernel));
317 for (
int m = 0; m < e.kernel; ++m)
319 const double t = 2.0 * std::numbers::pi * m / e.kernel;
320 e.window[
static_cast<size_t>(m)] =
static_cast<T
>(
321 0.35875 - 0.48829 * std::cos(t) + 0.14128 * std::cos(2.0 * t) - 0.01168 * std::cos(3.0 * t));
323 for (
int ch = 0; ch < kMaxChannels; ++ch) e.dry[ch].prepare(e.latency + 1);
325 while (ring < e.latency + kDetChunk + 1) ring <<= 1;
326 e.weightMask = ring - 1;
327 e.weights.assign(
static_cast<size_t>(MaxBands * 2) *
static_cast<size_t>(ring), T(0));
328 linear_ = std::move(engine);
331 int maxLaSamples =
static_cast<int>(sampleRate_ * runFactor() * 0.01) + 1;
332 for (
int ch = 0; ch < kMaxChannels; ++ch)
333 lookaheadBuf_[ch].
prepare(maxLaSamples);
337 for (
int ch = 0; ch < std::min(spec.
numChannels, kMaxChannels); ++ch)
338 deltaRing_[
static_cast<size_t>(ch)].prepare(kRemovalCapacity + std::max(1, spec.
maxBlockSize));
342 listenChannels_ = std::min(spec.
numChannels, kMaxChannels);
344 listenReach_ = linear_ ? linear_->latency
345 : maxLaSamples / runFactor() + 1 + (oversampler_ ? oversampler_->getLatency() : 0);
346 for (
int ch = 0; ch < listenChannels_; ++ch)
347 listenRing_[
static_cast<size_t>(ch)].prepare(listenReach_ + listenBlock_ + 1);
348 listenHeard_.assign(
static_cast<size_t>(listenChannels_) *
static_cast<size_t>(listenBlock_), T(0));
349 isPrepared_.store(
true, std::memory_order_relaxed);
368 if (!isPrepared_.load(std::memory_order_relaxed))
return;
369 const bool removal = removalListen_.load(std::memory_order_relaxed);
370 const bool keyed = keyListen_.load(std::memory_order_relaxed) >= 0 || listenBand_ >= 0;
371 if (!removal && !keyed)
373 processImpl(audio, sidechain);
379 const bool selfKeyed = sidechain.getNumChannels() <= 0 || sidechain.getNumSamples() < audio.getNumSamples()
380 || sidechain.getChannel(0) == audio.getChannel(0);
381 const int nCh = std::min(audio.getNumChannels(), kMaxChannels);
383 for (
int off = 0; off < audio.getNumSamples(); off += step)
385 const int m = std::min(step, audio.getNumSamples() - off);
386 auto part = audio.getSubView(off, m);
387 auto key = selfKeyed ? part : sidechain.getSubView(off, m);
388 const bool heard = keyed && hearKey(key, nCh, m);
391 for (
int ch = 0; ch < nCh; ++ch)
393 const T* d = part.getChannel(ch);
394 for (
int i = 0; i < m; ++i) deltaRing_[static_cast<size_t>(ch)].push(d[i]);
397 const int late = std::min(
getLatency(), kRemovalCapacity);
398 processImpl(part, key);
401 for (
int ch = 0; ch < nCh; ++ch)
403 T* d = part.getChannel(ch);
404 const auto& ring = deltaRing_[
static_cast<size_t>(ch)];
405 for (
int i = 0; i < m; ++i) d[i] = ring.read(late + (m - 1 - i)) - d[i];
408 if (heard) playKey(part, nCh, m);
417 void setRemovalListen(
bool on)
noexcept { removalListen_.store(on, std::memory_order_relaxed); }
419 [[nodiscard]]
bool isRemovalListen() const noexcept {
return removalListen_.load(std::memory_order_relaxed); }
438 keyListen_.store(band >= 0 && band < MaxBands ? band : -1, std::memory_order_relaxed);
441 [[nodiscard]]
int getKeyListen() const noexcept {
return keyListen_.load(std::memory_order_relaxed); }
449 if (sidechain.getNumChannels() <= 0 ||
450 sidechain.getNumSamples() < audio.getNumSamples())
452 if (audio.getNumChannels() <= 0)
return;
458 if (oversamplingFactor_ > 1 && oversampler_ && oversamplerSc_)
466 const int total = audio.getNumSamples();
468 for (
int off = 0; off < total; off += chunk)
470 const int m = std::min(chunk, total - off);
471 auto audioChunk = audio.getSubView(off, m);
472 auto upAudio = oversampler_->upsample(audioChunk);
473 auto upSc = oversamplerSc_->upsample(sidechain.getSubView(off, m));
475 processCore(upAudio, upSc, sampleRate_ * oversamplingFactor_);
477 oversampler_->downsample(audioChunk);
483 processCore(audio, sidechain, sampleRate_);
497 bool hearKey(AudioBufferView<T>& key,
int nCh,
int m)
noexcept
499 const int wanted = keyListen_.load(std::memory_order_relaxed);
500 if (wanted != listenBand_ && listenWeight_ <= 0.0)
502 listenBand_ = wanted;
503 listenDesigned_ =
false;
504 listenFirst_.
reset();
505 listenSecond_.
reset();
506 for (
int ch = 0; ch < listenChannels_; ++ch) listenRing_[static_cast<size_t>(ch)].reset();
508 if (listenBand_ < 0 || listenChannels_ <= 0 || m > listenBlock_)
return false;
509 listenGoal_ = wanted == listenBand_ ? 1.0 : 0.0;
515 if (listenBand_ < numBands_.load(std::memory_order_relaxed)
516 && paramsDirty_[listenBand_].exchange(
false, std::memory_order_acquire))
517 updateBandInternalState(listenBand_, sampleRate_ * runFactor());
518 const BandConfig cfg = states_[listenBand_].cfg;
519 const bool moved = !listenDesigned_ || cfg.frequency != listenCfg_.
frequency || cfg.q != listenCfg_.
q
520 || cfg.shape != listenCfg_.
shape || cfg.timing != listenCfg_.
timing
522 if (listenDesigned_ && cfg.scope != listenCfg_.
scope)
524 listenFirst_.
reset();
525 listenSecond_.
reset();
529 const DetectorDesign detector = designDetector(cfg, sampleRate_);
532 if (detector.two && !listenTwo_) listenSecond_.
reset();
533 listenTwo_ = detector.two;
536 listenDesigned_ =
true;
538 const int scCh = key.getNumChannels();
539 const int channels = std::min(nCh, listenChannels_);
540 const auto line = [
this](
int ch)
noexcept {
541 return listenHeard_.data() +
static_cast<size_t>(ch) *
static_cast<size_t>(listenBlock_);
543 const auto through = [
this, m](
const T* one,
const T* other, T sign,
int slot, T* out)
noexcept {
544 for (
int i = 0; i < m; ++i)
546 T x = std::isfinite(one[i]) ? one[i] : T(0);
547 if (other !=
nullptr) x = (x + sign * (std::isfinite(other[i]) ? other[i] : T(0))) * T(0.5);
549 out[i] = listenTwo_ ? listenSecond_.
processSample(x, slot) : x;
552 for (
int ch = 0; ch < channels; ++ch) std::fill_n(line(ch), m, T(0));
558 for (
int ch = 0; ch < channels; ++ch)
559 through(key.getChannel(std::min(ch, scCh - 1)),
nullptr, T(1), ch, line(ch));
562 through(key.getChannel(0),
nullptr, T(1), 0, line(0));
565 if (channels >= 2) through(key.getChannel(std::min(1, scCh - 1)),
nullptr, T(1), 1, line(1));
568 through(key.getChannel(0), scCh >= 2 ? key.getChannel(1) : nullptr, T(1), 0, line(0));
569 if (channels >= 2) std::copy_n(line(0), m, line(1));
572 if (channels >= 2 && scCh >= 2)
574 through(key.getChannel(0), key.getChannel(1), T(-1), 0, line(0));
575 for (
int i = 0; i < m; ++i) line(1)[i] = -line(0)[i];
580 for (
int ch = 0; ch < channels; ++ch)
582 const T* heard = line(ch);
583 auto& ring = listenRing_[
static_cast<size_t>(ch)];
584 for (
int i = 0; i < m; ++i) ring.push(heard[i]);
590 void playKey(AudioBufferView<T>& out,
int nCh,
int m)
noexcept
592 const int channels = std::min(nCh, listenChannels_);
593 const int late = std::min(
getLatency(), listenReach_);
594 const double step = 1.0 / std::max(1.0, kListenFadeMs * 0.001 * sampleRate_);
595 const double from = listenWeight_, goal = listenGoal_;
596 double weight = from;
597 for (
int ch = 0; ch < channels; ++ch)
599 T* d = out.getChannel(ch);
600 const auto& ring = listenRing_[
static_cast<size_t>(ch)];
602 for (
int i = 0; i < m; ++i)
604 weight = goal > weight ? std::min(goal, weight + step) : std::max(goal, weight - step);
605 const T heard = ring.read(late + (m - 1 - i));
606 if (weight >= 1.0) d[i] = heard;
607 else if (weight > 0.0) d[i] =
static_cast<T
>(d[i] +
static_cast<T
>(weight) * (heard - d[i]));
610 listenWeight_ = weight;
630 if (band < 0 || band >= MaxBands)
return;
636 const BandConfig& prev = masterConfigs_[
static_cast<size_t>(band)];
637 auto keep = [](T v, T fallback) {
return std::isfinite(v) ? v : fallback; };
639 c.
q = keep(c.
q, prev.
q);
642 c.
gain = std::clamp(keep(c.
gain, prev.
gain), T(-24), T(24));
662 masterConfigs_[
static_cast<size_t>(band)] = c;
663 staged_[
static_cast<size_t>(band)].publish(c);
664 paramsDirty_[band].store(
true, std::memory_order_release);
666 if (numBands_.load(std::memory_order_relaxed) < band + 1)
667 numBands_.store(band + 1, std::memory_order_relaxed);
672 numBands_.store(std::clamp(n, 1, MaxBands), std::memory_order_relaxed);
689 void setMixedBands(
bool on)
noexcept { mixedWanted_.store(on, std::memory_order_relaxed); }
692 [[nodiscard]]
bool isMixedBands() const noexcept {
return mixedWanted_.load(std::memory_order_relaxed); }
715 linkMode_.store(
static_cast<LinkMode>(std::clamp(
static_cast<int>(mode), 0, 1)), std::memory_order_relaxed);
743 structure_.store(
static_cast<Structure>(std::clamp(
static_cast<int>(structure), 0, 1)), std::memory_order_relaxed);
773 if (on == linearWanted_)
return;
775 isPrepared_.store(
false, std::memory_order_relaxed);
794 taps = std::clamp(taps, 0, 65536);
795 if (taps == linearTaps_)
return;
797 if (linearWanted_) isPrepared_.store(
false, std::memory_order_relaxed);
817 void getBandMagnitude(
int band, T dynamicGainDb,
const T* frequencies, T* magnitudes,
int numPoints)
const noexcept
819 if (frequencies ==
nullptr || magnitudes ==
nullptr)
return;
820 for (
int i = 0; i < numPoints; ++i) magnitudes[i] = T(1);
821 if (band < 0 || band >= MaxBands || sampleRate_ <= 0.0 || !std::isfinite(dynamicGainDb))
return;
823 const BandConfig cfg = staged_[
static_cast<size_t>(band)].read();
824 const double fs = sampleRate_ * runFactor();
825 const double rest =
static_cast<double>(cfg.
gain), moved =
static_cast<double>(dynamicGainDb);
830 const double range = rangeTowards(cfg, moved > 0.0);
831 const bool acts = range > kRestDb && moved != 0.0;
832 const double side = moved > 0.0 ? range : -range;
833 const double ratio = std::pow(10.0, moved / 20.0), endRatio = std::pow(10.0, side / 20.0);
834 const double weight = acts ? std::clamp((ratio - 1.0) / (endRatio - 1.0), 0.0, 1.0) : 0.0;
835 const double reach = 1.0 + std::pow(10.0, side / 40.0);
836 const AnalogSection atRest = prototypeOf(cfg, rest), atHalf = prototypeOf(cfg, 0.5 * side);
837 for (
int i = 0; i < numPoints; ++i)
839 const double omega = 2.0 * std::numbers::pi *
static_cast<double>(frequencies[i]);
840 const double base = std::abs(rest) > kRestDb ? std::sqrt(atRest.
magnitudeSquared(omega)) : 1.0;
841 const double moving = acts ? 1.0 + weight * reach * (std::sqrt(atHalf.magnitudeSquared(omega)) - 1.0) : 1.0;
842 magnitudes[i] =
static_cast<T
>(base * moving);
849 const double total = rest + moved;
851 if (std::abs(total) > 0.01)
857 static_cast<double>(cfg.
q), total);
break;
859 for (
int i = 0; i < numPoints; ++i)
860 magnitudes[i] =
static_cast<T
>(std::abs(responseOf(c,
static_cast<double>(frequencies[i]), fs)));
866 const double range = rangeTowards(cfg, moved > 0.0);
867 const bool acts = range > kRestDb && moved != 0.0;
868 const double side = moved > 0.0 ? range : -range;
869 const double ratio = std::pow(10.0, moved / 20.0), endRatio = std::pow(10.0, side / 20.0);
870 const double weight = acts ? std::clamp((ratio - 1.0) / (endRatio - 1.0), 0.0, 1.0) : 0.0;
871 const bool rests = std::abs(rest) > kRestDb;
874 for (
int i = 0; i < numPoints; ++i)
876 const double hz =
static_cast<double>(frequencies[i]);
877 const std::complex<double> moving = 1.0 + weight * (responseOf(atEnd, hz, fs) - 1.0);
878 magnitudes[i] =
static_cast<T
>(std::abs(responseOf(atRest, hz, fs) * moving));
904 oversamplingFactor_ = std::clamp(factor, 1, 4);
905 if (oversamplingFactor_ == 3) oversamplingFactor_ = 4;
906 oversamplingAutomatic_ =
false;
907 isPrepared_.store(
false, std::memory_order_relaxed);
925 if (on == oversamplingAutomatic_)
return;
926 oversamplingAutomatic_ = on;
927 isPrepared_.store(
false, std::memory_order_relaxed);
940 if (!std::isfinite(ms))
return;
941 lookaheadMs_ = std::clamp(ms, T(0), T(10));
955 if (linear_)
return linear_->latency;
956 const int factor = runFactor();
957 const int la = lookaheadSamples_.load(std::memory_order_relaxed) / factor;
958 const int os = oversampler_ ? oversampler_->getLatency() : 0;
965 if (band < 0 || band >= MaxBands)
return T(0);
966 return meterGainDb_[band].load(std::memory_order_relaxed);
974 for (
auto& c : bandCov_) c = {};
975 for (
int b = 0; b < MaxBands; ++b)
978 meterGainDb_[b].store(T(0), std::memory_order_relaxed);
979 paramsDirty_[b].store(
true, std::memory_order_relaxed);
981 for (
int ch = 0; ch < kMaxChannels; ++ch)
982 lookaheadBuf_[ch].
reset();
985 linear_->bank.reset();
986 for (
auto& line : linear_->dry) line.reset();
987 std::fill(linear_->weights.begin(), linear_->weights.end(), T(0));
988 linear_->weightPos = 0;
989 linear_->played.fill(
false);
990 linear_->snap =
true;
992 if (oversampler_) oversampler_->reset();
993 if (oversamplerSc_) oversamplerSc_->reset();
995 lastNumBands_ = MaxBands;
999 listenDesigned_ =
false;
1000 listenFirst_.
reset();
1001 listenSecond_.
reset();
1002 for (
auto& ring : listenRing_) ring.reset();
1010 const int n = numBands_.load(std::memory_order_relaxed);
1011 w.
write(
"numBands", n);
1012 w.
write(
"lookahead",
static_cast<float>(lookaheadMs_));
1016 w.
write(
"linear", linearWanted_);
1017 w.
write(
"linearTaps", linearTaps_);
1019 for (
int i = 0; i < n; ++i)
1024 const BandConfig c = staged_[
static_cast<size_t>(i)].read();
1025 std::snprintf(key,
sizeof(key),
"b%d.freq", i);
1027 std::snprintf(key,
sizeof(key),
"b%d.q", i);
1028 w.
write(key,
static_cast<float>(c.
q));
1029 std::snprintf(key,
sizeof(key),
"b%d.thresh", i);
1031 std::snprintf(key,
sizeof(key),
"b%d.shape", i);
1033 std::snprintf(key,
sizeof(key),
"b%d.on", i);
1035 std::snprintf(key,
sizeof(key),
"b%d.gain", i);
1036 w.
write(key,
static_cast<float>(c.
gain));
1037 std::snprintf(key,
sizeof(key),
"b%d.knee", i);
1038 w.
write(key,
static_cast<float>(c.
knee));
1039 std::snprintf(key,
sizeof(key),
"b%d.scope", i);
1041 std::snprintf(key,
sizeof(key),
"b%d.timing", i);
1043 std::snprintf(key,
sizeof(key),
"b%d.relative", i);
1045 std::snprintf(key,
sizeof(key),
"b%d.listenLo", i);
1047 std::snprintf(key,
sizeof(key),
"b%d.listenHi", i);
1049 std::snprintf(key,
sizeof(key),
"b%d.aRatio", i);
1051 std::snprintf(key,
sizeof(key),
"b%d.aAtk", i);
1053 std::snprintf(key,
sizeof(key),
"b%d.aRel", i);
1055 std::snprintf(key,
sizeof(key),
"b%d.aRange", i);
1057 std::snprintf(key,
sizeof(key),
"b%d.aBoost", i);
1059 std::snprintf(key,
sizeof(key),
"b%d.bRatio", i);
1061 std::snprintf(key,
sizeof(key),
"b%d.bAtk", i);
1063 std::snprintf(key,
sizeof(key),
"b%d.bRel", i);
1065 std::snprintf(key,
sizeof(key),
"b%d.bRange", i);
1067 std::snprintf(key,
sizeof(key),
"b%d.bBoost", i);
1078 const int n = std::clamp(r.
read(
"numBands", 0), 0, MaxBands);
1080 for (
int i = 0; i < n; ++i)
1083 std::snprintf(key,
sizeof(key),
"b%d.freq", i);
1085 std::snprintf(key,
sizeof(key),
"b%d.q", i);
1086 c.
q =
static_cast<T
>(r.
read(key, 1.0f));
1087 std::snprintf(key,
sizeof(key),
"b%d.thresh", i);
1089 std::snprintf(key,
sizeof(key),
"b%d.shape", i);
1091 std::snprintf(key,
sizeof(key),
"b%d.on", i);
1093 std::snprintf(key,
sizeof(key),
"b%d.gain", i);
1094 c.
gain =
static_cast<T
>(r.
read(key, 0.0f));
1095 std::snprintf(key,
sizeof(key),
"b%d.knee", i);
1096 c.
knee =
static_cast<T
>(r.
read(key, 0.0f));
1097 std::snprintf(key,
sizeof(key),
"b%d.scope", i);
1099 std::snprintf(key,
sizeof(key),
"b%d.timing", i);
1101 std::snprintf(key,
sizeof(key),
"b%d.relative", i);
1103 std::snprintf(key,
sizeof(key),
"b%d.listenLo", i);
1105 std::snprintf(key,
sizeof(key),
"b%d.listenHi", i);
1107 std::snprintf(key,
sizeof(key),
"b%d.aRatio", i);
1109 std::snprintf(key,
sizeof(key),
"b%d.aAtk", i);
1111 std::snprintf(key,
sizeof(key),
"b%d.aRel", i);
1113 std::snprintf(key,
sizeof(key),
"b%d.aRange", i);
1115 std::snprintf(key,
sizeof(key),
"b%d.aBoost", i);
1117 std::snprintf(key,
sizeof(key),
"b%d.bRatio", i);
1119 std::snprintf(key,
sizeof(key),
"b%d.bAtk", i);
1121 std::snprintf(key,
sizeof(key),
"b%d.bRel", i);
1123 std::snprintf(key,
sizeof(key),
"b%d.bRange", i);
1125 std::snprintf(key,
sizeof(key),
"b%d.bBoost", i);
1141 static constexpr int kMaxChannels = 16;
1142 static constexpr T kMinLevelDb = T(-100.0);
1143 static constexpr T kMinEnvelope = T(1e-12);
1144 static constexpr int kDetChunk = 64;
1150 static constexpr double kDetectorNarrowing = 1.5;
1151 static constexpr double kFastRiseFloor = 0.0003;
1152 static constexpr double kFastFallUnits = 12.0;
1153 static constexpr double kFastFallFloor = 0.020;
1154 static constexpr double kSlowRiseUnits = 40.0;
1155 static constexpr double kSlowRiseFloor = 0.080;
1156 static constexpr double kSlowFallUnits = 150.0;
1157 static constexpr double kSlowFallFloor = 0.400;
1158 static constexpr double kLastFallShare = 0.25;
1159 static constexpr double kDbPerNeper = 8.685889638065037;
1161 static constexpr double kReferenceFallDb = 2.0;
1162 static constexpr double kReferenceWatch = 0.5;
1163 static constexpr double kZoneFloorHz = 10.0;
1164 static constexpr double kListenFadeMs = 10.0;
1166 static constexpr double kKneePerOctave = 3.0;
1167 static constexpr double kMixedRampMs = 20.0;
1168 static constexpr double kRestDb = 0.01;
1173 T aboveAtkCoeff = T(0), aboveRelCoeff = T(0);
1174 T belowAtkCoeff = T(0), belowRelCoeff = T(0);
1179 double fastUp = 1.0, fastDown = 1.0, slowUp = 1.0, slowDown = 1.0, lastDown = 1.0;
1180 double fast = 0.0, slow = 0.0, heard = 0.0;
1183 double reference = -100.0, candidate = -100.0;
1184 double referenceFall = 0.0;
1189 bool cuts =
false, lifts =
false;
1190 double cutScale = 0.0, liftScale = 0.0;
1192 double cutReach = 0.0, liftReach = 0.0, cutReachGoal = 0.0, liftReachGoal = 0.0;
1193 double cutReachStep = 0.0, liftReachStep = 0.0;
1195 bool designed =
false;
1201 BiquadCascade<T, 1, kMaxChannels> rest, cut, lift;
1207 std::array<MixedBand, MaxBands> bands;
1208 std::array<std::array<T, kDetChunk>, kMaxChannels> cut {}, lift {};
1209 std::array<std::array<T, kDetChunk>, kMaxChannels> held {};
1210 std::array<double, kDetChunk> cutWeight {}, liftWeight {};
1213 [[nodiscard]]
static bool ofThePair(
const BandConfig& cfg)
noexcept
1219 struct DetectorDesign
1221 BiquadCoeffs first, second;
1241 [[nodiscard]]
static DetectorDesign designDetector(
const BandConfig& cfg,
double fs)
noexcept
1244 const double top = 0.45 * fs;
1245 const double low =
static_cast<double>(cfg.listenLow), high =
static_cast<double>(cfg.listenHigh);
1246 if (low > 0.0 && high > 0.0)
1248 const double lo = std::clamp(low, kZoneFloorHz, top), hi = std::clamp(high, kZoneFloorHz, top);
1252 const double middle = std::sqrt(lo * hi);
1253 const double there = d.first.getMagnitude(middle, fs) * d.second.getMagnitude(middle, fs);
1256 d.first.b0 /= there;
1257 d.first.b1 /= there;
1258 d.first.b2 /= there;
1261 d.unit = 1.0 / (std::numbers::pi * std::max(hi - lo, middle));
1264 if (low > 0.0 || high > 0.0)
1266 const double edge = std::clamp(low > 0.0 ? low : high, kZoneFloorHz, top);
1268 : BiquadCoeffs::makeLowPassMatched(fs, edge, T(0.707));
1269 d.unit = 1.0 / (std::numbers::pi * edge);
1274 const T detectorQ = program ? cfg.q *
static_cast<T
>(kDetectorNarrowing) : cfg.q;
1275 const double hz = std::clamp(
static_cast<double>(cfg.frequency), 10.0, top);
1280 d.unit = std::max(
static_cast<double>(detectorQ), 0.1) / (std::numbers::pi * hz);
1284 d.unit = 1.0 / (std::numbers::pi * hz);
1288 d.unit = 1.0 / (std::numbers::pi * hz);
1295 [[nodiscard]]
int runFactor() const noexcept {
return linearWanted_ ? 1 : std::max(oversamplingFactor_, 1); }
1298 [[nodiscard]]
static bool plays(
const BandConfig& cfg,
int numChannels)
noexcept
1300 if (!cfg.enabled)
return false;
1304 void processCore(AudioBufferView<T>& audio, AudioBufferView<T>& sidechain,
double currentFs)
noexcept
1306 const int nCh = std::min(audio.getNumChannels(), kMaxChannels);
1307 const int scCh = sidechain.getNumChannels();
1308 const int nS = audio.getNumSamples();
1309 if (scCh <= 0)
return;
1310 const int nb = numBands_.load(std::memory_order_relaxed);
1311 const int laSamples = lookaheadSamples_.load(std::memory_order_relaxed);
1312 dominantLink_ = linkMode_.load(std::memory_order_relaxed) ==
LinkMode::Dominant && nCh == 2;
1315 if (currentFs != linkRate_)
1317 linkRate_ = currentFs;
1318 linkCoeff_ = 1.0 - std::exp(-1.0 / (0.02 * currentFs));
1320 for (
auto& c : bandCov_)
1321 for (double& v : c) if (std::abs(v) < 1e-30) v = 0.0;
1325 const bool ladder = structure_.load(std::memory_order_relaxed) ==
Structure::Ladder;
1326 if (ladder != ladderActive_)
1328 ladderActive_ = ladder;
1329 for (
int b = 0; b < MaxBands; ++b)
1331 bandFilter_[b].reset();
1332 bandLadder_[
static_cast<size_t>(b)].
reset();
1333 bandLadder_[
static_cast<size_t>(b)].setCoeffs(bandFilter_[b].getCoeffs());
1338 const bool mixed = mixedWanted_.load(std::memory_order_relaxed) && mixedEngine_ !=
nullptr && !linear_;
1339 if (mixed != mixedActive_)
1341 mixedActive_ = mixed;
1342 for (
int b = 0; b < MaxBands; ++b) restartBand(b);
1348 if (nb > lastNumBands_)
1349 for (
int b = lastNumBands_; b < nb; ++b) restartBand(b);
1353 for (
int b = 0; b < nb; ++b)
1355 if (paramsDirty_[b].exchange(
false, std::memory_order_acquire))
1356 updateBandInternalState(b, currentFs);
1361 numFirst_ = numSecond_ = 0;
1362 for (
int b = 0; b < nb; ++b)
1364 playing_[
static_cast<size_t>(b)] = plays(states_[b].cfg, nCh);
1365 if (!playing_[
static_cast<size_t>(b)])
continue;
1366 if (ofThePair(states_[b].cfg)) second_[
static_cast<size_t>(numSecond_++)] = b;
1367 else first_[
static_cast<size_t>(numFirst_++)] = b;
1369 if (linear_) serviceLinear(nb);
1375 for (
int chunkStart = 0; chunkStart < nS; chunkStart += kDetChunk)
1377 const int chunkLen = std::min(kDetChunk, nS - chunkStart);
1379 for (
int b = 0; b < nb; ++b)
1381 if (!playing_[
static_cast<size_t>(b)])
continue;
1382 detectBand(b, sidechain, chunkStart, chunkLen, nCh, scCh);
1383 computeGains(b, chunkLen);
1386 if (linear_) applyLinear(audio, chunkStart, chunkLen, nCh, laSamples);
1387 else if (mixedActive_) applyMixed(audio, chunkStart, chunkLen, nCh, laSamples);
1388 else applyRedesigned(audio, chunkStart, chunkLen, nCh, nb, laSamples, currentFs);
1393 void setBandCoeffs(
int b,
const BiquadCoeffs& c)
noexcept
1395 bandFilter_[b].setCoeffsNow(c);
1396 if (ladderActive_) bandLadder_[
static_cast<size_t>(b)].setCoeffs(c);
1400 T runBand(
int b, T x,
int ch)
noexcept
1403 return static_cast<T
>(bandLadder_[
static_cast<size_t>(b)].processSample(
static_cast<double>(x), ch));
1404 return bandFilter_[b].processSample(x, ch);
1415 void detectBand(
int b, AudioBufferView<T>& sidechain,
int start,
int n,
int nCh,
int scCh)
noexcept
1417 const BandConfig& cfg = states_[b].cfg;
1419 T* levels = detLevelDb_[
static_cast<size_t>(b)].data();
1420 std::fill(levels, levels + n, kMinLevelDb);
1421 if (program) std::fill(detLinear_.begin(), detLinear_.begin() + n, 0.0);
1423 const Scope where = cfg.scope;
1425 const bool dominant = dominantLink_ && where ==
Scope::Stereo;
1426 for (
int k = 0; k < listeners; ++k)
1429 const T* first = sidechain.getChannel(std::min(k, scCh - 1)) + start;
1430 const T* second =
nullptr;
1435 first = sidechain.getChannel(std::min(1, scCh - 1)) + start;
1439 if (scCh >= 2) second = sidechain.getChannel(1) + start;
1444 if (second ==
nullptr)
1446 for (
int j = 0; j < n; ++j)
1447 detBand_[
static_cast<size_t>(j)] =
static_cast<double>(bandDetector_[b].processSample(
1448 std::isfinite(first[j]) ? first[j] : T(0), slot));
1452 for (
int j = 0; j < n; ++j)
1454 const T one = std::isfinite(first[j]) ? first[j] : T(0);
1455 const T other = std::isfinite(second[j]) ? second[j] : T(0);
1456 detBand_[
static_cast<size_t>(j)] =
static_cast<double>(
1457 bandDetector_[b].processSample((one + sign * other) * T(0.5), slot));
1460 if (states_[b].zoned)
1463 for (
int j = 0; j < n; ++j)
1464 detBand_[
static_cast<size_t>(j)] =
static_cast<double>(bandZone_[b].processSample(
1465 static_cast<T
>(detBand_[
static_cast<size_t>(j)]), slot));
1467 analytic_[
static_cast<size_t>(b * kMaxChannels + slot)].magnitudeBlock(
1468 detBand_.data(), detMag_.data(), n);
1475 std::copy_n(detBand_.begin(), n, pairBand_.begin());
1476 std::copy_n(detMag_.begin(), n, pairMag_.begin());
1479 auto& cov = bandCov_[
static_cast<size_t>(b)];
1480 for (
int j = 0; j < n; ++j)
1482 const size_t s =
static_cast<size_t>(j);
1483 const double l = pairBand_[s], r = detBand_[s];
1484 cov[0] += linkCoeff_ * (l * l - cov[0]);
1485 cov[1] += linkCoeff_ * (r * r - cov[1]);
1486 cov[2] += linkCoeff_ * (l * r - cov[2]);
1487 const double den = cov[0] * cov[1];
1488 const double rho2 = den > 1e-60 ? std::min(1.0, cov[2] * cov[2] / den) : 0.0;
1490 std::max(std::abs(r), detMag_[s]), rho2);
1491 if (program) detLinear_[s] = amp;
1492 else levels[j] =
gainToDecibels(std::max(
static_cast<T
>(amp), kMinEnvelope));
1501 for (
int j = 0; j < n; ++j)
1502 detLinear_[
static_cast<size_t>(j)] = std::max(detLinear_[
static_cast<size_t>(j)],
1503 std::max(std::abs(detBand_[
static_cast<size_t>(j)]), detMag_[
static_cast<size_t>(j)]));
1507 for (
int j = 0; j < n; ++j)
1509 const T detected = std::max(
static_cast<T
>(std::abs(detBand_[
static_cast<size_t>(j)])),
1510 static_cast<T
>(detMag_[
static_cast<size_t>(j)]));
1511 const T levelDb =
gainToDecibels(std::max(detected, kMinEnvelope));
1512 if (levelDb > levels[j]) levels[j] = levelDb;
1522 void computeGains(
int b,
int n)
noexcept
1524 BandState& st = states_[b];
1525 const BandConfig& cfg = st.cfg;
1527 const T* levels = detLevelDb_[
static_cast<size_t>(b)].data();
1528 T* gains = dynDb_[
static_cast<size_t>(b)].data();
1529 T& currentGain = currentGainDb_[b];
1531 for (
int j = 0; j < n; ++j)
1538 const double x = detLinear_[
static_cast<size_t>(j)];
1539 st.fast += (x > st.fast ? st.fastUp : st.fastDown) * (x - st.fast);
1540 st.slow += (st.fast > st.slow ? st.slowUp : st.slowDown) * (st.fast - st.slow);
1541 const double larger = std::max(st.fast, st.slow);
1542 st.heard = larger > st.heard ? larger : st.heard + st.lastDown * (larger - st.heard);
1543 levelDb = std::max(
static_cast<T
>(
fastLog(std::max(st.heard, 1e-12)) * kDbPerNeper), kMinLevelDb);
1547 levelDb = levels[j];
1550 T threshold = cfg.threshold;
1551 if (cfg.relativeThreshold)
1553 const double level =
static_cast<double>(levelDb);
1554 st.reference = std::max(level, st.reference - st.referenceFall);
1555 if (st.watching > 0)
1557 st.candidate = std::max(level, st.candidate - st.referenceFall);
1558 if (--st.watching == 0) st.reference = st.candidate;
1560 threshold =
static_cast<T
>(st.reference +
static_cast<double>(cfg.threshold));
1564 const T targetGainDb = computeTargetGain(cfg, levelDb, threshold, st.knee);
1568 currentGain = targetGainDb;
1573 const T diff = targetGainDb - currentGain;
1575 if (levelDb > threshold) {
1576 coeff = (std::abs(targetGainDb) > std::abs(currentGain))
1577 ? st.aboveAtkCoeff : st.aboveRelCoeff;
1579 coeff = (std::abs(targetGainDb) > std::abs(currentGain))
1580 ? st.belowAtkCoeff : st.belowRelCoeff;
1582 currentGain += coeff * diff;
1584 gains[j] = currentGain;
1588 if (st.fast < 1e-30) st.fast = 0.0;
1589 if (st.slow < 1e-30) st.slow = 0.0;
1590 if (st.heard < 1e-30) st.heard = 0.0;
1597 void applyRedesigned(AudioBufferView<T>& audio,
int start,
int n,
int nCh,
int nb,
1598 int laSamples,
double currentFs)
noexcept
1600 for (
int j = 0; j < n; ++j)
1602 const int i = start + j;
1603 for (
int b = 0; b < nb; ++b)
1605 if (!playing_[
static_cast<size_t>(b)])
continue;
1606 const T currentGain = dynDb_[
static_cast<size_t>(b)][
static_cast<size_t>(j)];
1617 if ((refreshCounter_ & 15u) == 0u)
1619 const T total = states_[b].cfg.gain + currentGain;
1620 if (std::abs(total) > T(0.01))
1622 switch (states_[b].cfg.shape)
1626 currentFs,
static_cast<double>(states_[b].cfg.frequency),
1627 static_cast<double>(states_[b].cfg.q),
static_cast<double>(total)));
1631 currentFs,
static_cast<double>(states_[b].cfg.frequency),
1632 static_cast<double>(total)));
1636 currentFs,
static_cast<double>(states_[b].cfg.frequency),
1637 static_cast<double>(total)));
1642 setBandCoeffs(b, BiquadCoeffs{});
1645 if ((refreshCounter_ & 63u) == 0u)
1646 meterGainDb_[b].store(currentGain, std::memory_order_relaxed);
1652 T frame[kMaxChannels] = {};
1653 bool finite[kMaxChannels] = {};
1654 for (
int ch = 0; ch < nCh; ++ch)
1656 T audioSample = audio.getChannel(ch)[i];
1657 if (laSamples > 0) {
1658 lookaheadBuf_[ch].push(audioSample);
1659 audioSample = lookaheadBuf_[ch].read(laSamples);
1661 frame[ch] = audioSample;
1662 finite[ch] = std::isfinite(audioSample);
1665 for (
int k = 0; k < numFirst_; ++k)
1667 const int b = first_[
static_cast<size_t>(k)];
1668 switch (states_[b].cfg.scope)
1671 if (finite[0]) frame[0] = runBand(b, frame[0], 0);
1674 if (finite[1]) frame[1] = runBand(b, frame[1], 1);
1677 for (
int ch = 0; ch < nCh; ++ch)
1678 if (finite[ch]) frame[ch] = runBand(b, frame[ch], ch);
1683 if (numSecond_ > 0 && finite[0] && (nCh < 2 || finite[1]))
1686 T middle = frame[0], sides = T(0);
1689 middle = (frame[0] + frame[1]) * T(0.5);
1690 sides = (frame[0] - frame[1]) * T(0.5);
1692 for (
int k = 0; k < numSecond_; ++k)
1694 const int b = second_[
static_cast<size_t>(k)];
1696 middle = runBand(b, middle, 0);
1698 sides = runBand(b, sides, 0);
1702 frame[0] = middle + sides;
1703 frame[1] = middle - sides;
1711 for (
int ch = 0; ch < nCh; ++ch)
1712 audio.getChannel(ch)[i] = frame[ch];
1720 void applyMixed(AudioBufferView<T>& audio,
int start,
int n,
int nCh,
int laSamples)
noexcept
1722 MixedEngine& engine = *mixedEngine_;
1723 T* channels[kMaxChannels] = {};
1724 bool bad[kMaxChannels] = {};
1725 bool anyBad =
false;
1729 for (
int ch = 0; ch < nCh; ++ch)
1731 T* data = audio.getChannel(ch) + start;
1732 channels[ch] = data;
1734 for (
int j = 0; j < n; ++j)
1736 lookaheadBuf_[ch].push(data[j]);
1737 data[j] = lookaheadBuf_[ch].read(laSamples);
1739 for (
int j = 0; j < n; ++j)
1740 if (!std::isfinite(data[j]))
1742 auto& held = engine.held[
static_cast<size_t>(ch)];
1743 if (!bad[ch]) held.fill(T(0));
1744 held[
static_cast<size_t>(j)] = data[j];
1746 bad[ch] = anyBad =
true;
1750 for (
int k = 0; k < numFirst_; ++k)
1752 const int b = first_[
static_cast<size_t>(k)];
1753 const Scope where = states_[b].cfg.scope;
1756 mixBand(b, AudioBufferView<T>(channels, nCh, n), n);
1760 T* single[1] = { channels[where ==
Scope::Left ? 0 : 1] };
1761 mixBand(b, AudioBufferView<T>(single, 1, n), n);
1767 AudioBufferView<T> pair(channels, nCh, n);
1769 for (
int k = 0; k < numSecond_; ++k)
1771 const int b = second_[
static_cast<size_t>(k)];
1772 T* single[1] = { channels[states_[b].cfg.scope ==
Scope::Mid ? 0 : 1] };
1773 mixBand(b, AudioBufferView<T>(single, 1, n), n);
1779 for (
int ch = 0; ch < nCh; ++ch)
1781 if (!bad[ch])
continue;
1782 const auto& held = engine.held[
static_cast<size_t>(ch)];
1783 for (
int j = 0; j < n; ++j)
1784 if (!std::isfinite(held[
static_cast<size_t>(j)])) channels[ch][j] = held[
static_cast<size_t>(j)];
1796 void mixBand(
int b, AudioBufferView<T> where,
int n)
noexcept
1798 BandState& st = states_[b];
1799 MixedEngine& engine = *mixedEngine_;
1800 MixedBand& filters = engine.bands[
static_cast<size_t>(b)];
1801 const T* gains = dynDb_[
static_cast<size_t>(b)].data();
1802 const int count = where.getNumChannels();
1804 bool down =
false, up =
false;
1805 for (
int j = 0; j < n; ++j)
1807 if (st.reachRamp > 0) stepReach(st);
1808 const double d =
static_cast<double>(gains[j]);
1809 const double cut = cutWeight(st, d), lift = liftWeight(st, d);
1810 engine.cutWeight[
static_cast<size_t>(j)] = cut;
1811 engine.liftWeight[
static_cast<size_t>(j)] = lift;
1812 down = down || cut > 0.0;
1813 up = up || lift > 0.0;
1815 meterGainDb_[b].store(gains[n - 1], std::memory_order_relaxed);
1820 if (st.rests) filters.rest.processBlock(where);
1821 T* copies[kMaxChannels] = {};
1824 for (
int ch = 0; ch < count; ++ch)
1826 copies[ch] = engine.cut[
static_cast<size_t>(ch)].data();
1827 std::copy_n(where.getChannel(ch), n, copies[ch]);
1829 filters.cut.processBlock(AudioBufferView<T>(copies, count, n));
1833 for (
int ch = 0; ch < count; ++ch)
1835 copies[ch] = engine.lift[
static_cast<size_t>(ch)].data();
1836 std::copy_n(where.getChannel(ch), n, copies[ch]);
1838 filters.lift.processBlock(AudioBufferView<T>(copies, count, n));
1841 for (
int ch = 0; ch < count; ++ch)
1843 T* y = where.getChannel(ch);
1846 const T* end = engine.cut[
static_cast<size_t>(ch)].data();
1847 for (
int j = 0; j < n; ++j)
1848 y[j] +=
static_cast<T
>(engine.cutWeight[
static_cast<size_t>(j)]) * (end[j] - y[j]);
1852 const T* end = engine.lift[
static_cast<size_t>(ch)].data();
1853 for (
int j = 0; j < n; ++j)
1854 y[j] +=
static_cast<T
>(engine.liftWeight[
static_cast<size_t>(j)]) * (end[j] - y[j]);
1861 static constexpr int kLinearBranches = 3;
1862 static constexpr int kLinearDefaultTaps = 4096;
1863 static constexpr int kLinearPartitions = 8;
1869 std::unique_ptr<FFTReal<T>> designFft;
1870 std::vector<T> spectrum, time, taps, window;
1871 std::array<RingBuffer<T>, kMaxChannels> dry;
1872 std::vector<T> weights;
1873 int weightMask = 0, weightPos = 0;
1874 std::array<std::array<bool, kLinearBranches>, MaxBands> pending {};
1875 std::array<bool, MaxBands> played {};
1876 std::array<std::array<T, kDetChunk>, kMaxChannels + 2> lines {};
1878 int kernel = 0,
latency = 0, channels = 0;
1883 [[nodiscard]]
int resolveLinearTaps(
double sampleRate)
const noexcept
1885 int taps = linearTaps_;
1888 taps = kLinearDefaultTaps;
1889 while (taps < 65536 && sampleRate > 1.5 * 48000.0 * (taps / kLinearDefaultTaps)) taps <<= 1;
1892 while (size < taps && size < 65536) size <<= 1;
1897 [[nodiscard]]
unsigned linearMask(
const BandConfig& cfg)
const noexcept
1899 const int channels = linear_->channels;
1904 case Scope::Mid:
return channels >= 2 ? 1u << channels : 1u;
1905 case Scope::Side:
return channels >= 2 ? 1u << (channels + 1) : 0u;
1906 default:
return (1u << channels) - 1u;
1919 void designLinearBranch(
int b,
int branch)
noexcept
1921 LinearEngine& e = *linear_;
1922 const BandState& st = states_[b];
1923 const BandConfig& cfg = st.cfg;
1924 const int index = b * kLinearBranches + branch;
1925 const bool has = branch == 0 ? st.rests : (branch == 1 ? st.cuts : st.lifts);
1928 e.bank.removeKernel(index);
1932 const AnalogSection rest = prototypeOf(cfg,
static_cast<double>(cfg.gain));
1933 const double range = branch == 1 ? -rangeTowards(cfg,
false) : rangeTowards(cfg, true);
1934 const AnalogSection half = prototypeOf(cfg, 0.5 * range);
1935 const double reach = 1.0 + std::pow(10.0, range / 40.0);
1936 const int bins = e.kernel / 2;
1937 const double step = 2.0 * std::numbers::pi * sampleRate_ /
static_cast<double>(e.kernel);
1938 for (
int k = 0; k <= bins; ++k)
1940 const double omega = step *
static_cast<double>(k);
1941 const double atRest = st.rests ? std::sqrt(rest.magnitudeSquared(omega)) : 1.0;
1942 const double level = branch == 0 ? atRest - 1.0
1943 : atRest * reach * (std::sqrt(half.magnitudeSquared(omega)) - 1.0);
1944 e.spectrum[
static_cast<size_t>(2 * k)] =
static_cast<T
>(level);
1945 e.spectrum[
static_cast<size_t>(2 * k + 1)] = T(0);
1947 e.designFft->inverse(e.spectrum.data(), e.time.data());
1949 const int centre = e.kernel / 2;
1950 for (
int m = 0; m < e.kernel; ++m)
1951 e.taps[
static_cast<size_t>(m)] = e.time[
static_cast<size_t>((m + centre) % e.kernel)] * e.window[
static_cast<size_t>(m)];
1952 e.bank.setKernel(index, e.taps.data(), linearMask(cfg));
1960 void serviceLinear(
int nb)
noexcept
1962 LinearEngine& e = *linear_;
1963 for (
int b = 0; b < MaxBands; ++b)
1965 const auto at =
static_cast<size_t>(b);
1966 const bool on = b < nb && playing_[at];
1967 if (on && !e.played[at])
1968 e.pending[at].fill(
true);
1969 if (!on && e.played[at])
1970 for (
int branch = 0; branch < kLinearBranches; ++branch)
1971 e.bank.removeKernel(b * kLinearBranches + branch);
1975 const int total = MaxBands * kLinearBranches;
1976 for (
int step = 0; step < total; ++step)
1978 const int index = (e.next + step) % total;
1979 const int b = index / kLinearBranches, branch = index % kLinearBranches;
1980 if (!e.pending[
static_cast<size_t>(b)][
static_cast<size_t>(branch)])
continue;
1981 e.pending[
static_cast<size_t>(b)][
static_cast<size_t>(branch)] =
false;
1982 if (!e.played[
static_cast<size_t>(b)])
continue;
1983 designLinearBranch(b, branch);
1984 if (e.snap)
continue;
1985 e.next = (index + 1) % total;
2003 void applyLinear(AudioBufferView<T>& audio,
int start,
int n,
int nCh,
int laSamples)
noexcept
2005 LinearEngine& e = *linear_;
2006 const size_t ring =
static_cast<size_t>(e.weightMask) + 1u;
2007 const int lineChannels = std::min(nCh, e.channels);
2008 const bool pair = e.channels >= 2;
2009 const int delay = e.latency - std::clamp(laSamples, 0, e.latency);
2012 for (
int k = 0; k < numFirst_ + numSecond_; ++k)
2014 const int b = k < numFirst_ ? first_[static_cast<size_t>(k)] : second_[
static_cast<size_t>(k - numFirst_)];
2015 const BandState& st = states_[b];
2016 const T* gains = dynDb_[
static_cast<size_t>(b)].data();
2017 T* cuts = e.weights.data() +
static_cast<size_t>(b * 2) * ring;
2018 T* lifts = cuts + ring;
2019 for (
int j = 0; j < n; ++j)
2021 const double d =
static_cast<double>(gains[j]);
2022 const auto at =
static_cast<size_t>((e.weightPos + j) & e.weightMask);
2023 cuts[at] =
static_cast<T
>(cutWeight(st, d));
2024 lifts[at] =
static_cast<T
>(liftWeight(st, d));
2026 meterGainDb_[b].store(gains[n - 1], std::memory_order_relaxed);
2032 const int run = std::min(n - done, e.bank.room());
2036 const T* heard[kMaxChannels + 2] = {};
2037 for (
int ch = 0; ch < lineChannels; ++ch)
2039 const T* x = audio.getChannel(ch) + start + done;
2040 T* line = e.lines[
static_cast<size_t>(ch)].data();
2041 for (
int j = 0; j < run; ++j) line[j] = std::isfinite(x[j]) ? x[j] : T(0);
2044 if (pair && lineChannels >= 1)
2046 T* middle = e.lines[
static_cast<size_t>(kMaxChannels)].data();
2047 T* sides = e.lines[
static_cast<size_t>(kMaxChannels + 1)].data();
2048 const T* left = e.lines[0].data();
2049 if (lineChannels >= 2)
2051 const T* right = e.lines[1].data();
2052 for (
int j = 0; j < run; ++j)
2054 middle[j] = (left[j] + right[j]) * T(0.5);
2055 sides[j] = (left[j] - right[j]) * T(0.5);
2057 heard[e.channels + 1] = sides;
2061 std::copy_n(left, run, middle);
2063 heard[e.channels] = middle;
2067 for (
int ch = 0; ch < nCh; ++ch)
2069 T* y = audio.getChannel(ch) + start + done;
2070 for (
int j = 0; j < run; ++j)
2072 e.dry[ch].push(y[j]);
2073 y[j] = e.dry[ch].read(e.latency);
2078 for (
int k = 0; k < numFirst_ + numSecond_; ++k)
2080 const int b = k < numFirst_ ? first_[static_cast<size_t>(k)] : second_[
static_cast<size_t>(k - numFirst_)];
2081 const Scope where = states_[b].cfg.scope;
2082 const T* sideWeights[kLinearBranches] = {
nullptr, e.weights.data() +
static_cast<size_t>(b * 2) * ring,
2083 e.weights.data() +
static_cast<size_t>(b * 2 + 1) * ring };
2084 for (
int branch = 0; branch < kLinearBranches; ++branch)
2086 const int index = b * kLinearBranches + branch;
2087 if (e.bank.isKernelSilent(index))
continue;
2088 const T* weights = sideWeights[branch];
2089 const auto add = [&](
int line,
int ch, T sign)
noexcept {
2090 if (ch >= nCh)
return;
2091 const T* wet = e.bank.tap(index, line);
2092 T* y = audio.getChannel(ch) + start + done;
2093 if (weights ==
nullptr)
2095 for (
int j = 0; j < run; ++j) y[j] += sign * wet[j];
2098 for (
int j = 0; j < run; ++j)
2099 y[j] += sign * weights[
static_cast<size_t>((e.weightPos + done + j - delay) & e.weightMask)] * wet[j];
2106 add(pair ? e.channels : 0, 0, T(1));
2107 if (pair)
add(e.channels, 1, T(1));
2110 if (pair) {
add(e.channels + 1, 0, T(1));
add(e.channels + 1, 1, T(-1)); }
2113 for (
int ch = 0; ch < lineChannels; ++ch)
add(ch, ch, T(1));
2119 e.bank.push(heard, run);
2122 e.weightPos = (e.weightPos + n) & e.weightMask;
2131 [[nodiscard]]
static T computeTargetGain(
const BandConfig& cfg, T levelDb, T threshold, T knee)
noexcept
2137 const T over = levelDb - threshold;
2138 const T half = knee * T(0.5);
2139 if (cfg.aboveRatio > T(1.001) && over > -half)
2141 const T bent = over < half ? (over + half) * (over + half) / (T(2) * knee) : over;
2142 const T amount = std::min(bent * (T(1) - T(1) / cfg.aboveRatio), cfg.aboveRangeDb);
2143 gainDb += cfg.aboveBoost ? amount : -amount;
2145 if (cfg.belowRatio > T(1.001) && over < half)
2147 const T under = -over;
2148 const T bent = under < half ? (under + half) * (under + half) / (T(2) * knee) : under;
2149 const T amount = std::min(bent * (T(1) - T(1) / cfg.belowRatio), cfg.belowRangeDb);
2150 gainDb += cfg.belowBoost ? amount : -amount;
2155 if (levelDb > threshold)
2157 if (cfg.aboveRatio > T(1.001)) {
2158 T overDb = levelDb - threshold;
2159 T amount = std::min(overDb * (T(1) - T(1) / cfg.aboveRatio), cfg.aboveRangeDb);
2160 gainDb += cfg.aboveBoost ? amount : -amount;
2165 if (cfg.belowRatio > T(1.001)) {
2166 T underDb = threshold - levelDb;
2167 T amount = std::min(underDb * (T(1) - T(1) / cfg.belowRatio), cfg.belowRangeDb);
2168 gainDb += cfg.belowBoost ? amount : -amount;
2175 void cleanBand(
int b)
noexcept
2177 bandDetector_[b].reset();
2178 bandZone_[b].reset();
2179 bandFilter_[b].reset();
2180 bandLadder_[
static_cast<size_t>(b)].
reset();
2181 currentGainDb_[b] = T(0);
2182 BandState& st = states_[b];
2183 st.fast = st.slow = st.heard = 0.0;
2184 st.reference = st.candidate =
static_cast<double>(kMinLevelDb);
2186 st.designed =
false;
2187 if (!analytic_.empty())
2188 for (
int ch = 0; ch < kMaxChannels; ++ch)
2189 analytic_[
static_cast<size_t>(b * kMaxChannels + ch)].reset();
2192 MixedBand& filters = mixedEngine_->bands[
static_cast<size_t>(b)];
2193 filters.rest.reset();
2194 filters.cut.reset();
2195 filters.lift.reset();
2199 const size_t ring =
static_cast<size_t>(linear_->weightMask) + 1u;
2200 std::fill_n(linear_->weights.begin() +
static_cast<std::ptrdiff_t
>(
static_cast<size_t>(b * 2) * ring),
2206 void restartBand(
int b)
noexcept
2209 meterGainDb_[b].store(T(0), std::memory_order_relaxed);
2210 paramsDirty_[b].store(
true, std::memory_order_release);
2214 [[nodiscard]]
static std::complex<double> responseOf(
const BiquadCoeffs& c,
double hz,
double fs)
noexcept
2216 const double w = 2.0 * std::numbers::pi * std::clamp(hz, 0.0, 0.5 * fs) / fs;
2217 const std::complex<double> z1 = std::polar(1.0, -w), z2 = std::polar(1.0, -2.0 * w);
2218 return (c.b0 + c.b1 * z1 + c.b2 * z2) / (1.0 + c.a1 * z1 + c.a2 * z2);
2222 [[nodiscard]]
static double rangeTowards(
const BandConfig& cfg,
bool boost)
noexcept
2225 if (cfg.aboveRatio > T(1.001) && cfg.aboveBoost == boost)
2226 range = std::max(range,
static_cast<double>(cfg.aboveRangeDb));
2227 if (cfg.belowRatio > T(1.001) && cfg.belowBoost == boost)
2228 range = std::max(range,
static_cast<double>(cfg.belowRangeDb));
2233 [[nodiscard]]
static AnalogSection prototypeOf(
const BandConfig& cfg,
double gainDb)
noexcept
2235 const double hz = std::clamp(
static_cast<double>(cfg.frequency), 1.0, 80000.0);
2236 const double q = std::max(
static_cast<double>(cfg.q), 0.025);
2237 const double gain = std::clamp(gainDb, -96.0, 96.0);
2247 [[nodiscard]] BiquadCoeffs designShape(
const BandConfig& cfg,
double gainDb)
const noexcept
2255 bool rests =
false, cuts =
false, lifts =
false;
2256 double cutRange = 0.0, liftRange = 0.0;
2259 [[nodiscard]]
static Sides sidesOf(
const BandConfig& cfg)
noexcept
2262 s.cutRange = rangeTowards(cfg,
false);
2263 s.liftRange = rangeTowards(cfg,
true);
2264 s.cuts = s.cutRange > kRestDb;
2265 s.lifts = s.liftRange > kRestDb;
2266 s.rests = std::abs(
static_cast<double>(cfg.gain)) > kRestDb;
2283 static void adoptSides(BandState& st,
const Sides& s,
int ramp = 0) noexcept
2285 const double cutGoal = s.cuts ? 1.0 - std::pow(10.0, -s.cutRange / 20.0) : 0.0;
2286 const double liftGoal = s.lifts ? std::pow(10.0, s.liftRange / 20.0) - 1.0 : 0.0;
2288 const bool glide = ramp > 0 && st.designed;
2289 const bool cutGlides = glide && st.cuts && s.cuts, liftGlides = glide && st.lifts && s.lifts;
2290 st.cutReachGoal = cutGoal;
2291 st.liftReachGoal = liftGoal;
2292 if (!cutGlides) st.cutReach = cutGoal;
2293 if (!liftGlides) st.liftReach = liftGoal;
2294 st.reachRamp = (cutGlides || liftGlides) ? ramp : 0;
2295 st.cutReachStep = st.reachRamp > 0 ? (cutGoal - st.cutReach) / st.reachRamp : 0.0;
2296 st.liftReachStep = st.reachRamp > 0 ? (liftGoal - st.liftReach) / st.reachRamp : 0.0;
2300 st.cutScale = st.cutReach > 0.0 ? 1.0 / st.cutReach : 0.0;
2301 st.liftScale = st.liftReach > 0.0 ? 1.0 / st.liftReach : 0.0;
2306 static void stepReach(BandState& st)
noexcept
2308 if (--st.reachRamp == 0)
2310 st.cutReach = st.cutReachGoal;
2311 st.liftReach = st.liftReachGoal;
2315 st.cutReach += st.cutReachStep;
2316 st.liftReach += st.liftReachStep;
2318 st.cutScale = st.cutReach > 0.0 ? 1.0 / st.cutReach : 0.0;
2319 st.liftScale = st.liftReach > 0.0 ? 1.0 / st.liftReach : 0.0;
2323 [[nodiscard]]
static double cutWeight(
const BandState& st,
double d)
noexcept
2325 return (d < 0.0 && st.cuts) ? std::min((1.0 -
fastPow10(d * 0.05)) * st.cutScale, 1.0) : 0.0;
2329 [[nodiscard]]
static double liftWeight(
const BandState& st,
double d)
noexcept
2331 return (d > 0.0 && st.lifts) ? std::min((
fastPow10(d * 0.05) - 1.0) * st.liftScale, 1.0) : 0.0;
2350 [[nodiscard]] BiquadCoeffs designEnd(
const BandConfig& cfg,
double rangeDb)
const noexcept
2352 const BiquadCoeffs half = designShape(cfg, 0.5 * rangeDb);
2353 const double reach = 1.0 + std::pow(10.0, rangeDb / 40.0);
2354 BiquadCoeffs end = half;
2355 end.b0 = 1.0 + reach * (half.b0 - 1.0);
2356 end.b1 = half.a1 + reach * (half.b1 - half.a1);
2357 end.b2 = half.a2 + reach * (half.b2 - half.a2);
2362 void designMixedBand(
int b,
double fs)
noexcept
2364 BandState& st = states_[b];
2365 const BandConfig& cfg = st.cfg;
2366 MixedBand& filters = mixedEngine_->bands[
static_cast<size_t>(b)];
2367 const double rest =
static_cast<double>(cfg.gain);
2368 const Sides sides = sidesOf(cfg);
2369 const bool rests = sides.rests, cuts = sides.cuts, lifts = sides.lifts;
2370 const double cutRange = sides.cutRange, liftRange = sides.liftRange;
2374 const int ramp =
static_cast<int>(kMixedRampMs * 0.001 * fs);
2375 const auto place = [&](BiquadCascade<T, 1, kMaxChannels>& filter,
const BiquadCoeffs& design,
bool was,
bool is)
noexcept {
2377 if (!was || !st.designed) filter.reset();
2378 filter.rampCoeffs(0, design, was && st.designed ? ramp : 0);
2380 if (rests) place(filters.rest, designShape(cfg, rest), st.rests, rests);
2381 if (cuts) place(filters.cut, designEnd(cfg, -cutRange), st.cuts, cuts);
2382 if (lifts) place(filters.lift, designEnd(cfg, liftRange), st.lifts, lifts);
2384 adoptSides(st, sides, ramp);
2387 void updateBandInternalState(
int b,
double fs)
noexcept
2397 if (!staged_[
static_cast<size_t>(b)].tryRead(cfg))
2402 paramsDirty_[b].store(
true, std::memory_order_release);
2406 BandState& st = states_[b];
2407 const BandConfig old = st.cfg;
2412 if ((cfg.enabled && !old.enabled) || cfg.scope != old.scope)
2417 const bool moved = cfg.frequency != old.frequency || cfg.q != old.q || cfg.shape != old.shape
2418 || cfg.timing != old.timing
2419 || cfg.listenLow != old.listenLow || cfg.listenHigh != old.listenHigh;
2420 if (cfg.relativeThreshold && old.relativeThreshold && moved)
2424 bandDetector_[b].reset();
2425 bandZone_[b].reset();
2426 for (
int ch = 0; ch < kMaxChannels; ++ch)
2427 analytic_[
static_cast<size_t>(b * kMaxChannels + ch)].reset();
2428 st.fast = st.slow = st.heard = 0.0;
2429 st.candidate =
static_cast<double>(kMinLevelDb);
2430 st.watching = std::max(1,
static_cast<int>(kReferenceWatch * fs));
2432 else if (cfg.relativeThreshold && !old.relativeThreshold)
2434 st.reference = st.candidate =
static_cast<double>(kMinLevelDb);
2437 st.referenceFall = kReferenceFallDb / fs;
2445 const DetectorDesign detector = designDetector(cfg, fs);
2446 bandDetector_[b].setCoeffsNow(detector.first);
2447 bandZone_[b].setCoeffsNow(detector.second);
2448 if (detector.two && !st.zoned) bandZone_[b].reset();
2449 st.zoned = detector.two;
2451 auto calcCoeff = [fs](T ms) -> T {
2452 const double tauSec = std::max(
static_cast<double>(ms), 0.01) / 1000.0;
2453 return static_cast<T
>(1.0 - std::exp(-1.0 / (fs * tauSec)));
2456 st.aboveAtkCoeff = calcCoeff(cfg.aboveAttackMs);
2457 st.aboveRelCoeff = calcCoeff(cfg.aboveReleaseMs);
2458 st.belowAtkCoeff = calcCoeff(cfg.belowAttackMs);
2459 st.belowRelCoeff = calcCoeff(cfg.belowReleaseMs);
2462 const double unit = detector.unit;
2463 const auto pole = [fs](
double seconds)
noexcept {
return 1.0 - std::exp(-1.0 / (fs * seconds)); };
2464 const double fastFall = std::max(kFastFallUnits * unit, kFastFallFloor);
2465 st.fastUp = pole(std::max(unit, kFastRiseFloor));
2466 st.fastDown = pole(fastFall);
2467 st.slowUp = pole(std::max(kSlowRiseUnits * unit, kSlowRiseFloor));
2468 st.slowDown = pole(std::max(kSlowFallUnits * unit, kSlowFallFloor));
2469 st.lastDown = pole(kLastFallShare * fastFall);
2474 if (cfg.knee >= T(0))
2480 const double range = std::max(rangeTowards(cfg,
false), rangeTowards(cfg,
true));
2482 ? 2.0 / std::numbers::ln2 * std::asinh(0.5 / std::max(
static_cast<double>(cfg.q), 0.05))
2484 st.knee =
static_cast<T
>(std::min(kKneePerOctave * (1.0 + octaves) * std::sqrt(range / 12.0), 40.0));
2490 adoptSides(st, sidesOf(cfg));
2491 linear_->pending[
static_cast<size_t>(b)].fill(
true);
2493 else if (mixedActive_)
2495 designMixedBand(b, fs);
2499 void updateLookahead() noexcept
2501 if (sampleRate_ > 0) {
2502 int samples =
static_cast<int>(sampleRate_ * runFactor() * lookaheadMs_ / T(1000));
2503 lookaheadSamples_.store(samples, std::memory_order_relaxed);
2508 std::atomic<bool> isPrepared_ {
false };
2510 double sampleRate_ = 0;
2512 std::atomic<int> numBands_ { 0 };
2526 std::atomic<T> frequency { T(1000) };
2527 std::atomic<T> q { T(1.0) };
2528 std::atomic<T> threshold { T(-20) };
2530 std::atomic<bool> enabled {
true };
2531 std::atomic<T> gain { T(0) };
2532 std::atomic<T> knee { T(0) };
2535 std::atomic<bool> relative {
false };
2536 std::atomic<T> listenLow { T(0) };
2537 std::atomic<T> listenHigh { T(0) };
2539 std::atomic<T> aboveRatio { T(1) };
2540 std::atomic<T> aboveAttackMs { T(5) };
2541 std::atomic<T> aboveReleaseMs { T(50) };
2542 std::atomic<T> aboveRangeDb { T(12) };
2543 std::atomic<bool> aboveBoost {
false };
2545 std::atomic<T> belowRatio { T(1) };
2546 std::atomic<T> belowAttackMs { T(10) };
2547 std::atomic<T> belowReleaseMs { T(100) };
2548 std::atomic<T> belowRangeDb { T(12) };
2549 std::atomic<bool> belowBoost {
false };
2551 std::atomic<unsigned> seq { 0 };
2564 void publish(
const BandConfig& c)
noexcept
2566 seq.fetch_add(1, std::memory_order_acq_rel);
2567 std::atomic_thread_fence(std::memory_order_release);
2568 frequency.store(c.frequency, std::memory_order_relaxed);
2569 q.store(c.q, std::memory_order_relaxed);
2570 threshold.store(c.threshold, std::memory_order_relaxed);
2571 shape.store(
static_cast<int>(c.shape), std::memory_order_relaxed);
2572 enabled.store(c.enabled, std::memory_order_relaxed);
2573 gain.store(c.gain, std::memory_order_relaxed);
2574 knee.store(c.knee, std::memory_order_relaxed);
2575 scope.store(
static_cast<int>(c.scope), std::memory_order_relaxed);
2576 timing.store(
static_cast<int>(c.timing), std::memory_order_relaxed);
2577 relative.store(c.relativeThreshold, std::memory_order_relaxed);
2578 listenLow.store(c.listenLow, std::memory_order_relaxed);
2579 listenHigh.store(c.listenHigh, std::memory_order_relaxed);
2580 aboveRatio.store(c.aboveRatio, std::memory_order_relaxed);
2581 aboveAttackMs.store(c.aboveAttackMs, std::memory_order_relaxed);
2582 aboveReleaseMs.store(c.aboveReleaseMs, std::memory_order_relaxed);
2583 aboveRangeDb.store(c.aboveRangeDb, std::memory_order_relaxed);
2584 aboveBoost.store(c.aboveBoost, std::memory_order_relaxed);
2585 belowRatio.store(c.belowRatio, std::memory_order_relaxed);
2586 belowAttackMs.store(c.belowAttackMs, std::memory_order_relaxed);
2587 belowReleaseMs.store(c.belowReleaseMs, std::memory_order_relaxed);
2588 belowRangeDb.store(c.belowRangeDb, std::memory_order_relaxed);
2589 belowBoost.store(c.belowBoost, std::memory_order_relaxed);
2590 seq.fetch_add(1, std::memory_order_release);
2598 static constexpr int kSeqlockMaxAttempts = 3;
2606 void loadWordsRelaxed(BandConfig& c)
const noexcept
2608 c.frequency = frequency.load(std::memory_order_relaxed);
2609 c.q = q.load(std::memory_order_relaxed);
2610 c.threshold = threshold.load(std::memory_order_relaxed);
2611 c.shape =
static_cast<BandShape>(shape.load(std::memory_order_relaxed));
2612 c.enabled = enabled.load(std::memory_order_relaxed);
2613 c.gain = gain.load(std::memory_order_relaxed);
2614 c.knee = knee.load(std::memory_order_relaxed);
2615 c.scope =
static_cast<Scope>(scope.load(std::memory_order_relaxed));
2616 c.timing =
static_cast<Timing>(timing.load(std::memory_order_relaxed));
2617 c.relativeThreshold = relative.load(std::memory_order_relaxed);
2618 c.listenLow = listenLow.load(std::memory_order_relaxed);
2619 c.listenHigh = listenHigh.load(std::memory_order_relaxed);
2620 c.aboveRatio = aboveRatio.load(std::memory_order_relaxed);
2621 c.aboveAttackMs = aboveAttackMs.load(std::memory_order_relaxed);
2622 c.aboveReleaseMs = aboveReleaseMs.load(std::memory_order_relaxed);
2623 c.aboveRangeDb = aboveRangeDb.load(std::memory_order_relaxed);
2624 c.aboveBoost = aboveBoost.load(std::memory_order_relaxed);
2625 c.belowRatio = belowRatio.load(std::memory_order_relaxed);
2626 c.belowAttackMs = belowAttackMs.load(std::memory_order_relaxed);
2627 c.belowReleaseMs = belowReleaseMs.load(std::memory_order_relaxed);
2628 c.belowRangeDb = belowRangeDb.load(std::memory_order_relaxed);
2629 c.belowBoost = belowBoost.load(std::memory_order_relaxed);
2652 [[nodiscard]]
bool tryRead(BandConfig& out)
const noexcept
2654 for (
int attempt = 0; attempt < kSeqlockMaxAttempts; ++attempt)
2656 const unsigned s0 = seq.load(std::memory_order_acquire);
2657 if ((s0 & 1u) != 0u)
continue;
2659 loadWordsRelaxed(c);
2660 std::atomic_thread_fence(std::memory_order_acquire);
2661 if (s0 == seq.load(std::memory_order_relaxed))
2685 [[nodiscard]] BandConfig read() const noexcept
2690 s0 = seq.load(std::memory_order_acquire);
2691 loadWordsRelaxed(c);
2692 std::atomic_thread_fence(std::memory_order_acquire);
2693 s1 = seq.load(std::memory_order_relaxed);
2694 }
while ((s0 & 1u) != 0u || s0 != s1);
2700 std::array<StagedBand, MaxBands> staged_ {};
2706 std::array<BandConfig, MaxBands> masterConfigs_ {};
2707 std::array<std::atomic<bool>, MaxBands> paramsDirty_ {};
2708 std::array<BandState, MaxBands> states_ {};
2709 unsigned refreshCounter_ = 0;
2710 int lastNumBands_ = MaxBands;
2715 std::array<Biquad<T, kMaxChannels>, MaxBands> bandDetector_ {};
2716 std::array<Biquad<T, kMaxChannels>, MaxBands> bandZone_ {};
2717 std::array<Biquad<T, kMaxChannels>, MaxBands> bandFilter_ {};
2718 std::array<NormalizedLadder<T, kMaxChannels>, MaxBands> bandLadder_ {};
2720 bool ladderActive_ =
false;
2721 std::array<T, MaxBands> currentGainDb_ {};
2722 std::array<std::atomic<T>, MaxBands> meterGainDb_ {};
2724 int oversamplingFactor_ = 1;
2725 bool oversamplingAutomatic_ =
false;
2726 static constexpr double kAutomaticInternalRate = 88200.0;
2727 std::unique_ptr<Oversampling<T>> oversampler_;
2728 std::unique_ptr<Oversampling<T>> oversamplerSc_;
2730 T lookaheadMs_ = T(0);
2731 std::atomic<int> lookaheadSamples_ { 0 };
2732 std::array<RingBuffer<T>, kMaxChannels> lookaheadBuf_ {};
2733 std::vector<HilbertIIR<double>> analytic_;
2734 std::array<double, kDetChunk> detBand_ {};
2735 std::array<double, kDetChunk> detMag_ {};
2736 std::array<double, kDetChunk> pairBand_ {};
2737 std::array<double, kDetChunk> pairMag_ {};
2738 std::array<std::array<double, 3>, MaxBands> bandCov_ {};
2740 bool dominantLink_ =
false;
2741 double linkRate_ = 0.0;
2742 double linkCoeff_ = 0.0;
2743 std::array<std::array<T, kDetChunk>, MaxBands> detLevelDb_ {};
2744 std::array<double, kDetChunk> detLinear_ {};
2745 std::array<std::array<T, kDetChunk>, MaxBands> dynDb_ {};
2747 std::array<bool, MaxBands> playing_ {};
2748 std::array<int, MaxBands> first_ {};
2749 std::array<int, MaxBands> second_ {};
2750 int numFirst_ = 0, numSecond_ = 0;
2752 std::atomic<bool> mixedWanted_ {
false };
2753 static constexpr int kRemovalCapacity = 32768;
2754 std::atomic<bool> removalListen_ {
false };
2755 std::array<RingBuffer<T>, kMaxChannels> deltaRing_ {};
2758 std::atomic<int> keyListen_ { -1 };
2759 int listenBand_ = -1;
2760 double listenWeight_ = 0.0, listenGoal_ = 0.0;
2761 bool listenDesigned_ =
false, listenTwo_ =
false;
2762 BandConfig listenCfg_ {};
2763 Biquad<T, kMaxChannels> listenFirst_ {}, listenSecond_ {};
2764 std::array<RingBuffer<T>, kMaxChannels> listenRing_ {};
2765 std::vector<T> listenHeard_;
2766 int listenChannels_ = 0, listenBlock_ = 0, listenReach_ = 0;
2768 bool mixedActive_ =
false;
2769 std::unique_ptr<MixedEngine> mixedEngine_;
2770 std::unique_ptr<LinearEngine> linear_;
2771 bool linearWanted_ =
false;
2772 int linearTaps_ = 0;
2773 FilterDesign::Band designBand_;