Emphasis167fTests.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Round 167f (REA_rodada167f §1): FL's Emphasis on its own engine
  2. namespace
  3. {
  4. std::vector<std::uint8_t> emphasisState (int version, std::array<int, 13> raw, std::array<std::uint8_t, 3> options)
  5. {
  6. // FL's 0xD5 layout: i32 version · 13 i32 controls · i32 UI page · i32 (v ≥ 7) · 3 option bytes (v ≥ 9 / 10)
  7. std::vector<std::uint8_t> d (4 + 13 * 4 + 8, 0);
  8. std::memcpy (d.data(), &version, 4);
  9. std::memcpy (d.data() + 4, raw.data(), 13 * 4);
  10. const std::int32_t one = 1;
  11. std::memcpy (d.data() + 60, &one, 4);
  12. if (version >= 9)
  13. d.push_back (options[0]);
  14. if (version >= 10)
  15. {
  16. d.push_back (options[1]);
  17. d.push_back (options[2]);
  18. }
  19. return d;
  20. }
  21. } // namespace
  22. TEST (emphasis_state_and_engine_values_match_fl)
  23. {
  24. using namespace lynx;
  25. // the "Default" preset (version 10): the controls, the option bytes, FL's texts
  26. const auto def = emphasisState (10, { 0, 0, 1, 0, 0, 0, 0, 10000, 10000, 9912, 0, 2, 7 }, { 0, 1, 1 });
  27. const auto s = emphasis::parse (def);
  28. CHECK (s && s->raw == emphasis::rawDefault);
  29. auto slot = effectFromFl ("Emphasis", def);
  30. CHECK (slot && slot->kind == EffectKind::flEmphasis);
  31. if (slot)
  32. for (int k = 0; k < emphasis::numParams; ++k)
  33. CHECK (std::abs (slot->params[(std::size_t) k] - effectInfo (EffectKind::flEmphasis).params[(std::size_t) k].defaultValue) < 1e-6f);
  34. // versions before 8 store the envelope order 0-based; before 9 / 10 the option bytes are missing (off)
  35. const auto v7 = emphasis::parse (emphasisState (7, { 0, 0, 0, 0, 0, 0, 0, 10000, 10000, 9912, 0, 2, 7 }, {}));
  36. CHECK (v7 && v7->raw[2] == 1 && v7->raw[13] == 0 && v7->raw[14] == 0 && v7->raw[15] == 0);
  37. const auto v9 = emphasis::parse (emphasisState (9, { 0, 0, 3, 0, 0, 0, 0, 10000, 10000, 9912, 0, 2, 7 }, { 1, 1, 1 }));
  38. CHECK (v9 && v9->raw[2] == 3 && v9->raw[13] == 1 && v9->raw[14] == 0);
  39. CHECK (! emphasis::parse (std::vector<std::uint8_t> (20, 0)));
  40. // what FL's front end sends to the engine (EXEC: the plug-in's calls logged in our harness)
  41. CHECK (emphasis::engineValue (2, 4) == 3.0f);
  42. CHECK (emphasis::engineValue (6, 625) == 1.5f);
  43. CHECK (emphasis::engineValue (6, 2081) == 4.9944f);
  44. CHECK (emphasis::engineValue (7, 7475) == 0.7475f);
  45. CHECK (emphasis::engineValue (8, 5033) == 0.5033f);
  46. const std::pair<int, std::uint32_t> ceiling[] { { 1000, 0xc1fabac9u }, { 3000, 0xc19c6707u }, { 7000, 0xc0e6e0c7u },
  47. { 9500, 0xbf938d0au }, { 9900, 0xbe6ae35bu }, { 9912, 0xbe4eabd5u } };
  48. for (const auto& [raw, bits] : ceiling)
  49. {
  50. const float v = emphasis::engineValue (9, raw);
  51. std::uint32_t u;
  52. std::memcpy (&u, &v, 4);
  53. CHECK (u == bits);
  54. }
  55. CHECK (emphasis::engineValue (9, 10000) == 0.0f && std::isinf (emphasis::engineValue (9, 0)));
  56. CHECK (formatEffectParam (EffectKind::flEmphasis, 5, 1.0f, 120.0) == "Steady");
  57. CHECK (formatEffectParam (EffectKind::flEmphasis, 11, 1.0f, 120.0) == "16x");
  58. CHECK (formatEffectParam (EffectKind::flEmphasis, 4, 1.0f, 120.0) == "M/S");
  59. CHECK (formatEffectParam (EffectKind::flEmphasis, 8, 0.0f, 120.0) == "Off");
  60. CHECK (formatEffectParam (EffectKind::flEmphasis, 2, 1.0f, 120.0) == "4");
  61. // the editor shows every control (plugin_layouts_name_real_parameters checks them all)
  62. CHECK (plugui::hasFlLayout (EffectKind::flEmphasis));
  63. }
  64. TEST (emphasis_matches_fl_engine)
  65. {
  66. // FL's Emphasis through the import (state → slot → effect) against goldens of the installation's Emphasis_x64.dll
  67. // run in our harness (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  68. // 512-frame calls): FNV-1a of the output's float bits, at 44.1 and 48 kHz
  69. auto signal = [] {
  70. std::vector<float> l (24000), r (24000);
  71. std::uint32_t s = 12345;
  72. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  73. for (int i = 0; i < 24000; ++i)
  74. {
  75. const float a = amps[(i / 3000) % 8];
  76. s = s * 1664525u + 1013904223u;
  77. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  78. s = s * 1664525u + 1013904223u;
  79. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  80. }
  81. return std::pair { l, r };
  82. };
  83. struct Case
  84. {
  85. const char* name;
  86. int version;
  87. std::array<int, 13> raw;
  88. std::array<std::uint8_t, 3> options;
  89. std::uint64_t hash44, hash48;
  90. };
  91. const Case cases[] {
  92. { "default", 10, { 0, 0, 1, 0, 0, 0, 0, 10000, 10000, 9912, 0, 2, 7 }, { 0, 1, 1 }, 0x6dbb0cdfd687f2abull, 0xf990cbf2ee5448bfull },
  93. { "M/S +6 dB (NIGHT TERROR)", 8, { 0, 0, 1, 0, 1, 0, 2499, 7475, 10000, 9900, 0, 1, 8 }, { 0, 0, 0 }, 0x5b8303e83cbc37e5ull, 0x2b8742c7006b815bull },
  94. { "steady, true peak, 4th order", 8, { 0, 0, 4, 1, 0, 3, 889, 10000, 10000, 9874, 0, 1, 6 }, { 0, 0, 0 }, 0xc665f77415828310ull, 0xdbe633988df90965ull },
  95. { "loud, 16x, level matching", 10, { 0, 0, 1, 0, 0, 1, 4000, 10000, 10000, 9912, 1, 4, 0 }, { 1, 0, 1 }, 0x9500799e32c644e2ull, 0xe2cbb82388789043ull },
  96. { "transient, no oversampling, delta", 10, { 0, 1, 2, 0, 0, 2, 6000, 10000, 5000, 9912, 0, 0, 9 }, { 0, 1, 0 }, 0x53159b307e2c57daull, 0x87aa8e9a0d1e1ec2ull },
  97. { "bypass", 10, { 1, 0, 1, 0, 0, 0, 3000, 10000, 10000, 9912, 0, 2, 7 }, { 0, 1, 1 }, 0xbe5984abc51cdaa6ull, 0xdaf3a86157b4b7cbull },
  98. };
  99. for (const auto& c : cases)
  100. for (const double rate : { 44100.0, 48000.0 })
  101. {
  102. const auto slot = lynx::effectFromFl ("Emphasis", emphasisState (c.version, c.raw, c.options));
  103. CHECK (slot && slot->kind == lynx::EffectKind::flEmphasis);
  104. if (! slot)
  105. continue;
  106. auto fx = lynx::makeEffect (*slot);
  107. fx->prepare (rate);
  108. auto [l, r] = signal();
  109. for (int i = 0; i < 24000; i += 512)
  110. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  111. std::uint64_t h = 0xcbf29ce484222325ull;
  112. for (int i = 0; i < 24000; ++i)
  113. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  114. {
  115. std::uint32_t u;
  116. std::memcpy (&u, &v, 4);
  117. h = (h ^ u) * 0x100000001b3ull;
  118. }
  119. if (h != (rate == 44100.0 ? c.hash44 : c.hash48))
  120. std::printf ("emphasis %s @ %.0f: 0x%016llx\n", c.name, rate, (unsigned long long) h);
  121. CHECK (h == (rate == 44100.0 ? c.hash44 : c.hash48));
  122. }
  123. }
  124. TEST (emphasis_reports_its_latency_before_the_first_block)
  125. {
  126. // FL's engine reports 2381 samples for the Default preset at 44.1 kHz (4x oversampling); LYNX knows it from the
  127. // slot's controls before the first block (PDC) and keeps it after
  128. using namespace lynx;
  129. auto slot = makeEffectSlot (EffectKind::flEmphasis);
  130. auto fx = makeEffect (slot);
  131. fx->prepare (44100.0);
  132. CHECK (fx->latencySamples() == 2381);
  133. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  134. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  135. CHECK (fx->latencySamples() == 2381);
  136. }