4
0

PolyEngine.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Polyphonic engine - analysis entry point.
  2. #include "PolyInternal.h"
  3. namespace polyphonic
  4. {
  5. Analysis analyze(const float* mono, int numSamples, const Params& params, const NoteEvidence* evidence,
  6. const std::atomic<bool>* cancel, const Progress& progress)
  7. {
  8. using namespace detail;
  9. Analysis out;
  10. out.params = params;
  11. out.numSamples = numSamples;
  12. if (mono == nullptr || numSamples <= 0)
  13. return out;
  14. auto report = [&](float v) { if (progress) progress(v); };
  15. auto cancelled = [&] { return cancel != nullptr && cancel->load(); };
  16. Context ctx;
  17. ctx.p = params;
  18. ctx.evidence = params.detectionMode == DetectionMode::NeuralGuided ? evidence : nullptr;
  19. ctx.audio = mono;
  20. ctx.numSamples = numSamples;
  21. analyzeSpectra(ctx, cancel);
  22. if (cancelled()) return out;
  23. out.numFrames = ctx.numFrames;
  24. out.numBins = ctx.numBins;
  25. report(0.2f);
  26. // note candidates from the harmonic-sum landscape, then the learned real-note/ghost filter
  27. auto cands = extractCandidates(ctx, cancel);
  28. if (cancelled()) return out;
  29. report(0.5f);
  30. pruneCandidates(ctx, cands);
  31. refineCandidates(ctx, cands);
  32. cands = splitReattacks(ctx, cands);
  33. scoreCandidates(ctx, cands, evidence, params.exportFeatures ? &out.candidateFeatures : nullptr);
  34. report(0.6f);
  35. std::vector<Candidate> active, inactive;
  36. const float threshold = params.activationThresholdFor(ctx.p.detectionMode);
  37. for (auto& c : cands)
  38. (c.probability >= threshold ? active : inactive).push_back(c);
  39. active = mergeFragments(ctx, std::move(active));
  40. refineOnsets(ctx, active);
  41. // refined onsets change starts and pitch curves: join notes of one pitch that still overlap
  42. active = mergeFragments(ctx, std::move(active), true);
  43. // LYNX modification (round 190lo): slow re-attacks (pads, strings) split after the note filter
  44. if (params.slowReattacks)
  45. active = splitSlowReattacks(ctx, active);
  46. std::stable_sort(active.begin(), active.end(), [](const Candidate& a, const Candidate& b) {
  47. return a.start != b.start ? a.start < b.start : a.midi() < b.midi();
  48. });
  49. auto toNote = [](const Candidate& c, int id, bool isActive) {
  50. Note n;
  51. n.id = id;
  52. n.startFrame = c.start;
  53. n.endFrame = c.end;
  54. n.f0 = c.f0;
  55. n.inharmonicity = c.B;
  56. n.energy = c.energy;
  57. n.probability = c.probability;
  58. n.active = isActive;
  59. return n;
  60. };
  61. for (const auto& c : active)
  62. out.notes.push_back(toNote(c, static_cast<int>(out.notes.size()), true));
  63. for (const auto& c : inactive)
  64. {
  65. Note n = toNote(c, static_cast<int>(out.notes.size()), false);
  66. // an inactive candidate that sits on a partial of an active note belongs to that note
  67. float bestErr = 40.0f;
  68. for (size_t ai = 0; ai < active.size(); ++ai)
  69. {
  70. const auto& a = active[ai];
  71. if (a.end < c.start || a.start > c.end)
  72. continue;
  73. const int mid = (std::max(a.start, c.start) + std::min(a.end, c.end)) / 2;
  74. const float ratio = c.f0At(mid) / a.f0At(mid);
  75. const int h = static_cast<int>(std::lround(ratio));
  76. if (h < 1)
  77. continue;
  78. const float err = std::fabs(1200.0f * std::log2(ratio / (h * std::sqrt(1.0f + a.B * h * h))));
  79. if (err < bestErr)
  80. {
  81. bestErr = err;
  82. n.parent = static_cast<int>(ai);
  83. }
  84. }
  85. out.notes.push_back(std::move(n));
  86. }
  87. if (cancelled()) return out;
  88. if (params.deferMasks)
  89. {
  90. // LYNX modification (round 190lk): no masks now; ensureMasks() builds the blocks an edit needs
  91. out.masks.assign(out.notes.size(), {});
  92. out.maskBlocksBuilt.assign(maskBlocks(params, out.numFrames).size(), 0);
  93. }
  94. else
  95. buildMasks(ctx, active, out);
  96. report(1.0f);
  97. return out;
  98. }
  99. } // namespace polyphonic