100 if (!std::isfinite(windowMs)) windowMs = 300.0;
102 windowMs_ = std::clamp(windowMs, 1.0, 600000.0);
103 alpha_ = 1.0 - std::exp(-1.0 / (windowMs_ * 0.001 * fs));
105 gonioDecim_ = std::max(1,
static_cast<int>(std::llround(std::min(fs / 24000.0, 65536.0))));
106 prepared_.store(
true, std::memory_order_relaxed);
119 lr_ = ll_ = rr_ = 0.0;
121 for (
auto& p : gonio_)
122 p.store(0u, std::memory_order_relaxed);
123 gonioWrite_.store(0, std::memory_order_relaxed);
124 correlation_.store(T(0), std::memory_order_relaxed);
125 balance_.store(T(0), std::memory_order_relaxed);
126 dominantDb_.store(T(-200), std::memory_order_relaxed);
140 if (!prepared_.load(std::memory_order_relaxed))
return;
142 const int nS = buffer.getNumSamples();
143 const int nCh = buffer.getNumChannels();
144 if (nS <= 0 || nCh <= 0)
return;
146 const T* L = buffer.getChannel(0);
147 const T* R = (nCh >= 2) ? buffer.getChannel(1) : L;
149 const double alpha = alpha_;
150 double lr = lr_, ll = ll_, rr = rr_;
151 int wp = gonioWrite_.load(std::memory_order_relaxed);
153 for (
int i = 0; i < nS; ++i)
155 const double l =
static_cast<double>(L[i]);
156 const double r =
static_cast<double>(R[i]);
157 lr += alpha * (l * r - lr);
158 ll += alpha * (l * l - ll);
159 rr += alpha * (r * r - rr);
161 if (++decimCount_ >= gonioDecim_)
164 if (std::isfinite(l) && std::isfinite(r))
167 p.
mid =
static_cast<float>((l + r) * 0.7071067811865476);
168 p.
side =
static_cast<float>((l - r) * 0.7071067811865476);
169 gonio_[
static_cast<size_t>(wp)].store(std::bit_cast<std::uint64_t>(p),
170 std::memory_order_relaxed);
176 gonioWrite_.store(wp, std::memory_order_release);
184 if (!std::isfinite(lr + ll + rr))
186 lr_ = ll_ = rr_ = 0.0;
193 if (ll < 1e-100) ll = 0.0;
194 if (rr < 1e-100) rr = 0.0;
195 if (std::abs(lr) < 1e-100) lr = 0.0;
201 const double denom = std::sqrt(ll * rr);
202 const double corr = (denom > 1e-12) ? std::clamp(lr / denom, -1.0, 1.0) : 0.0;
203 correlation_.store(
static_cast<T
>(corr), std::memory_order_relaxed);
205 const double total = ll + rr;
206 const double bal = (total > 1e-12) ? (rr - ll) / total : 0.0;
207 balance_.store(
static_cast<T
>(bal), std::memory_order_relaxed);
209 dominantDb_.store(
static_cast<T
>(dom > 1e-20 ? 10.0 * std::log10(dom) : -200.0), std::memory_order_relaxed);