HardcoreTests.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "core/HardcoreFx.h"
  2. // Hardcore, exact (REA_rodada71): goldens of Hardcore_x64.dll (the installation's plug-in, run unmodified) on states of
  3. // our own — 50 parameters from an LCG, the box types and switches set below — and this test's input, rendered with the
  4. // state before the rate, blocks of 512. Bit-identical. The cabinet cases read the installation's impulses (skipped
  5. // without it).
  6. namespace
  7. {
  8. std::vector<std::uint8_t> hardcoreTestState (std::uint32_t seed, std::array<int, 5> types, std::array<int, 5> on, int eq, int cab)
  9. {
  10. std::array<float, 50> v {};
  11. auto x = seed;
  12. for (auto& f : v)
  13. {
  14. x = (x * 1103515245u + 12345u) & 0x7fffffffu;
  15. f = (float) ((x >> 8) & 0x7fffffu) / 8388608.0f;
  16. }
  17. v[0] = 0.55f, v[1] = 0.45f, v[2] = (float) eq, v[11] = (float) cab;
  18. v[12] = v[38] = v[44] = 0.5f;
  19. for (int b = 0; b < 5; ++b)
  20. v[(std::size_t) (39 + b)] = (float) on[(std::size_t) b], v[(std::size_t) (45 + b)] = (float) types[(std::size_t) b];
  21. std::vector<std::uint8_t> s { 'S', 'S', 'L', 'F', 0xcc, 0x01, 0, 0, '1', 'R', 'C', 'H' };
  22. const auto* p = (const std::uint8_t*) v.data();
  23. s.insert (s.end(), p, p + 200);
  24. const char name[] = "LYNX test";
  25. s.insert (s.end(), name, name + sizeof (name) - 1);
  26. s.resize (8 + 0x1cc, 0);
  27. return s;
  28. }
  29. std::vector<std::uint8_t> hardcoreInstallFile (const std::string& relative)
  30. {
  31. if (realFlpDir.empty())
  32. return {};
  33. auto path = std::filesystem::path (realFlpDir).parent_path();
  34. std::string rel = relative;
  35. std::replace (rel.begin(), rel.end(), '\\', '/');
  36. std::ifstream in (path / rel, std::ios::binary);
  37. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  38. }
  39. } // namespace
  40. TEST (hardcore_exact)
  41. {
  42. constexpr int n = 8192;
  43. std::vector<float> inL (n), inR (n);
  44. for (int i = 0; i < n; ++i)
  45. {
  46. inL[(std::size_t) i] = (float) (((i * 37) % 200 - 100) / 60.0);
  47. inR[(std::size_t) i] = (float) (((i * 53) % 170 - 85) / 50.0);
  48. }
  49. struct Gold { int i; float l, r; };
  50. auto run = [&] (const std::vector<std::uint8_t>& st, bool cabinet, std::initializer_list<Gold> gold) {
  51. const auto s = lynx::hardcore::parse (st);
  52. CHECK (s.has_value() && s->presetName == "LYNX test");
  53. if (! s)
  54. return;
  55. lynx::hardcore::Engine e;
  56. if (cabinet)
  57. for (int c = 1; c <= 20; ++c)
  58. e.setCabinetFile (c, hardcoreInstallFile (lynx::hardcore::cabinetPath (c)));
  59. for (int i = 0; i < 50; ++i)
  60. e.set (i, s->values[(std::size_t) i]);
  61. e.prepare (44100.0);
  62. std::vector<float> l (inL), r (inR);
  63. for (int i = 0; i < n; i += 512)
  64. e.process (l.data() + i, r.data() + i, 512);
  65. for (const auto& g : gold)
  66. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  67. };
  68. // distortion A, chorus, flanger, phaser, reverb; EQ on
  69. run (hardcoreTestState (1, { 1, 2, 3, 4, 5 }, { 1, 1, 1, 1, 1 }, 1, 0), false,
  70. { { 100, 0.15404899418354034f, 0.15402227640151978f }, { 1000, 0.5094000101089478f, 0.18492065370082855f },
  71. { 2047, -1.0110447406768799f, -0.9156813621520996f }, { 4000, -1.7594877481460571f, 0.10223101079463959f },
  72. { 8191, 0.14906616508960724f, 1.9577150344848633f } });
  73. // delay, noise gate, equalizer, compressor, modulator
  74. run (hardcoreTestState (2, { 6, 7, 8, 9, 10 }, { 1, 1, 1, 1, 1 }, 0, 0), false,
  75. { { 100, 0.0f, 0.0f }, { 1000, -0.02293410710990429f, -0.049560099840164185f },
  76. { 2047, 0.0009527528891339898f, 0.002417751122266054f }, { 4000, -0.07934026420116425f, -0.12522825598716736f },
  77. { 8191, -0.29015353322029114f, -0.10772112756967545f } });
  78. // tube drive, an empty box, a switched-off distortion, chorus, delay; EQ on
  79. run (hardcoreTestState (3, { 11, 0, 1, 2, 6 }, { 1, 1, 0, 1, 1 }, 1, 0), false,
  80. { { 100, 0.06743612140417099f, 0.11324237287044525f }, { 1000, -0.019734300673007965f, -0.3736976683139801f },
  81. { 2047, 0.8394869565963745f, 1.0702182054519653f }, { 4000, -0.34187665581703186f, -0.32515519857406616f },
  82. { 8191, 0.4963859021663666f, 0.36226585507392883f } });
  83. if (! hardcoreInstallFile (lynx::hardcore::cabinetPath (4)).empty())
  84. {
  85. // cabinets "Model D" (float WAV) and "Model T" (24-bit stereo WAV)
  86. run (hardcoreTestState (4, { 6, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 }, 0, 4), true,
  87. { { 100, 0.1504828929901123f, 0.12085545808076859f }, { 1000, -0.15621426701545715f, -0.12705086171627045f },
  88. { 2047, -0.17077459394931793f, 0.22107036411762238f }, { 4000, -0.15526969730854034f, -0.03145243600010872f },
  89. { 8191, 0.4664047658443451f, -0.37835803627967834f } });
  90. run (hardcoreTestState (5, { 2, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 }, 1, 20), true,
  91. { { 100, 0.05236158147454262f, -0.0008346981485374272f }, { 1000, 3.7916646003723145f, 6.497415542602539f },
  92. { 2047, -6.757109642028809f, -4.441843032836914f }, { 4000, 2.6982486248016357f, -0.5133857131004333f },
  93. { 8191, -2.7014360427856445f, 1.8544065952301025f } });
  94. // the FL import: the slot carries the installation's impulses; the mixer effect equals the engine
  95. const auto st = hardcoreTestState (4, { 6, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 }, 0, 4);
  96. const auto slot = lynx::effectFromFl ("Hardcore", st, 120.0, hardcoreInstallFile);
  97. CHECK (slot && slot->kind == lynx::EffectKind::hardcore && lynx::hardcore::hasCabinet (*slot, 4)
  98. && lynx::hardcore::hasCabinet (*slot, 20));
  99. if (slot)
  100. {
  101. auto fx = lynx::makeEffect (*slot);
  102. fx->prepare (44100.0);
  103. std::vector<float> l (inL), r (inR);
  104. fx->process (l.data(), r.data(), 700, slot->params, 120.0);
  105. fx->process (l.data() + 700, r.data() + 700, n - 700, slot->params, 120.0);
  106. CHECK (l[1000] == -0.15621426701545715f && r[8191] == -0.37835803627967834f);
  107. }
  108. }
  109. // import without the installation: the exact engine, cabinet off; parameters round trip through the slot
  110. const auto st = hardcoreTestState (1, { 1, 2, 3, 4, 5 }, { 1, 1, 1, 1, 1 }, 1, 7);
  111. const auto slot = lynx::effectFromFl ("Hardcore", st, 120.0);
  112. CHECK (slot && slot->kind == lynx::EffectKind::hardcore && slot->state.empty());
  113. if (slot)
  114. {
  115. const auto s = *lynx::hardcore::parse (st);
  116. for (int i = 0; i < 50; ++i)
  117. CHECK (lynx::hardcore::fromSlot (i, slot->params[(std::size_t) i]) == s.values[(std::size_t) i]);
  118. CHECK (lynx::formatEffectParam (lynx::EffectKind::hardcore, 45, slot->params[45], 120.0) == "Distortion");
  119. CHECK (lynx::formatEffectParam (lynx::EffectKind::hardcore, 11, slot->params[11], 120.0) == "Model G");
  120. }
  121. CHECK (lynx::effectInfo (lynx::EffectKind::hardcore).params.size() == 50);
  122. CHECK (lynx::hardcore::cabinetPath (9) == "Plugins\\Fruity\\Effects\\Hardcore\\Imp\\Model I .wav");
  123. CHECK (! lynx::hardcore::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  124. }