107 filter_.prepare(spec);
108 filter_.setLinked(
true);
109 analysis_.setLinked(
true);
110 analysis_.prepare(spec);
111 hopSeconds_ =
static_cast<double>(filter_.getHopSize()) / spec.
sampleRate;
112 for (
auto& s : state_)
114 for (
auto* v : { &s.share, &s.shareDb, &s.action, &s.attack, &s.release }) v->assign(kSize, T(0));
119 for (
auto* v : { &power_, &level_, &held_, &total_ }) v->assign(kSize, T(0));
120 sum_.assign(kSize + 1, 0.0);
121 queue_.assign(kSize, 0);
124 for (
auto& d : dirty_) d.store(
true, std::memory_order_release);
133 for (
auto& s : state_)
135 std::fill(s.action.begin(), s.action.end(), T(0));
145 if (index < 0 || index >=
kBands)
return;
146 auto& p = published_[
static_cast<size_t>(index)];
147 p.shape.store(std::clamp(
static_cast<int>(band.shape), 0, 3), std::memory_order_relaxed);
148 const auto put = [](std::atomic<T>& a, T v, T lo, T hi) {
if (std::isfinite(v)) a.store(std::clamp(v, lo, hi), std::memory_order_relaxed); };
149 put(p.frequency, band.frequency, T(20), T(24000));
150 put(p.q, band.q, T(0.1), T(20));
151 put(p.threshold, band.threshold, T(-60), T(0));
152 put(p.ratio, band.ratio, T(1), T(20));
153 put(p.range, band.range, T(-24), T(24));
154 put(p.attack, band.attack, T(0.5), T(500));
155 put(p.release, band.release, T(5), T(2000));
156 dirty_[
static_cast<size_t>(index)].store(
true, std::memory_order_release);
163 if (index < 0 || index >=
kBands)
return b;
164 const auto& p = published_[
static_cast<size_t>(index)];
165 b.
shape =
static_cast<Shape>(p.shape.load(std::memory_order_relaxed));
166 b.
frequency = p.frequency.load(std::memory_order_relaxed);
167 b.
q = p.q.load(std::memory_order_relaxed);
168 b.
threshold = p.threshold.load(std::memory_order_relaxed);
169 b.
ratio = p.ratio.load(std::memory_order_relaxed);
170 b.
range = p.range.load(std::memory_order_relaxed);
171 b.
attack = p.attack.load(std::memory_order_relaxed);
172 b.
release = p.release.load(std::memory_order_relaxed);
183 if (std::isfinite(octaves))
187 [[nodiscard]] T
getDetail() const noexcept {
return detail_.load(std::memory_order_relaxed); }
197 void setTimeFocus(
bool on)
noexcept { timeFocus_.store(on, std::memory_order_relaxed); }
199 [[nodiscard]]
bool getTimeFocus() const noexcept {
return timeFocus_.load(std::memory_order_relaxed); }
202 [[nodiscard]]
int getLatency() const noexcept {
return filter_.getLatency(); }
204 [[nodiscard]]
double getGain(
double hz)
const noexcept
206 if (total_.empty() || !(hz > 0.0))
return 0.0;
207 const double at = (kPoints - 1) * std::log(hz / 20.0) / std::log(1000.0);
208 return static_cast<double>(total_[
static_cast<size_t>(std::lround(std::clamp(at, 0.0,
static_cast<double>(kPoints - 1))))]);
220 [[nodiscard]] T
getDisplayGain(
int point)
const noexcept {
return filter_.playedValue(0, point); }
222 [[nodiscard]] T
getPeakGain() const noexcept {
return filter_.playedExtreme(); }
229 run(buffer, key.getNumChannels() > 0 && key.getNumSamples() >= buffer.getNumSamples() ? &key :
nullptr);
232 [[nodiscard]] std::vector<uint8_t>
getState()
const
238 for (
int i = 0; i <
kBands; ++i)
241 const auto put = [&](
const char* name,
float v) { std::snprintf(key,
sizeof(key),
"b%d.%s", i, name); w.
write(key, v); };
242 std::snprintf(key,
sizeof(key),
"b%d.shape", i);
244 put(
"freq",
static_cast<float>(b.
frequency));
245 put(
"q",
static_cast<float>(b.
q));
246 put(
"thr",
static_cast<float>(b.
threshold));
247 put(
"ratio",
static_cast<float>(b.
ratio));
248 put(
"range",
static_cast<float>(b.
range));
249 put(
"att",
static_cast<float>(b.
attack));
250 put(
"rel",
static_cast<float>(b.
release));
267 for (
int i = 0; i <
kBands; ++i)
270 const auto get = [&](
const char* name,
float def) { std::snprintf(key,
sizeof(key),
"b%d.%s", i, name);
return static_cast<T
>(r.
read(key, def)); };
271 std::snprintf(key,
sizeof(key),
"b%d.shape", i);
274 b.
q = get(
"q", 1.0f);
276 b.
ratio = get(
"ratio", 3.0f);
277 b.
range = get(
"range", -12.0f);
278 b.
attack = get(
"att", 10.0f);
279 b.
release = get(
"rel", 150.0f);
282 std::snprintf(key,
sizeof(key),
"b%d.cells", i);
283 const int cells = std::clamp(r.
read(key, 32), 1, 128);
284 const double q = std::clamp(
static_cast<double>(
getBand(i).q), 0.1, 20.0);
285 const double band = b.
shape ==
Shape::Bell ? 2.0 * std::asinh(0.5 / q) / std::numbers::ln2 : kShelfOctaves;
286 const double width = 2.0 * band / cells;
287 if (finest == 0.0 || width < finest) finest = width;
289 setDetail(first && finest > 0.0 ?
static_cast<T
>(finest) :
static_cast<T
>(r.
read(
"detail", 1.0f / 12.0f)));
296 static constexpr size_t kSize =
static_cast<size_t>(kPoints);
297 static constexpr double kSilent = -200.0;
298 static constexpr double kLeastLevel = -120.0;
299 static constexpr double kShelfOctaves = 2.0;
303 std::atomic<int> shape { 0 };
304 std::atomic<T> frequency { T(1000) }, q { T(1) }, threshold { T(-12) }, ratio { T(3) };
305 std::atomic<T> range { T(-12) }, attack { T(10) }, release { T(150) };
312 int first = 0, end = 0;
313 std::vector<T> share, shareDb;
314 std::vector<T> action, attack, release;
315 double peak = kSilent;
316 double candidate = kSilent;
321 void redesign(
int index)
noexcept
323 auto& s = state_[
static_cast<size_t>(index)];
324 const bool wasActive = s.active;
325 const int oldFirst = s.first, oldEnd = s.end;
327 const Band& b = s.band;
328 s.active = b.shape !=
Shape::Off && std::abs(
static_cast<double>(b.range)) > 1e-3;
331 std::fill(s.share.begin(), s.share.end(), T(0));
334 const double f0 = std::clamp(
static_cast<double>(b.frequency), 20.0, 0.45 * spec_.
sampleRate);
335 const double range =
static_cast<double>(b.range), q = std::max(
static_cast<double>(b.q), 0.025);
339 : b.shape ==
Shape::
LowShelf ? AnalogSection::lowShelf(f0, range)
340 : AnalogSection::highShelf(f0, range);
341 const double attack = 0.001 *
static_cast<double>(b.attack), release = 0.001 *
static_cast<double>(b.release);
342 int first = -1, last = -1;
343 for (
int i = 0; i < kPoints; ++i)
345 const auto u =
static_cast<size_t>(i);
347 const double share = std::clamp(proto.magnitudeDb(hz) / range, 0.0, 1.0);
348 if (!(share > 0.01))
continue;
349 if (first < 0) first = i;
351 s.share[u] =
static_cast<T
>(share);
352 s.shareDb[u] =
static_cast<T
>(10.0 * std::log10(share));
353 const double scale = std::sqrt(1000.0 / hz);
354 s.attack[u] =
static_cast<T
>(std::exp(-hopSeconds_ / std::max(1e-4, attack * scale)));
355 s.release[u] =
static_cast<T
>(std::exp(-hopSeconds_ / std::max(1e-3, release * scale)));
357 s.active = first >= 0;
365 for (
int i = 0; i < kPoints; ++i)
366 if (i < s.first || i >= s.end || s.share[
static_cast<size_t>(i)] == T(0)) s.action[
static_cast<size_t>(i)] = T(0);
367 if (!s.active)
return;
370 if (wasActive && (std::abs(s.first - oldFirst) > 1 || std::abs(s.end - oldEnd) > 1))
372 s.candidate = kSilent;
373 s.watching = std::max(1,
static_cast<int>(0.5 / hopSeconds_));
377 void run(AudioBufferView<T>& buffer,
const AudioBufferView<T>* key)
noexcept
379 if (!prepared_)
return;
380 const int channels = std::min(buffer.getNumChannels(), channels_);
381 if (channels <= 0)
return;
382 for (
int i = 0; i <
kBands; ++i)
383 if (dirty_[
static_cast<size_t>(i)].exchange(
false, std::memory_order_acquire)) redesign(i);
384 if (
const T detail = detail_.load(std::memory_order_relaxed); detail != lastDetail_)
386 lastDetail_ = detail;
387 analysis_.setWidth(
static_cast<double>(detail));
388 const double points =
static_cast<double>(detail) * (kPoints - 1) * std::numbers::ln2 / std::log(1000.0);
389 hold_ = std::max(1,
static_cast<int>(std::lround(points / 8.0)));
392 const bool focus = timeFocus_.load(std::memory_order_relaxed);
393 analysis_.setTimeFocus(focus);
396 filter_.processBlock(
398 [&](
int offset,
int count)
noexcept {
399 for (
int ch = 0; ch < channels; ++ch)
401 const T* from = key !=
nullptr ? key->getChannel(std::min(ch, key->getNumChannels() - 1)) : buffer.getChannel(ch);
402 analysis_.feed(ch, from + offset, count);
405 [&](int, T* curveDb)
noexcept {
406 analysis_.update(channels);
409 if (!filter_.isDisplayDue(shownCurve_))
return;
410 shownCurve_ = filter_.getCurveCount();
411 filter_.publishPlayed(channels);
419 void readLevels(
int first,
int end)
noexcept
421 analysis_.read(0, power_.data(), first, end);
422 std::fill(level_.begin(), level_.begin() + first, T(kSilent));
423 for (
int i = first; i < end; ++i)
424 level_[
static_cast<size_t>(i)] = T(4.3429448190325183) * std::log(power_[
static_cast<size_t>(i)] + T(1e-20));
425 std::fill(level_.begin() + end, level_.end(), T(kSilent));
427 const int half = hold_ / 2;
428 if (half == 0)
return;
430 for (
size_t i = 0; i < kSize; ++i)
433 sum +=
static_cast<double>(held_[i]);
436 for (
int i = 0; i < kPoints; ++i)
438 const int from = std::max(0, i - half), to = std::min(kPoints, i + half + 1);
439 held_[
static_cast<size_t>(i)] =
static_cast<T
>((sum_[
static_cast<size_t>(to)] - sum_[
static_cast<size_t>(from)]) / (to - from));
444 void holdLargest() noexcept
446 if (hold_ <= kFewPoints)
449 T* from = level_.data();
450 T* to = held_.data();
451 for (
int pass = 0; pass < hold_; ++pass)
453 to[0] = std::max(from[0], from[1]);
454 for (
int i = 1; i < kPoints - 1; ++i) to[i] = std::max(std::max(from[i - 1], from[i]), from[i + 1]);
455 to[kPoints - 1] = std::max(from[kPoints - 2], from[kPoints - 1]);
458 if (from != held_.data()) std::copy_n(from, kPoints, held_.data());
461 int head = 0, tail = 0, next = 0;
462 for (
int i = 0; i < kPoints; ++i)
464 for (; next < kPoints && next <= i + hold_; ++next)
466 const T v = level_[
static_cast<size_t>(next)];
467 while (tail > head && level_[
static_cast<size_t>(queue_[
static_cast<size_t>(tail - 1)])] <= v) --tail;
468 queue_[
static_cast<size_t>(tail++)] = next;
470 while (queue_[
static_cast<size_t>(head)] < i - hold_) ++head;
471 held_[
static_cast<size_t>(i)] = level_[
static_cast<size_t>(queue_[
static_cast<size_t>(head)])];
475 static constexpr int kFewPoints = 4;
477 void design(T* curve)
noexcept
479 std::fill(total_.begin(), total_.end(), T(0));
482 int first = kPoints, end = 0;
483 for (
const auto& s : state_)
486 first = std::min(first, s.first);
487 end = std::max(end, s.end);
491 const int reach = hold_ + hold_ / 2 + 1;
492 readLevels(std::max(0, first - reach), std::min(kPoints, end + reach));
494 for (
auto& s : state_)
496 if (!s.active)
continue;
497 const Band& b = s.band;
498 const T* held = held_.data();
500 for (
int i = s.first; i < s.end; ++i) top = std::max(top, held[i] + s.shareDb[
static_cast<size_t>(i)]);
502 const double loudest =
static_cast<double>(top);
503 s.peak = loudest > s.peak ? loudest : std::max(loudest, s.peak - 2.0 * hopSeconds_);
506 s.candidate = loudest > s.candidate ? loudest : std::max(loudest, s.candidate - 2.0 * hopSeconds_);
507 if (--s.watching == 0) s.peak = s.candidate;
509 const T threshold =
static_cast<T
>(std::max(s.peak +
static_cast<double>(b.threshold), kLeastLevel));
510 const T range = std::abs(b.range);
511 const T perDb = (T(1) - T(1) / b.ratio) / range;
512 for (
int i = s.first; i < s.end; ++i)
514 const auto u =
static_cast<size_t>(i);
515 const T target = std::min(T(1), std::max(T(0), held[i] + s.shareDb[u] - threshold) * perDb);
517 a = target + (target > a ? s.attack[u] : s.release[u]) * (a - target);
518 if (target == T(0) && a < T(1e-5)) a = T(0);
519 total_[u] += b.range * a * s.share[u];
522 std::copy(total_.begin(), total_.end(), curve);
526 int channels_ = 0, hold_ = 1, shownCurve_ = 0;
527 bool prepared_ =
false;
528 double hopSeconds_ = 256.0 / 48000.0;
529 T lastDetail_ = T(-1);
530 DetailFilter<T> filter_;
531 DetailAnalysis<T> analysis_;
532 std::array<Published, kBands> published_ {};
533 std::array<std::atomic<bool>,
kBands> dirty_ {};
534 std::atomic<T> detail_ { T(1.0 / 12.0) };
535 std::atomic<bool> timeFocus_ {
false };
536 std::array<State, kBands> state_ {};
537 std::vector<T> power_, level_, held_, total_;
538 std::vector<double> sum_;
539 std::vector<int> queue_;