212 : quality_(quality), mode_(mode), depth_(depth), band_(band),
213 firstStage_(firStageDesign(quality, depth, 0)), firstTransition_(firTransition(quality))
215 assert(factor >= 1 && (factor & (factor - 1)) == 0 &&
"Factor must be a power of 2");
216 assert(factor <= (1 << kMaxStages) &&
"Factor must be <= 16 (kMaxStages stages)");
223 factor = std::clamp(factor, 1, 1 << kMaxStages);
225 while ((1 << (numStages_ + 1)) <= factor)
227 factor_ = 1 << numStages_;
231 for (
int s = 0; s < numStages_; ++s)
232 iir_[
static_cast<size_t>(s)].design(iirCoefficientCount(quality_, depth_, s),
233 iirTransition(quality_, s));
256 for (
int stage = 0; stage < numStages_; ++stage)
259 const int stageMaxSamples = spec.
maxBlockSize * (1 << stage);
262 iir_[
static_cast<size_t>(stage)].allocate(spec.
numChannels, stageMaxSamples);
266 const StageDesign d = stageDesign(stage);
267 filters_[
static_cast<size_t>(stage)].design(d.taps,
static_cast<T
>(d.beta),
282 for (
int i = 0; i < numStages_; ++i)
284 filters_[
static_cast<size_t>(i)].
reset();
285 iir_[
static_cast<size_t>(i)].
reset();
304 double minimumInternalRate = 176400.0,
305 int maxFactor = 1 << kMaxStages)
noexcept
307 if (!(sampleRate > 0.0) || !(sampleRate < 1e12))
return 1;
308 const int limit = maxFactor < 1 ? 1 : (maxFactor > (1 << kMaxStages) ? (1 << kMaxStages) : maxFactor);
311 while (factor * 2 <= limit && sampleRate * factor < minimumInternalRate * (1.0 - 1e-9))
317 [[nodiscard]]
int getFactor() const noexcept {
return factor_; }
345 return stageDesign(stage).taps;
370 [[nodiscard]]
int getLatency() const noexcept {
return latency_; }
387 return static_cast<double>(latency_);
389 const double w = 2.0 * 3.14159265358979323846 * std::clamp(frequencyHz / fs, 1e-6, 0.4999);
390 return iirDelay(w,
true);
423 const int nS = std::clamp(numSamples, 0, baseSpec_.
maxBlockSize);
424 return upBuffer_.
toView().getSubView(0, nS * factor_);
439 const int nCh = std::min(input.getNumChannels(), upBuffer_.
getNumChannels());
442 const int nS = std::min(input.getNumSamples(), baseSpec_.
maxBlockSize);
446 for (
int ch = 0; ch < nCh; ++ch)
447 std::memcpy(upBuffer_.
getChannel(ch), input.getChannel(ch),
static_cast<std::size_t
>(nS) *
sizeof(T));
448 return upBuffer_.
toView().getSubView(0, nS);
455 for (
int stage = 0; stage < numStages_; ++stage)
457 for (
int ch = 0; ch < nCh; ++ch)
458 iir_[
static_cast<size_t>(stage)].upsampleChannel(
459 stage == 0 ? input.getChannel(ch) : upBuffer_.
getChannel(ch),
463 return upBuffer_.
toView().getSubView(0, nS * factor_);
467 filters_[0].processUpsample(input, upBuffer_.
toView(), nCh, nS);
470 int currentLen = nS * 2;
471 for (
int stage = 1; stage < numStages_; ++stage)
473 filters_[
static_cast<size_t>(stage)].processUpsampleInPlace(upBuffer_.
toView(), nCh, currentLen);
477 return upBuffer_.
toView().getSubView(0, nS * factor_);
492 const int nCh = std::min(output.getNumChannels(), upBuffer_.
getNumChannels());
493 const int nS = std::min(output.getNumSamples(), baseSpec_.
maxBlockSize);
497 for (
int ch = 0; ch < nCh; ++ch)
498 std::memcpy(output.getChannel(ch), upBuffer_.
getChannel(ch),
static_cast<std::size_t
>(nS) *
sizeof(T));
505 int len = nS * factor_;
506 for (
int stage = numStages_ - 1; stage >= 0; --stage)
508 for (
int ch = 0; ch < nCh; ++ch)
509 iir_[
static_cast<size_t>(stage)].downsampleChannel(
511 stage == 0 ? output.getChannel(ch) : upBuffer_.
getChannel(ch), ch, len);
517 int currentLen = nS * factor_;
519 for (
int stage = numStages_ - 1; stage > 0; --stage)
521 filters_[
static_cast<size_t>(stage)].processDownsampleInPlace(upBuffer_.
toView(), nCh, currentLen);
526 filters_[0].processDownsample(upBuffer_.
toView(), output, nCh, currentLen);
536 static constexpr int kMaxStages = 4;
545 struct PolyphaseHalfBand
547 std::vector<T> evenTaps;
550 int delaySamples = 0;
560 std::vector<ChannelState> upChannels;
561 std::vector<ChannelState> downChannels;
562 std::vector<T> scratch;
571 void design(
int taps, T beta,
int numChannels,
int maxBlockSamples)
573 halfOrder = (taps - 1) / 2;
578 assert((halfOrder & 1) == 1 &&
"half-band polyphase requires odd halfOrder");
583 delaySamples = (halfOrder + 1) / 2;
586 centerTap = fullCoeffs[
static_cast<std::size_t
>(halfOrder)];
592 for (
int i = 0; i < taps; i += 2)
593 evenTaps.push_back(fullCoeffs[
static_cast<std::size_t
>(i)]);
598 const int upHistorySize =
static_cast<int>(evenTaps.size()) + maxBlockSamples;
599 const int downHistorySize = halfOrder + maxBlockSamples;
601 upChannels.resize(
static_cast<std::size_t
>(numChannels));
602 downChannels.resize(
static_cast<std::size_t
>(numChannels));
604 for (
int ch = 0; ch < numChannels; ++ch)
606 upChannels[ch].history.assign(
static_cast<std::size_t
>(upHistorySize), T(0));
607 downChannels[ch].evenHist.assign(
static_cast<std::size_t
>(downHistorySize), T(0));
608 downChannels[ch].oddHist.assign(
static_cast<std::size_t
>(downHistorySize), T(0));
610 scratch.assign(
static_cast<std::size_t
>(maxBlockSamples), T(0));
616 void reset() noexcept
618 for (
auto& ch : upChannels)
619 std::fill(ch.history.begin(), ch.history.end(), T(0));
620 for (
auto& ch : downChannels)
622 std::fill(ch.evenHist.begin(), ch.evenHist.end(), T(0));
623 std::fill(ch.oddHist.begin(), ch.oddHist.end(), T(0));
632 void upsampleChannel(
const T* src, T* dst, ChannelState& state,
int n)
noexcept
634 constexpr int W = simd::kVecWidth<T>;
635 using O = simd::Vec<T, W>;
636 const int numTaps =
static_cast<int>(evenTaps.size());
637 auto& hist = state.history;
640 std::memmove(hist.data() + numTaps, src,
static_cast<std::size_t
>(n) *
sizeof(T));
644 const T* centre = hist.data() + numTaps - delaySamples;
645 const T centre2 = centerTap * T(2);
646 const auto c2 = O::set1(centre2);
648 for (; i + W <= n; i += W)
649 O::storeInterleave2(dst + 2 * i, O::load(scratch.data() + i),
650 O::mul(O::load(centre + i), c2));
653 dst[2 * i] = scratch[
static_cast<std::size_t
>(i)];
654 dst[2 * i + 1] = centre[i] * centre2;
659 std::memmove(hist.data(), hist.data() + n,
static_cast<std::size_t
>(numTaps) *
sizeof(T));
668 void downsampleChannel(
const T* src, T* dst, ChannelState& state,
int len)
noexcept
670 constexpr int W = simd::kVecWidth<T>;
671 using O = simd::Vec<T, W>;
672 const int outLen = len / 2;
673 const int numTaps =
static_cast<int>(evenTaps.size());
674 T* even = state.evenHist.data();
675 T* odd = state.oddHist.data();
679 for (; m + W <= outLen; m += W)
682 O::loadDeinterleave(src + 2 * m, e, o);
683 O::store(even + halfOrder + m, e);
684 O::store(odd + halfOrder + m, o);
686 for (; m < outLen; ++m)
688 even[halfOrder + m] = src[2 * m];
689 odd[halfOrder + m] = src[2 * m + 1];
695 const T* centre = odd + (halfOrder - 1) / 2;
696 const auto c = O::set1(centerTap);
698 for (; k + W <= outLen; k += W)
699 O::store(dst + k, O::madd(c, O::load(centre + k), O::load(dst + k)));
700 for (; k < outLen; ++k)
704 O::store(lanes, O::madd(c, O::set1(centre[k]), O::set1(dst[k])));
708 std::memmove(even, even + outLen,
static_cast<std::size_t
>(halfOrder) *
sizeof(T));
709 std::memmove(odd, odd + outLen,
static_cast<std::size_t
>(halfOrder) *
sizeof(T));
712 void processUpsample(AudioBufferView<const T> input, AudioBufferView<T> output,
int nCh,
int nS)
noexcept
714 for (
int ch = 0; ch < nCh; ++ch)
715 upsampleChannel(input.getChannel(ch), output.getChannel(ch),
716 upChannels[
static_cast<std::size_t
>(ch)], nS);
719 void processUpsampleInPlace(AudioBufferView<T> buffer,
int nCh,
int currentLen)
noexcept
721 for (
int ch = 0; ch < nCh; ++ch)
722 upsampleChannel(buffer.getChannel(ch), buffer.getChannel(ch),
723 upChannels[
static_cast<std::size_t
>(ch)], currentLen);
726 void processDownsample(AudioBufferView<const T> input, AudioBufferView<T> output,
int nCh,
int currentLen)
noexcept
728 for (
int ch = 0; ch < nCh; ++ch)
729 downsampleChannel(input.getChannel(ch), output.getChannel(ch),
730 downChannels[
static_cast<std::size_t
>(ch)], currentLen);
733 void processDownsampleInPlace(AudioBufferView<T> buffer,
int nCh,
int currentLen)
noexcept
735 for (
int ch = 0; ch < nCh; ++ch)
736 downsampleChannel(buffer.getChannel(ch), buffer.getChannel(ch),
737 downChannels[
static_cast<std::size_t
>(ch)], currentLen);
748 template <
int W,
typename D =
double>
struct BranchPair
751 static V set(D a, D b)
noexcept {
return { a, b }; }
752 static V load(
const D* p)
noexcept {
return { p[0], p[1] }; }
753 static void store(D* p, V v)
noexcept { p[0] = v.
a; p[1] = v.b; }
754 static V step(V c, V x, V x1, V y1)
noexcept
756 return { c.a * (x.a - y1.a) + x1.a, c.b * (x.b - y1.b) + x1.b };
758 static D first(V v)
noexcept {
return v.a; }
759 static D second(V v)
noexcept {
return v.b; }
761 template <
typename D>
struct BranchPair<2, D>
763 using O = simd::Vec<D, 2>;
764 using V =
typename O::V;
765 static V set(D a, D b)
noexcept {
return O::set2(a, b); }
766 static V load(
const D* p)
noexcept {
return O::load(p); }
767 static void store(D* p, V v)
noexcept { O::store(p, v); }
768 static V step(V c, V x, V x1, V y1)
noexcept
770 return O::add(O::mul(c, O::sub(x, y1)), x1);
772 static D first(V v)
noexcept {
return O::lane0(v); }
773 static D second(V v)
noexcept {
return O::lane1(v); }
775 using Pair = BranchPair<simd::kVecDoubleNarrow>;
789 struct PolyphaseIirHalfBand
791 static constexpr int kMaxPairs = 8;
794 alignas(16) std::array<double, 2 * kMaxPairs> coef {};
801 alignas(16) std::array<
double, 2 * (kMaxPairs + 1)>
s {};
803 std::vector<ChannelState> up, down;
804 std::vector<T> scratch;
826 void design(
int numCoefs,
double transition)
noexcept
828 constexpr double kPi = 3.14159265358979323846;
829 numCoefs = std::clamp(numCoefs & ~1, 2, 2 * kMaxPairs);
830 numPairs = numCoefs / 2;
831 const double fp = 0.25 - 0.5 * std::clamp(transition, 1e-4, 0.4999);
832 const double k = std::tan(kPi * fp) * std::tan(kPi * fp);
833 const double q = nome(k);
834 const int order = 2 * numCoefs + 1;
835 std::array<double, 2 * kMaxPairs> a {};
836 for (
int i = 1; i <= numCoefs; ++i)
838 double num = 0.0, den = 0.0;
839 for (
int m = 0; m < 20; ++m)
841 const double sign = (m & 1) ? -1.0 : 1.0;
842 num += sign * std::pow(q, m * (m + 1.0)) * std::sin((2 * m + 1) * kPi * i / order);
843 if (m > 0) den += sign * std::pow(q,
static_cast<double>(m) * m) * std::cos(2 * m * kPi * i / order);
845 const double w = 2.0 * std::pow(q, 0.25) * num / (1.0 + 2.0 * den);
846 const double w2 = w * w;
847 const double sigma = std::sqrt(std::max(0.0, (1.0 - k * w2) * (1.0 - w2 / k))) / (1.0 + w2);
848 a[
static_cast<size_t>(i - 1)] = (1.0 - sigma) / (1.0 + sigma);
850 std::sort(a.begin(), a.begin() + numCoefs);
851 for (
int i = 0; i < numCoefs; ++i)
852 coef[
static_cast<size_t>(i)] = a[
static_cast<size_t>(i)];
856 static double nome(
double k)
noexcept
858 const double kp = std::sqrt(1.0 - k * k);
859 const double e = 0.5 * (1.0 - std::sqrt(kp)) / (1.0 + std::sqrt(kp));
860 const double e4 = e * e * e * e;
861 return e * (1.0 + e4 * (2.0 + e4 * (15.0 + 150.0 * e4)));
865 static double attenuationDb(
int numCoefs,
double transition)
noexcept
867 constexpr double kPi = 3.14159265358979323846;
868 const double fp = 0.25 - 0.5 * transition;
869 const double q = nome(std::tan(kPi * fp) * std::tan(kPi * fp));
870 return 10.0 * std::log10(1.0 + std::pow(q, -(2.0 * numCoefs + 1.0) / 2.0) / 4.0);
874 std::complex<double> response(
double w)
const noexcept
876 const std::complex<double> z2 = std::polar(1.0, -2.0 * w);
877 std::complex<double> a0 = 1.0, a1 = 1.0;
878 for (
int k = 0; k < numPairs; ++k)
880 const double c0 = coef[
static_cast<size_t>(2 * k)];
881 const double c1 = coef[
static_cast<size_t>(2 * k + 1)];
882 a0 *= (c0 + z2) / (1.0 + c0 * z2);
883 a1 *= (c1 + z2) / (1.0 + c1 * z2);
885 return 0.5 * (a0 + std::polar(1.0, -w) * a1);
888 void allocate(
int numChannels,
int maxInputSamples)
890 const auto n =
static_cast<size_t>(std::max(0, numChannels));
891 up.assign(n, ChannelState {});
892 down.assign(n, ChannelState {});
893 scratch.assign(
static_cast<size_t>(std::max(0, maxInputSamples)), T(0));
896 void reset() noexcept
898 for (
auto& s : up) s = ChannelState {};
899 for (
auto& s : down) s = ChannelState {};
908 template <
int P,
bool Up>
909 static void runBlock(
const double* coefs, ChannelState& st,
const T* src, T* dst,
int n)
noexcept
911 using V =
typename Pair::V;
912 V prev = Pair::load(st.s.data());
915 for (
int k = 0; k < P; ++k)
917 y1[k] = Pair::load(st.s.data() + 2 * (k + 1));
918 c[k] = Pair::load(coefs + 2 * k);
920 for (
int i = 0; i < n; ++i)
925 const double x =
static_cast<double>(src[i]);
926 in = Pair::set(x, x);
930 in = Pair::set(
static_cast<double>(src[2 * i + 1]),
static_cast<double>(src[2 * i]));
934 for (
int k = 0; k < P; ++k)
936 const V yOld = y1[k];
937 const V y = Pair::step(c[k], in, xPrev, yOld);
944 dst[2 * i] =
static_cast<T
>(Pair::first(in));
945 dst[2 * i + 1] =
static_cast<T
>(Pair::second(in));
949 dst[i] =
static_cast<T
>(0.5 * (Pair::first(in) + Pair::second(in)));
952 Pair::store(st.s.data(), prev);
953 double sum = Pair::first(prev) + Pair::second(prev);
954 for (
int k = 0; k < P; ++k)
956 Pair::store(st.s.data() + 2 * (k + 1), y1[k]);
957 sum += Pair::first(y1[k]) + Pair::second(y1[k]);
959 if (!std::isfinite(sum)) st = ChannelState {};
963 void dispatch(ChannelState& st,
const T* src, T* dst,
int n)
noexcept
965 const double* c = coef.data();
968 case 1: runBlock<1, Up>(c, st, src, dst, n);
break;
969 case 2: runBlock<2, Up>(c, st, src, dst, n);
break;
970 case 3: runBlock<3, Up>(c, st, src, dst, n);
break;
971 case 4: runBlock<4, Up>(c, st, src, dst, n);
break;
972 case 5: runBlock<5, Up>(c, st, src, dst, n);
break;
973 case 6: runBlock<6, Up>(c, st, src, dst, n);
break;
974 case 7: runBlock<7, Up>(c, st, src, dst, n);
break;
975 default: runBlock<8, Up>(c, st, src, dst, n);
break;
980 void upsampleChannel(
const T* src, T* dst,
int ch,
int n)
noexcept
982 if (
static_cast<size_t>(ch) >= up.size())
return;
985 std::memcpy(scratch.data(), src,
static_cast<size_t>(n) *
sizeof(T));
986 src = scratch.data();
988 dispatch<true>(up[
static_cast<size_t>(ch)], src, dst, n);
992 void downsampleChannel(
const T* src, T* dst,
int ch,
int len)
noexcept
994 if (
static_cast<size_t>(ch) >= down.size())
return;
995 dispatch<false>(down[
static_cast<size_t>(ch)], src, dst, len / 2);
999 struct StageDesign {
int taps;
double beta; };
1015 static constexpr StageDesign firStageDesign(
Quality q,
Depth depth,
int stage)
noexcept
1017 constexpr StageDesign kLow[kMaxStages] = { { 31, 3.395 }, { 11, 2.90 }, { 7, 2.35 }, { 15, 9.45 } };
1018 constexpr StageDesign kMed[kMaxStages] = { { 63, 5.653 }, { 19, 6.25 }, { 15, 7.90 }, { 15, 9.50 } };
1019 constexpr StageDesign kHigh[kMaxStages] = { { 127, 7.857 }, { 23, 7.95 }, { 23, 12.55 }, { 15, 9.50 } };
1020 constexpr StageDesign kMax[kMaxStages] = { { 255, 10.056 },{ 31, 11.20 }, { 23, 12.70 }, { 31, 15.65 } };
1021 constexpr StageDesign kLowDeep[kMaxStages] = { { 91, 10.45 }, { 35, 11.35 }, { 23, 12.30 }, { 31, 15.65 } };
1022 constexpr StageDesign kMedDeep[kMaxStages] = { { 115, 10.45 }, { 31, 10.40 }, { 23, 12.40 }, { 31, 15.65 } };
1023 constexpr StageDesign kHighDeep[kMaxStages] = { { 163, 10.10 }, { 31, 10.85 }, { 23, 12.55 }, { 31, 15.65 } };
1024 const int s = std::clamp(stage, 0, kMaxStages - 1);
1028 case Quality::Low:
return deep ? kLowDeep[s] : kLow[s];
1038 [[nodiscard]] StageDesign stageDesign(
int stage)
const noexcept
1040 return stage == 0 ? firstStage_ : firStageDesign(quality_, depth_, stage);
1044 static constexpr double firstStageDepthDb(
Quality q,
Depth depth)
noexcept
1058 static constexpr double kAudibleEdgeHz = 20000.0;
1060 static constexpr int kShortestFit = 7;
1063 static double kaiserBeta(
double depthDb)
noexcept
1065 if (depthDb > 50.0)
return 0.1102 * (depthDb - 8.7);
1066 if (depthDb > 21.0)
return 0.5842 * std::pow(depthDb - 21.0, 0.4) + 0.07886 * (depthDb - 21.0);
1079 static double halfBandStopPeakDb(
int taps,
double beta,
double edge)
1082 const int half = (taps - 1) / 2;
1083 const int points = std::max(64, 4 * taps);
1085 for (
int p = 0; p <= points; ++p)
1087 const double f = edge + (0.5 - edge) *
static_cast<double>(p) /
static_cast<double>(points);
1088 const double theta = 2.0 * 3.14159265358979323846 * f;
1089 const double twice = 2.0 * std::cos(2.0 * theta);
1090 double before = std::cos(theta), now = before;
1091 double sum = full[
static_cast<size_t>(half)];
1092 for (
int k = 1; k <= half; k += 2)
1094 sum += (full[
static_cast<size_t>(half + k)] + full[
static_cast<size_t>(half - k)]) * now;
1095 const double next = twice * now - before;
1099 peak = std::max(peak, std::abs(sum));
1101 return 20.0 * std::log10(std::max(peak, 1e-12));
1112 static StageDesign fitHalfBand(
double transition,
double depthDb, StageDesign otherwise)
1114 const double edge = 0.25 + 0.5 * transition;
1115 const double estimate = (depthDb - 7.95) / (14.36 * transition) + 1.0;
1116 int taps = 4 *
static_cast<int>(estimate / 4.0) + 3 - 8;
1117 if (taps < kShortestFit) taps = kShortestFit;
1118 const double centre = kaiserBeta(depthDb);
1119 for (; taps < otherwise.taps; taps += 4)
1121 double bestBeta = centre, bestPeak = halfBandStopPeakDb(taps, centre, edge);
1122 for (
int i = -8; i <= 16; ++i)
1124 const double beta = centre + 0.25 *
static_cast<double>(i);
1125 if (i == 0 || beta < 0.5)
continue;
1126 const double peak = halfBandStopPeakDb(taps, beta, edge);
1127 if (peak < bestPeak) { bestPeak = peak; bestBeta = beta; }
1129 const double around = bestBeta;
1130 for (
int i = -4; i <= 4; ++i)
1132 const double beta = around + 0.05 *
static_cast<double>(i);
1133 if (i == 0 || beta < 0.5)
continue;
1134 const double peak = halfBandStopPeakDb(taps, beta, edge);
1135 if (peak < bestPeak) { bestPeak = peak; bestBeta = beta; }
1137 if (bestPeak <= -depthDb)
return { taps, bestBeta };
1149 void fitFirstStage(
double sampleRate)
1151 firstStage_ = firStageDesign(quality_, depth_, 0);
1152 firstTransition_ = firTransition(quality_);
1155 if (sampleRate > 2.0 * kAudibleEdgeHz && sampleRate < 1e12)
1157 const double wide = 0.5 - kAudibleEdgeHz / sampleRate;
1158 if (wide > firstTransition_)
1160 firstTransition_ = wide;
1162 firstStage_ = fitHalfBand(wide, firstStageDepthDb(quality_, depth_), firstStage_);
1166 iir_[0].design(iirCoefficientCountOver(firstStageDepthDb(quality_, depth_), firstTransition_),
1173 static constexpr double firTransition(
Quality q)
noexcept
1177 case Quality::Low:
return (40.0 - 7.95) / (14.36 * 30.0);
1182 return (80.0 - 7.95) / (14.36 * 126.0);
1187 static double iirTransition(
Quality q,
int stage)
noexcept
1189 const double t0 = firTransition(q);
1190 return stage == 0 ? t0 : 0.5 - (1.0 + 2.0 * t0) /
static_cast<double>(1 << (stage + 1));
1196 static int iirCoefficientCount(
Quality q,
Depth depth,
int stage)
noexcept
1198 return iirCoefficientCountOver(firstStageDepthDb(q, depth), iirTransition(q, stage));
1202 static int iirCoefficientCountOver(
double depthDb,
double transition)
noexcept
1205 while (n < 2 * PolyphaseIirHalfBand::kMaxPairs
1206 && PolyphaseIirHalfBand::attenuationDb(n, transition) < depthDb)
1213 double iirDelay(
double w,
bool roundTrip)
const noexcept
1215 constexpr int kSteps = 512;
1217 for (
int s = 0; s < numStages_; ++s)
1219 const double scale = 1.0 /
static_cast<double>(1 << (s + 1));
1220 double prev = 0.0, unwrapped = 0.0;
1221 for (
int i = 1; i <= kSteps; ++i)
1223 const double wi = w * scale *
static_cast<double>(i) / kSteps;
1224 const double ph = std::arg(iir_[
static_cast<size_t>(s)].response(wi));
1225 double d = ph - prev;
1226 while (d > 3.14159265358979323846) d -= 2.0 * 3.14159265358979323846;
1227 while (d < -3.14159265358979323846) d += 2.0 * 3.14159265358979323846;
1232 const double down = unwrapped + w * scale;
1233 total += roundTrip ? (unwrapped + down) : down;
1238 void computeDelays() noexcept
1240 if (numStages_ == 0) { latency_ = 0; decimationDelay_ = 0;
return; }
1243 const double w = 1e-4;
1244 latency_ =
static_cast<int>(std::lround(iirDelay(w,
true)));
1245 decimationDelay_ =
static_cast<int>(std::lround(iirDelay(w,
false) * factor_));
1248 int latency = 0, decimation = 0;
1249 for (
int s = 0; s < numStages_; ++s)
1251 const int halfOrder = (stageDesign(s).taps - 1) / 2;
1252 latency += (halfOrder + 1) / (1 << s);
1253 decimation += halfOrder * (factor_ >> (s + 1));
1256 decimationDelay_ = decimation;
1265 StageDesign firstStage_;
1266 double firstTransition_;
1268 int decimationDelay_ = 0;
1269 AudioSpec baseSpec_ {};
1270 AudioBuffer<T, MaxChannels> upBuffer_;
1272 std::array<PolyphaseHalfBand, kMaxStages> filters_;
1273 std::array<PolyphaseIirHalfBand, kMaxStages> iir_;