StemSplitTests.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // --- Extract stems from sample (issue #27) ---------------------------------------------------------------------------
  2. #include "core/StemSplit.h"
  3. TEST (stemsplit_overlap_add_rebuilds_the_signal)
  4. {
  5. using namespace lynx::stemsplit;
  6. // lengths around the segment borders: shorter than one, exactly one, one more frame, several
  7. for (const std::int64_t frames : { (std::int64_t) 1, (std::int64_t) 1000, segmentFrames, segmentFrames + 1, segmentFrames * 3 + 17 })
  8. {
  9. std::vector<float> left ((std::size_t) frames), right ((std::size_t) frames);
  10. for (std::int64_t i = 0; i < frames; ++i)
  11. {
  12. left[(std::size_t) i] = (float) std::sin ((double) i * 0.01);
  13. right[(std::size_t) i] = (float) std::cos ((double) i * 0.003);
  14. }
  15. // a fake model: stem s is the input times (s + 1)
  16. std::int64_t calls = 0;
  17. double last = 0.0;
  18. Stems stems;
  19. const auto ok = separate (left, right, [&] (const float* in, float* out) {
  20. ++calls;
  21. for (int s = 0; s < numStems; ++s)
  22. for (std::int64_t i = 0; i < 2 * segmentFrames; ++i)
  23. out[s * 2 * segmentFrames + i] = in[i] * (float) (s + 1);
  24. return true;
  25. }, [&] (double p) { last = p; }, stems);
  26. CHECK (ok);
  27. CHECK (calls == numSegments (frames));
  28. CHECK (last == 1.0);
  29. for (int s = 0; s < numStems; ++s)
  30. {
  31. CHECK ((std::int64_t) stems[(std::size_t) s][0].size() == frames);
  32. double worst = 0.0;
  33. for (std::int64_t i = 0; i < frames; ++i)
  34. {
  35. worst = std::max (worst, (double) std::abs (stems[(std::size_t) s][0][(std::size_t) i] - left[(std::size_t) i] * (float) (s + 1)));
  36. worst = std::max (worst, (double) std::abs (stems[(std::size_t) s][1][(std::size_t) i] - right[(std::size_t) i] * (float) (s + 1)));
  37. }
  38. CHECK (worst < 1.0e-3);
  39. }
  40. }
  41. }
  42. TEST (stemsplit_cancel_stops_at_the_segment)
  43. {
  44. using namespace lynx::stemsplit;
  45. std::vector<float> mono ((std::size_t) segmentFrames * 3, 0.1f);
  46. int calls = 0;
  47. Stems stems;
  48. CHECK (! separate (mono, mono, [&] (const float*, float*) { return ++calls < 2; }, {}, stems));
  49. CHECK (calls == 2);
  50. }
  51. TEST (stemsplit_clips_are_aligned_on_new_tracks)
  52. {
  53. using namespace lynx;
  54. Song song;
  55. ChannelDef channel;
  56. channel.name = "song";
  57. channel.instrument = InstrumentKind::audioClip;
  58. channel.samplePath = "/a/song.wav";
  59. channel.sampler.stretchBeats = 16.0f;
  60. song.channels.push_back (channel);
  61. Clip source;
  62. source.kind = ClipKind::audio;
  63. source.channel = (int) song.channels.size() - 1;
  64. source.track = 2;
  65. source.start = 960;
  66. source.length = 4800;
  67. source.offset = 96;
  68. source.gain = 0.5f;
  69. song.playlist.push_back (source);
  70. Clip below = source;
  71. below.track = 3;
  72. song.playlist.push_back (below);
  73. const std::array<std::string, stemsplit::numStems> paths { "/d.wav", "/b.wav", "/i.wav", "/v.wav" };
  74. const auto before = song;
  75. CHECK (! stemsplit::addStemClips (song, 5, "/a/song.wav", paths)); // the clip is gone
  76. CHECK (! stemsplit::addStemClips (song, 0, "/other.wav", paths)); // another file now
  77. CHECK (song.playlist.size() == before.playlist.size() && song.channels.size() == before.channels.size());
  78. CHECK (stemsplit::addStemClips (song, 0, "/a/song.wav", paths));
  79. CHECK (song.playlist.size() == before.playlist.size() + 4);
  80. CHECK (song.playlist[0].track == 2 && song.playlist[0].channel == source.channel); // the source stays
  81. CHECK (song.playlist[1].track == 7); // the clip below moved down
  82. for (int s = 0; s < 4; ++s)
  83. {
  84. const auto& clip = song.playlist[before.playlist.size() + (std::size_t) s];
  85. const auto& stem = song.channels[(std::size_t) clip.channel];
  86. CHECK (clip.track == 3 + s);
  87. CHECK (clip.start == 960 && clip.length == 4800 && clip.offset == 96 && clip.gain == 0.5f);
  88. CHECK (stem.samplePath == paths[(std::size_t) s]);
  89. CHECK (stem.sampler.stretchBeats == 16.0f);
  90. CHECK (stem.instrument == InstrumentKind::audioClip);
  91. }
  92. CHECK (song.channels[(std::size_t) song.playlist.back().channel].name == "song - vocals");
  93. }
  94. // --- round 167x: the separation leaves cores to the audio callback; a long resample can be cancelled -----------------
  95. #include "core/AudioEdit.h"
  96. TEST (stemsplit_threads_leave_cores_for_audio_167x)
  97. {
  98. using lynx::stemsplit::separationThreads;
  99. CHECK (separationThreads (6, 12) == 5); // physical − 1
  100. CHECK (separationThreads (8, 8) == 6); // logical − 2 caps it without SMT
  101. CHECK (separationThreads (16, 32) == 15);
  102. CHECK (separationThreads (2, 4) == 1);
  103. CHECK (separationThreads (1, 1) == 1); // never 0
  104. CHECK (separationThreads (0, 0) == 1); // (unknown counts)
  105. CHECK (separationThreads (4, 2) == 1);
  106. }
  107. TEST (stemsplit_resample_stops_when_asked_167x)
  108. {
  109. std::vector<float> in (200000, 0.25f);
  110. int asked = 0;
  111. const auto stopped = lynx::audioedit::resampleChannel (in, 183750, [&] { return ++asked >= 2; });
  112. CHECK (stopped.empty());
  113. CHECK (asked == 2);
  114. const auto whole = lynx::audioedit::resampleChannel (in, 183750, [] { return false; });
  115. CHECK (whole.size() == 183750);
  116. CHECK (std::abs (whole[90000] - 0.25f) < 1.0e-4f);
  117. CHECK (lynx::audioedit::resampleChannel (in, 183750) == whole); // the old overload is unchanged
  118. }