Mod190doTests.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Round 190do: deterministic modulation laws of Spiral Echo, Orbit, Lattice Mod and Gemini Doubler, measured black-box
  2. // on the tp172d reference renders (/mnt/e/lynx-work/mod190do/HANDOFF.md). Each effect gets an appended law parameter:
  3. // new instances run the measured law, a project or preset saved before keeps the classic one. What is checked: the
  4. // parameters and their load rule, that the classic laws still render what the base commit rendered (fingerprints from
  5. // the base commit's objects, /mnt/e/lynx-work/mod190do/gold), the measured laws' own behaviour, and their stability.
  6. #include <cmath>
  7. #include <cstdint>
  8. #include <cstring>
  9. #include "core/EchoSuite.h"
  10. #include "core/Effects.h"
  11. #include "core/VoiceTools.h"
  12. namespace mod190do
  13. {
  14. using lynx::EffectKind;
  15. struct Stereo
  16. {
  17. std::vector<float> l, r;
  18. };
  19. struct LawOf
  20. {
  21. EffectKind kind;
  22. std::size_t index;
  23. const char* name;
  24. };
  25. inline const LawOf laws[] = {
  26. { EffectKind::spiralEcho, (std::size_t) lynx::echosuite::spiral::law, "Law" },
  27. { EffectKind::orbit, (std::size_t) lynx::echosuite::orbit::law, "Law" },
  28. { EffectKind::latticeMod, (std::size_t) lynx::echosuite::lattice::law, "Law" },
  29. { EffectKind::geminiDoubler, (std::size_t) lynx::voicetools::gemini::headLaw, "Head law" },
  30. };
  31. // `seconds` of the effect's answer to `in` (both channels), from a fresh instance, in blocks
  32. inline Stereo render (EffectKind kind, const lynx::EffectParams& p, const Stereo& in, double sr = 48000.0, int block = 256)
  33. {
  34. auto fx = lynx::makeEffect (kind);
  35. fx->prepare (sr);
  36. auto out = in;
  37. const auto n = (int) out.l.size();
  38. for (int off = 0; off < n; off += block)
  39. fx->process (out.l.data() + off, out.r.data() + off, std::min (block, n - off), p, 120.0);
  40. return out;
  41. }
  42. inline Stereo impulse (double seconds, double sr = 48000.0, float a = 0.5f)
  43. {
  44. const auto n = (std::size_t) (seconds * sr);
  45. Stereo s { std::vector<float> (n, 0.0f), std::vector<float> (n, 0.0f) };
  46. s.l[0] = s.r[0] = a;
  47. return s;
  48. }
  49. inline bool finite (const Stereo& s)
  50. {
  51. for (const auto* v : { &s.l, &s.r })
  52. for (const float x : *v)
  53. if (! std::isfinite (x) || std::abs (x) > 64.0f)
  54. return false;
  55. return true;
  56. }
  57. } // namespace mod190do
  58. TEST (mod190do_new_instances_run_the_measured_laws_and_saved_projects_keep_the_classic_ones)
  59. {
  60. using namespace lynx;
  61. for (const auto& law : mod190do::laws)
  62. {
  63. const auto& info = effectInfo (law.kind).params;
  64. CHECK (info.size() == law.index + 1 && std::string (info[law.index].name) == law.name);
  65. CHECK (formatEffectParam (law.kind, (int) law.index, 0.0f, 120.0) == "Classic");
  66. CHECK (formatEffectParam (law.kind, (int) law.index, 1.0f, 120.0) == "Measured");
  67. auto fresh = makeEffectSlot (law.kind);
  68. CHECK (fresh.params[law.index] >= 0.5f);
  69. auto old = makeEffectSlot (law.kind);
  70. upgradeLoadedEffectParams (old, law.index); // saved by 0.1.26: one parameter short
  71. CHECK (old.params[law.index] < 0.5f);
  72. for (const float v : { 0.0f, 1.0f })
  73. {
  74. auto again = makeEffectSlot (law.kind);
  75. again.params[law.index] = v;
  76. const auto count = savedEffectParamCount (again);
  77. // (round 190fy: a Spiral Echo also saves its hidden loop revision, one past the Law; round 190it: a Lattice Mod
  78. // its hidden tap-network revision)
  79. // round 190ly: and a Spiral Echo its hidden rate-independent loop phase, one further
  80. CHECK (count == law.index + 1 + (law.kind == EffectKind::spiralEcho ? 2u : law.kind == EffectKind::latticeMod ? 1u : 0u));
  81. auto loaded = makeEffectSlot (law.kind);
  82. for (std::size_t i = 0; i < count; ++i)
  83. loaded.params[i] = std::clamp ((float) savedEffectParamValue (again.params[i]), 0.0f, 1.0f);
  84. upgradeLoadedEffectParams (loaded, count);
  85. CHECK (loaded.params[law.index] == v);
  86. }
  87. }
  88. }
  89. TEST (mod190do_gemini_measured_head_reads_one_tap_that_drifts_and_jumps_back)
  90. {
  91. using namespace lynx;
  92. namespace gm = voicetools::gemini;
  93. // voice 1 alone (left), direct off, its default delay (9.4 ms) and +6 cents; impulses every 0.25 s
  94. auto p = makeEffectSlot (EffectKind::geminiDoubler).params;
  95. p[gm::directOn] = 0.0f;
  96. p[(std::size_t) gm::voiceParam (1, gm::vOn)] = 0.0f;
  97. p[gm::lowOn] = p[gm::highOn] = 0.0f;
  98. const double sr = 48000.0, every = 0.25 * sr, delay = 0.094 * 100.0 * 0.001 * sr; // 451.2 samples
  99. const int pulses = 24;
  100. mod190do::Stereo in { std::vector<float> ((std::size_t) (every * pulses + 2000), 0.0f), {} };
  101. for (int k = 0; k < pulses; ++k)
  102. in.l[(std::size_t) (k * every)] = 0.5f;
  103. in.r = in.l;
  104. for (const float law : { 1.0f, 0.0f })
  105. {
  106. p[gm::headLaw] = law;
  107. const auto y = mod190do::render (EffectKind::geminiDoubler, p, in, sr);
  108. int single = 0;
  109. std::vector<double> at;
  110. for (int k = 0; k < pulses; ++k)
  111. {
  112. // the answer to pulse k: its strongest sample and the energy more than 4 samples away from it
  113. const auto a = (std::size_t) (k * every);
  114. std::size_t best = 0;
  115. double e = 0.0, peak = 0.0;
  116. for (std::size_t i = 1; i < 1500; ++i)
  117. {
  118. const auto v = (double) y.l[a + i];
  119. e += v * v;
  120. if (std::abs (v) > peak)
  121. peak = std::abs (v), best = i;
  122. }
  123. double near = 0.0;
  124. for (std::size_t i = best - 4; i <= best + 4; ++i)
  125. near += (double) y.l[a + i] * y.l[a + i];
  126. single += near > 0.97 * e ? 1 : 0;
  127. at.push_back ((double) best);
  128. }
  129. if (law >= 0.5f)
  130. {
  131. // one head: all its energy at one place (a few pulses land on a jump's short crossfade)
  132. CHECK (single >= pulses - 3);
  133. // it reads earlier than the delay (pitch up), never more than the range ahead of it, and drifts by
  134. // (1 − 2^(6/1200)) per sample between pulses except across a jump back
  135. int drifts = 0, jumps = 0;
  136. for (int k = 1; k < pulses; ++k)
  137. {
  138. const auto d = at[(std::size_t) k] - at[(std::size_t) k - 1];
  139. drifts += std::abs (d + (std::pow (2.0, 6.0 / 1200.0) - 1.0) * every) < 1.5 ? 1 : 0;
  140. jumps += d > 200.0 ? 1 : 0;
  141. }
  142. CHECK (drifts >= pulses - 4 && jumps >= 1);
  143. for (const auto v : at)
  144. CHECK (v <= delay + 2.0 && v >= delay - 260.0);
  145. }
  146. else
  147. CHECK (single < pulses / 2); // the classic law reads two taps half a 13.3 ms window apart
  148. }
  149. }
  150. namespace mod190do
  151. {
  152. // the delay (samples) of the strongest tap of an Orbit's answer to impulses every `every` samples, mix 100 %, per channel
  153. inline std::array<std::vector<double>, 2> orbitTaps (float mode, float law, int pulses, int every)
  154. {
  155. using namespace lynx;
  156. namespace ob = echosuite::orbit;
  157. auto p = makeEffectSlot (EffectKind::orbit).params;
  158. p[ob::mix] = 1.0f;
  159. p[ob::feedback] = 0.5f; // none
  160. p[ob::rate] = 0.75f; // about +1 Hz
  161. p[ob::depth] = 0.371f; // about 5 ms
  162. p[ob::mode] = mode;
  163. p[ob::law] = law;
  164. Stereo in { std::vector<float> ((std::size_t) (pulses * every), 0.0f), {} };
  165. for (int k = 0; k < pulses; ++k)
  166. in.l[(std::size_t) (k * every)] = 0.5f;
  167. in.r = in.l;
  168. const auto y = render (EffectKind::orbit, p, in);
  169. std::array<std::vector<double>, 2> at;
  170. for (int c = 0; c < 2; ++c)
  171. for (int k = 0; k + 1 < pulses; ++k)
  172. {
  173. const auto& v = c == 0 ? y.l : y.r;
  174. std::size_t best = 0;
  175. double peak = 0.0;
  176. for (int i = 1; i < every; ++i)
  177. if (std::abs ((double) v[(std::size_t) (k * every + i)]) > peak)
  178. peak = std::abs ((double) v[(std::size_t) (k * every + i)]), best = (std::size_t) i;
  179. at[(std::size_t) c].push_back ((double) best);
  180. }
  181. return at;
  182. }
  183. // the shortest and the longest delay (samples) at which any tap of the left channel reads with at least 10 % weight
  184. // (an answer above 0.05 to the 0.5 impulses), over the whole run
  185. inline std::pair<double, double> orbitReach (float law, int pulses, int every)
  186. {
  187. using namespace lynx;
  188. namespace ob = echosuite::orbit;
  189. auto p = makeEffectSlot (EffectKind::orbit).params;
  190. p[ob::mix] = 1.0f, p[ob::feedback] = 0.5f, p[ob::rate] = 0.75f, p[ob::depth] = 0.371f, p[ob::mode] = 1.0f / 11.0f;
  191. p[ob::law] = law;
  192. Stereo in { std::vector<float> ((std::size_t) (pulses * every), 0.0f), {} };
  193. for (int k = 0; k < pulses; ++k)
  194. in.l[(std::size_t) (k * every)] = 0.5f;
  195. in.r = in.l;
  196. const auto y = render (EffectKind::orbit, p, in);
  197. double lo = 1e9, hi = -1e9;
  198. for (int k = 0; k + 1 < pulses; ++k)
  199. for (int i = 1; i < every; ++i)
  200. if (std::abs (y.l[(std::size_t) (k * every + i)]) > 0.05f)
  201. lo = std::min (lo, (double) i), hi = std::max (hi, (double) i);
  202. return { lo, hi };
  203. }
  204. // the share of steps where the tap moved the given way (down = shorter delay = rising pitch), ignoring wraps
  205. inline double moving (const std::vector<double>& at, bool down)
  206. {
  207. int n = 0, ok = 0;
  208. for (std::size_t k = 1; k < at.size(); ++k)
  209. {
  210. const auto d = at[k] - at[k - 1];
  211. if (std::abs (d) > 60.0 || d == 0.0)
  212. continue;
  213. ++n;
  214. ok += (d < 0.0) == down ? 1 : 0;
  215. }
  216. return n > 0 ? (double) ok / n : 0.0;
  217. }
  218. } // namespace mod190do
  219. TEST (mod190do_orbit_measured_rise_sweeps_the_whole_depth_and_rise_fall_splits_the_channels)
  220. {
  221. using namespace mod190do;
  222. const auto depth = lynx::echosuite::orbit::depthMs (0.371f) * 48.0; // about 240 samples
  223. // Rise, measured: two taps sweep the whole depth (classic: half of it, above a 0.25 ms base); with sin² weights a
  224. // tap reads at 10 % or more over 0.1 … 0.9 of its sweep
  225. for (const float law : { 1.0f, 0.0f })
  226. {
  227. const auto [lo, hi] = orbitReach (law, 160, 300);
  228. if (law >= 0.5f)
  229. CHECK (hi - lo > 0.7 * depth && lo < 0.15 * depth);
  230. else
  231. CHECK (hi - lo < 0.5 * depth && lo > 0.2 * depth);
  232. const auto at = orbitTaps (1.0f / 11.0f, law, 160, 300);
  233. CHECK (moving (at[0], true) > 0.9 && moving (at[1], true) > 0.9); // both channels rise
  234. }
  235. // Rise / fall, measured: the left channel rises, the right one falls
  236. {
  237. const auto at = orbitTaps (2.0f / 11.0f, 1.0f, 160, 300);
  238. CHECK (moving (at[0], true) > 0.9 && moving (at[1], false) > 0.9);
  239. }
  240. }
  241. TEST (mod190do_spiral_measured_loop_limits_lower_and_glides_slower)
  242. {
  243. using namespace lynx;
  244. namespace sp = echosuite::spiral;
  245. const double sr = 48000.0;
  246. auto p = makeEffectSlot (EffectKind::spiralEcho).params;
  247. p[sp::mix] = 1.0f;
  248. p[sp::shift] = 0.5f;
  249. p[sp::stereo] = 0.0f;
  250. p[sp::delay] = sp::delayValue (20.0);
  251. // loud noise: the measured loop's limiter holds the repeats lower than the classic tanh (the reference is 3 … 4 dB
  252. // under the classic loop on the loudest 100 ms of programme, tp172d S3); quiet noise passes both alike
  253. const auto rmsDb = [] (const mod190do::Stereo& s, std::size_t from) {
  254. double e = 0.0;
  255. for (std::size_t i = from; i < s.l.size(); ++i)
  256. e += (double) s.l[i] * s.l[i];
  257. return 10.0 * std::log10 (e / (double) (s.l.size() - from) + 1e-30);
  258. };
  259. for (const double a : { 0.7, 0.003 })
  260. {
  261. mod190do::Stereo in { std::vector<float> ((std::size_t) sr, 0.0f), {} };
  262. std::uint32_t s = 5u;
  263. for (auto& v : in.l)
  264. {
  265. s = s * 1664525u + 1013904223u;
  266. v = (float) (a * ((double) (s >> 8) / 8388608.0 - 1.0));
  267. }
  268. in.r = in.l;
  269. p[sp::law] = 1.0f;
  270. const auto m = rmsDb (mod190do::render (EffectKind::spiralEcho, p, in, sr), 24000);
  271. p[sp::law] = 0.0f;
  272. const auto c = rmsDb (mod190do::render (EffectKind::spiralEcho, p, in, sr), 24000);
  273. if (a > 0.1)
  274. CHECK (c - m > 0.8); // 1.3 dB on this noise (the round's renders: 1.31)
  275. else
  276. CHECK (std::abs (c - m) < 0.5); // 0.25 dB: the cuts' bilinear shapes
  277. }
  278. // feedback at its top (loop gain 1.5): the measured loop runs away into its limiter and stays bounded
  279. {
  280. p[sp::law] = 1.0f;
  281. p[sp::feedback] = 1.0f;
  282. mod190do::Stereo in { std::vector<float> ((std::size_t) (3 * sr), 0.0f), {} };
  283. for (std::size_t i = 0; i < 4800; ++i)
  284. in.l[i] = (float) (0.9 * std::sin (0.07 * (double) i));
  285. in.r = in.l;
  286. const auto y = mod190do::render (EffectKind::spiralEcho, p, in, sr);
  287. CHECK (mod190do::finite (y));
  288. float peak = 0.0f;
  289. for (const float v : y.l)
  290. peak = std::max (peak, std::abs (v));
  291. CHECK (peak < 2.0f && peak > 0.05f);
  292. p[sp::feedback] = 0.5f;
  293. }
  294. // a change of delay glides with a time constant of about 0.175 s (the reference's, refcap0124 delay sweeps; the
  295. // classic loop: 0.05 s): 10 → 20 ms, the echo of an impulse 175 ms after the change is about 63 % of the way
  296. {
  297. p[sp::law] = 1.0f;
  298. p[sp::feedback] = 0.0f;
  299. auto fx = makeEffect (EffectKind::spiralEcho);
  300. fx->prepare (sr);
  301. const int block = 64, n = (int) (1.0 * sr);
  302. std::vector<float> l ((std::size_t) n, 0.0f), r ((std::size_t) n, 0.0f);
  303. const int change = 24000, probe = change + (int) (0.175 * sr);
  304. l[(std::size_t) probe] = r[(std::size_t) probe] = 0.5f;
  305. for (int off = 0; off < n; off += block)
  306. {
  307. p[sp::delay] = sp::delayValue (off < change ? 10.0 : 20.0);
  308. fx->process (l.data() + off, r.data() + off, block, p, 120.0);
  309. }
  310. std::size_t best = 0;
  311. for (std::size_t i = (std::size_t) probe; i < (std::size_t) probe + 1500; ++i)
  312. if (std::abs (l[i]) > std::abs (l[best]))
  313. best = i;
  314. const auto lag = (double) (best - (std::size_t) probe), way = (lag - 480.0) / 480.0;
  315. CHECK (way > 0.5 && way < 0.78);
  316. }
  317. }
  318. TEST (mod190do_lattice_measured_detune_reaches_the_reference_spread)
  319. {
  320. using namespace lynx;
  321. namespace lt = echosuite::lattice;
  322. // the default (one tap, 10 ms, detune 50 % of 50 % depth): impulses every 50 ms, the strongest tap of each answer.
  323. // The reference's taps spread over about ±200 samples there (refcap0124 one-parameter captures: 340 … 739 samples);
  324. // the classic law over ±56
  325. const double sr = 48000.0;
  326. const int pulses = 160, every = 2400;
  327. mod190do::Stereo in { std::vector<float> ((std::size_t) (pulses * every), 0.0f), {} };
  328. for (int k = 0; k < pulses; ++k)
  329. in.l[(std::size_t) (k * every)] = 0.5f;
  330. in.r = in.l;
  331. double spread[2] {};
  332. for (const int law : { 0, 1 })
  333. {
  334. auto p = makeEffectSlot (EffectKind::latticeMod).params;
  335. p[lt::mix] = 1.0f;
  336. p[lt::law] = (float) law;
  337. const auto y = mod190do::render (EffectKind::latticeMod, p, in, sr);
  338. CHECK (mod190do::finite (y));
  339. double lo = 1e9, hi = -1e9;
  340. for (int k = 0; k + 1 < pulses; ++k)
  341. {
  342. std::size_t best = 0;
  343. for (int i = 2; i < every; ++i)
  344. if (std::abs (y.l[(std::size_t) (k * every + i)]) > std::abs (y.l[(std::size_t) (k * every) + best]))
  345. best = (std::size_t) i;
  346. lo = std::min (lo, (double) best), hi = std::max (hi, (double) best);
  347. }
  348. spread[law] = hi - lo;
  349. }
  350. CHECK (spread[1] > 2.0 * spread[0] && spread[1] < 600.0);
  351. }
  352. TEST (mod190do_measured_laws_stay_finite_at_every_extreme)
  353. {
  354. using namespace lynx;
  355. for (const auto& law : mod190do::laws)
  356. for (const double sr : { 44100.0, 48000.0, 96000.0 })
  357. for (int variant = 0; variant < 6; ++variant)
  358. {
  359. auto p = makeEffectSlot (law.kind).params;
  360. std::uint32_t s = 1234u + (std::uint32_t) variant * 77u;
  361. for (std::size_t i = 0; i < p.size(); ++i)
  362. {
  363. s = s * 1664525u + 1013904223u;
  364. p[i] = variant == 0 ? 0.0f : variant == 1 ? 1.0f : (float) (s >> 8) / 16777216.0f;
  365. }
  366. p[law.index] = 1.0f;
  367. mod190do::Stereo in { std::vector<float> ((std::size_t) (0.6 * sr), 0.0f), {} };
  368. for (std::size_t i = 0; i < in.l.size() / 3; ++i)
  369. in.l[i] = (float) (0.9 * std::sin (0.03 * (double) i + 0.001 * (double) (i * i % 977)));
  370. in.r = in.l;
  371. CHECK (mod190do::finite (mod190do::render (law.kind, p, in, sr, 333)));
  372. }
  373. }
  374. TEST (mod190do_classic_laws_render_as_the_base_commit_did)
  375. {
  376. using namespace lynx;
  377. struct Case
  378. {
  379. EffectKind kind;
  380. std::size_t law;
  381. int idx;
  382. float val;
  383. int idx2;
  384. float val2;
  385. double sr;
  386. std::uint64_t hash;
  387. double energy, a, b;
  388. };
  389. // an impulse (0.5 / −0.25) and a noise burst, 1.5 s in blocks of 256: FNV-1a of the float bits, the energy, two
  390. // samples. The goldens come from the base commit's objects (/mnt/e/lynx-work/mod190do/gold: gold_base, without the
  391. // law parameters, prints the same lines as gold_new, the round's objects at law = Classic)
  392. namespace es = echosuite;
  393. namespace gm = voicetools::gemini;
  394. const Case cases[] = {
  395. { EffectKind::spiralEcho, es::spiral::law, -1, 0.0f, -1, 0.0f, 48000.0, 0xd96e67995d390584ull, 41.7843959, 3.93556002e-05, -0.000187082827 },
  396. { EffectKind::spiralEcho, es::spiral::law, 1, 0.62f, 4, 0.7f, 44100.0, 0x8d2c7b9cde6b4a98ull, 61.6467675, 2.87903231e-06, -0.000423245307 },
  397. { EffectKind::orbit, es::orbit::law, -1, 0.0f, -1, 0.0f, 48000.0, 0x1bce9ed14615a104ull, 92.4305534, 0.0, 0.0 },
  398. { EffectKind::orbit, es::orbit::law, 5, 0.27f, 3, 0.8f, 48000.0, 0x69cd4799f484b016ull, 113.427882, 0.0, -2.43045232e-19 },
  399. { EffectKind::orbit, es::orbit::law, 5, 0.5f, 1, 0.8f, 96000.0, 0x9b656c1afdc07cf7ull, 105.793828, 0.0, 0.0 },
  400. { EffectKind::geminiDoubler, gm::headLaw, -1, 0.0f, -1, 0.0f, 48000.0, 0xbc5577e0eef5af08ull, 173.873472, 0.0, -5.03066149e-43 },
  401. { EffectKind::geminiDoubler, gm::headLaw, gm::align, 1.0f, gm::voiceParam (0, gm::vDepth), 0.3f, 48000.0, 0x78231649d3b1c374ull, 164.168348, -2.80259693e-45, -5.36697312e-43 },
  402. { EffectKind::geminiDoubler, gm::headLaw, gm::voiceParam (0, gm::vOctave), 1.0f, gm::directGain, 0.5f, 44100.0, 0xb514966ee0730f90ull, 53.1590003, 0.0, -2.31074117e-42 },
  403. };
  404. int sameBits = 0;
  405. for (const auto& c : cases)
  406. {
  407. auto slot = makeEffectSlot (c.kind);
  408. slot.params[c.law] = 0.0f;
  409. if (c.idx >= 0)
  410. slot.params[(std::size_t) c.idx] = c.val;
  411. if (c.idx2 >= 0)
  412. slot.params[(std::size_t) c.idx2] = c.val2;
  413. auto fx = makeEffect (slot);
  414. fx->prepare (c.sr);
  415. const int n = (int) (1.5 * c.sr), block = 256;
  416. std::vector<float> l ((std::size_t) n, 0.0f), r ((std::size_t) n, 0.0f);
  417. unsigned s = 777u;
  418. l[0] = 0.5f;
  419. r[0] = -0.25f;
  420. for (int i = n / 3; i < n / 3 + 2000; ++i)
  421. {
  422. s = s * 1664525u + 1013904223u;
  423. l[(std::size_t) i] = ((float) (s >> 8) / 8388608.0f - 1.0f) * 0.3f;
  424. s = s * 1664525u + 1013904223u;
  425. r[(std::size_t) i] = ((float) (s >> 8) / 8388608.0f - 1.0f) * 0.3f;
  426. }
  427. for (int off = 0; off < n; off += block)
  428. fx->process (l.data() + off, r.data() + off, std::min (block, n - off), slot.params, 120.0);
  429. std::uint64_t h = 1469598103934665603ull;
  430. double e = 0.0;
  431. for (int i = 0; i < n; ++i)
  432. for (const float v : { l[(std::size_t) i], r[(std::size_t) i] })
  433. {
  434. std::uint32_t u;
  435. std::memcpy (&u, &v, 4);
  436. for (int b = 0; b < 4; ++b)
  437. {
  438. h ^= (u >> (8 * b)) & 0xffu;
  439. h *= 1099511628211ull;
  440. }
  441. e += (double) v * v;
  442. }
  443. // the same sound with any maths library: energy within 0.01 %, two samples within 2e-6
  444. CHECK (std::abs (e - c.energy) <= 1e-4 * c.energy);
  445. CHECK (std::abs ((double) l[(std::size_t) (n / 7)] - c.a) < 2e-6);
  446. CHECK (std::abs ((double) r[(std::size_t) (n / 2)] - c.b) < 2e-6);
  447. sameBits += h == c.hash ? 1 : 0;
  448. }
  449. // bit for bit where the maths library is the one the goldens were made with (glibc 2.43 on x86-64: the local
  450. // builds); other libm versions round some transcendental calls differently (round 190bz's note)
  451. std::printf (" classic laws: %d of 8 renders hash-identical to the base commit\n", sameBits);
  452. #if defined (__linux__) && defined (__x86_64__) && defined (__GLIBC__) && (__GLIBC__ > 2 || __GLIBC_MINOR__ >= 43) \
  453. && ! defined (LYNX_VERB_HASH_OFF)
  454. CHECK (sameBits == 8);
  455. #endif
  456. }
  457. TEST (mod190do_measured_spiral_loop_gain_and_orbit_manual_delay)
  458. {
  459. using namespace lynx;
  460. namespace sp = echosuite::spiral;
  461. namespace ob = echosuite::orbit;
  462. const double sr = 48000.0;
  463. // Spiral, measured loop: 1.5 × feedback a pass below the limiter (a 1 kHz tone at 0.02, cuts open, 100 ms)
  464. for (const float fb : { 0.25f, 0.5f })
  465. {
  466. auto p = makeEffectSlot (EffectKind::spiralEcho).params;
  467. p[sp::mix] = 1.0f, p[sp::lowCut] = 0.0f, p[sp::highCut] = 1.0f, p[sp::feedback] = fb, p[sp::stereo] = 0.0f;
  468. p[sp::delay] = sp::delayValue (100.0);
  469. mod190do::Stereo in { std::vector<float> (24000, 0.0f), {} };
  470. for (std::size_t i = 0; i < 1920; ++i)
  471. in.l[i] = (float) (0.02 * std::sin (2.0 * 3.14159265358979323846 * 1000.0 * (double) i / sr));
  472. in.r = in.l;
  473. const auto y = mod190do::render (EffectKind::spiralEcho, p, in, sr);
  474. const auto amp = [&y, sr] (std::size_t from, std::size_t len) {
  475. double c = 0.0, s = 0.0;
  476. for (std::size_t i = from; i < from + len; ++i)
  477. {
  478. const auto ph = 2.0 * 3.14159265358979323846 * 1000.0 * (double) i / sr;
  479. c += y.l[i] * std::cos (ph), s += y.l[i] * std::sin (ph);
  480. }
  481. return 2.0 * std::sqrt (c * c + s * s) / (double) len;
  482. };
  483. const auto a1 = amp (4800 + 300, 1440), a2 = amp (9600 + 300, 1440);
  484. CHECK (std::abs (20.0 * std::log10 (a1 / 0.02)) < 0.3);
  485. CHECK (std::abs (20.0 * std::log10 (a2 / a1) - 20.0 * std::log10 (1.5 * fb)) < 0.3);
  486. }
  487. // Orbit, measured sweep, depth 0: the echo sits at the manual delay itself (the classic voices added 0.25 ms)
  488. {
  489. auto p = makeEffectSlot (EffectKind::orbit).params;
  490. p[ob::mix] = 1.0f, p[ob::feedback] = 0.5f, p[ob::depth] = 0.0f;
  491. p[ob::manual] = 0.5f + 0.5f * (float) std::pow (20.0 / 50.0, 0.25);
  492. for (const auto mode : { ob::Mode::rise, ob::Mode::riseFall })
  493. {
  494. p[ob::mode] = ob::modeValue (mode);
  495. const auto y = mod190do::render (EffectKind::orbit, p, mod190do::impulse (0.1, sr));
  496. std::size_t at = 100;
  497. for (std::size_t i = 100; i < y.l.size(); ++i)
  498. if (std::abs (y.l[i]) > std::abs (y.l[at]))
  499. at = i;
  500. CHECK ((double) at > 19.8 * 48.0 && (double) at < 20.3 * 48.0);
  501. }
  502. }
  503. }