// Polyphonic engine - STFT frames, instantaneous frequency, spectral peaks with tonality and whitening. // Everything here streams over frames: memory does not grow with the length of the recording // beyond the (sparse) peak list. #include "PolyInternal.h" #include namespace polyphonic { namespace detail { std::vector hannPeriodic(int n) { std::vector w(static_cast(n)); for (int i = 0; i < n; ++i) w[static_cast(i)] = static_cast(0.5 - 0.5 * std::cos(2.0 * kPi * i / n)); return w; } StftFrames::StftFrames(const float* signal, int numSamples, const Params& p) : x(signal), n(numSamples), N(p.fftSize), H(p.hop) { frames = 1 + (numSamples + H - 1) / H; bins = N / 2 + 1; window = hannPeriodic(N); buffer.resize(static_cast(N)); norm = 2.0f / std::accumulate(window.begin(), window.end(), 0.0f); fft = std::make_unique(N); } void StftFrames::spectrum(int t, std::complex* out) { const int first = t * H - N / 2; for (int i = 0; i < N; ++i) { const int s = first + i; buffer[static_cast(i)] = (s >= 0 && s < n) ? x[s] * window[static_cast(i)] : 0.0f; } fft->forward(buffer.data(), out); for (int k = 0; k < bins; ++k) out[k] *= norm; } static inline float princarg(float a) { const float twoPi = static_cast(2.0 * kPi); a = std::fmod(a + static_cast(kPi), twoPi); if (a < 0) a += twoPi; return a - static_cast(kPi); } // Exact running median over `width` bins (edges replicated). void medianRow(const float* in, int K, int width, float* out, std::vector& sorted) { const int half = width / 2; auto value = [&](int k) { return in[std::clamp(k, 0, K - 1)]; }; sorted.clear(); for (int k = -half; k <= half; ++k) sorted.push_back(value(k)); std::sort(sorted.begin(), sorted.end()); for (int k = 0; k < K; ++k) { out[k] = sorted[static_cast(half)]; if (k + 1 == K) break; const float leaving = value(k - half); const float entering = value(k + half + 1); sorted.erase(std::lower_bound(sorted.begin(), sorted.end(), leaving)); sorted.insert(std::upper_bound(sorted.begin(), sorted.end(), entering), entering); } } int Peaks::nearest(int t, float bins) const { // peaks of a frame are sorted by bin: binary search, then compare the two neighbours const int b = begin(t), e = end(t); if (b == e) return -1; const auto first = fbin.begin() + b, last = fbin.begin() + e; const auto it = std::lower_bound(first, last, bins); int j = static_cast(it - fbin.begin()); if (j >= e) return e - 1; if (j > b && std::fabs(fbin[static_cast(j - 1)] - bins) <= std::fabs(fbin[static_cast(j)] - bins)) return j - 1; return j; } namespace { struct Band { int k0; std::vector w; float sum; }; // Klapuri (2006) whitening bands: centres 229 (10^((b+1)/21.4) - 1) Hz with triangular responses. std::vector whiteningCentres(const Params& p) { std::vector cb; for (int b = 0; b < 32; ++b) { const double c = 229.0 * (std::pow(10.0, (b + 1) / 21.4) - 1.0); if (c < p.sampleRate / 2) cb.push_back(c); } return cb; } std::vector whiteningBands(const std::vector& cb, const Params& p, int K) { const double binHz = p.sampleRate / p.fftSize; std::vector bands; for (size_t b = 0; b < cb.size(); ++b) { const double lo = b > 0 ? cb[b - 1] : 0.0; const double hi = b + 1 < cb.size() ? cb[b + 1] : p.sampleRate / 2; Band band{std::max(0, static_cast(std::floor(lo / binHz))), {}, 0.0f}; const int k1 = std::min(K - 1, static_cast(std::ceil(hi / binHz))); for (int k = band.k0; k <= k1; ++k) { const double f = k * binHz; const double v = std::max(0.0, std::min((f - lo) / std::max(cb[b] - lo, 1e-9), (hi - f) / std::max(hi - cb[b], 1e-9))); band.w.push_back(static_cast(v)); band.sum += static_cast(v); } bands.push_back(std::move(band)); } return bands; } } // namespace void analyzeSpectra(Context& ctx, const std::atomic* cancel, int frameLo, int frameHi) { const Params& p = ctx.p; StftFrames frames(ctx.audio, ctx.numSamples, p); const int T = frames.numFrames(), K = frames.numBins(); ctx.numFrames = T; ctx.numBins = K; const double binHz = p.sampleRate / p.fftSize; const auto cb = whiteningCentres(p); const auto bands = whiteningBands(cb, p, K); std::vector gamma(cb.size()), gains(static_cast(K)); std::vector> cur(static_cast(K)), nxt(static_cast(K)), prev(static_cast(K)); std::vector mag(static_cast(K)), noise(static_cast(K)), IF(static_cast(K)), scratch; Peaks& pk = ctx.peaks; pk = Peaks{}; pk.frameStart.assign(static_cast(T) + 1, 0); const float absThr = std::pow(10.0f, p.peakAbsDb / 20.0f); const float relFactor = std::pow(10.0f, p.peakRelDb / 20.0f); const float overNoise = std::pow(10.0f, p.peakOverNoiseDb / 20.0f); const float ifScale = static_cast(p.fftSize / (2.0 * kPi * p.hop)); // LYNX modification (round 190lk): peaks for frames [frameLo, frameHi) only (the others stay empty), so the // masks of a few NMF blocks can be built on demand without a pass over the whole recording. Each frame's peaks // depend only on that frame and its neighbours, so they are the same as in a whole pass. const int tLo = std::clamp(frameLo, 0, T); const int tHi = frameHi < 0 ? T : std::clamp(frameHi, tLo, T); for (int t = 0; t <= tLo && t <= T; ++t) pk.frameStart[static_cast(t)] = 0; if (tLo > 0 && tLo < T) { frames.spectrum(tLo - 1, prev.data()); frames.spectrum(tLo, cur.data()); if (tLo + 1 < T) frames.spectrum(tLo + 1, nxt.data()); } else { frames.spectrum(0, cur.data()); if (T > 1) frames.spectrum(1, nxt.data()); } for (int t = tLo; t < tHi; ++t) { if (cancel && cancel->load()) return; if (t > tLo) { prev.swap(cur); cur.swap(nxt); if (t + 1 < T) frames.spectrum(t + 1, nxt.data()); } // instantaneous frequency from the phase advance (frame 0 uses frames 0 -> 1) const auto& a = (t == 0) ? cur : prev; const auto& b = (t == 0) ? (T > 1 ? nxt : cur) : cur; for (int k = 0; k < K; ++k) { mag[static_cast(k)] = std::abs(cur[static_cast(k)]); const float expected = static_cast(2.0 * kPi * k * p.hop / p.fftSize); const float dphi = (t == 0 && T <= 1) ? 0.0f : std::arg(b[static_cast(k)]) - std::arg(a[static_cast(k)]); IF[static_cast(k)] = static_cast(k) + princarg(dphi - expected) * ifScale; } medianRow(mag.data(), K, p.noiseMedianBins, noise.data(), scratch); // whitening gains if (p.whitenNu < 1.0f) { for (size_t q = 0; q < bands.size(); ++q) { double s = 0.0; for (size_t j = 0; j < bands[q].w.size(); ++j) { const float m = mag[static_cast(bands[q].k0) + j]; s += bands[q].w[j] * static_cast(m) * m; } const double sigma = std::sqrt(s / std::max(1e-9, static_cast(bands[q].sum))); gamma[q] = static_cast(std::pow(sigma + 1e-7, p.whitenNu - 1.0)); } size_t q = 0; for (int k = 0; k < K; ++k) { const double f = k * binHz; if (f <= cb.front()) { gains[static_cast(k)] = gamma.front(); continue; } if (f >= cb.back()) { gains[static_cast(k)] = gamma.back(); continue; } while (q + 1 < cb.size() && cb[q + 1] < f) ++q; const double u = (f - cb[q]) / (cb[q + 1] - cb[q]); gains[static_cast(k)] = static_cast(gamma[q] * (1.0 - u) + gamma[q + 1] * u); } } else { std::fill(gains.begin(), gains.end(), 1.0f); } // peaks pk.frameStart[static_cast(t)] = static_cast(pk.bin.size()); float frameMax = 0.0f; for (int k = 0; k < K; ++k) frameMax = std::max(frameMax, mag[static_cast(k)]); const float thr = std::max(absThr, frameMax * relFactor); for (int k = 2; k < K - 2; ++k) { const float v = mag[static_cast(k)]; if (!(v > mag[static_cast(k - 1)] && v >= mag[static_cast(k + 1)] && v > mag[static_cast(k - 2)] && v >= mag[static_cast(k + 2)])) continue; if (!(v > thr) || !(v > noise[static_cast(k)] * overNoise)) continue; const float la = std::log(mag[static_cast(k - 1)] + 1e-12f), lb = std::log(v + 1e-12f), lc = std::log(mag[static_cast(k + 1)] + 1e-12f); const float den = la - 2.0f * lb + lc; float d = std::fabs(den) > 1e-12f ? 0.5f * (la - lc) / den : 0.0f; d = std::clamp(d, -0.5f, 0.5f); const float amp = std::exp(lb - 0.25f * (la - lc) * d); const float fif = IF[static_cast(k)]; const float fbin = std::fabs(fif - k) <= 1.0f ? fif : k + d; const float dev = std::max(std::fabs(IF[static_cast(k - 1)] - fif), std::fabs(IF[static_cast(k + 1)] - fif)); const float ton = std::exp(-(dev / p.tonalDevBins) * (dev / p.tonalDevBins)); pk.bin.push_back(k); pk.fbin.push_back(fbin); pk.amp.push_back(amp); pk.tonality.push_back(ton); pk.wamp.push_back(amp * gains[static_cast(k)]); } // keep each frame's peaks sorted by refined frequency for binary search { const int b0 = pk.frameStart[static_cast(t)], b1 = static_cast(pk.bin.size()); bool sorted = true; for (int j = b0 + 1; j < b1 && sorted; ++j) sorted = pk.fbin[static_cast(j - 1)] <= pk.fbin[static_cast(j)]; if (!sorted) { std::vector o(static_cast(b1 - b0)); for (int j = 0; j < b1 - b0; ++j) o[static_cast(j)] = b0 + j; std::stable_sort(o.begin(), o.end(), [&](int x, int y) { return pk.fbin[static_cast(x)] < pk.fbin[static_cast(y)]; }); auto permute = [&](auto& v) { auto copy = v; for (int j = 0; j < b1 - b0; ++j) v[static_cast(b0 + j)] = copy[static_cast(o[static_cast(j)])]; }; permute(pk.bin); permute(pk.fbin); permute(pk.amp); permute(pk.tonality); permute(pk.wamp); } } } for (int t = tHi; t <= T; ++t) pk.frameStart[static_cast(t)] = static_cast(pk.bin.size()); } } // namespace detail } // namespace polyphonic