101 const int clamped = std::clamp(count, 0, MaxSections);
102 if (clamped == numSections_)
return;
103 const int before = numSections_;
104 numSections_ = clamped;
106 for (
int s = std::max(before, 0); s < clamped; ++s)
107 if (enabled_[
static_cast<size_t>(s)]) seedFromInput(s);
125 if (section < 0 || section >= MaxSections)
return;
126 const auto s =
static_cast<size_t>(section);
147 if (section < 0 || section >= MaxSections)
return;
148 if (samples <= 0) {
setCoeffs(section, target);
return; }
149 const auto s =
static_cast<size_t>(section);
151 rampLeft_[s] = samples;
152 const double inverse = 1.0 /
static_cast<double>(samples);
153 step_[s].b0 = (target.b0 - current_[s].b0) * inverse;
154 step_[s].b1 = (target.b1 - current_[s].b1) * inverse;
155 step_[s].b2 = (target.b2 - current_[s].b2) * inverse;
156 step_[s].a1 = (target.a1 - current_[s].a1) * inverse;
157 step_[s].a2 = (target.a2 - current_[s].a2) * inverse;
174 if (section < 0 || section >= MaxSections)
return;
175 rampCoeffs(section, target_[
static_cast<size_t>(section)].bypassed(), samples);
181 return current_[
static_cast<size_t>(std::clamp(section, 0, MaxSections - 1))];
187 return target_[
static_cast<size_t>(std::clamp(section, 0, MaxSections - 1))];
191 [[nodiscard]]
bool isRamping(
int section)
const noexcept
193 return section >= 0 && section < MaxSections && rampLeft_[static_cast<size_t>(section)] > 0;
197 [[nodiscard]]
bool isRamping() const noexcept {
return ramping_; }
213 if (section < 0 || section >= MaxSections)
return;
214 const auto s =
static_cast<size_t>(section);
215 if (enabled_[s] == on)
return;
218 if (on && section < numSections_) seedFromInput(section);
224 return section >= 0 && section < MaxSections && enabled_[static_cast<size_t>(section)];
230 for (
auto& channel : node_)
231 for (
auto& each : channel) each = {};
245 const int numChannels = std::min(buffer.getNumChannels(), MaxChannels);
246 const int numSamples = buffer.getNumSamples();
247 if (numChannels <= 0 || numSamples <= 0)
return;
251 for (
int ch = 0; ch < numChannels; ++ch)
253 const T* data = buffer.getChannel(ch);
254 auto& input = node_[
static_cast<size_t>(ch)][0];
255 if (numSamples >= 2) input.h2 =
static_cast<double>(data[numSamples - 2]);
256 else input.h2 = input.h1;
257 input.h1 =
static_cast<double>(data[numSamples - 1]);
265 while (pos < numSamples)
267 int length = std::min(kChunk, numSamples - pos);
269 for (
int k = 0; k < numActive_; ++k)
271 const int left = rampLeft_[
static_cast<size_t>(active_[
static_cast<size_t>(k)])];
272 if (left > 0) length = std::min(length, left);
276 if constexpr (kPairChannels)
277 for (; ch + 1 < numChannels; ch += 2)
278 runChunk<simd::kVecDoubleNarrow>(buffer.getChannel(ch) + pos, buffer.getChannel(ch + 1) + pos,
280 for (; ch < numChannels; ++ch)
281 runChunk<1>(buffer.getChannel(ch) + pos,
nullptr, length, ch, ch);
287 for (
int ch = 0; ch < numChannels; ++ch)
304 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
305 auto& nodes = node_[
static_cast<size_t>(channel)];
307 double u1 = nodes[0].h1, u2 = nodes[0].h2;
308 nodes[0].h2 = nodes[0].h1;
310 for (
int k = 0; k < numActive_; ++k)
312 const auto s =
static_cast<size_t>(active_[
static_cast<size_t>(k)]);
314 auto& out = nodes[s + 1];
315 const double p = (c.
b1 * u1 + c.
b2 * u2) - c.
a2 * out.h2;
316 const double y = (p + c.
b0 * u) - c.
a1 * out.h1;
342 [[nodiscard]]
double processSample(
double input,
int channel, std::uint64_t skip)
noexcept
344 static_assert(MaxSections <= 64,
"the mask has one bit per section");
345 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
346 auto& nodes = node_[
static_cast<size_t>(channel)];
348 double u1 = nodes[0].h1, u2 = nodes[0].h2;
349 nodes[0].h2 = nodes[0].h1;
351 for (
int k = 0; k < numActive_; ++k)
353 const auto s =
static_cast<size_t>(active_[
static_cast<size_t>(k)]);
354 if ((skip & (std::uint64_t { 1 } << s)) != 0)
continue;
356 auto& out = nodes[s + 1];
357 const double p = (c.
b1 * u1 + c.
b2 * u2) - c.
a2 * out.h2;
358 const double y = (p + c.
b0 * u) - c.
a1 * out.h1;
388 if (first < 0 || second < 0 || first >= MaxChannels || second >= MaxChannels || first == second)
return;
389 auto& a = node_[
static_cast<size_t>(first)];
390 auto& b = node_[
static_cast<size_t>(second)];
391 const double scale = back ? 1.0 : 0.5;
392 for (
size_t n = 0; n < a.size(); ++n)
394 const double sum1 = a[n].h1 + b[n].h1, difference1 = a[n].h1 - b[n].h1;
395 const double sum2 = a[n].h2 + b[n].h2, difference2 = a[n].h2 - b[n].h2;
396 a[n].h1 = scale * sum1;
397 b[n].h1 = scale * difference1;
398 a[n].h2 = scale * sum2;
399 b[n].h2 = scale * difference2;
409 if (!ramping_ || samples <= 0)
return;
410 for (
int section = 0; section < MaxSections; ++section)
412 const auto s =
static_cast<size_t>(section);
413 if (rampLeft_[s] <= 0)
continue;
414 if (samples >= rampLeft_[s])
416 current_[s] = target_[s];
420 rampLeft_[s] -= samples;
424 current_[s] = next_[s];
429 const double count =
static_cast<double>(samples);
430 current_[s].b0 += step_[s].b0 * count;
431 current_[s].b1 += step_[s].b1 * count;
432 current_[s].b2 += step_[s].b2 * count;
433 current_[s].a1 += step_[s].a1 * count;
434 current_[s].a2 += step_[s].a2 * count;
442 static constexpr int kChunk = 64;
443 static constexpr int kMaxGroups = (MaxSections +
kGroup - 1) /
kGroup > 0 ? (MaxSections +
kGroup - 1) /
kGroup : 1;
444 static constexpr bool kPairChannels = simd::kVecDoubleNarrow == 2 &&
sizeof(
void*) == 8;
456 using O = simd::Vec<double, W>;
457 using V =
typename O::V;
458 static V pair(
double first,
double second)
noexcept
460 if constexpr (W == 1) { (void)second;
return first; }
461 else return O::set2(first, second);
463 static double first(V v)
noexcept
465 if constexpr (W == 1)
return v;
466 else return O::lane0(v);
468 static double second(V v)
noexcept
470 if constexpr (W == 1)
return v;
471 else return O::lane1(v);
477 void runChunk(T* left, T* right,
int length,
int chLeft,
int chRight)
noexcept
480 using V =
typename L::V;
482 if constexpr (W == 1)
485 for (
int i = 0; i < length; ++i) io[i] = static_cast<double>(left[i]);
489 for (
int i = 0; i < length; ++i)
490 io[i] = L::pair(
static_cast<double>(left[i]),
static_cast<double>(right[i]));
493 auto& nodesLeft = node_[
static_cast<size_t>(chLeft)];
494 auto& nodesRight = node_[
static_cast<size_t>(chRight)];
498 V startU1[kMaxGroups], startU2[kMaxGroups];
499 for (
int first = 0, group = 0; first < numActive_; first +=
kGroup, ++group)
501 const auto node =
static_cast<size_t>(input_[
static_cast<size_t>(active_[
static_cast<size_t>(first)])]);
502 startU1[group] = L::pair(nodesLeft[node].h1, nodesRight[node].h1);
503 startU2[group] = L::pair(nodesLeft[node].h2, nodesRight[node].h2);
506 for (
int first = 0, group = 0; first < numActive_; first +=
kGroup, ++group)
508 const int count = std::min(
kGroup, numActive_ - first);
510 for (
int k = 0; k < count; ++k)
511 moving = moving || rampLeft_[
static_cast<size_t>(active_[
static_cast<size_t>(first + k)])] > 0;
514 case 1: runGroup<1, W>(io, length, first, moving, startU1[group], startU2[group], nodesLeft, nodesRight);
break;
515 case 2: runGroup<2, W>(io, length, first, moving, startU1[group], startU2[group], nodesLeft, nodesRight);
break;
516 case 3: runGroup<3, W>(io, length, first, moving, startU1[group], startU2[group], nodesLeft, nodesRight);
break;
517 default: runGroup<4, W>(io, length, first, moving, startU1[group], startU2[group], nodesLeft, nodesRight);
break;
521 for (
int i = 0; i < length; ++i)
523 left[i] =
static_cast<T
>(L::first(io[i]));
524 if constexpr (W != 1) right[i] =
static_cast<T
>(L::second(io[i]));
536 template <
int M,
int W>
537 void runGroup(
typename Lanes<W>::V* io,
int length,
int first,
bool moving,
538 typename Lanes<W>::V u1,
typename Lanes<W>::V u2,
539 std::array<Node, MaxSections + 1>& nodesLeft,
540 std::array<Node, MaxSections + 1>& nodesRight)
noexcept
543 using O =
typename L::O;
544 using V =
typename L::V;
547 V b0[M], b1[M], b2[M], a1[M], a2[M];
549 for (
int k = 0; k < M; ++k)
551 section[k] =
static_cast<size_t>(active_[
static_cast<size_t>(first + k)]);
552 const BiquadCoeffs& c = current_[section[k]];
553 b0[k] = O::set1(c.b0); b1[k] = O::set1(c.b1); b2[k] = O::set1(c.b2);
554 a1[k] = O::set1(c.a1); a2[k] = O::set1(c.a2);
555 y1[k] = L::pair(nodesLeft[section[k] + 1].h1, nodesRight[section[k] + 1].h1);
556 y2[k] = L::pair(nodesLeft[section[k] + 1].h2, nodesRight[section[k] + 1].h2);
561 for (
int i = 0; i < length; ++i)
567 for (
int k = 0; k < M; ++k)
569 const V past = O::sub(O::add(O::mul(b1[k], p1), O::mul(b2[k], p2)), O::mul(a2[k], y2[k]));
570 const V y = O::sub(O::add(past, O::mul(b0[k], u)), O::mul(a1[k], y1[k]));
582 V s0[M], s1[M], s2[M], t1[M], t2[M];
583 for (
int k = 0; k < M; ++k)
585 const bool ramps = rampLeft_[section[k]] > 0;
586 const BiquadCoeffs& d = step_[section[k]];
587 s0[k] = O::set1(ramps ? d.b0 : 0.0); s1[k] = O::set1(ramps ? d.b1 : 0.0);
588 s2[k] = O::set1(ramps ? d.b2 : 0.0); t1[k] = O::set1(ramps ? d.a1 : 0.0);
589 t2[k] = O::set1(ramps ? d.a2 : 0.0);
591 for (
int i = 0; i < length; ++i)
597 for (
int k = 0; k < M; ++k)
599 b0[k] = O::add(b0[k], s0[k]); b1[k] = O::add(b1[k], s1[k]); b2[k] = O::add(b2[k], s2[k]);
600 a1[k] = O::add(a1[k], t1[k]); a2[k] = O::add(a2[k], t2[k]);
601 const V past = O::sub(O::add(O::mul(b1[k], p1), O::mul(b2[k], p2)), O::mul(a2[k], y2[k]));
602 const V y = O::sub(O::add(past, O::mul(b0[k], u)), O::mul(a1[k], y1[k]));
613 for (
int k = 0; k < M; ++k)
615 if (rampLeft_[section[k]] <= 0)
continue;
616 next_[section[k]].b0 = L::first(b0[k]); next_[section[k]].b1 = L::first(b1[k]);
617 next_[section[k]].b2 = L::first(b2[k]); next_[section[k]].a1 = L::first(a1[k]);
618 next_[section[k]].a2 = L::first(a2[k]);
619 moved_[section[k]] =
true;
627 nodesLeft[0].h1 = L::first(u1);
628 nodesLeft[0].h2 = L::first(u2);
630 for (
int k = 0; k < M; ++k)
632 nodesLeft[section[k] + 1].h1 = L::first(y1[k]);
633 nodesLeft[section[k] + 1].h2 = L::first(y2[k]);
635 if constexpr (W != 1)
639 nodesRight[0].h1 = L::second(u1);
640 nodesRight[0].h2 = L::second(u2);
642 for (
int k = 0; k < M; ++k)
644 nodesRight[section[k] + 1].h1 = L::second(y1[k]);
645 nodesRight[section[k] + 1].h2 = L::second(y2[k]);
651 void rebuildRoute() noexcept
655 for (
int s = 0; s < numSections_; ++s)
657 if (!enabled_[
static_cast<size_t>(s)])
continue;
658 active_[
static_cast<size_t>(numActive_++)] = s;
659 input_[
static_cast<size_t>(s)] = previous;
665 void seedFromInput(
int section)
noexcept
667 const auto from =
static_cast<size_t>(input_[
static_cast<size_t>(section)]);
668 for (
auto& channel : node_)
669 channel[static_cast<size_t>(section) + 1] = channel[from];
672 void updateRamping() noexcept
675 for (
int left : rampLeft_) ramping_ = ramping_ || left > 0;
679 void recover(
int channel)
noexcept
681 auto& nodes = node_[
static_cast<size_t>(channel)];
682 double sum = nodes[0].h1 + nodes[0].h2;
683 for (
int k = 0; k < numActive_; ++k)
685 const Node& each = nodes[
static_cast<size_t>(active_[
static_cast<size_t>(k)]) + 1];
686 sum += each.h1 + each.h2;
688 if (!std::isfinite(sum))
689 for (
auto& each : nodes) each = {};
692 std::array<BiquadCoeffs, MaxSections> current_ {};
693 std::array<BiquadCoeffs, MaxSections> target_ {};
694 std::array<BiquadCoeffs, MaxSections> step_ {};
695 std::array<BiquadCoeffs, MaxSections> next_ {};
696 std::array<bool, MaxSections> moved_ {};
697 std::array<int, MaxSections> rampLeft_ {};
698 std::array<bool, MaxSections> enabled_ {};
699 std::array<int, MaxSections> active_ {};
700 std::array<int, MaxSections> input_ {};
701 int numSections_ = 0;
703 bool ramping_ =
false;
705 std::array<std::array<Node, MaxSections + 1>, MaxChannels> node_ {};