160 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0))
return;
161 for (Bank& bank : bank_)
162 if (!bank.cascade) bank.cascade.reset(
new (std::nothrow)
Cascade());
163 if (!bank_[0].cascade || !bank_[1].cascade)
return;
164 for (Bank& bank : bank_) clear(bank);
166 sampleRate_.store(sampleRate, std::memory_order_relaxed);
167 rampSamples_ = std::max(1,
static_cast<int>(
kRampSeconds * sampleRate));
168 stretch_ = std::max(8,
static_cast<int>(std::lround(
kStretchSeconds * sampleRate)));
169 fadeStep_ = 1.0 /
static_cast<double>(rampSamples_);
170 for (Glide& g : glide_) g.moving =
false;
174 stretchLeft_ = stretch_;
184 dirty_.store(
true, std::memory_order_release);
195 if (!bank_[0].cascade || !bank_[1].cascade)
return;
196 if (changing_) finishChange();
197 for (Bank& bank : bank_) bank.cascade->reset();
198 compNow_ = compTarget_;
216 if (band < 0 || band >=
kBands)
return;
217 Published& p = published_[
static_cast<size_t>(band)];
218 p.shape.store(std::clamp(
static_cast<int>(settings.shape), 0,
static_cast<int>(
Shape::Tilt)),
219 std::memory_order_relaxed);
220 p.placement.store(std::clamp(
static_cast<int>(settings.placement), 0,
static_cast<int>(
Placement::Side)),
221 std::memory_order_relaxed);
222 if (std::isfinite(settings.frequency) && settings.frequency > T(0))
223 p.frequency.store(std::clamp(settings.frequency, T(10), T(40000)), std::memory_order_relaxed);
224 if (std::isfinite(settings.gainDb))
225 p.gainDb.store(std::clamp(settings.gainDb, T(-24), T(24)), std::memory_order_relaxed);
226 if (std::isfinite(settings.q))
227 p.q.store(std::clamp(settings.q, T(0.1), T(20)), std::memory_order_relaxed);
228 if (std::isfinite(settings.slope))
229 p.slope.store(std::clamp(settings.slope, T(-48), T(48)), std::memory_order_relaxed);
230 dirty_.store(
true, std::memory_order_release);
237 if (band < 0 || band >=
kBands)
return out;
238 const Published& p = published_[
static_cast<size_t>(band)];
239 out.
shape =
static_cast<Shape>(p.shape.load(std::memory_order_relaxed));
240 out.
frequency = p.frequency.load(std::memory_order_relaxed);
241 out.
gainDb = p.gainDb.load(std::memory_order_relaxed);
242 out.
q = p.q.load(std::memory_order_relaxed);
243 out.
slope = p.slope.load(std::memory_order_relaxed);
251 for (
const Published& p : published_)
252 if (p.shape.load(std::memory_order_relaxed) !=
static_cast<int>(
Shape::Off))
return true;
270 out.
sampleRate = sampleRate_.load(std::memory_order_relaxed);
272 for (
int b = 0; b <
kBands; ++b)
289 loudnessComp_.store(on, std::memory_order_relaxed);
290 dirty_.store(
true, std::memory_order_release);
297 [[nodiscard]]
double getCompensationDb() const noexcept {
return compensationDb_.load(std::memory_order_relaxed); }
307 for (
int b = 0; b <
kBands; ++b)
310 std::snprintf(key,
sizeof(key),
"%s%d.shape", prefix, b);
311 w.
write(key,
static_cast<int32_t
>(band.
shape));
312 std::snprintf(key,
sizeof(key),
"%s%d.freq", prefix, b);
314 std::snprintf(key,
sizeof(key),
"%s%d.gain", prefix, b);
316 std::snprintf(key,
sizeof(key),
"%s%d.q", prefix, b);
317 w.
write(key,
static_cast<float>(band.
q));
318 std::snprintf(key,
sizeof(key),
"%s%d.slope", prefix, b);
320 std::snprintf(key,
sizeof(key),
"%s%d.place", prefix, b);
323 std::snprintf(key,
sizeof(key),
"%sloudness", prefix);
332 for (
int b = 0; b <
kBands; ++b)
335 std::snprintf(key,
sizeof(key),
"%s%d.shape", prefix, b);
336 band.
shape =
static_cast<Shape>(std::clamp(
static_cast<int>(r.read(key, int32_t(0))), 0,
338 std::snprintf(key,
sizeof(key),
"%s%d.freq", prefix, b);
339 band.
frequency =
static_cast<T
>(r.read(key, 1000.0f));
340 std::snprintf(key,
sizeof(key),
"%s%d.gain", prefix, b);
341 band.
gainDb =
static_cast<T
>(r.read(key, 0.0f));
342 std::snprintf(key,
sizeof(key),
"%s%d.q", prefix, b);
343 band.
q =
static_cast<T
>(r.read(key, 0.7071f));
344 std::snprintf(key,
sizeof(key),
"%s%d.slope", prefix, b);
345 band.
slope =
static_cast<T
>(r.read(key, 12.0f));
346 std::snprintf(key,
sizeof(key),
"%s%d.place", prefix, b);
347 band.
placement =
static_cast<Placement>(std::clamp(
static_cast<int>(r.read(key, int32_t(0))), 0,
351 std::snprintf(key,
sizeof(key),
"%sloudness", prefix);
363 if (!bank_[0].cascade || !bank_[1].cascade)
return false;
364 if (dirty_.load(std::memory_order_relaxed) && dirty_.exchange(
false, std::memory_order_acquire))
367 refreshCompensation();
374 [[nodiscard]]
bool isActive() const noexcept {
return active_; }
378 [[nodiscard]]
bool isPlaced() const noexcept {
return placed_; }
382 [[nodiscard]]
int getStretch() const noexcept {
return stretch_; }
397 if (!active_)
return input;
398 return static_cast<T
>(run(
static_cast<double>(input), channel,
Placement::Stereo) * compNow_);
420 if (!active_)
return;
421 const int nCh = std::min(channels, MaxChannels);
422 const bool parts = nCh == 2 && placed_;
423 if (parts != inParts_) turn(parts);
426 const double left =
static_cast<double>(frame[0]), right =
static_cast<double>(frame[1]);
429 frame[0] =
static_cast<T
>((mid + side) * compNow_);
430 frame[1] =
static_cast<T
>((mid - side) * compNow_);
434 for (
int ch = 0; ch < nCh; ++ch)
435 frame[ch] =
static_cast<T
>(run(
static_cast<double>(frame[ch]), ch, part) * compNow_);
446 if (!active_ || samples <= 0)
return;
449 const int n = std::min(samples, compLeft_);
451 compNow_ = compLeft_ == 0 ? compTarget_ : compNow_ + compStep_ *
static_cast<double>(n);
453 Cascade& live = *bank_[
static_cast<size_t>(live_)].cascade;
457 stretchLeft_ -= samples;
458 if (stretchLeft_ <= 0) nextStretch();
462 fade_ += fadeStep_ *
static_cast<double>(samples);
464 if (fade_ >= 1.0) finishChange();
473 std::atomic<int> shape { 0 };
474 std::atomic<int> placement { 0 };
475 std::atomic<T> frequency { T(1000) };
476 std::atomic<T> gainDb { T(0) };
477 std::atomic<T> q { T(0.7071) };
478 std::atomic<T> slope { T(12) };
484 std::unique_ptr<Cascade> cascade;
485 std::uint64_t midOnly = 0;
486 std::uint64_t sideOnly = 0;
505 [[nodiscard]]
static int design(
const Band& band,
const FilterDesign::Band& fit, BiquadCoeffs* out,
506 bool keepFlat)
noexcept
508 const double hz = std::clamp(
static_cast<double>(band.frequency), 10.0, 0.45 * fit.getSampleRate());
509 const double gainDb =
static_cast<double>(band.gainDb);
510 const double q =
static_cast<double>(band.q);
511 const double slope =
static_cast<double>(band.slope);
512 const double steep = std::clamp(std::abs(slope), 6.0, 48.0);
513 const int shelfOrder = std::clamp(
static_cast<int>(std::lround(std::abs(slope) / 6.0)), 1, 4);
514 const bool shaped = keepFlat || std::abs(gainDb) >= 1e-6;
531 [[nodiscard]]
static bool same(
const Band& a,
const Band& b)
noexcept
533 return a.shape == b.shape && a.placement == b.placement && a.frequency == b.frequency
534 && a.gainDb == b.gainDb && a.q == b.q && a.slope == b.slope;
542 [[nodiscard]]
static Band between(
const Band& from,
const Band& to,
double p)
noexcept
544 if (p >= 1.0)
return to;
545 const auto geometric = [p](T a, T b) {
546 const double la = std::log(
static_cast<double>(a)), lb = std::log(
static_cast<double>(b));
547 return static_cast<T
>(std::exp(la + (lb - la) * p));
549 const auto linear = [p](T a, T b) {
550 return static_cast<T
>(
static_cast<double>(a) + (
static_cast<double>(b) -
static_cast<double>(a)) * p);
553 at.frequency = geometric(from.frequency, to.frequency);
554 at.q = geometric(from.q, to.q);
555 at.gainDb = linear(from.gainDb, to.gainDb);
556 at.slope = linear(from.slope, to.slope);
561 static void clear(Bank& bank)
noexcept
566 bank.cascade->setCoeffs(s, BiquadCoeffs {});
567 bank.cascade->setSectionEnabled(s,
false);
569 bank.cascade->reset();
577 void build(Bank& bank,
const std::array<Band, kBands>& bands, std::array<int, kBands>& counts)
noexcept
582 for (
int b = 0; b <
kBands; ++b)
584 const Band& band = bands[
static_cast<size_t>(b)];
586 const int count = design(band, fit_, sections,
false);
587 counts[
static_cast<size_t>(b)] = count;
593 bank.cascade->setCoeffs(s, sections[j]);
594 bank.cascade->setSectionEnabled(s,
true);
595 const std::uint64_t bit = std::uint64_t { 1 } << s;
602 bank.cascade->setSectionEnabled(s,
false);
606 bank.cascade->reset();
623 void refreshCompensation() noexcept
626 const double fs = sampleRate_.load(std::memory_order_relaxed);
627 if (loudnessComp_.load(std::memory_order_relaxed))
629 constexpr int kPoints = 96;
633 const double top = std::min(20000.0, 0.45 * fs);
634 double num = 0.0, den = 0.0;
635 for (
int i = 0; i < kPoints; ++i)
637 const double hz = 20.0 * std::pow(top / 20.0,
static_cast<double>(i) / (kPoints - 1));
638 const double k = shelf.getMagnitude(hz, fs) * low.getMagnitude(hz, fs);
639 const double w = k * k, g = std::pow(10.0, r.magnitudeDb(hz) / 10.0);
643 if (num > 0.0 && den > 0.0) target = std::sqrt(den / num);
645 compensationDb_.store(20.0 * std::log10(target), std::memory_order_relaxed);
649 compTarget_ = compNow_ = target;
653 if (target == compTarget_)
return;
654 compTarget_ = target;
655 const int ramp = std::max(1,
static_cast<int>(0.05 * fs));
656 compStep_ = (compTarget_ - compNow_) /
static_cast<double>(ramp);
660 void apply() noexcept
662 if (changing_) { waiting_ =
true;
return; }
664 std::array<Band, kBands> wanted;
665 for (
int b = 0; b < kBands; ++b) wanted[static_cast<size_t>(b)] =
getBand(b);
667 Bank& live = bank_[
static_cast<size_t>(live_)];
670 build(live, wanted, count_);
677 bool sameForm =
true;
678 for (
int b = 0; b <
kBands && sameForm; ++b)
680 const auto index =
static_cast<size_t>(b);
681 const Band& now = goal_[index];
682 const Band& next = wanted[index];
683 if (same(now, next))
continue;
685 sameForm = next.shape == now.shape && next.placement == now.placement
686 && design(next, fit_, sections,
false) == count_[index];
688 if (!sameForm) { startChange(wanted);
return; }
691 const int elapsed = stretch_ - stretchLeft_;
692 bool started =
false;
693 for (
int b = 0; b <
kBands; ++b)
695 const auto index =
static_cast<size_t>(b);
696 if (same(goal_[index], wanted[index]) || count_[index] == 0)
698 goal_[index] = wanted[index];
701 Glide& g = glide_[index];
702 g.from = g.moving ? between(g.from, g.to, progress(g, elapsed)) : goal_[index];
703 g.to = wanted[index];
704 g.left = rampSamples_ + elapsed;
706 goal_[index] = wanted[index];
717 [[nodiscard]]
double progress(
const Glide& g,
int elapsed)
const noexcept
719 return std::clamp(1.0 -
static_cast<double>(g.left - elapsed) /
static_cast<double>(rampSamples_), 0.0, 1.0);
727 void nextStretch() noexcept
729 const int elapsed = stretch_ - stretchLeft_;
730 stretchLeft_ = stretch_;
731 Cascade& live = *bank_[
static_cast<size_t>(live_)].cascade;
732 bool any =
false, lost =
false;
733 for (
int b = 0; b <
kBands; ++b)
735 const auto index =
static_cast<size_t>(b);
736 Glide& g = glide_[index];
737 if (!g.moving)
continue;
739 const int after = g.left - stretch_;
740 const double p = after > 0 ? 1.0 -
static_cast<double>(after) /
static_cast<double>(rampSamples_) : 1.0;
742 const int count = design(between(g.from, g.to, p), fit_, sections,
true);
743 if (count != count_[index])
750 const int length = std::max(1, std::min(stretch_, g.left));
751 for (
int j = 0; j < count; ++j)
753 if (after > 0) any =
true;
754 else g.moving =
false;
757 if (lost && !changing_) startChange(goal_);
768 void startChange(
const std::array<Band, kBands>& wanted)
noexcept
770 Bank& next = bank_[
static_cast<size_t>(1 - live_)];
771 build(next, wanted, count_);
773 for (Glide& g : glide_) g.moving = false;
782 void finishChange() noexcept
784 clear(bank_[
static_cast<size_t>(live_)]);
788 stretchLeft_ = stretch_;
793 dirty_.store(
true, std::memory_order_release);
797 void refresh() noexcept
799 const Bank& live = bank_[
static_cast<size_t>(live_)];
800 const Bank& next = bank_[
static_cast<size_t>(1 - live_)];
801 active_ = live.any || changing_;
802 placed_ = (live.midOnly | live.sideOnly) != 0 || (changing_ && (next.midOnly | next.sideOnly) != 0);
810 void turn(
bool parts)
noexcept
812 for (Bank& bank : bank_) bank.cascade->turnPairMemory(0, 1, !parts);
817 [[nodiscard]]
static std::uint64_t leftOut(
const Bank& bank,
Placement part)
noexcept
824 [[nodiscard]]
double run(
double x,
int channel,
Placement part)
noexcept
826 Bank& live = bank_[
static_cast<size_t>(live_)];
827 const double out = live.any ? live.cascade->processSample(x, channel, leftOut(live, part)) : x;
828 if (!changing_)
return out;
830 Bank& next = bank_[
static_cast<size_t>(1 - live_)];
831 int& at = phase_[
static_cast<size_t>(channel)];
832 const double w = std::min(1.0, fade_ + fadeStep_ *
static_cast<double>(at));
834 const double in = w * x;
835 const double entering = next.any ? next.cascade->processSample(in, channel, leftOut(next, part)) : in;
836 return (1.0 - w) * out + entering;
839 std::array<Published, kBands> published_ {};
840 std::atomic<bool> dirty_ {
true };
841 std::atomic<double> sampleRate_ { 48000.0 };
842 std::atomic<bool> loudnessComp_ {
false };
843 std::atomic<double> compensationDb_ { 0.0 };
844 double compTarget_ = 1.0, compNow_ = 1.0, compStep_ = 0.0;
846 bool compSnap_ =
true;
849 std::array<Bank, 2> bank_ {};
850 FilterDesign::Band fit_;
851 std::array<Band, kBands> goal_ {};
852 std::array<int, kBands> count_ {};
853 std::array<Glide, kBands> glide_ {};
854 std::array<int, MaxChannels> phase_ {};
856 int rampSamples_ = 960;
858 int stretchLeft_ = 32;
859 double fade_ = 0.0, fadeStep_ = 1.0 / 960.0;
860 bool settled_ =
false;
861 bool gliding_ =
false;
862 bool changing_ =
false;
863 bool waiting_ =
false;
864 bool active_ =
false;
865 bool placed_ =
false;
866 bool inParts_ =
false;