LoopWrap167qTests.inc 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Round 167q (loopwrap): the real-time transport's loop region (AudioEngine::audioDeviceIOCallbackWithContext) split
  2. // each block where the pass ends with ceil((loopEnd − position) / tps). The position is a double moved by n · tps, so
  3. // the remaining length of a 192000-sample pass came out as 192000.00000001 and was rounded up: every wrap played one
  4. // sample more (measured with LYNX.exe --cliplooptest: the passes drifted 0, 1, 2… samples). The transport now uses
  5. // transportSamplesUntil / transportReached (core/MarkerPlan.h), with a tick epsilon on both sides.
  6. namespace loopwrap167q
  7. {
  8. // The callback's loop-region arithmetic, playback started at the song's start (before the region, as in
  9. // --cliplooptest): the number of samples each of `passes` passes lasted (between two wraps).
  10. inline std::vector<long> passLengths (double bpm, double rate, int block, double loopStart, double loopEnd, int passes)
  11. {
  12. const auto tps = lynx::ticksPerSample (bpm, rate);
  13. double position = 0.0; // playback starts at the song's start (the position's rounding error grows on the way)
  14. long played = 0, passStart = -1;
  15. std::vector<long> out;
  16. while ((int) out.size() < passes)
  17. {
  18. for (int done = 0; done < block;)
  19. {
  20. auto n = block - done;
  21. if (lynx::transportReached (position, loopEnd))
  22. position = loopStart;
  23. n = lynx::transportSamplesUntil (position, loopEnd, tps, n);
  24. position += n * tps;
  25. played += n;
  26. done += n;
  27. if (lynx::transportReached (position, loopEnd))
  28. {
  29. position = loopStart;
  30. if (passStart >= 0)
  31. out.push_back (played - passStart);
  32. passStart = played;
  33. }
  34. }
  35. }
  36. return out;
  37. }
  38. } // namespace loopwrap167q
  39. TEST (loopwrap167q_loop_region_wraps_do_not_drift)
  40. {
  41. const auto ppq = (double) lynx::ppq;
  42. struct Case
  43. {
  44. double bpm, rate;
  45. int block;
  46. double start, end;
  47. };
  48. for (const auto& c : { Case { 120.0, 48000.0, 512, 4 * ppq, 12 * ppq }, Case { 120.0, 48000.0, 480, 4 * ppq, 8 * ppq },
  49. Case { 120.0, 44100.0, 441, 0.0, 4 * ppq }, Case { 133.7, 48000.0, 512, 4 * ppq, 12 * ppq },
  50. Case { 97.0, 44100.0, 256, 3 * ppq, 7 * ppq }, Case { 174.0, 96000.0, 1024, 0.0, 16 * ppq } })
  51. {
  52. const auto tps = lynx::ticksPerSample (c.bpm, c.rate);
  53. const auto exact = (c.end - c.start) / tps; // samples per pass
  54. auto lengths = loopwrap167q::passLengths (c.bpm, c.rate, c.block, c.start, c.end, 100);
  55. lengths.resize (100);
  56. // every pass the same whole number of samples: the pass rounded up once (a wrap restarts on the region's exact
  57. // start tick, so a fraction of a sample is never carried: nothing accumulates); a whole-sample pass exactly
  58. const auto expected = (long) std::ceil (exact - 1.0e-6);
  59. long total = 0;
  60. bool each = true;
  61. for (const auto l : lengths)
  62. {
  63. total += l;
  64. each = each && l == expected;
  65. }
  66. const auto drift = (double) (total - 100 * expected);
  67. if (std::getenv ("LYNX_PRINT_LOOPWRAP") != nullptr)
  68. std::printf ("loopwrap %.1f BPM %.0f Hz block %d: pass %.4f samples, 100 passes = %ld samples (drift %.0f vs 100 x %ld)\n",
  69. c.bpm, c.rate, c.block, exact, total, drift, expected);
  70. CHECK (lengths.size() == 100 && each && drift == 0.0);
  71. }
  72. }
  73. // The helpers: a point within the epsilon counts as reached; the samples until a point are never one too many.
  74. TEST (loopwrap167q_transport_helpers)
  75. {
  76. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  77. CHECK (lynx::transportReached (1000.0 - 1.0e-9, 1000.0));
  78. CHECK (! lynx::transportReached (1000.0 - tps, 1000.0));
  79. CHECK (lynx::transportSamplesUntil (1000.0 - 64 * tps, 1000.0, tps, 512) == 64);
  80. CHECK (lynx::transportSamplesUntil (1000.0 - 64 * tps + 1.0e-9, 1000.0, tps, 512) == 64);
  81. CHECK (lynx::transportSamplesUntil (1000.0 - 64 * tps - 1.0e-9, 1000.0, tps, 512) == 64);
  82. CHECK (lynx::transportSamplesUntil (1000.0 - 63.5 * tps, 1000.0, tps, 512) == 64); // a fraction: up to the point
  83. CHECK (lynx::transportSamplesUntil (0.0, 1000.0, tps, 512) == 512);
  84. CHECK (lynx::transportSamplesUntil (1000.0, 1000.0, tps, 512) == 1);
  85. }