192 void prepare(
double sourceRate,
double targetRate,
195 prepareTransactional(sourceRate, targetRate, quality,
196 channelStates_.size());
211 const size_t requestedChannels = spec.
numChannels > 0
214 prepareTransactional(spec.
sampleRate, targetRate, quality,
215 std::max(channelStates_.size(), requestedChannels));
226 resetChannelState(mono_);
227 for (
auto& cs : channelStates_)
228 resetChannelState(cs);
244 [[nodiscard]] std::vector<T>
process(
const T* input,
int inputLength)
246 if (table_.empty() || input ==
nullptr || inputLength <= 0)
return {};
247 std::vector<T> output(
static_cast<size_t>(outputLengthFor(inputLength)));
248 (void)
process(input, inputLength, output.data(),
static_cast<int64_t
>(output.size()));
268 int64_t
process(
const T* input,
int inputLength, T* output, int64_t outputCapacity)
270 if (table_.empty() || input ==
nullptr || inputLength <= 0
271 || output ==
nullptr || outputCapacity <= 0)
273 const int64_t outputLength = std::min<int64_t>(outputLengthFor(inputLength), outputCapacity);
276 if (outputLength <= 0)
280 runTwoStage(input, inputLength, 0, outputLength, output);
284 for (; outIdx < outputLength; ++outIdx)
292 const int64_t num = outIdx * stepM_;
293 intPos = num / phasesL_;
294 phase = num % phasesL_;
298 const double srcPos =
static_cast<double>(outIdx) / ratio_;
299 intPos =
static_cast<int64_t
>(srcPos);
300 frac = srcPos -
static_cast<double>(intPos);
302 if (intPos >= inputLength)
break;
303 output[outIdx] = interpolateOffline(input, inputLength,
static_cast<int>(intPos), phase, frac);
305 std::fill(output + outIdx, output + outputLength, T(0));
322 [[nodiscard]] std::vector<T>
processRange(
const T* input,
int inputLength,
323 int64_t firstOutput, int64_t count)
325 if (table_.empty() || input ==
nullptr || inputLength <= 0 || count <= 0)
return {};
326 std::vector<T> output(
static_cast<size_t>(count));
329 runTwoStage(input, inputLength, firstOutput, count, output.data());
332 for (int64_t i = 0; i < count; ++i)
334 const int64_t outIdx = firstOutput + i;
341 const int64_t num = outIdx * stepM_;
342 intPos = num / phasesL_;
343 if (num % phasesL_ != 0 && num < 0) --intPos;
344 phase = num - intPos * phasesL_;
348 const double srcPos =
static_cast<double>(outIdx) / ratio_;
349 const double fl = std::floor(srcPos);
350 intPos =
static_cast<int64_t
>(fl);
354 const int64_t firstTap = intPos - halfTaps_ + 1;
355 if (firstTap + taps_ <= 0 || firstTap >= inputLength)
continue;
356 output[
static_cast<size_t>(i)] =
357 interpolateOffline(input, inputLength,
static_cast<int>(intPos), phase, frac);
373 const TwoStageShape s = twoStageShape(sourceRate_, targetRate_, ratio_, quality_);
376 return static_cast<int64_t
>(std::ceil(
static_cast<double>(s.half1 + s.stage2.taps / 2 + 1)
377 * ratio_ /
static_cast<double>(s.up))) + 1;
379 return static_cast<int64_t
>(std::ceil((taps_ - halfTaps_ + 1) * ratio_)) + 1;
392 return processChannel(input, inputLength, output, mono_);
406 int numCh = std::min(input.getNumChannels(), output.getNumChannels());
407 int inLen = input.getNumSamples();
409 assert(
static_cast<int>(channelStates_.size()) >= numCh &&
410 "Resampler channels not allocated! Call prepare(spec...) first.");
413 numCh = std::min(numCh,
static_cast<int>(channelStates_.size()));
416 for (
int ch = 0; ch < numCh; ++ch)
418 outCount = processChannel(input.getChannel(ch), inLen,
419 output.getChannel(ch),
420 channelStates_[
static_cast<size_t>(ch)]);
433 const double maxOut =
434 std::ceil(
static_cast<double>(inputLength) * ratio_) + 2.0;
435 return static_cast<int>(
436 std::min(maxOut,
static_cast<double>(std::numeric_limits<int>::max())));
440 [[nodiscard]]
double getRatio() const noexcept {
return ratio_; }
455 return static_cast<int>(std::round(
static_cast<double>(halfTaps_) * ratio_));
461 [[nodiscard]]
int outputLengthFor(
int inputLength)
const noexcept
463 const double n = std::ceil(
static_cast<double>(inputLength) * ratio_);
464 return static_cast<int>(std::min(n,
static_cast<double>(std::numeric_limits<int>::max())));
468 static constexpr int kTablePhases = 512;
471 static constexpr int64_t kMaxExactPhases = 4096;
472 static constexpr int64_t kMaxExactCoefficients = int64_t(1) << 21;
476 std::vector<T> history;
478 double fractionalPos = 0.0;
482 struct Spec {
double passband;
double attenuationDb; };
484 static_assert(std::is_nothrow_copy_assignable_v<T>,
485 "Resampler sample storage must be reset without throwing");
486 static_assert(std::is_nothrow_swappable_v<ChannelState>,
487 "Resampler state commit must be no-throw");
488 static_assert(std::is_nothrow_swappable_v<std::vector<T>>,
489 "Resampler table commit must be no-throw");
490 static_assert(std::is_nothrow_swappable_v<std::vector<ChannelState>>,
491 "Resampler channel commit must be no-throw");
493 static Spec qualitySpec(
Quality q)
noexcept
502 return { 0.90, 100.0 };
506 [[nodiscard]]
static double kaiserBeta(
double a)
noexcept
508 if (a > 50.0)
return 0.1102 * (a - 8.7);
509 if (a >= 21.0)
return 0.5842 * std::pow(a - 21.0, 0.4) + 0.07886 * (a - 21.0);
514 [[nodiscard]]
static double besselI0(
double x)
noexcept
519 static constexpr std::array<double, 201> kInvSquares = [] {
520 std::array<double, 201> a {};
521 for (
int k = 1; k <= 200; ++k) a[static_cast<size_t>(k)] = 1.0 / (
static_cast<double>(k) *
static_cast<double>(k));
524 const double y = 0.25 * x * x;
525 double sum = 1.0, term = 1.0;
526 for (
int k = 1; k <= 200; ++k)
528 term *= y * kInvSquares[
static_cast<size_t>(k)];
530 if (term < 1e-17 * sum)
break;
541 bool identity =
false;
544 [[nodiscard]]
static Design design(
double ratio,
Quality quality)
noexcept
552 const Spec spec = qualitySpec(quality);
553 const double scale = std::min(1.0, ratio);
556 const double transition = 0.5 * scale * (1.0 - spec.passband);
557 d.cutoff = scale * 0.5 * (1.0 + spec.passband);
558 d.beta = kaiserBeta(spec.attenuationDb);
560 const double n = (spec.attenuationDb - 7.95) / (14.36 * transition);
561 const double capped = std::min(n,
static_cast<double>(
kMaxTaps));
562 d.taps = 2 *
static_cast<int>(std::ceil(0.5 * capped));
563 d.taps = std::max(d.taps, 4);
576 template <
typename D>
577 static void buildPhase(D* dst,
const Design& d,
double frac)
579 const int half = d.taps / 2;
582 for (
int j = 0; j < d.taps; ++j)
583 dst[j] = (j == half - 1) ? D(1) : D(0);
586 constexpr double kPi = std::numbers::pi;
587 const double i0Beta = besselI0(d.beta);
590 for (
int j = 0; j < d.taps; ++j)
592 const double t =
static_cast<double>(j - half + 1) - frac;
593 const double x = t * d.cutoff;
594 const double sincVal = (std::abs(x) < 1e-12)
596 : d.cutoff * std::sin(kPi * x) / (kPi * x);
597 const double wx = t /
static_cast<double>(half);
598 const double win = (std::abs(wx) >= 1.0)
600 : besselI0(d.beta * std::sqrt(1.0 - wx * wx)) / i0Beta;
601 dst[j] =
static_cast<D
>(sincVal * win);
602 sum += sincVal * win;
604 if (std::abs(sum) > 1e-12)
606 const double inv = 1.0 / sum;
607 for (
int j = 0; j < d.taps; ++j)
608 dst[j] =
static_cast<D
>(
static_cast<double>(dst[j]) * inv);
621 template <
typename D>
622 static void buildTable(D* dst,
const Design& d, int64_t L, int64_t offset, int64_t numPhases)
624 const auto taps =
static_cast<size_t>(d.taps);
627 for (int64_t p = 0; p < numPhases; ++p)
628 buildPhase(dst +
static_cast<size_t>(p) * taps, d,
629 static_cast<double>(p + offset) /
static_cast<double>(L));
632 constexpr double kPi = std::numbers::pi;
633 const int half = d.taps / 2;
634 const double i0Beta = besselI0(d.beta);
635 const double scale = 1.0 /
static_cast<double>(L);
637 const int64_t kMax =
static_cast<int64_t
>(half + 1) * L + numPhases + 2;
638 std::vector<double> grid(
static_cast<size_t>(kMax) + 1, 0.0);
639 const int64_t kWindow =
static_cast<int64_t
>(half) * L;
640 for (int64_t k = 0; k < std::min(kWindow, kMax + 1); ++k)
642 const double t =
static_cast<double>(k) * scale;
643 const double x = t * d.cutoff;
644 const double sincVal = (x < 1e-12) ? d.cutoff : d.cutoff * std::sin(kPi * x) / (kPi * x);
645 const double wx = t /
static_cast<double>(half);
646 grid[
static_cast<size_t>(k)] = sincVal * besselI0(d.beta * std::sqrt(1.0 - wx * wx)) / i0Beta;
648 for (int64_t p = 0; p < numPhases; ++p)
650 D* row = dst +
static_cast<size_t>(p) * taps;
652 int64_t k =
static_cast<int64_t
>(1 - half) * L - (p + offset);
653 for (
size_t j = 0; j < taps; ++j, k += L)
655 const double v = grid[
static_cast<size_t>(k < 0 ? -k : k)];
656 row[j] =
static_cast<D
>(v);
659 if (std::abs(sum) > 1e-12)
661 const double inv = 1.0 / sum;
662 for (
size_t j = 0; j < taps; ++j)
663 row[j] =
static_cast<D
>(
static_cast<double>(row[j]) * inv);
672 [[nodiscard]]
static std::pair<int64_t, int64_t> rationalRatio(
673 double sourceRate,
double targetRate)
noexcept
676 const double rs = std::round(sourceRate), rt = std::round(targetRate);
677 if (std::abs(rs - sourceRate) < 1e-9 && std::abs(rt - targetRate) < 1e-9
678 && rs < 1e12 && rt < 1e12)
680 int64_t a =
static_cast<int64_t
>(rt), b =
static_cast<int64_t
>(rs);
681 int64_t x = a, y = b;
682 while (y != 0) {
const int64_t r = x % y; x = y; y = r; }
683 if (x > 0)
return { a / x, b / x };
687 const double r = targetRate / sourceRate;
688 int64_t h0 = 0, h1 = 1, k0 = 1, k1 = 0;
690 for (
int it = 0; it < 64; ++it)
692 const double fl = std::floor(v);
693 if (fl > 1e12)
break;
694 const auto an =
static_cast<int64_t
>(fl);
695 const int64_t h2 = an * h1 + h0, k2 = an * k1 + k0;
696 if (k2 > (int64_t(1) << 32) || h2 > (int64_t(1) << 32))
break;
697 h0 = h1; h1 = h2; k0 = k1; k1 = k2;
698 if (std::abs(
static_cast<double>(h1) /
static_cast<double>(k1) - r) <= 1e-15 * r)
700 const double rem = v - fl;
701 if (rem < 1e-15)
break;
707 static void initialiseChannelState(ChannelState& state,
int taps)
709 state.history.assign(
static_cast<size_t>(taps * 2), T(0));
711 state.fractionalPos = 0.0;
715 void prepareTransactional(
double sourceRate,
double targetRate,
716 Quality quality,
size_t channelCount)
718 if (!std::isfinite(sourceRate) || !std::isfinite(targetRate))
return;
723 const double stagedSourceRate = std::max(sourceRate, 1.0);
724 const double stagedTargetRate = std::max(targetRate, 1.0);
725 const double stagedRatio = stagedTargetRate / stagedSourceRate;
727 const Design d = design(stagedRatio, quality);
729 auto [l, m] = rationalRatio(stagedSourceRate, stagedTargetRate);
730 const bool exact = d.identity
731 || (l > 0 && l <= kMaxExactPhases
732 && l *
static_cast<int64_t
>(d.taps) <= kMaxExactCoefficients);
733 if (d.identity) { l = 1; m = 1; }
738 std::vector<T> stagedTable;
742 stagedTable.resize(
static_cast<size_t>(l * d.taps));
743 buildTable(stagedTable.data(), d, l, 0, l);
750 stagedTable.resize(
static_cast<size_t>((kTablePhases + 3) * d.taps));
751 buildTable(stagedTable.data(), d, kTablePhases, -1, kTablePhases + 3);
753 ChannelState stagedMono;
754 initialiseChannelState(stagedMono, d.taps);
755 std::vector<ChannelState> stagedChannels(channelCount);
756 for (
auto& state : stagedChannels)
757 initialiseChannelState(state, d.taps);
762 sourceRate_ = stagedSourceRate;
763 targetRate_ = stagedTargetRate;
765 ratio_ = stagedRatio;
766 srcStep_ = 1.0 / stagedRatio;
768 halfTaps_ = d.taps / 2;
770 phasesL_ = exact ? l : 0;
771 stepM_ = exact ? m : 0;
772 table_.swap(stagedTable);
774 swap(mono_, stagedMono);
775 channelStates_.swap(stagedChannels);
779 T interpolateContiguous(
const T* src, int64_t phase,
double frac)
const noexcept
786 const double exactPhase = frac *
static_cast<double>(kTablePhases);
787 int p0 =
static_cast<int>(exactPhase);
788 if (p0 > kTablePhases - 1) p0 = kTablePhases - 1;
789 const double u = exactPhase -
static_cast<double>(p0);
790 const T* k = table_.data() +
static_cast<size_t>(p0) *
static_cast<size_t>(taps_);
796 const double wm = -u * (u - 1.0) * (u - 2.0) / 6.0;
797 const double w0 = (u + 1.0) * (u - 1.0) * (u - 2.0) / 2.0;
798 const double w1 = -(u + 1.0) * u * (u - 2.0) / 2.0;
799 const double w2 = (u + 1.0) * u * (u - 1.0) / 6.0;
800 return static_cast<T
>(wm * sm + w0 * s0 + w1 * s1 + w2 * s2);
804 T interpolateOffline(
const T* data,
int length,
int intPos,
805 int64_t phase,
double frac)
808 const int firstSrc = intPos - halfTaps_ + 1;
809 if (firstSrc >= 0 && firstSrc + taps_ <= length)
810 return interpolateContiguous(data + firstSrc, phase, frac);
814 std::vector<T>& w = edgeScratch_;
815 w.assign(
static_cast<size_t>(taps_), T(0));
816 for (
int j = 0; j < taps_; ++j)
818 const int srcIdx = firstSrc + j;
819 if (srcIdx >= 0 && srcIdx < length)
820 w[
static_cast<size_t>(j)] = data[srcIdx];
822 return interpolateContiguous(w.data(), phase, frac);
828 static constexpr int kTwoStagePhases = 2048;
835 double cutoff1 = 1.0;
849 static constexpr double kStage1MarginDb = 12.0;
850 static constexpr double kStage2MarginDb = 40.0;
852 [[nodiscard]]
static TwoStageShape twoStageShape(
double sourceRate,
double targetRate,
853 double ratio,
Quality quality)
noexcept
856 const Spec spec = qualitySpec(quality);
857 const double atten1 = spec.attenuationDb + kStage1MarginDb;
858 const double atten2 = spec.attenuationDb + kStage2MarginDb;
859 const double band = 0.5 * std::min(1.0, ratio);
862 s.up = ((1.0 + spec.passband) * band <= 0.5) ? 1 : 2;
863 const double b = band /
static_cast<double>(s.up);
865 const double order1 = std::min((atten1 - 7.95) / (14.36 * (1.0 - spec.passband) * b),
867 s.half1 = std::max(1,
static_cast<int>(std::ceil(0.5 * order1)));
868 s.cutoff1 = (1.0 + spec.passband) * b;
869 s.beta1 = kaiserBeta(atten1);
872 const double order2 = (atten2 - 7.95) / (14.36 * (1.0 - (1.0 + spec.passband) * b));
873 s.stage2.taps = std::max(4, 4 *
static_cast<int>(std::ceil(0.25 * order2)));
874 s.stage2.cutoff = spec.passband * b + 1.0 - b;
875 s.stage2.beta = kaiserBeta(atten2);
876 const auto [l, m] = rationalRatio(sourceRate *
static_cast<double>(s.up), targetRate);
894 double sourceRate = 0.0;
895 double targetRate = 0.0;
903 double midStep = 1.0;
904 std::vector<double> gain1;
905 std::vector<double> table2;
906 std::vector<int64_t> advance2;
907 std::vector<int32_t> cycleOf2;
908 std::optional<detail::fft::SplitFFT<double>> engine;
909 std::optional<detail::fft::SplitFFT<double>> engineHalf;
910 std::optional<detail::fft::SplitFFT<double>> engineDecim;
911 std::vector<double> mid;
915 [[nodiscard]]
bool useTwoStage()
918 if (!plan_.ready || plan_.sourceRate != sourceRate_ || plan_.targetRate != targetRate_
919 || plan_.quality != quality_)
922 TwoStagePlan fresh = buildTwoStagePlan(sourceRate_, targetRate_, ratio_, quality_);
923 plan_ = std::move(fresh);
928 [[nodiscard]]
static TwoStagePlan buildTwoStagePlan(
double sourceRate,
double targetRate,
931 constexpr double kPi = std::numbers::pi;
933 p.sourceRate = sourceRate;
934 p.targetRate = targetRate;
936 p.shape = twoStageShape(sourceRate, targetRate, ratio, quality);
937 const TwoStageShape& s = p.shape;
938 const int order1 = 2 * s.half1;
939 const int taps2 = s.stage2.taps;
943 while (n < 4 * order1 || n < 4 * taps2 + 16) n *= 2;
945 p.block = n - order1;
951 std::vector<double> h(
static_cast<size_t>(n), 0.0);
952 const double i0Beta = besselI0(s.beta1);
954 for (
int t = -s.half1; t <= s.half1; ++t)
956 const double x =
static_cast<double>(t) * s.cutoff1;
957 const double sincVal = (std::abs(x) < 1e-12)
959 : s.cutoff1 * std::sin(kPi * x) / (kPi * x);
960 const double wx =
static_cast<double>(t) /
static_cast<double>(s.half1);
961 const double win = besselI0(s.beta1 * std::sqrt(std::max(0.0, 1.0 - wx * wx))) / i0Beta;
962 h[
static_cast<size_t>(t >= 0 ? t : n + t)] = sincVal * win;
963 sum += sincVal * win;
966 const double gain =
static_cast<double>(s.up) / (sum *
static_cast<double>(n));
967 for (
auto& v : h) v *= gain;
969 FFTReal<double> fft(
static_cast<size_t>(n));
970 std::vector<double> spec(
static_cast<size_t>(n) + 2, 0.0);
971 fft.forward(h.data(), spec.data());
973 p.gain1.assign(
static_cast<size_t>(n), 0.0);
974 for (
int k = 0; k <= n / 2; ++k)
976 p.gain1[
static_cast<size_t>(k)] = spec[
static_cast<size_t>(2 * k)];
977 p.gain1[
static_cast<size_t>((n - k) % n)] = spec[
static_cast<size_t>(2 * k)];
983 p.engine.emplace(
static_cast<size_t>(n));
984 if (s.up == 2) p.engineHalf.emplace(
static_cast<size_t>(n / 2));
988 if (s.pick && s.up == 1 && s.step2 == 2) p.engineDecim.emplace(
static_cast<size_t>(n / 2));
991 const int64_t l = s.phases2, m = s.step2;
992 p.exact2 = l > 0 && l <= kMaxExactPhases
993 && l *
static_cast<int64_t
>(taps2) <= kMaxExactCoefficients;
994 p.midStep =
static_cast<double>(s.up) / ratio;
999 p.table2 = { 1.0, 0.0 };
1009 p.table2.resize(
static_cast<size_t>(l * taps2));
1010 p.advance2.resize(
static_cast<size_t>(l));
1011 p.cycleOf2.resize(
static_cast<size_t>(l));
1012 for (int64_t k = 0; k < l; ++k)
1014 const int64_t ph = (k * (m % l)) % l;
1015 buildPhase(p.table2.data() + k * taps2, s.stage2,
1016 static_cast<double>(ph) /
static_cast<double>(l));
1017 p.advance2[
static_cast<size_t>(k)] = m / l + (ph + m % l >= l ? 1 : 0);
1018 p.cycleOf2[
static_cast<size_t>(ph)] =
static_cast<int32_t
>(k);
1024 p.table2.resize(
static_cast<size_t>(kTwoStagePhases + 3) *
static_cast<size_t>(taps2));
1025 for (
int ph = 0; ph < kTwoStagePhases + 3; ++ph)
1026 buildPhase(p.table2.data() +
static_cast<size_t>(ph) *
static_cast<size_t>(taps2), s.stage2,
1027 static_cast<double>(ph - 1) /
static_cast<double>(kTwoStagePhases));
1031 p.mid.assign(
static_cast<size_t>(taps2 + s.up + 2 * p.block), 0.0);
1049 void twoStagePair(
const T* input,
int inputLength, int64_t s,
double* dst)
noexcept
1051 TwoStagePlan& p = plan_;
1052 const int n = p.fftSize, up = p.shape.up, half1 = p.shape.half1, block = p.block;
1053 const int frameInputs = n / up;
1054 const int64_t q0 = (s - half1) / up;
1055 const int64_t q1 = q0 + block / up;
1056 if (std::max<int64_t>(q0, 0) >= std::min<int64_t>(q1 + frameInputs, inputLength))
1058 std::fill(dst, dst + 2 * block, 0.0);
1061 detail::fft::SplitFFT<double>& first = (up == 1) ? *p.engine : *p.engineHalf;
1062 const double* zr =
nullptr;
1063 const double* zi =
nullptr;
1064 bool direct =
false;
1065 if constexpr (std::is_same_v<T, double>)
1068 if (q0 >= 0 && q1 + frameInputs <= inputLength)
1070 first.runFromSplit(input + q0, input + q1, zr, zi);
1076 const auto load = [&](
double* frame, int64_t q)
noexcept {
1077 const int64_t a = std::clamp<int64_t>(-q, 0, frameInputs);
1078 const int64_t b = std::clamp<int64_t>(inputLength - q, a, frameInputs);
1079 std::fill(frame, frame + a, 0.0);
1080 for (int64_t t = a; t < b; ++t) frame[t] = static_cast<double>(input[q + t]);
1081 std::fill(frame + b, frame + frameInputs, 0.0);
1083 load(first.inRe(), q0);
1084 load(first.inIm(), q1);
1091 const double* wr =
nullptr;
1092 const double* wi =
nullptr;
1098 p.engineDecim->runScaledSwapFolded(zr, zi, p.gain1.data(), 2, wr, wi);
1099 for (
int i = half1 & 1; i < block; i += 2)
1101 dst[i] = wi[(i + half1) / 2];
1102 dst[block + i] = wr[(i + half1) / 2];
1106 p.engine->runScaledSwap(zr, zi, p.gain1.data(),
static_cast<size_t>(n / up - 1), wr, wi);
1107 std::copy(wi + half1, wi + half1 + block, dst);
1108 std::copy(wr + half1, wr + half1 + block, dst + block);
1112 [[nodiscard]]
double twoStageTable(
const double* src,
double frac)
const noexcept
1114 const int taps2 = plan_.shape.stage2.taps;
1115 const double exactPhase = frac *
static_cast<double>(kTwoStagePhases);
1116 int p0 =
static_cast<int>(exactPhase);
1117 if (p0 > kTwoStagePhases - 1) p0 = kTwoStagePhases - 1;
1118 const double u = exactPhase -
static_cast<double>(p0);
1119 const double* k = plan_.table2.data() +
static_cast<size_t>(p0) *
static_cast<size_t>(taps2);
1120 const double*
const rows[4] = { k, k + taps2, k + 2 * taps2, k + 3 * taps2 };
1123 const double sm = y[0], s0 = y[1], s1 = y[2], s2 = y[3];
1124 const double wm = -u * (u - 1.0) * (u - 2.0) / 6.0;
1125 const double w0 = (u + 1.0) * (u - 1.0) * (u - 2.0) / 2.0;
1126 const double w1 = -(u + 1.0) * u * (u - 2.0) / 2.0;
1127 const double w2 = (u + 1.0) * u * (u - 1.0) / 6.0;
1128 return wm * sm + w0 * s0 + w1 * s1 + w2 * s2;
1138 void runTwoStage(
const T* input,
int inputLength, int64_t firstOutput, int64_t count, T* out)
1140 if (plan_.shape.pick) runTwoStageAs<true, true>(input, inputLength, firstOutput, count, out);
1141 else if (plan_.exact2) runTwoStageAs<true, false>(input, inputLength, firstOutput, count, out);
1142 else runTwoStageAs<false, false>(input, inputLength, firstOutput, count, out);
1145 template <
bool Exact,
bool Pick>
1146 void runTwoStageAs(
const T* input,
int inputLength, int64_t firstOutput, int64_t count, T* out)
1150 TwoStagePlan& p = plan_;
1151 const int up = p.shape.up, half1 = p.shape.half1, block = p.block;
1152 const int taps2 = p.shape.stage2.taps, half2 = taps2 / 2;
1153 const int carry = taps2 + up;
1154 const int64_t pairBlock = 2 *
static_cast<int64_t
>(block);
1155 const int64_t l = p.phases2;
1156 const double midStep = p.midStep;
1158 const int64_t nzLo = -
static_cast<int64_t
>(half1);
1159 const int64_t nzHi =
static_cast<int64_t
>(up) * (inputLength - 1) + half1;
1160 double*
const mid = p.mid.data();
1161 int64_t origin = 0, bufStart = 0, bufEnd = 0;
1162 bool haveBuf =
false;
1167 int64_t anchor = 0, phase = 0, wholeStep = 0, remStep = 0;
1169 if constexpr (Exact)
1171 wholeStep = p.step2 / l;
1172 remStep = p.step2 % l;
1174 const int64_t r = firstOutput * remStep;
1175 int64_t q = r / l, rem = r % l;
1176 if (rem < 0) { rem += l; --q; }
1177 anchor = firstOutput * wholeStep + q;
1184 if constexpr (!Exact)
1186 const double pos =
static_cast<double>(firstOutput + i) * midStep;
1187 const double fl = std::floor(pos);
1188 anchor =
static_cast<int64_t
>(fl);
1192 int64_t w0 = anchor - half2 + 1;
1193 if (w0 + taps2 <= nzLo || w0 > nzHi)
1196 if constexpr (Exact)
1198 anchor += wholeStep;
1200 if (phase >= l) { phase -= l; ++anchor; }
1204 if (!haveBuf || w0 >= bufEnd)
1211 const int64_t d = w0 - half1;
1212 const int64_t j = d / pairBlock - ((d % pairBlock) < 0 ? 1 : 0);
1213 bufStart = bufEnd = half1 + j * pairBlock;
1216 while (bufEnd < w0 + taps2)
1220 const int64_t keep = std::max(w0, bufStart);
1221 const int r =
static_cast<int>(bufEnd - keep);
1222 if (r > 0) std::copy(mid + (keep - origin), mid + (bufEnd - origin), mid + (carry - r));
1223 twoStagePair(input, inputLength, bufEnd, mid + carry);
1224 origin = bufEnd - carry;
1226 bufEnd += pairBlock;
1229 const int64_t last = std::min(bufEnd - taps2, nzHi);
1230 if constexpr (Exact)
1232 const double* src = mid + (w0 - origin);
1237 out[i++] =
static_cast<T
>(*src);
1241 if (phase >= l) { phase -= l; ++src; ++w0; }
1242 }
while (i < count && w0 <= last);
1249 const double*
const rows = p.table2.data();
1250 const int64_t*
const advance = p.advance2.data();
1251 int64_t k = p.cycleOf2[
static_cast<size_t>(phase)];
1252 const double* row = rows + k * taps2;
1264 const int64_t cycleStep = p.step2;
1265 const int64_t batch = std::min<int64_t>(
1266 count - i, ((last - w0 + 1) * l - phase - 1) / cycleStep + 1);
1267 const int64_t cycles = batch / l, rest = batch % l;
1268 const int64_t spanned = cycles + (rest > 0 ? 1 : 0);
1269 const int64_t groups = (spanned + 7) / 8;
1270 for (int64_t g = 0, c0 = 0; g < groups; ++g)
1272 const int64_t size = (spanned - c0 + (groups - g) - 1) / (groups - g);
1273 const double* s = src + c0 * cycleStep;
1274 const double* r = row;
1276 const int64_t positions = std::min(l, batch - c0 * l);
1277 for (int64_t t = 0; t < positions; ++t)
1279 const int m =
static_cast<int>(std::min(size, cycles + (t < rest ? 1 : 0) - c0));
1282 T*
const o = out + i + c0 * l + t;
1283 for (
int x = 0; x < m; ++x) o[x * l] = static_cast<T>(y[x]);
1286 if (++kk == l) { kk = 0; r = rows; }
1291 for (int64_t t = 0; t < rest; ++t)
1294 if (++k == l) k = 0;
1296 src += cycles * cycleStep;
1297 w0 = origin + (src - mid);
1299 phase = (k * remStep) % l;
1301 anchor = w0 + half2 - 1;
1307 out[i++] =
static_cast<T
>(twoStageTable(mid + (w0 - origin), frac));
1308 if (i == count)
break;
1309 const double pos =
static_cast<double>(firstOutput + i) * midStep;
1310 const double fl = std::floor(pos);
1311 anchor =
static_cast<int64_t
>(fl);
1313 w0 = anchor - half2 + 1;
1314 }
while (w0 <= last);
1320 static void resetChannelState(ChannelState& cs)
noexcept
1322 std::fill(cs.history.begin(), cs.history.end(), T(0));
1324 cs.fractionalPos = 0.0;
1328 int processChannel(
const T* input,
int inputLength, T* output,
1329 ChannelState& state)
noexcept
1331 if (state.history.empty())
return 0;
1339 T*
const hist = state.history.data();
1340 const int n = taps_;
1341 int writePos = state.writePos;
1346 const int64_t l = phasesL_, m = stepM_;
1347 int64_t phase = state.phase;
1348 for (
int i = 0; i < inputLength; ++i)
1352 const T x = input[i];
1354 hist[writePos + n] = x;
1355 if (++writePos >= n)
1360 output[outIdx++] = interpolateContiguous(hist + writePos, phase, 0.0);
1365 state.phase = phase;
1369 const double step = srcStep_;
1370 double frac = state.fractionalPos;
1371 for (
int i = 0; i < inputLength; ++i)
1373 const T x = input[i];
1375 hist[writePos + n] = x;
1376 if (++writePos >= n)
1381 output[outIdx++] = interpolateContiguous(hist + writePos, 0, frac);
1386 state.fractionalPos = frac;
1389 state.writePos = writePos;
1393 double sourceRate_ = 44100.0;
1394 double targetRate_ = 48000.0;
1395 double ratio_ = 1.0;
1396 double srcStep_ = 1.0;
1401 int64_t phasesL_ = 0;
1407 std::vector<T> table_;
1408 std::vector<T> edgeScratch_;
1410 std::vector<ChannelState> channelStates_;