| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- // Polyphonic engine - analysis entry point.
- #include "PolyInternal.h"
- namespace polyphonic
- {
- Analysis analyze(const float* mono, int numSamples, const Params& params, const NoteEvidence* evidence,
- const std::atomic<bool>* cancel, const Progress& progress)
- {
- using namespace detail;
- Analysis out;
- out.params = params;
- out.numSamples = numSamples;
- if (mono == nullptr || numSamples <= 0)
- return out;
- auto report = [&](float v) { if (progress) progress(v); };
- auto cancelled = [&] { return cancel != nullptr && cancel->load(); };
- Context ctx;
- ctx.p = params;
- ctx.evidence = params.detectionMode == DetectionMode::NeuralGuided ? evidence : nullptr;
- ctx.audio = mono;
- ctx.numSamples = numSamples;
- analyzeSpectra(ctx, cancel);
- if (cancelled()) return out;
- out.numFrames = ctx.numFrames;
- out.numBins = ctx.numBins;
- report(0.2f);
- // note candidates from the harmonic-sum landscape, then the learned real-note/ghost filter
- auto cands = extractCandidates(ctx, cancel);
- if (cancelled()) return out;
- report(0.5f);
- pruneCandidates(ctx, cands);
- refineCandidates(ctx, cands);
- cands = splitReattacks(ctx, cands);
- scoreCandidates(ctx, cands, evidence, params.exportFeatures ? &out.candidateFeatures : nullptr);
- report(0.6f);
- std::vector<Candidate> active, inactive;
- const float threshold = params.activationThresholdFor(ctx.p.detectionMode);
- for (auto& c : cands)
- (c.probability >= threshold ? active : inactive).push_back(c);
- active = mergeFragments(ctx, std::move(active));
- refineOnsets(ctx, active);
- // refined onsets change starts and pitch curves: join notes of one pitch that still overlap
- active = mergeFragments(ctx, std::move(active), true);
- // LYNX modification (round 190lo): slow re-attacks (pads, strings) split after the note filter
- if (params.slowReattacks)
- active = splitSlowReattacks(ctx, active);
- std::stable_sort(active.begin(), active.end(), [](const Candidate& a, const Candidate& b) {
- return a.start != b.start ? a.start < b.start : a.midi() < b.midi();
- });
- auto toNote = [](const Candidate& c, int id, bool isActive) {
- Note n;
- n.id = id;
- n.startFrame = c.start;
- n.endFrame = c.end;
- n.f0 = c.f0;
- n.inharmonicity = c.B;
- n.energy = c.energy;
- n.probability = c.probability;
- n.active = isActive;
- return n;
- };
- for (const auto& c : active)
- out.notes.push_back(toNote(c, static_cast<int>(out.notes.size()), true));
- for (const auto& c : inactive)
- {
- Note n = toNote(c, static_cast<int>(out.notes.size()), false);
- // an inactive candidate that sits on a partial of an active note belongs to that note
- float bestErr = 40.0f;
- for (size_t ai = 0; ai < active.size(); ++ai)
- {
- const auto& a = active[ai];
- if (a.end < c.start || a.start > c.end)
- continue;
- const int mid = (std::max(a.start, c.start) + std::min(a.end, c.end)) / 2;
- const float ratio = c.f0At(mid) / a.f0At(mid);
- const int h = static_cast<int>(std::lround(ratio));
- if (h < 1)
- continue;
- const float err = std::fabs(1200.0f * std::log2(ratio / (h * std::sqrt(1.0f + a.B * h * h))));
- if (err < bestErr)
- {
- bestErr = err;
- n.parent = static_cast<int>(ai);
- }
- }
- out.notes.push_back(std::move(n));
- }
- if (cancelled()) return out;
- if (params.deferMasks)
- {
- // LYNX modification (round 190lk): no masks now; ensureMasks() builds the blocks an edit needs
- out.masks.assign(out.notes.size(), {});
- out.maskBlocksBuilt.assign(maskBlocks(params, out.numFrames).size(), 0);
- }
- else
- buildMasks(ctx, active, out);
- report(1.0f);
- return out;
- }
- } // namespace polyphonic
|