161 initDefaultFrequencies();
184 numChannels_ = std::min(
static_cast<int>(spec.
numChannels), 16);
195 lpDesignFft_.reset();
201 const double wanted = spec.
sampleRate * kLpFirSeconds;
202 while (firLen < wanted && firLen < kLpMaxFirLength) firLen <<= 1;
204 lpPart_ = firLen / kLpPartitions;
207 lpLatency_ = firLength_ / 2 + kLpLeadBlocks * lpPart_;
209 const size_t specLen =
static_cast<size_t>(2 * lpPart_ + 2);
210 const size_t chans =
static_cast<size_t>(numChannels_);
211 const size_t designBins =
static_cast<size_t>(firLength_ + 1);
215 lpKernelSpec_.assign(2 *
static_cast<size_t>(kMaxSplits * kLpPartitions) * specLen, T(0));
216 lpPartOn_.assign(2 *
static_cast<size_t>(kMaxSplits * kLpPartitions), uint8_t(0));
217 lpFdl_.assign(chans *
static_cast<size_t>(kLpPartitions) * specLen, T(0));
218 lpRing_.assign(chans *
static_cast<size_t>(kLpRingBlocks * lpPart_), T(0));
219 lpOut_.assign(chans * 2 *
static_cast<size_t>(MaxBands * lpPart_), T(0));
220 lpAccum_.assign(specLen, T(0));
221 lpResult_.assign(
static_cast<size_t>(2 * lpPart_), T(0));
225 const double part =
static_cast<double>(lpPart_);
226 lpCostFft_ = (kLpFftUnitsPerPoint + kLpCopyUnitsPerSample) * 2.0 * part;
227 lpCostCma_ = part + 1.0;
228 lpCostIfft_ = kLpFftUnitsPerPoint * 2.0 * part + kLpCopyUnitsPerSample * part;
229 lpCostResidualPerBand_ = kLpResidualUnitsPerSample * part;
230 lpCostMagChunk_ = kLpMagUnitsPerBin *
static_cast<double>((designBins + kLpMagChunks - 1) / kLpMagChunks);
231 lpCostPartitionStep_ = lpCostIfft_ + kLpCopyUnitsPerSample * part;
235 lpCarry_.assign(designBins, 1.0);
236 lpBinFreq_.assign(designBins, 0.0);
237 lpMag_.assign(designBins, T(0));
238 for (
size_t k = 0; k < designBins; ++k)
239 lpBinFreq_[k] = spec.
sampleRate *
static_cast<double>(k) /
static_cast<double>(2 * firLength_);
241 lpWindow_.assign(
static_cast<size_t>(firLength_), T(1));
242 const int taper = firLength_ / 4;
243 for (
int i = 0; i < firLength_; ++i)
245 const int edge = std::min(i, firLength_ - 1 - i);
247 lpWindow_[
static_cast<size_t>(i)] =
static_cast<T
>(
248 0.5 - 0.5 * std::cos(std::numbers::pi * (
static_cast<double>(edge) + 0.5)
249 /
static_cast<double>(taper)));
251 lpTimeResponse_.assign(
static_cast<size_t>(2 * firLength_ + 2), T(0));
252 lpFirKernel_.assign(
static_cast<size_t>(2 * firLength_), T(0));
253 lpScratch_.assign(
static_cast<size_t>(2 * lpPart_), T(0));
255 lpDesignFft_ = std::make_unique<FFTReal<T>>(2 * firLength_);
256 lpDesignFftSteps_ = lpDesignFft_->getInverseStepCount();
257 lpCostDesignFftStep_ = kLpDesignFftUnitsPerPoint * 2.0 *
static_cast<double>(firLength_)
258 /
static_cast<double>(lpDesignFftSteps_);
259 lpFft_ = std::make_unique<FFTReal<T>>(2 * lpPart_);
266 for (
auto& k : banks_)
268 if (!k) k = std::make_unique<IirBank>();
269 k->allpass.resize(
static_cast<size_t>(MaxBands * kMaxSplits));
274 ladderTmp_.assign(
static_cast<size_t>(spec.
maxBlockSize), 0.0);
275 ladderActive_ =
false;
280 fadeLength_ = std::max(1,
static_cast<int>(std::lround(kFadeSeconds * spec.
sampleRate)));
281 fadeBands_.assign(
static_cast<size_t>(MaxBands * numChannels_ * kFadeChunk), T(0));
282 fadeLower_.assign(
static_cast<size_t>(MaxBands * numChannels_ * kFadeChunk), T(0));
283 fadeTop_.assign(
static_cast<size_t>(numChannels_ * kFadeChunk), T(0));
284 lpFadeOld_.assign(
static_cast<size_t>(MaxBands) *
static_cast<size_t>(lpPart_), T(0));
289 for (
int i = 0; i < kMaxSplits; ++i)
291 freqSmoothers_[i].prepare(spec.
sampleRate, 5.0);
293 T target = targetFrequencies_[i].load(std::memory_order_relaxed);
294 freqSmoothers_[i].reset(target);
295 frequencies_[i] = target;
298 lastMode_ = filterMode_.load(std::memory_order_relaxed);
299 dirty_.store(
true, std::memory_order_relaxed);
303 lpSetBands_ = { 0, 0 };
304 lpSetParts_ = { 0, 0 };
305 lpDesignActive_ =
false;
306 lpDesignRedo_ =
false;
307 if (lpFft_ !=
nullptr) designLinearPhaseNow();
337 if (!prepared_ || bandOutputs ==
nullptr)
return 0;
339 const FilterMode mode = filterMode_.load(std::memory_order_relaxed);
341 if (mode != lastMode_)
348 if (lpActive) syncFrequenciesToTargets();
352 if (freqUpdatePending_.exchange(
false, std::memory_order_acquire))
358 syncFrequenciesToTargets();
359 lpMagDirty_.store(
true, std::memory_order_relaxed);
363 for (
int i = 0; i < kMaxSplits; ++i)
364 freqSmoothers_[i].setTargetValue(targetFrequencies_[i].load(std::memory_order_relaxed));
370 const bool ladder = structure_.load(std::memory_order_relaxed) ==
Structure::Ladder && !bank(0).ladders.empty();
371 if (!lpActive && ladder != ladderActive_)
373 ladderActive_ = ladder;
376 if (ladder) updateCoefficients();
381 if (!lpActive && dirty_.load(std::memory_order_relaxed) &&
382 dirty_.exchange(
false, std::memory_order_acquire))
384 updateCoefficients();
387 const int n = std::min(numOutputBands, numBands_.load(std::memory_order_relaxed));
394 int nS = std::min(input.getNumSamples(), spec_.
maxBlockSize);
395 int nCh = std::min(input.getNumChannels(), numChannels_);
396 for (
int b = 0; b < n; ++b)
398 nS = std::min(nS, bandOutputs[b].getNumSamples());
399 nCh = std::min(nCh, bandOutputs[b].getNumChannels());
401 if (nS <= 0)
return 0;
405 processLinearPhase(input, bandOutputs, n, nS, nCh);
406 passExtraChannels(input, bandOutputs, n, nS, nCh);
412 const Topology topology = topology_.load(std::memory_order_relaxed);
418 else if (fadeLeft_ == 0 && (n != iirBands_ || topology != iirTopo_)) startBandFade(n, topology);
419 const int written = std::min(iirBands_, numOutputBands);
420 processIIR(input, bandOutputs, written, nS, nCh);
421 passExtraChannels(input, bandOutputs, written, nS, nCh);
445 numBands_.store(std::clamp(n, 2, MaxBands), std::memory_order_relaxed);
446 initDefaultFrequencies();
447 dirty_.store(
true, std::memory_order_release);
448 lpMagDirty_.store(
true, std::memory_order_release);
464 const int bands = std::clamp(n, 2, MaxBands);
465 for (
int s = 1; s < bands - 1; ++s)
467 const T below = targetFrequencies_[
static_cast<size_t>(s - 1)].load(std::memory_order_relaxed);
468 const T at = targetFrequencies_[
static_cast<size_t>(s)].load(std::memory_order_relaxed);
470 targetFrequencies_[
static_cast<size_t>(s)].store(
471 std::sqrt(std::max(below, T(1)) * T(20000)), std::memory_order_relaxed);
473 numBands_.store(bands, std::memory_order_relaxed);
474 freqUpdatePending_.store(
true, std::memory_order_release);
475 dirty_.store(
true, std::memory_order_release);
476 lpMagDirty_.store(
true, std::memory_order_release);
487 if (!std::isfinite(freqHz))
return;
488 if (index >= 0 && index < numBands_.load(std::memory_order_relaxed) - 1)
490 freqHz = std::max(freqHz, T(1));
493 std::array<T, kMaxSplits> localTargets;
494 for (
int i = 0; i < kMaxSplits; ++i)
495 localTargets[i] = targetFrequencies_[i].load(std::memory_order_relaxed);
497 localTargets[
static_cast<size_t>(index)] = freqHz;
505 const int activeSplits = std::clamp(numBands_.load(std::memory_order_relaxed) - 1, 0, kMaxSplits);
506 for (
int i = 1; i < activeSplits; ++i)
508 const T key = localTargets[
static_cast<size_t>(i)];
510 while (j >= 0 && localTargets[
static_cast<size_t>(j)] > key)
512 localTargets[
static_cast<size_t>(j + 1)] = localTargets[
static_cast<size_t>(j)];
515 localTargets[
static_cast<size_t>(j + 1)] = key;
518 for (
int i = 0; i < kMaxSplits; ++i)
519 targetFrequencies_[i].store(localTargets[
static_cast<size_t>(i)], std::memory_order_relaxed);
521 freqUpdatePending_.store(
true, std::memory_order_release);
522 dirty_.store(
true, std::memory_order_release);
523 lpMagDirty_.store(
true, std::memory_order_release);
538 if (order == 6 || order == 12 || order == 24 || order == 36 || order == 48)
540 order_.store(order, std::memory_order_relaxed);
541 dirty_.store(
true, std::memory_order_release);
542 lpMagDirty_.store(
true, std::memory_order_release);
549 const int m = std::clamp(
static_cast<int>(mode), 0, 1);
550 filterMode_.store(
static_cast<FilterMode>(m), std::memory_order_relaxed);
551 dirty_.store(
true, std::memory_order_release);
552 lpMagDirty_.store(
true, std::memory_order_release);
584 structure_.store(
static_cast<Structure>(std::clamp(
static_cast<int>(structure), 0, 1)),
585 std::memory_order_relaxed);
618 topology_.store(
static_cast<Topology>(std::clamp(
static_cast<int>(topology), 0, 1)), std::memory_order_relaxed);
619 dirty_.store(
true, std::memory_order_release);
627 [[nodiscard]]
int getNumBands() const noexcept {
return numBands_.load(std::memory_order_relaxed); }
628 [[nodiscard]]
int getOrder() const noexcept {
return order_.load(std::memory_order_relaxed); }
634 if (index < 0 || index >= kMaxSplits)
return T(0);
635 return targetFrequencies_[
static_cast<size_t>(index)].load(std::memory_order_relaxed);
653 for (
auto& k : banks_)
654 if (k) resetBank(*k);
657 std::fill(lpRing_.begin(), lpRing_.end(), T(0));
658 std::fill(lpFdl_.begin(), lpFdl_.end(), T(0));
659 std::fill(lpOut_.begin(), lpOut_.end(), T(0));
663 lpOutBands_ = lpSetBands_[
static_cast<size_t>(lpActiveSet_)];
681 [[nodiscard]] std::vector<uint8_t>
getState()
const
685 w.
write(
"numBands", n);
691 for (
int i = 0; i < n - 1; ++i)
693 std::snprintf(key,
sizeof(key),
"x%d", i);
711 for (
int i = 0; i < n - 1; ++i)
713 std::snprintf(key,
sizeof(key),
"x%d", i);
714 const float f = r.
read(key, -1.0f);
721 static constexpr int kMaxSplits = MaxBands - 1;
722 static constexpr int kMaxStagesPerFilter = 4;
723 static constexpr double kLpFirSeconds = 4096.0 / 48000.0;
724 static constexpr int kLpMaxFirLength = 1 << 16;
725 static constexpr int kLpPartitions = 8;
726 static_assert(kLpPartitions % 2 == 0,
"the FIR centre must fall on a partition boundary");
730 static constexpr int kLpLeadBlocks = 2;
735 static constexpr int kLpRingBlocks = 8;
736 static_assert(kLpRingBlocks >= kLpPartitions / 2 + 2
737 && (kLpRingBlocks & (kLpRingBlocks - 1)) == 0,
738 "the input history must hold the residual delay and the staged block");
739 static constexpr int kGlideGrid = 32;
740 static constexpr double kFadeSeconds = 0.02;
741 static constexpr int kFadeChunk = 64;
746 static constexpr double kLpFftUnitsPerPoint = 2.4;
747 static constexpr double kLpDesignFftUnitsPerPoint = 2.7;
748 static constexpr double kLpMagUnitsPerBin = 3.4;
749 static constexpr double kLpCopyUnitsPerSample = 0.1;
750 static constexpr double kLpResidualUnitsPerSample = 0.35;
751 static constexpr int kLpMagChunks = 4;
754 static constexpr double kLpSkipL1 = 1e-6;
758 static constexpr double kLpDesignShare = 0.45;
768 BiquadCascade<T, kMaxStagesPerFilter, 16> stages;
780 [[nodiscard]]
static BiquadCoeffs makeFirstOrderAllPass(
double sampleRate,
double freq)
noexcept
782 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
783 const double w = std::tan(std::numbers::pi * freq / sampleRate);
784 const double a = (w - 1.0) / (w + 1.0);
794 void initDefaultFrequencies() noexcept
796 int numSplits = numBands_.load(std::memory_order_relaxed) - 1;
797 const T logMin = std::log(T(100));
798 const T logMax = std::log(T(10000));
800 for (
int s = 0; s < numSplits; ++s)
802 T t =
static_cast<T
>(s + 1) /
static_cast<T
>(numSplits + 1);
803 targetFrequencies_[s].store(std::exp(logMin + t * (logMax - logMin)), std::memory_order_relaxed);
805 freqUpdatePending_.store(
true, std::memory_order_release);
811 void syncFrequenciesToTargets() noexcept
813 for (
int i = 0; i < kMaxSplits; ++i)
815 const T t = targetFrequencies_[i].load(std::memory_order_relaxed);
817 freqSmoothers_[i].reset(t);
821 [[nodiscard]]
static double clampSplitFreq(
double f,
double sr)
noexcept
823 return std::clamp(f, 20.0, sr * 0.499);
826 void updateCoefficients(
int ladderRamp = 0) noexcept
828 if (spec_.
sampleRate <= 0 || !banks_[0])
return;
830 int numSplits = numBands_.load(std::memory_order_relaxed) - 1;
845 switch (order_.load(std::memory_order_relaxed))
851 numStagesPerFilter_ = 1;
852 numStagesAllpass_ = 0;
853 for (
int s = 0; s < numSplits; ++s)
855 double f = clampSplitFreq(
static_cast<double>(frequencies_[s]), sr);
869 numStagesPerFilter_ = 4;
870 numStagesAllpass_ = 2;
871 for (
int s = 0; s < numSplits; ++s)
873 double f = clampSplitFreq(
static_cast<double>(frequencies_[s]), sr);
878 auto hp1Inverted = hp1;
879 hp1Inverted.b0 = -hp1Inverted.b0;
880 hp1Inverted.b1 = -hp1Inverted.b1;
881 hp1Inverted.b2 = -hp1Inverted.b2;
882 setSplitSection(s, 0, lp1, hp1Inverted, ladderRamp);
883 setSplitSection(s, 1, lp2, hp2, ladderRamp);
884 setSplitSection(s, 2, lp1, hp1, ladderRamp);
885 setSplitSection(s, 3, lp2, hp2, ladderRamp);
886 const auto ap1 = makeFirstOrderAllPass(sr, f);
888 setCorrection(s, numSplits, 0, ap1, ladderRamp);
889 setCorrection(s, numSplits, 1, ap2, ladderRamp);
894 numStagesPerFilter_ = 2;
895 numStagesAllpass_ = 1;
896 for (
int s = 0; s < numSplits; ++s)
898 double f = clampSplitFreq(
static_cast<double>(frequencies_[s]), sr);
901 auto apC = makeFirstOrderAllPass(sr, f);
912 setSplitSection(s, 0, lpC, hpC0, ladderRamp);
913 setSplitSection(s, 1, lpC, hpC, ladderRamp);
914 setCorrection(s, numSplits, 0, apC, ladderRamp);
919 numStagesPerFilter_ = 2;
920 numStagesAllpass_ = 1;
921 for (
int s = 0; s < numSplits; ++s)
923 double f = clampSplitFreq(
static_cast<double>(frequencies_[s]), sr);
926 constexpr double qB = 0.70710678118654752440;
931 for (
int st = 0; st < 2; ++st)
932 setSplitSection(s, st, lpC, hpC, ladderRamp);
933 setCorrection(s, numSplits, 0, apC, ladderRamp);
939 numStagesPerFilter_ = 4;
940 numStagesAllpass_ = 2;
944 constexpr double q1 = 0.54119610014619698440;
945 constexpr double q2 = 1.30656296487637652786;
946 const double qArr[4] = { q1, q2, q1, q2 };
948 for (
int s = 0; s < numSplits; ++s)
950 double f = clampSplitFreq(
static_cast<double>(frequencies_[s]), sr);
952 for (
int st = 0; st < 4; ++st)
957 setCorrection(s, numSplits, 0, ap1, ladderRamp);
958 setCorrection(s, numSplits, 1, ap2, ladderRamp);
966 const bool tree = treeInUse();
967 for (
int i = 0; i < 2; ++i)
969 if (i != bank_ && i != fadeBank_)
continue;
970 IirBank& k = bank(i);
971 for (
int s = 0; s < numSplits; ++s)
973 k.splits[
static_cast<size_t>(s)].lp.setNumSections(numStagesPerFilter_);
974 k.splits[
static_cast<size_t>(s)].hp.setNumSections(numStagesPerFilter_);
975 for (
int b = 0; b < (tree ? numSplits : s); ++b)
976 if (b != s) k.allpass[
static_cast<size_t>(b * kMaxSplits + s)].stages.setNumSections(numStagesAllpass_);
981 static constexpr int kLadderSplitStride = 2 * kMaxStagesPerFilter;
982 static constexpr int kLadderAllpassBase = kMaxSplits * kLadderSplitStride;
983 static constexpr int kLadderCount = kLadderAllpassBase + MaxBands * kMaxSplits * 2;
986 [[nodiscard]]
static constexpr int ladderLow(
int s,
int st)
noexcept {
return s * kLadderSplitStride + st; }
988 [[nodiscard]]
static constexpr int ladderHigh(
int s,
int st)
noexcept
990 return s * kLadderSplitStride + kMaxStagesPerFilter + st;
993 [[nodiscard]]
static constexpr int ladderAllpass(
int b,
int s,
int st)
noexcept
995 return kLadderAllpassBase + (b * kMaxSplits + s) * 2 + st;
1004 std::array<SplitPoint, kMaxSplits> splits {};
1005 std::vector<AllPassChain> allpass;
1006 std::vector<NormalizedLadder<T, 16>> ladders;
1012 T* p[MaxBands][16] {};
1015 [[nodiscard]] IirBank& bank(
int index)
noexcept {
return *banks_[
static_cast<size_t>(index)]; }
1019 [[nodiscard]]
bool treeInUse() const noexcept
1028 void setSplitSection(
int s,
int st,
const BiquadCoeffs& low,
const BiquadCoeffs& high,
int ramp)
noexcept
1030 for (
int i = 0; i < 2; ++i)
1032 if (i != bank_ && i != fadeBank_)
continue;
1033 IirBank& k = bank(i);
1034 k.splits[
static_cast<size_t>(s)].lp.setCoeffs(st, low);
1035 k.splits[
static_cast<size_t>(s)].hp.setCoeffs(st, high);
1038 k.ladders[
static_cast<size_t>(ladderLow(s, st))].rampCoeffs(low, ramp);
1039 k.ladders[
static_cast<size_t>(ladderHigh(s, st))].rampCoeffs(high, ramp);
1045 void setAllpassSection(
int b,
int s,
int st,
const BiquadCoeffs& c,
int ramp)
noexcept
1047 for (
int i = 0; i < 2; ++i)
1049 if (i != bank_ && i != fadeBank_)
continue;
1050 IirBank& k = bank(i);
1051 k.allpass[
static_cast<size_t>(b * kMaxSplits + s)].stages.setCoeffs(st, c);
1053 k.ladders[
static_cast<size_t>(ladderAllpass(b, s, st))].rampCoeffs(c, ramp);
1059 void setCorrection(
int s,
int numSplits,
int st,
const BiquadCoeffs& c,
int ramp)
noexcept
1061 const int end = treeInUse() ? numSplits : s;
1062 for (
int b = 0; b < end; ++b)
1063 if (b != s) setAllpassSection(b, s, st, c, ramp);
1068 void runLadders(IirBank& k,
const T* src, T* dst,
int length,
int first,
int count,
int channel)
noexcept
1070 double* tmp = ladderTmp_.data();
1071 for (
int i = 0; i < length; ++i) tmp[i] = static_cast<double>(src[i]);
1072 for (
int j = 0; j < count; ++j)
1073 k.ladders[
static_cast<size_t>(first + j)].processSegment(tmp, length, channel);
1074 for (
int i = 0; i < length; ++i) dst[i] = static_cast<T>(tmp[i]);
1078 void advanceLadders(IirBank& k,
int first,
int count,
int length)
noexcept
1080 for (
int j = 0; j < count; ++j)
1081 k.ladders[
static_cast<size_t>(first + j)].advanceRamp(length);
1084 void processIIR(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nS,
int nCh)
noexcept
1086 DenormalGuard guard;
1087 const int numSplits = numBands - 1;
1096 if (glideRemain_ == 0)
1098 bool anySmoothing =
false;
1099 for (
int i = 0; i < numSplits; ++i)
1100 anySmoothing = anySmoothing || freqSmoothers_[i].isSmoothing();
1101 if (!anySmoothing)
break;
1102 for (
int i = 0; i < numSplits; ++i)
1104 for (
int s = 0; s < kGlideGrid; ++s)
1105 (
void)freqSmoothers_[i].getNextValue();
1106 frequencies_[i] = freqSmoothers_[i].getCurrentValue();
1109 updateCoefficients(kGlideGrid);
1110 glideRemain_ = kGlideGrid;
1112 const int len = std::min(glideRemain_, nS - offset);
1113 processRange(input, outputs, numBands, nCh, offset, len);
1115 glideRemain_ -= len;
1118 processRange(input, outputs, numBands, nCh, offset, nS - offset);
1121 [[nodiscard]]
static BandPtrs bandsOf(AudioBufferView<T>* outputs,
int numBands,
int nCh,
int offset)
noexcept
1124 for (
int b = 0; b < numBands; ++b)
1125 for (
int ch = 0; ch < nCh; ++ch) out.p[b][ch] = outputs[b].getChannel(ch) + offset;
1130 void processRange(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nCh,
int offset,
1131 int length)
noexcept
1133 while (fadeLeft_ > 0 && length > 0)
1135 const int len = std::min({ length, fadeLeft_, kFadeChunk });
1136 if (fadeBank_ >= 0) processBankFadeRange(input, outputs, numBands, nCh, offset, len);
1137 else processFadeRange(input, outputs, numBands, nCh, offset, len);
1141 if (fadeLeft_ == 0 && fadeBank_ >= 0)
1149 BandPtrs out = bandsOf(outputs, numBands, nCh, offset);
1150 runTopology(bank(bank_), iirTopo_, numBands, input, offset, out, nCh, length);
1155 static void resetBank(IirBank& k)
noexcept
1157 for (
auto& sp : k.splits)
1162 for (
auto& apChain : k.allpass)
1163 apChain.stages.
reset();
1164 for (
auto& l : k.ladders)
1169 void startBandFade(
int bands,
Topology topology)
noexcept
1172 fadeFrom_ = iirBands_;
1173 fadeFromTopo_ = iirTopo_;
1175 iirTopo_ = topology;
1176 fadeLeft_ = fadeLength_;
1182 fadeBank_ = 1 - bank_;
1183 IirBank& to = bank(fadeBank_);
1184 const IirBank& from = bank(bank_);
1185 to.splits = from.splits;
1186 std::copy(from.allpass.begin(), from.allpass.end(), to.allpass.begin());
1187 std::copy(from.ladders.begin(), from.ladders.end(), to.ladders.begin());
1193 IirBank& k = bank(bank_);
1194 for (
int s = fadeFrom_ - 1; s < bands - 1; ++s)
1196 k.splits[
static_cast<size_t>(s)].lp.reset();
1197 k.splits[
static_cast<size_t>(s)].hp.reset();
1198 for (
int b = 0; b < s; ++b)
1199 k.allpass[
static_cast<size_t>(b * kMaxSplits + s)].stages.reset();
1200 if (!k.ladders.empty())
1202 for (
int st = 0; st < kMaxStagesPerFilter; ++st)
1204 k.ladders[
static_cast<size_t>(ladderLow(s, st))].
reset();
1205 k.ladders[
static_cast<size_t>(ladderHigh(s, st))].
reset();
1207 for (
int b = 0; b < s; ++b)
1208 for (
int st = 0; st < 2; ++st)
1209 k.ladders[
static_cast<size_t>(ladderAllpass(b, s, st))].
reset();
1215 void runSplit(IirBank& k,
int s, T*
const* work, T*
const* low,
int nCh,
int len)
noexcept
1219 const int perFilter = numStagesPerFilter_;
1220 for (
int ch = 0; ch < nCh; ++ch)
1222 runLadders(k, work[ch], low[ch], len, ladderLow(s, 0), perFilter, ch);
1223 runLadders(k, work[ch], work[ch], len, ladderHigh(s, 0), perFilter, ch);
1225 advanceLadders(k, ladderLow(s, 0), perFilter, len);
1226 advanceLadders(k, ladderHigh(s, 0), perFilter, len);
1229 for (
int ch = 0; ch < nCh; ++ch) std::copy(work[ch], work[ch] + len, low[ch]);
1230 k.splits[
static_cast<size_t>(s)].lp.processBlock(AudioBufferView<T>(low, nCh, len));
1231 k.splits[
static_cast<size_t>(s)].hp.processBlock(AudioBufferView<T>(work, nCh, len));
1235 void runAllpass(IirBank& k,
int b,
int s, T*
const* band,
int nCh,
int len)
noexcept
1239 for (
int ch = 0; ch < nCh; ++ch)
1240 runLadders(k, band[ch], band[ch], len, ladderAllpass(b, s, 0), numStagesAllpass_, ch);
1241 advanceLadders(k, ladderAllpass(b, s, 0), numStagesAllpass_, len);
1244 k.allpass[
static_cast<size_t>(b * kMaxSplits + s)].stages.processBlock(AudioBufferView<T>(band, nCh, len));
1255 void runTree(IirBank& k,
int lo,
int hi, T*
const* buf, BandPtrs& out,
int nCh,
int len)
noexcept
1257 if (lo >= hi)
return;
1258 const int r = lo + (hi - lo - 1) / 2;
1259 T*
const* low = out.p[r];
1260 runSplit(k, r, buf, low, nCh, len);
1261 if (numStagesAllpass_ > 0)
1263 for (
int s = r + 1; s < hi; ++s) runAllpass(k, r, s, low, nCh, len);
1264 for (
int s = lo; s < r; ++s) runAllpass(k, r, s, buf, nCh, len);
1266 runTree(k, lo, r, low, out, nCh, len);
1267 runTree(k, r + 1, hi, buf, out, nCh, len);
1272 void runTopology(IirBank& k,
Topology topology,
int numBands, AudioBufferView<T> input,
int offset, BandPtrs& out,
1273 int nCh,
int len)
noexcept
1275 const int numSplits = numBands - 1;
1280 for (
int ch = 0; ch < nCh; ++ch)
1282 const T* src = input.getChannel(ch) + offset;
1283 work[ch] = getWorkBufChannel(ch);
1284 for (
int i = 0; i < len; ++i)
1285 work[ch][i] = std::isfinite(src[i]) ? src[i] : T(0);
1290 runTree(k, 0, numSplits, work, out, nCh, len);
1291 for (
int ch = 0; ch < nCh; ++ch) std::copy(work[ch], work[ch] + len, out.p[numBands - 1][ch]);
1299 for (
int s = 0; s < numSplits; ++s)
1300 runSplit(k, s, work, out.p[s], nCh, len);
1302 for (
int ch = 0; ch < nCh; ++ch) std::copy(work[ch], work[ch] + len, out.p[numBands - 1][ch]);
1304 for (
int s = 1; s < numSplits; ++s)
1305 for (
int b = 0; b < s; ++b)
1306 runAllpass(k, b, s, out.p[b], nCh, len);
1309 [[nodiscard]] T* fadeBandOf(std::vector<T>& scratch,
int band,
int ch)
noexcept
1311 return scratch.data()
1312 + (
static_cast<size_t>(band) *
static_cast<size_t>(numChannels_) +
static_cast<size_t>(ch))
1313 *
static_cast<size_t>(kFadeChunk);
1325 void processFadeRange(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nCh,
1326 int offset,
int len)
noexcept
1328 IirBank& k = bank(bank_);
1329 const int most = std::max(fadeFrom_, iirBands_);
1330 const int fewest = std::min(fadeFrom_, iirBands_);
1331 const bool adding = iirBands_ > fadeFrom_;
1333 for (
int ch = 0; ch < nCh; ++ch)
1335 const T* src = input.getChannel(ch) + offset;
1336 work[ch] = getWorkBufChannel(ch);
1337 for (
int i = 0; i < len; ++i) work[ch][i] = std::isfinite(src[i]) ? src[i] : T(0);
1340 for (
int ch = 0; ch < nCh; ++ch) top[ch] = fadeTop_.data() +
static_cast<size_t>(ch) * kFadeChunk;
1341 for (
int s = 0; s < most - 1; ++s)
1343 if (s == fewest - 1)
1344 for (
int ch = 0; ch < nCh; ++ch) std::copy(work[ch], work[ch] + len, top[ch]);
1346 for (
int ch = 0; ch < nCh; ++ch) low[ch] = fadeBandOf(fadeBands_, s, ch);
1347 runSplit(k, s, work, low, nCh, len);
1349 for (
int ch = 0; ch < nCh; ++ch)
1350 std::copy(work[ch], work[ch] + len, fadeBandOf(fadeBands_, most - 1, ch));
1353 for (
int s = 1; s < most - 1; ++s)
1355 if (s == fewest - 1)
1356 for (
int b = 0; b < fewest - 1; ++b)
1357 for (
int ch = 0; ch < nCh; ++ch)
1358 std::copy_n(fadeBandOf(fadeBands_, b, ch), len, fadeBandOf(fadeLower_, b, ch));
1359 for (
int b = 0; b < s; ++b)
1362 for (
int ch = 0; ch < nCh; ++ch) band[ch] = fadeBandOf(fadeBands_, b, ch);
1363 runAllpass(k, b, s, band, nCh, len);
1368 const T step = T(1) /
static_cast<T
>(fadeLength_);
1369 const T start =
static_cast<T
>(fadeLength_ - fadeLeft_);
1370 for (
int b = 0; b < numBands; ++b)
1371 for (
int ch = 0; ch < nCh; ++ch)
1373 T* out = outputs[b].getChannel(ch) + offset;
1374 const T* lg = fadeBandOf(fadeBands_, b, ch);
1375 const T* sm = b < fewest - 1 ? fadeBandOf(fadeLower_, b, ch) : b == fewest - 1 ? top[ch] : nullptr;
1376 for (
int i = 0; i < len; ++i)
1378 const T w = (start +
static_cast<T
>(i + 1)) * step;
1380 if (!adding && b == fewest - 1)
1381 for (
int j = b + 1; j < most; ++j) inLarge += fadeBandOf(fadeBands_, j, ch)[i];
1382 const T inSmall = sm !=
nullptr ? sm[i] : T(0);
1383 out[i] = adding ? inSmall + (inLarge - inSmall) * w : inLarge + (inSmall - inLarge) * w;
1394 void processBankFadeRange(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nCh,
1395 int offset,
int len)
noexcept
1398 for (
int b = 0; b < fadeFrom_; ++b)
1399 for (
int ch = 0; ch < nCh; ++ch) before.p[b][ch] = fadeBandOf(fadeBands_, b, ch);
1400 runTopology(bank(bank_), fadeFromTopo_, fadeFrom_, input, offset, before, nCh, len);
1401 BandPtrs now = bandsOf(outputs, numBands, nCh, offset);
1402 runTopology(bank(fadeBank_), iirTopo_, numBands, input, offset, now, nCh, len);
1404 const T step = T(1) /
static_cast<T
>(fadeLength_);
1405 const T start =
static_cast<T
>(fadeLength_ - fadeLeft_);
1406 for (
int b = 0; b < numBands; ++b)
1407 for (
int ch = 0; ch < nCh; ++ch)
1409 T* out = now.p[b][ch];
1410 for (
int i = 0; i < len; ++i)
1413 if (b < numBands - 1) was = b < fadeFrom_ ? before.p[b][ch][i] : T(0);
1415 for (
int j = b; j < fadeFrom_; ++j) was += before.p[j][ch][i];
1416 const T w = (start +
static_cast<T
>(i + 1)) * step;
1417 out[i] = was + (out[i] - was) * w;
1422 inline T* getWorkBufChannel(
int ch)
noexcept
1424 return workBuf_.data() +
static_cast<size_t>(ch) *
static_cast<size_t>(spec_.
maxBlockSize);
1430 void passExtraChannels(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nS,
1431 int firstCh)
noexcept
1433 const int inCh = input.getNumChannels();
1434 for (
int ch = firstCh; ch < inCh; ++ch)
1436 for (
int b = 0; b < numBands; ++b)
1438 if (ch >= outputs[b].getNumChannels())
continue;
1439 T* dst = outputs[b].getChannel(ch);
1442 const T* src = input.getChannel(ch);
1443 std::copy(src, src + nS, dst);
1447 std::fill(dst, dst + nS, T(0));
1464 [[nodiscard]] T* lpKernelSet(
int set)
noexcept
1466 const size_t specLen =
static_cast<size_t>(2 * lpPart_ + 2);
1467 return lpKernelSpec_.data() +
static_cast<size_t>(set) *
static_cast<size_t>(kMaxSplits * kLpPartitions) * specLen;
1472 [[nodiscard]]
double lpPartitionCost(
int bands,
int chans,
int parts)
const noexcept
1474 return static_cast<double>(chans)
1475 * (lpCostFft_ +
static_cast<double>(parts) * lpCostCma_
1476 +
static_cast<double>(bands - 1) * lpCostIfft_
1477 +
static_cast<double>(bands) * lpCostResidualPerBand_);
1481 void startLinearPhaseDesign() noexcept
1483 lpDesignBands_ = std::clamp(numBands_.load(std::memory_order_relaxed), 2, MaxBands);
1485 for (
int sp = 0; sp < lpDesignBands_ - 1; ++sp)
1486 lpDesignFc_[
static_cast<size_t>(sp)] = clampSplitFreq(
static_cast<double>(frequencies_[
static_cast<size_t>(sp)]), sr);
1487 switch (order_.load(std::memory_order_relaxed))
1489 case 6: lpDesignExpo_ = 1;
break;
1490 case 12: lpDesignExpo_ = 2;
break;
1491 case 36: lpDesignExpo_ = 6;
break;
1492 case 48: lpDesignExpo_ = 8;
break;
1493 default: lpDesignExpo_ = 4;
break;
1495 lpDesignSet_ = 1 - lpActiveSet_;
1499 lpDesignRedo_ =
false;
1500 lpDesignActive_ =
true;
1504 lpDesignRate_ = kLpDesignShare * lpPartitionCost(lpDesignBands_, 2, (lpDesignBands_ - 1) * kLpPartitions)
1505 /
static_cast<double>(lpPart_);
1513 void designBandMagnitudes(
double fc,
int begin,
int end)
noexcept
1515 const double* freq = lpBinFreq_.data();
1516 double* carry = lpCarry_.data();
1517 T* mag = lpMag_.data();
1518 const double invFc = 1.0 / fc;
1519 for (
int k = begin; k < end; ++k)
1521 const double r = freq[k] * invFc;
1523 if constexpr (Expo >= 2) rPow = r * r;
1524 if constexpr (Expo == 6) rPow *= rPow * rPow;
1527 if constexpr (Expo >= 4) rPow *= rPow;
1528 if constexpr (Expo >= 8) rPow *= rPow;
1530 const double lp = 1.0 / (1.0 + rPow);
1531 const double c = carry[k];
1532 mag[k] =
static_cast<T
>(c * lp);
1533 carry[k] = c * (rPow * lp);
1536 T* re = lpTimeResponse_.data();
1537 for (
int k = begin; k < end; ++k) re[2 * k] = mag[k];
1542 [[nodiscard]]
double linearPhaseDesignStepCost() const noexcept
1544 if (lpDesignStep_ < kLpMagChunks)
return lpCostMagChunk_;
1545 if (lpDesignStep_ < kLpMagChunks + lpDesignFftSteps_)
return lpCostDesignFftStep_;
1546 return lpCostPartitionStep_;
1551 double linearPhaseDesignStep() noexcept
1553 const int designSize = 2 * firLength_;
1554 const int numBins = firLength_ + 1;
1555 const int b = lpDesignBand_;
1557 if (lpDesignStep_ < kLpMagChunks)
1567 const int chunk = (numBins + kLpMagChunks - 1) / kLpMagChunks;
1568 const int begin = std::min(numBins, lpDesignStep_ * chunk);
1569 const int end = std::min(numBins, begin + chunk);
1570 if (b == 0) std::fill(lpCarry_.begin() + begin, lpCarry_.begin() + end, 1.0);
1572 const double fc = lpDesignFc_[
static_cast<size_t>(b)];
1573 switch (lpDesignExpo_)
1575 case 1: designBandMagnitudes<1>(fc, begin, end);
break;
1576 case 2: designBandMagnitudes<2>(fc, begin, end);
break;
1577 case 6: designBandMagnitudes<6>(fc, begin, end);
break;
1578 case 8: designBandMagnitudes<8>(fc, begin, end);
break;
1579 default: designBandMagnitudes<4>(fc, begin, end);
break;
1582 return lpCostMagChunk_;
1585 if (lpDesignStep_ < kLpMagChunks + lpDesignFftSteps_)
1589 lpDesignFft_->inverseStep(lpDesignStep_ - kLpMagChunks, lpTimeResponse_.data(), lpFirKernel_.data());
1591 return lpCostDesignFftStep_;
1599 const int part = lpDesignStep_ - kLpMagChunks - lpDesignFftSteps_;
1600 const int halfLen = firLength_ / 2;
1601 const size_t specLen =
static_cast<size_t>(2 * lpPart_ + 2);
1602 const int first = part * lpPart_;
1605 const int shift = (first >= halfLen) ? -halfLen : designSize - halfLen;
1606 const T* src = lpFirKernel_.data() + (first + shift);
1607 const T* win = lpWindow_.data() + first;
1614 for (
int j = 0; j < lpPart_; ++j) l1 += std::abs(static_cast<double>(lpScratch_[
static_cast<size_t>(j)]));
1615 const size_t slot =
static_cast<size_t>(lpDesignSet_ * kMaxSplits * kLpPartitions + b * kLpPartitions + part);
1616 const bool keep = l1 >= kLpSkipL1;
1617 lpPartOn_[slot] = keep ? 1 : 0;
1618 double cost = 2.0 * kLpCopyUnitsPerSample *
static_cast<double>(lpPart_);
1622 std::fill(lpScratch_.begin() + lpPart_, lpScratch_.end(), T(0));
1623 lpFft_->forward(lpScratch_.data(),
1624 lpKernelSet(lpDesignSet_) +
static_cast<size_t>(b * kLpPartitions + part) * specLen);
1625 cost = lpCostPartitionStep_;
1628 if (++lpDesignStep_ == kLpMagChunks + lpDesignFftSteps_ + kLpPartitions)
1631 if (++lpDesignBand_ == lpDesignBands_ - 1)
1636 lpActiveSet_ = lpDesignSet_;
1637 lpSetBands_[
static_cast<size_t>(lpActiveSet_)] = lpDesignBands_;
1638 lpSetParts_[
static_cast<size_t>(lpActiveSet_)] = lpDesignParts_;
1639 lpDesignActive_ =
false;
1646 void designLinearPhaseNow() noexcept
1648 lpMagDirty_.store(
false, std::memory_order_relaxed);
1649 startLinearPhaseDesign();
1650 while (lpDesignActive_) (void)linearPhaseDesignStep();
1651 lpDesignCredit_ = 0.0;
1658 void advanceLinearPhaseDesign(
int nS)
noexcept
1660 if (lpMagDirty_.load(std::memory_order_relaxed) &&
1661 lpMagDirty_.exchange(
false, std::memory_order_acquire))
1663 if (lpDesignActive_) lpDesignRedo_ =
true;
1664 else { startLinearPhaseDesign(); lpDesignCredit_ = 0.0; }
1666 if (!lpDesignActive_)
return;
1673 const double budget = lpDesignRate_ *
static_cast<double>(nS);
1674 const double maxStep = std::max(lpCostMagChunk_, std::max(lpCostDesignFftStep_, lpCostPartitionStep_));
1675 lpDesignCredit_ = std::min(lpDesignCredit_ + budget, budget + maxStep);
1676 while (lpDesignActive_)
1680 if (lpDesignSet_ == lpTaskSet_ && lpTaskUnit_ < lpTaskUnits_)
break;
1682 if (lpDesignSet_ == lpTaskFadeSet_ && lpTaskUnit_ < lpTaskUnits_)
break;
1683 if (lpLaunchedSet_ >= 0 && lpLaunchedSet_ != lpActiveSet_ && lpDesignSet_ == lpLaunchedSet_)
break;
1684 if (linearPhaseDesignStepCost() > lpDesignCredit_)
break;
1685 lpDesignCredit_ -= linearPhaseDesignStep();
1686 if (!lpDesignActive_ && lpDesignRedo_) startLinearPhaseDesign();
1688 if (!lpDesignActive_) lpDesignCredit_ = 0.0;
1703 inline T* lpRingOf(
int ch)
noexcept
1705 return lpRing_.data() +
static_cast<size_t>(ch) *
static_cast<size_t>(kLpRingBlocks * lpPart_);
1707 inline T* lpOutOf(
int ch,
int buf,
int band)
noexcept
1709 return lpOut_.data()
1710 + ((
static_cast<size_t>(ch) * 2 +
static_cast<size_t>(buf)) * MaxBands +
static_cast<size_t>(band))
1711 *
static_cast<size_t>(lpPart_);
1717 [[nodiscard]]
int lpUnitsPerChannel() const noexcept
1719 const int fading = lpTaskFadeSet_ >= 0 ? lpTaskFadeBands_ - 1 : 0;
1720 return 2 + (lpTaskBands_ - 1 + fading) * (kLpPartitions + 1);
1724 void launchLinearPhaseTask(
int block)
noexcept
1728 lpTaskFadeSet_ = (lpLaunchedSet_ >= 0 && lpLaunchedSet_ != lpActiveSet_) ? lpLaunchedSet_ : -1;
1729 lpTaskFadeBands_ = lpTaskFadeSet_ >= 0 ? std::max(2, lpSetBands_[
static_cast<size_t>(lpTaskFadeSet_)]) : 0;
1730 lpLaunchedSet_ = lpActiveSet_;
1731 lpTaskSet_ = lpActiveSet_;
1732 lpTaskBands_ = std::max(2, lpSetBands_[
static_cast<size_t>(lpActiveSet_)]);
1733 lpTaskBlock_ = block;
1734 lpTaskFdl_ = (lpTaskFdl_ + 1) % kLpPartitions;
1736 lpTaskUnits_ = numChannels_ * lpUnitsPerChannel();
1738 lpTaskTotal_ = lpPartitionCost(lpTaskBands_, numChannels_, lpSetParts_[
static_cast<size_t>(lpActiveSet_)]);
1739 if (lpTaskFadeSet_ >= 0)
1740 lpTaskTotal_ +=
static_cast<double>(numChannels_)
1741 * (
static_cast<double>(lpSetParts_[
static_cast<size_t>(lpTaskFadeSet_)]) * lpCostCma_
1742 +
static_cast<double>(lpTaskFadeBands_ - 1) * lpCostIfft_
1743 +
static_cast<double>(lpTaskFadeBands_ + lpTaskBands_) * lpCostResidualPerBand_);
1747 double runLinearPhaseUnit(
int unit)
noexcept
1749 const int perCh = lpUnitsPerChannel();
1750 const int ch = unit / perCh;
1751 const int u = unit - ch * perCh;
1752 const int N = lpPart_;
1753 const size_t specLen =
static_cast<size_t>(2 * N + 2);
1754 T* fdl = lpFdl_.data() +
static_cast<size_t>(ch) *
static_cast<size_t>(kLpPartitions) * specLen;
1755 const int next = 1 - lpPlay_;
1760 const T* ring = lpRingOf(ch);
1761 const int prev = (lpTaskBlock_ + kLpRingBlocks - 1) & (kLpRingBlocks - 1);
1762 std::copy_n(ring +
static_cast<size_t>(prev) *
static_cast<size_t>(N), N, lpScratch_.begin());
1763 std::copy_n(ring +
static_cast<size_t>(lpTaskBlock_) *
static_cast<size_t>(N), N, lpScratch_.begin() + N);
1764 lpFft_->forward(lpScratch_.data(), fdl +
static_cast<size_t>(lpTaskFdl_) * specLen);
1768 const int conv = lpTaskBands_ - 1;
1769 const int delayed = (lpTaskBlock_ + kLpRingBlocks - kLpPartitions / 2) & (kLpRingBlocks - 1);
1770 const T* delayedInput = lpRingOf(ch) +
static_cast<size_t>(delayed) *
static_cast<size_t>(N);
1773 if (lpTaskFadeSet_ >= 0) blendLinearPhaseFade(ch, next, delayedInput);
1776 T* dst = lpOutOf(ch, next, conv);
1777 std::copy_n(delayedInput, N, dst);
1778 for (
int b = 0; b < conv; ++b)
1780 return static_cast<double>(lpTaskBands_) * lpCostResidualPerBand_;
1784 int band = (u - 1) / (kLpPartitions + 1);
1785 const int step = (u - 1) - band * (kLpPartitions + 1);
1786 const bool previous = band >= conv;
1787 const int set = previous ? lpTaskFadeSet_ : lpTaskSet_;
1788 if (previous) band -= conv;
1789 if (step < kLpPartitions)
1791 if (step == 0) std::fill(lpAccum_.begin(), lpAccum_.end(), T(0));
1792 if (lpPartOn_[
static_cast<size_t>(set * kMaxSplits * kLpPartitions + band * kLpPartitions + step)] == 0)
1794 const T* kernel = lpKernelSet(set) +
static_cast<size_t>(band * kLpPartitions + step) * specLen;
1795 const int slot = (lpTaskFdl_ - step + kLpPartitions) % kLpPartitions;
1799 lpFft_->inverse(lpAccum_.data(), lpResult_.data());
1800 T* to = previous ? lpFadeOld_.data() +
static_cast<size_t>(band) *
static_cast<size_t>(N) : lpOutOf(ch, next, band);
1801 std::copy_n(lpResult_.begin() + N, N, to);
1813 void blendLinearPhaseFade(
int ch,
int next,
const T* delayedInput)
noexcept
1815 const int N = lpPart_;
1816 const int before = lpTaskFadeBands_;
1817 const int conv = lpTaskBands_ - 1;
1818 auto old = [&](
int b) {
return lpFadeOld_.data() +
static_cast<size_t>(b) *
static_cast<size_t>(N); };
1819 T* oldResidual = old(before - 1);
1820 std::copy_n(delayedInput, N, oldResidual);
1821 for (
int b = 0; b < before - 1; ++b)
simd::addWithGainT(oldResidual, old(b), T(-1), N);
1822 const T invN = T(1) /
static_cast<T
>(N);
1823 for (
int b = 0; b < conv; ++b)
1825 T* out = lpOutOf(ch, next, b);
1826 const T* from = b < before ? old(b) : nullptr;
1827 for (
int i = 0; i < N; ++i)
1829 const T was = from !=
nullptr ? from[i] : T(0);
1830 out[i] = was + (out[i] - was) * (
static_cast<T
>(i + 1) * invN);
1837 void runLinearPhaseTask(
int staged)
noexcept
1839 if (lpTaskSet_ < 0)
return;
1840 const double target = (staged >= lpPart_)
1841 ? lpTaskTotal_ + 1.0
1842 : lpTaskTotal_ * static_cast<double>(staged) / static_cast<double>(lpPart_);
1843 while (lpTaskUnit_ < lpTaskUnits_ && lpTaskDone_ < target)
1844 lpTaskDone_ += runLinearPhaseUnit(lpTaskUnit_++);
1847 void processLinearPhase(AudioBufferView<T> input, AudioBufferView<T>* outputs,
int numBands,
int nS,
1850 advanceLinearPhaseDesign(nS);
1852 const int N = lpPart_;
1856 const int run = std::min(nS - done, N - lpPos_);
1860 const int have = lpOutBands_;
1861 const size_t stage =
static_cast<size_t>(lpStageBlock_) *
static_cast<size_t>(N)
1862 +
static_cast<size_t>(lpPos_);
1863 for (
int ch = 0; ch < numChannels_; ++ch)
1868 T* dst = lpRingOf(ch) + stage;
1871 std::fill_n(dst, run, T(0));
1874 const T* src = input.getChannel(ch) + done;
1875 for (
int i = 0; i < run; ++i)
1876 dst[i] = std::isfinite(src[i]) ? src[i] : T(0);
1877 for (
int b = 0; b < numBands; ++b)
1879 T* out = outputs[b].getChannel(ch) + done;
1880 if (b < have) std::copy_n(lpOutOf(ch, lpPlay_, b) + lpPos_, run, out);
1881 else std::fill_n(out, run, T(0));
1883 for (
int b = numBands; b < have; ++b)
1885 simd::addT(outputs[numBands - 1].getChannel(ch) + done, lpOutOf(ch, lpPlay_, b) + lpPos_, run);
1890 runLinearPhaseTask(lpPos_);
1897 if (lpTaskSet_ >= 0)
1899 lpPlay_ = 1 - lpPlay_;
1900 lpOutBands_ = lpTaskBands_;
1902 launchLinearPhaseTask(lpStageBlock_);
1903 lpStageBlock_ = (lpStageBlock_ + 1) & (kLpRingBlocks - 1);
1912 bool prepared_ =
false;
1913 std::atomic<int> numBands_ { 2 };
1914 std::atomic<int> order_ { 24 };
1915 int numChannels_ = 2;
1917 std::atomic<bool> dirty_ {
true };
1919 std::array<std::atomic<T>, kMaxSplits> targetFrequencies_ {};
1920 std::atomic<bool> freqUpdatePending_ {
false };
1922 std::array<T, kMaxSplits> frequencies_ {};
1923 std::array<SmoothedValue<T>, kMaxSplits> freqSmoothers_;
1925 int numStagesPerFilter_ = 2;
1926 int numStagesAllpass_ = 1;
1929 std::array<std::unique_ptr<IirBank>, 2> banks_;
1932 std::vector<T> workBuf_;
1937 std::vector<double> ladderTmp_;
1940 bool ladderActive_ =
false;
1943 std::unique_ptr<FFTReal<T>> lpFft_;
1944 std::unique_ptr<FFTReal<T>> lpDesignFft_;
1949 int lpStageBlock_ = 0;
1951 int lpOutBands_ = 0;
1952 int lpActiveSet_ = 0;
1953 std::array<int, 2> lpSetBands_ { 0, 0 };
1954 std::array<int, 2> lpSetParts_ { 0, 0 };
1956 int lpTaskSet_ = -1;
1957 int lpTaskBands_ = 2;
1958 int lpTaskBlock_ = 0;
1960 int lpTaskUnit_ = 0;
1961 int lpTaskUnits_ = 0;
1962 double lpTaskDone_ = 0.0;
1963 double lpTaskTotal_ = 0.0;
1964 int lpLaunchedSet_ = -1;
1965 int lpTaskFadeSet_ = -1;
1966 int lpTaskFadeBands_ = 0;
1968 double lpCostFft_ = 0.0, lpCostCma_ = 0.0, lpCostIfft_ = 0.0, lpCostResidualPerBand_ = 0.0;
1969 double lpCostMagChunk_ = 0.0, lpCostDesignFftStep_ = 0.0, lpCostPartitionStep_ = 0.0;
1971 bool lpDesignActive_ =
false;
1972 bool lpDesignRedo_ =
false;
1973 int lpDesignSet_ = 1;
1974 int lpDesignBand_ = 0;
1975 int lpDesignStep_ = 0;
1976 int lpDesignBands_ = 0;
1977 int lpDesignParts_ = 0;
1978 int lpDesignExpo_ = 4;
1979 int lpDesignFftSteps_ = 1;
1980 std::array<double, kMaxSplits> lpDesignFc_ {};
1981 double lpDesignCredit_ = 0.0;
1982 double lpDesignRate_ = 0.0;
1983 int glideRemain_ = 0;
1990 int fadeLength_ = 960;
1991 std::vector<T> fadeBands_, fadeLower_, fadeTop_;
1992 std::atomic<bool> lpMagDirty_ {
true };
1994 std::vector<T> lpKernelSpec_;
1995 std::vector<uint8_t> lpPartOn_;
1996 std::vector<T> lpFdl_;
1997 std::vector<T> lpRing_;
1998 std::vector<T> lpOut_;
1999 std::vector<T> lpAccum_, lpResult_, lpScratch_;
2000 std::vector<T> lpFadeOld_;
2001 std::vector<double> lpCarry_;
2002 std::vector<double> lpBinFreq_;
2003 std::vector<T> lpMag_;
2004 std::vector<T> lpWindow_;
2005 std::vector<T> lpTimeResponse_;
2006 std::vector<T> lpFirKernel_;