LinuxMouse165Tests.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Round 165 (linuxmouse165): Fruity WaveShaper's curve edited in LYNX (FL's point editor) — the state written back,
  2. // point edits, and the running effect taking a new curve without being rebuilt.
  3. #include "core/FruityShapers.h"
  4. TEST (linuxmouse165_waveshaper_state_round_trip)
  5. {
  6. namespace fs = lynx::flshape;
  7. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true);
  8. const auto a = fs::parseWaveShaper (stA);
  9. CHECK (a.has_value());
  10. if (! a)
  11. return;
  12. const auto bytes = fs::writeWaveShaper (*a, stA);
  13. const auto b = fs::parseWaveShaper (bytes);
  14. CHECK (b && b->pre == 150 && b->mix == 100 && b->post == 120 && b->center && ! b->unipolar && b->oversampling == 1 && b->hq && b->smooth);
  15. CHECK (b && b->points.size() == a->points.size());
  16. if (b)
  17. {
  18. for (std::size_t i = 0; i < a->points.size() && i < b->points.size(); ++i)
  19. CHECK (b->points[i].y == a->points[i].y && b->points[i].tension == a->points[i].tension && b->points[i].mode == a->points[i].mode
  20. && std::abs (b->points[i].dt - a->points[i].dt) < 1e-6f);
  21. CHECK (fs::shaperTable (b->points, false) == fs::shaperTable (a->points, false));
  22. CHECK (fs::shaperTable (b->points, true) == fs::shaperTable (a->points, true));
  23. }
  24. // the map's 20-byte header and the editor flag come from the old state (same layout, same size)
  25. CHECK (bytes.size() == stA.size());
  26. if (bytes.size() == stA.size())
  27. CHECK (std::equal (stA.end() - 26, stA.end(), bytes.end() - 26));
  28. // an empty slot: FL's default instance with the slot's controls, default curve (0,0)→(1,1)
  29. float params[8] = { 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
  30. const auto fresh = fs::waveShaperForEditing ({}, params);
  31. CHECK (fresh.pre == 128 && fresh.mix == 128 && fresh.unipolar && fresh.points.size() == 2);
  32. const auto fb = fs::parseWaveShaper (fs::writeWaveShaper (fresh));
  33. CHECK (fb && fb->unipolar && fb->points.size() == 2 && fb->points[1].dt == 1.0f && fb->points[1].y == 1.0f && fb->points[0].y == 0.0f);
  34. }
  35. TEST (linuxmouse165_waveshaper_point_edits)
  36. {
  37. namespace fs = lynx::flshape;
  38. auto pts = fs::waveShaperForEditing ({}).points; // (0,0) (1,1)
  39. // add between the ends, in time order
  40. CHECK (fs::addWaveShaperPoint (pts, 0.5f, 0.2f) == 1);
  41. CHECK (fs::addWaveShaperPoint (pts, 0.25f, 0.8f) == 1);
  42. auto t = fs::waveShaperTimes (pts);
  43. CHECK (pts.size() == 4 && t[1] == 0.25f && t[2] == 0.5f && t[3] == 1.0f && pts[1].y == 0.8f && pts[2].y == 0.2f);
  44. // the ends keep their time; inner points stay between their neighbours; levels clamp to 0…1
  45. CHECK (fs::moveWaveShaperPoint (pts, 0, 0.4f, 0.3f) == 0);
  46. CHECK (fs::moveWaveShaperPoint (pts, 3, 0.1f, 1.5f) == 3);
  47. CHECK (fs::moveWaveShaperPoint (pts, 1, 0.9f, -1.0f) == 1);
  48. t = fs::waveShaperTimes (pts);
  49. CHECK (t[0] == 0.0f && pts[0].y == 0.3f && t[3] == 1.0f && pts[3].y == 1.0f && t[1] == 0.5f && pts[1].y == 0.0f);
  50. // the ends can't be removed; an inner point can, and the next one keeps its time
  51. CHECK (! fs::removeWaveShaperPoint (pts, 0) && ! fs::removeWaveShaperPoint (pts, 3));
  52. CHECK (fs::removeWaveShaperPoint (pts, 1));
  53. t = fs::waveShaperTimes (pts);
  54. CHECK (pts.size() == 3 && t[1] == 0.5f && t[2] == 1.0f && pts[1].y == 0.2f);
  55. // the drawn level follows the table: the default curve is the identity in both polarities
  56. const auto ident = fs::waveShaperForEditing ({}).points;
  57. for (const bool uni : { false, true })
  58. {
  59. const auto table = fs::shaperTable (ident, uni);
  60. CHECK (std::abs (fs::waveShaperLevelAt (table, uni, 0.25f) - 0.25f) < 1e-3f && std::abs (fs::waveShaperLevelAt (table, uni, 1.0f) - 1.0f) < 1e-3f);
  61. }
  62. CHECK (fs::waveShaperModes().size() == 13 && fs::waveShaperModes().front().mode == 0);
  63. }
  64. TEST (linuxmouse165_waveshaper_takes_a_new_curve_while_running)
  65. {
  66. namespace fs = lynx::flshape;
  67. const auto st = waveShaperTestState (100, 128, 128, false, false, 0, false, false); // stateless kernel: exact compare
  68. auto edited = fs::waveShaperForEditing (st);
  69. fs::moveWaveShaperPoint (edited.points, 2, 0.6f, 0.1f);
  70. const auto st2 = fs::writeWaveShaper (edited, st);
  71. auto slot = lynx::effectFromFl ("Fruity WaveShaper", st, 120.0);
  72. CHECK (slot.has_value());
  73. if (! slot)
  74. return;
  75. auto running = lynx::makeEffect (*slot);
  76. running->prepare (44100.0);
  77. std::vector<float> l (512), r (512);
  78. auto fill = [&] { for (int n = 0; n < 512; ++n) l[(std::size_t) n] = r[(std::size_t) n] = (float) std::sin (n * 0.05) * 0.9f; };
  79. fill();
  80. running->process (l.data(), r.data(), 512, slot->params, 120.0);
  81. CHECK (running->acceptState (st2));
  82. fill();
  83. running->process (l.data(), r.data(), 512, slot->params, 120.0);
  84. auto slot2 = *slot;
  85. slot2.state = st2;
  86. auto rebuilt = lynx::makeEffect (slot2);
  87. rebuilt->prepare (44100.0);
  88. std::vector<float> l2 (512), r2 (512);
  89. for (int n = 0; n < 512; ++n)
  90. l2[(std::size_t) n] = r2[(std::size_t) n] = (float) std::sin (n * 0.05) * 0.9f;
  91. rebuilt->process (l2.data(), r2.data(), 512, slot2.params, 120.0);
  92. CHECK (l == l2 && r == r2);
  93. CHECK (! running->acceptState ({ 1, 2, 3 })); // not a WaveShaper state: the slot is built again
  94. }