Match124xTests.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Round 124 (extras): her corpus' other third-party plugins measured on the official trials and their LYNX substitutes
  2. // matched (docs/analise/REA_rodada124_extras.md). Golden curves are the references' own measured responses at her
  3. // saved settings.
  4. #include "core/FabFilterStates.h"
  5. #include "core/FacetEq.h"
  6. namespace m124x
  7. {
  8. constexpr double sr = 48000.0;
  9. // a FabFilter "FFBS" component state (version 1) holding `values`
  10. inline std::vector<std::uint8_t> ffbs (const std::vector<float>& values)
  11. {
  12. std::vector<std::uint8_t> b { 'F', 'F', 'B', 'S' };
  13. auto put = [&b] (std::uint32_t u) {
  14. for (int i = 0; i < 4; ++i)
  15. b.push_back ((std::uint8_t) (u >> (8 * i)));
  16. };
  17. put (1);
  18. put ((std::uint32_t) values.size());
  19. for (const auto v : values)
  20. {
  21. std::uint32_t u;
  22. std::memcpy (&u, &v, 4);
  23. put (u);
  24. }
  25. return b;
  26. }
  27. inline lynx::EffectSlot substitute (const char* name, const std::vector<std::uint8_t>& state, int& translated)
  28. {
  29. lynx::PluginIdentity id;
  30. id.format = lynx::ForeignPlugin::Format::vst3;
  31. id.name = name;
  32. id.state = state;
  33. const auto* entry = lynx::findPluginSubstitute (id, false);
  34. CHECK (entry != nullptr);
  35. translated = 0;
  36. return entry != nullptr ? lynx::makeSubstituteSlot (*entry, &id, &translated) : lynx::EffectSlot {};
  37. }
  38. // |H(f)| in dB of an effect (left channel), from its impulse response
  39. inline std::vector<double> magnitudeDb (const lynx::EffectSlot& slot, const std::vector<double>& hz, int length = 1 << 15)
  40. {
  41. auto fx = lynx::makeEffect (slot);
  42. fx->prepare (sr);
  43. fx->reset();
  44. std::vector<float> l ((std::size_t) length, 0.0f), r ((std::size_t) length, 0.0f);
  45. l[0] = r[0] = 1.0f;
  46. for (int off = 0; off < length; off += 256)
  47. fx->process (l.data() + off, r.data() + off, std::min (256, length - off), slot.params, 120.0);
  48. std::vector<double> out;
  49. for (const auto f : hz)
  50. {
  51. double re = 0.0, im = 0.0;
  52. const auto w = 2.0 * 3.141592653589793 * f / sr;
  53. for (int i = 0; i < length; ++i)
  54. {
  55. re += l[(std::size_t) i] * std::cos (w * i);
  56. im -= l[(std::size_t) i] * std::sin (w * i);
  57. }
  58. out.push_back (10.0 * std::log10 (re * re + im * im + 1e-30));
  59. }
  60. return out;
  61. }
  62. // one Pro-Q 4 band (23 values) in a 600-value state; the globals as her states hold them
  63. inline void proQBand (std::vector<float>& v, int band, double hz, double gainDb, double qNorm, int shape, int slope)
  64. {
  65. const auto b = (std::size_t) (23 * band);
  66. const float values[23] { 1, 1, (float) std::log2 (hz), (float) gainDb, (float) qNorm, (float) shape, (float) slope, 2, 1, 0, 1, 1, 1,
  67. 50, 50, 0, 0, 6.644f, 11.55f, 0, 0, 50, 0 };
  68. std::copy (std::begin (values), std::end (values), v.begin() + (std::ptrdiff_t) b);
  69. }
  70. inline std::vector<float> proQGlobals()
  71. {
  72. std::vector<float> v (600, 0.0f);
  73. for (int b = 0; b < 24; ++b)
  74. {
  75. v[(std::size_t) (23 * b + 1)] = 1.0f;
  76. v[(std::size_t) (23 * b + 2)] = 9.966f;
  77. v[(std::size_t) (23 * b + 4)] = 0.5f;
  78. v[(std::size_t) (23 * b + 6)] = 2.0f;
  79. v[(std::size_t) (23 * b + 7)] = 2.0f;
  80. }
  81. const float g[] { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, -1, 1, 2, 2, 3, 0, 1, 1, 2, 0, 0, 1, 1 };
  82. std::copy (std::begin (g), std::end (g), v.begin() + 552);
  83. return v;
  84. }
  85. } // namespace m124x
  86. TEST (pro_q4_states_translate_onto_facet_and_match_the_reference_round124)
  87. {
  88. using namespace lynx;
  89. // the reference's |H| in dB (official trial, impulse response, exact DTFT) at her four saved states
  90. const std::vector<double> hz { 29.297, 58.594, 99.609, 199.219, 498.047, 1001.953, 1998.047, 4998.047, 10001.953, 15000.0, 19001.953 };
  91. struct Case
  92. {
  93. const char* what;
  94. std::vector<std::array<double, 5>> bands; // hz, gain dB, Q (norm), shape, slope index
  95. std::array<double, 11> ref;
  96. };
  97. const Case cases[] {
  98. { "bell 153.5 Hz -1.5 dB Q 1", { { 153.51, -1.5, 0.5, 0, 2 } },
  99. { -0.110, -0.429, -1.072, -1.316, -0.283, -0.071, -0.018, -0.003, -0.001, -0.000, -0.000 } },
  100. { "low cut 508.8 Hz + high cut 9868 Hz, 12 dB/oct", { { 508.77, 0, 0.51, 2, 2 }, { 9868.0, 0, 0.508, 4, 2 } },
  101. { -49.584, -37.532, -28.289, -16.206, -2.557, 0.028, 0.092, -0.026, -2.615, -7.935, -12.274 } },
  102. { "low cut 82.4 Hz Q 0.471, 24 dB/oct", { { 82.370, 0, 0.398, 2, 4 } },
  103. { -37.090, -16.766, -6.662, -1.604, -0.246, -0.060, -0.015, -0.002, -0.001, -0.000, -0.000 } },
  104. { "bells 116.8 Hz -4.56 dB Q 4.27, 9899 Hz -0.12 dB Q 1.96", { { 116.76, -4.56, 0.696692, 0, 2 }, { 9898.7, -0.12, 0.591071, 0, 2 } },
  105. { -0.037, -0.223, -2.364, -0.380, -0.032, -0.008, -0.005, -0.023, -0.120, -0.047, -0.024 } },
  106. };
  107. for (const auto& c : cases)
  108. {
  109. auto v = m124x::proQGlobals();
  110. int b = 0;
  111. for (const auto& band : c.bands)
  112. m124x::proQBand (v, b++, band[0], band[1], band[2], (int) band[3], (int) band[4]);
  113. int n = 0;
  114. const auto slot = m124x::substitute ("FabFilter Pro-Q 4", m124x::ffbs (v), n);
  115. CHECK (slot.kind == EffectKind::facetEq && n == 5 * (int) c.bands.size() + 5); // round 144: + resolution, character
  116. const auto mag = m124x::magnitudeDb (slot, hz);
  117. double worst = 0.0;
  118. for (std::size_t k = 0; k < hz.size(); ++k)
  119. worst = std::max (worst, std::abs (mag[k] - c.ref[k]));
  120. CHECK (worst < 0.1);
  121. if (worst >= 0.1 || std::getenv ("M124X_VERBOSE"))
  122. for (std::size_t k = 0; k < hz.size(); ++k) std::printf (" %.0f Hz lynx %.3f ref %.3f\n", hz[k], mag[k], c.ref[k]);
  123. if (worst >= 0.1)
  124. std::printf (" Pro-Q 4 %s: worst %.3f dB\n", c.what, worst);
  125. }
  126. // a band placed on one side only, or unused, is not taken; a state of another size is not read
  127. {
  128. auto v = m124x::proQGlobals();
  129. m124x::proQBand (v, 0, 1000.0, 6.0, 0.5, 0, 2);
  130. v[7] = 3.0f; // mid
  131. int n = 0;
  132. const auto slot = m124x::substitute ("FabFilter Pro-Q 4", m124x::ffbs (v), n);
  133. CHECK (n == 5 && facet::shape (slot.params[(std::size_t) facet::param (0, 0)]) == facet::Shape::off);
  134. v.resize (599);
  135. m124x::substitute ("FabFilter Pro-Q 4", m124x::ffbs (v), n);
  136. CHECK (n == 0);
  137. }
  138. }
  139. TEST (waves_s1_imager_translates_onto_the_stereo_shaper_matrix_round124)
  140. {
  141. using namespace lynx;
  142. // L → L, R → L, L → R, R → R of the official trial (white noise, least squares; REA_rodada124_extras §4)
  143. struct Case
  144. {
  145. const char* values;
  146. double ll, rl, lr, rr;
  147. };
  148. const Case cases[] {
  149. { "0 1.22 1 650 0 0 * * * * * 0 0 0 0 0 0 0", 1.0395, -0.1030, -0.1030, 1.0395 }, // her state 0
  150. { "0 3 1 650 0 0 * * * * * 0 0 0 0 0 0 0", 1.63299, -0.81650, -0.81650, 1.63299 }, // her state 1
  151. { "0 0.38 1 650 0 0 * * * * * 0 0 0 0 0 0 0", 0.69, 0.31, 0.31, 0.69 },
  152. { "0 1.88 1 650 0 0 * * * * * 0 0 0 0 0 0 0", 1.23126, -0.37622, -0.37622, 1.23126 },
  153. { "0 1 1 650 22.5 0 * * * * * 0 0 0 0 0 0 0", 0.92388, -0.38268, 0.38268, 0.92388 },
  154. { "0 1.88 1 650 22.5 0 * * * * * 0 0 0 0 0 0 0", 1.28151, -0.81877, 0.12360, 0.99357 },
  155. { "-12 1 1 650 0 0 * * * * * 0 0 0 0 0 0 0", 0.25119, 0.0, 0.0, 0.25119 },
  156. };
  157. const auto slotFor = [] (const char* values, int& n) {
  158. PluginIdentity id;
  159. id.format = ForeignPlugin::Format::vst3;
  160. id.name = "WaveShell1-VST3 16.0_x64";
  161. id.state = substtest::wavesComponent ("S1 Imager", values);
  162. const auto* entry = findPluginSubstitute (id, false);
  163. CHECK (entry != nullptr);
  164. n = -1;
  165. return entry != nullptr ? makeSubstituteSlot (*entry, &id, &n) : EffectSlot {};
  166. };
  167. for (const auto& c : cases)
  168. {
  169. int n = 0;
  170. const auto slot = slotFor (c.values, n);
  171. CHECK (slot.kind == EffectKind::stereoShaper && n == 4);
  172. auto fx = makeEffect (slot);
  173. fx->prepare (48000.0);
  174. fx->reset();
  175. // a steady input after the gains have settled: [L, R] = [1, 0], then [0, 1]
  176. double out[2][2] {};
  177. for (int src = 0; src < 2; ++src)
  178. {
  179. std::vector<float> l (9600, src == 0 ? 1.0f : 0.0f), r (9600, src == 1 ? 1.0f : 0.0f);
  180. for (int off = 0; off < 9600; off += 480)
  181. fx->process (l.data() + off, r.data() + off, 480, slot.params, 120.0);
  182. out[src][0] = l.back();
  183. out[src][1] = r.back();
  184. }
  185. const auto worst = std::max ({ std::abs (out[0][0] - c.ll), std::abs (out[1][0] - c.rl), std::abs (out[0][1] - c.lr), std::abs (out[1][1] - c.rr) });
  186. CHECK (worst < 2e-4);
  187. if (worst >= 2e-4)
  188. std::printf (" S1 %s: L=%.5f·L%+.5f·R R=%.5f·L%+.5f·R\n", c.values, out[0][0], out[1][0], out[0][1], out[1][1]);
  189. }
  190. // asymmetry (not measured beyond 0) leaves the slot untranslated
  191. int n = -1;
  192. slotFor ("0 1.22 1 650 0 45 * * * * * 0 0 0 0 0 0 0", n);
  193. CHECK (n == 0);
  194. }