PolyLazy.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // LYNX addition (round 190lk, not part of PitchNet): the masks of a deferred analysis (Params::deferMasks), built on
  2. // demand for the frames an edit reaches. Licensed as the rest of this folder (AGPL-3.0).
  3. //
  4. // PitchNet's analyze() keeps every active note's masks (partial regions x frames) for the whole recording: about
  5. // 0.6 MB per second of dense chords, so a five-minute take held ~180 MB of masks besides its audio. LYNX analyses a
  6. // clip once, keeps only its notes (and saves them in the project), and builds the masks of the NMF blocks an edit
  7. // reaches when it renders. A block's masks depend only on that block's frames and the notes in it, and a frame's
  8. // spectral peaks only on that frame and its neighbours, so the masks are exactly the ones analyze() would have kept.
  9. #include "PolyInternal.h"
  10. namespace polyphonic
  11. {
  12. namespace
  13. {
  14. constexpr double kLeadSeconds = 0.05; // = PolyRenderer's kClaimLeadSeconds
  15. constexpr double kTailSeconds = 2.0; // = PolyRenderer's kClaimTailSeconds
  16. std::vector<std::pair<int, int>> merged(std::vector<std::pair<int, int>> spans)
  17. {
  18. std::sort(spans.begin(), spans.end());
  19. std::vector<std::pair<int, int>> out;
  20. for (const auto& s : spans)
  21. {
  22. if (s.second < s.first)
  23. continue;
  24. if (!out.empty() && s.first <= out.back().second + 1)
  25. out.back().second = std::max(out.back().second, s.second);
  26. else
  27. out.push_back(s);
  28. }
  29. return out;
  30. }
  31. } // namespace
  32. std::vector<std::pair<int, int>> framesForEdits(const Analysis& A, const std::map<int, NoteEdit>& edits)
  33. {
  34. const Params& p = A.params;
  35. const double fps = p.sampleRate / p.hop;
  36. const int lead = static_cast<int>(std::ceil(kLeadSeconds * fps)) + 1;
  37. const int tail = static_cast<int>(std::ceil(kTailSeconds * fps)) + 1;
  38. const int T = A.numFrames;
  39. std::vector<std::pair<int, int>> spans;
  40. for (const auto& [index, edit] : edits)
  41. {
  42. if (index < 0 || index >= static_cast<int>(A.notes.size()) || !A.notes[static_cast<size_t>(index)].active)
  43. continue;
  44. const auto& n = A.notes[static_cast<size_t>(index)];
  45. int lo = n.startFrame - p.preFrames - lead;
  46. int hi = n.endFrame + p.postFrames + tail;
  47. // where it goes: moved by whole frames (rounded out), lengthened up to 4x
  48. const int move = static_cast<int>(std::ceil(std::fabs(edit.timeOffsetSeconds) * fps)) + 1;
  49. if (edit.timeOffsetSeconds > 0.0)
  50. hi += move;
  51. else if (edit.timeOffsetSeconds < 0.0)
  52. lo -= move;
  53. if (edit.stretch > 1.0f)
  54. hi += static_cast<int>(std::ceil((n.endFrame - n.startFrame + 1) * (edit.stretch - 1.0f))) + 1;
  55. spans.push_back({std::max(0, lo), std::min(T - 1, hi)});
  56. }
  57. spans = merged(std::move(spans));
  58. // the whole span of every active note that overlaps (their masks' first / last frames and claims are read too)
  59. std::vector<std::pair<int, int>> widened = spans;
  60. for (const auto& n : A.notes)
  61. {
  62. if (!n.active)
  63. continue;
  64. const int a = n.startFrame - p.preFrames, b = n.endFrame + p.postFrames;
  65. for (const auto& s : spans)
  66. if (b >= s.first && a <= s.second)
  67. {
  68. widened.push_back({std::max(0, a - lead), std::min(T - 1, b + tail)});
  69. break;
  70. }
  71. }
  72. return merged(std::move(widened));
  73. }
  74. bool ensureMasks(Analysis& A, const float* mono, int numSamples, int frameLo, int frameHi, const std::atomic<bool>* cancel)
  75. {
  76. using namespace detail;
  77. if (mono == nullptr || numSamples <= 0 || A.numFrames <= 0 || frameHi < frameLo)
  78. return true;
  79. const auto blocks = maskBlocks(A.params, A.numFrames);
  80. if (A.maskBlocksBuilt.empty())
  81. {
  82. // a full analysis: every block is there unless the masks were released
  83. bool any = false;
  84. for (const auto& m : A.masks)
  85. any = any || !m.partials.empty();
  86. if (any)
  87. return true;
  88. A.maskBlocksBuilt.assign(blocks.size(), 0);
  89. }
  90. if (A.masks.size() != A.notes.size())
  91. A.masks.assign(A.notes.size(), {});
  92. std::vector<Candidate> active;
  93. for (const auto& n : A.notes)
  94. {
  95. if (!n.active)
  96. break; // active notes come first (analyze())
  97. Candidate c;
  98. c.start = n.startFrame;
  99. c.end = n.endFrame;
  100. c.f0 = n.f0;
  101. c.B = n.inharmonicity;
  102. c.energy = n.energy;
  103. c.probability = n.probability;
  104. active.push_back(std::move(c));
  105. }
  106. std::vector<size_t> sizesBefore(A.masks.size());
  107. for (size_t i = 0; i < A.masks.size(); ++i)
  108. sizesBefore[i] = A.masks[i].partials.size();
  109. bool appendedOutOfOrder = false;
  110. for (size_t b = 0; b < blocks.size(); ++b)
  111. {
  112. const auto& block = blocks[b];
  113. if (A.maskBlocksBuilt[b] || block.coreHi <= frameLo || block.coreLo > frameHi)
  114. continue;
  115. if (cancel != nullptr && cancel->load())
  116. return false;
  117. Context ctx;
  118. ctx.p = A.params;
  119. ctx.audio = mono;
  120. ctx.numSamples = numSamples;
  121. analyzeSpectra(ctx, cancel, block.lo, block.hi);
  122. if (cancel != nullptr && cancel->load())
  123. return false;
  124. // a later block may already be built: the new partials then go before them
  125. for (size_t c = b + 1; c < blocks.size(); ++c)
  126. appendedOutOfOrder = appendedOutOfOrder || A.maskBlocksBuilt[c] != 0;
  127. buildMaskBlock(ctx, active, block, A);
  128. A.maskBlocksBuilt[b] = 1;
  129. }
  130. if (appendedOutOfOrder)
  131. for (size_t i = 0; i < A.masks.size(); ++i)
  132. if (A.masks[i].partials.size() != sizesBefore[i])
  133. std::stable_sort(A.masks[i].partials.begin(), A.masks[i].partials.end(), [](const PartialMask& x, const PartialMask& y) {
  134. return x.frame != y.frame ? x.frame < y.frame : x.harmonic < y.harmonic;
  135. });
  136. return true;
  137. }
  138. void releaseMasks(Analysis& A)
  139. {
  140. A.masks.assign(A.notes.size(), {});
  141. A.maskBlocksBuilt.assign(detail::maskBlocks(A.params, A.numFrames).size(), 0);
  142. }
  143. size_t maskBytes(const Analysis& A)
  144. {
  145. size_t bytes = 0;
  146. for (const auto& m : A.masks)
  147. bytes += m.partials.capacity() * sizeof(PartialMask) + m.weights.capacity() * sizeof(float);
  148. return bytes;
  149. }
  150. } // namespace polyphonic