Pdc188eTests.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // --- Round 188e: issue #113, a Patcher holding a Fruity Limiter left its track late --------------------------------
  2. // The reporter's finding (verified): the Fruity Limiter learned its look-ahead (its latency) only in process(), and the
  3. // Patcher plans its delay compensation right after building its nodes, before any block: it planned 0, so the track
  4. // played the look-ahead late. Fixed at the root (the latency is known after prepare()) and in the graph (a node whose
  5. // latency moves later makes the rack build the graph again, as the mixer does for its slots).
  6. #include "core/DynamicsSuite.h"
  7. namespace pdc188e
  8. {
  9. // where an impulse put in at `at` comes out (the largest |sample|), through `fx` in blocks of 512
  10. int delayOf (lynx::Effect& fx, const lynx::EffectParams& params, int at = 64, int frames = 8192)
  11. {
  12. std::vector<float> l ((std::size_t) frames, 0.0f), r ((std::size_t) frames, 0.0f);
  13. l[(std::size_t) at] = r[(std::size_t) at] = 0.25f; // (under the limiter's threshold)
  14. for (int done = 0; done < frames; done += 512)
  15. fx.process (l.data() + done, r.data() + done, std::min (512, frames - done), params, 120.0);
  16. int best = 0;
  17. for (int i = 1; i < frames; ++i)
  18. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  19. best = i;
  20. return best - at;
  21. }
  22. // FL's Fruity Limiter with an ~11 ms look-ahead (979 samples at 44.1 kHz: the delay the reporter measured)
  23. lynx::EffectSlot limiter()
  24. {
  25. auto slot = lynx::makeEffectSlot (lynx::EffectKind::fruityLimiter);
  26. slot.params[3] = 0.401f; // the look-ahead control (raw 4010 of 0 … 10000)
  27. return slot;
  28. }
  29. lynx::PatchWire wire (int from, int to, int fromPort = 0, int toPort = 0)
  30. {
  31. lynx::PatchWire w;
  32. w.from = from;
  33. w.to = to;
  34. w.fromPort = fromPort;
  35. w.toPort = toPort;
  36. return w;
  37. }
  38. // The audio path of FL's "Multiband Sidechain" Patcher template, as the reporter describes it: a wet path through
  39. // Parametric EQ 2 → Fruity Limiter → a "Wet" level node, a dry path through a "Dry" level node, both to the output,
  40. // and a Peak Controller on the side-chain input (it only measures)
  41. lynx::EffectSlot multibandSidechain (const lynx::EffectSlot& limiterNode)
  42. {
  43. using namespace lynx;
  44. auto slot = makeEffectSlot (EffectKind::patcher);
  45. slot.patcher.nodes.clear();
  46. slot.patcher.wires.clear();
  47. auto add = [&slot] (EffectSlot fx) {
  48. PatchNode node;
  49. node.effect = std::move (fx);
  50. slot.patcher.nodes.push_back (node);
  51. return (int) slot.patcher.nodes.size() - 1;
  52. };
  53. const auto dry = add (makeEffectSlot (EffectKind::fruityBalance));
  54. const auto eq = add (makeEffectSlot (EffectKind::parametricEq));
  55. const auto lim = add (limiterNode);
  56. const auto wet = add (makeEffectSlot (EffectKind::fruityBalance));
  57. const auto peak = add (makeEffectSlot (EffectKind::peakController));
  58. for (const auto& w : { wire (patchFromHost, dry), wire (patchFromHost, eq), wire (eq, lim), wire (lim, wet), wire (dry, patchToHost),
  59. wire (wet, patchToHost), wire (patchFromHost, peak, 1, 0) })
  60. slot.patcher.wires.push_back (w);
  61. return slot;
  62. }
  63. // An effect that delays by `samples` but reports it only once it has run (a plugin that reports late)
  64. class LateReporting final : public lynx::Effect
  65. {
  66. public:
  67. explicit LateReporting (int samples) : latency (samples), line (samples) {}
  68. void prepare (double) noexcept override {}
  69. void reset() noexcept override {}
  70. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  71. {
  72. line.process (l, r, n);
  73. ran.store (true);
  74. }
  75. int latencySamples() const noexcept override { return ran.load() ? latency : 0; }
  76. private:
  77. int latency;
  78. lynx::DelayLine line;
  79. std::atomic<bool> ran { false };
  80. };
  81. // Two channels playing an impulse, channel k on insert k + 1; renders channel `which` alone through `state` and
  82. // returns where its impulse lands on the master
  83. int masterPeak (const lynx::RackState& state, int which, int frames = 4096)
  84. {
  85. lynx::RackRenderer renderer;
  86. renderer.prepare (state.sampleRate, 256);
  87. lynx::LiveEventQueue live;
  88. lynx::NoteEvent on;
  89. on.channel = which;
  90. on.key = 60;
  91. on.velocity = 1.0f;
  92. live.push (on);
  93. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  94. for (int done = 0; done < frames; done += 256)
  95. renderer.process (&state, false, 0.0, 256, l.data() + done, r.data() + done, done == 0 ? &live : nullptr);
  96. int best = 0;
  97. for (int i = 1; i < frames; ++i)
  98. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  99. best = i;
  100. return best;
  101. }
  102. lynx::Song twoTracks (const lynx::EffectSlot& onInsert1, const lynx::EffectSlot& onInsert2)
  103. {
  104. lynx::Song s;
  105. s.mixer = lynx::Song::defaultMixer (3);
  106. for (int i = 0; i < 2; ++i)
  107. {
  108. lynx::ChannelDef c;
  109. c.instrument = lynx::InstrumentKind::sampler;
  110. c.samplePath = "impulse";
  111. c.volume = 1.0f;
  112. c.mixerTrack = i + 1;
  113. s.channels.push_back (c);
  114. }
  115. s.patterns.emplace_back();
  116. s.mixer[1].effects.push_back (onInsert1);
  117. s.mixer[2].effects.push_back (onInsert2);
  118. return s;
  119. }
  120. lynx::RackState::Factories impulseFactories()
  121. {
  122. lynx::RackState::Factories f;
  123. f.samples = [] (const std::string&) {
  124. auto d = std::make_shared<lynx::SampleData>();
  125. d->left.assign (64, 0.0f);
  126. d->left[0] = 0.25f;
  127. return std::shared_ptr<const lynx::SampleData> (d);
  128. };
  129. return f;
  130. }
  131. } // namespace pdc188e
  132. TEST (pdc188e_fruity_limiter_reports_its_lookahead_before_the_first_block)
  133. {
  134. using namespace lynx;
  135. for (const double rate : { 44100.0, 48000.0 })
  136. {
  137. const auto slot = pdc188e::limiter();
  138. auto fx = makeEffect (slot);
  139. fx->prepare (rate);
  140. const auto before = fx->latencySamples();
  141. const auto measured = pdc188e::delayOf (*fx, slot.params);
  142. std::printf (" Fruity Limiter at %.0f Hz: reports %d before the first block, then %d; delays %d\n", rate, before,
  143. fx->latencySamples(), measured);
  144. CHECK (before > 900);
  145. CHECK (before == measured && fx->latencySamples() == before);
  146. }
  147. // Fruity Compressor: its reported look-ahead follows the slot from the start (64 with it on, 0 with it off)
  148. auto comp = makeEffectSlot (EffectKind::fruityCompressor);
  149. for (const bool on : { true, false })
  150. {
  151. comp.params[fldyn::compressorControls] = on ? 1.0f : 0.0f;
  152. auto fx = makeEffect (comp);
  153. fx->prepare (44100.0);
  154. CHECK (fx->latencySamples() == (on ? 64 : 0));
  155. }
  156. }
  157. TEST (pdc188e_effects_report_their_latency_before_the_first_block)
  158. {
  159. // Every built-in effect, at its defaults and at random settings (look-ahead / mode / oversampling switches among
  160. // them): what it reports after prepare(), before any block, is what it reports once it has run with those settings.
  161. // A Patcher plans its compensation from the first value (issue #113 found the Fruity Limiter reporting 0 there).
  162. using namespace lynx;
  163. std::uint32_t seed = 0x188e113u;
  164. auto next = [&seed] {
  165. seed ^= seed << 13;
  166. seed ^= seed >> 17;
  167. seed ^= seed << 5;
  168. return (float) (seed >> 8) * (1.0f / 16777216.0f);
  169. };
  170. int checked = 0;
  171. for (int k = 0; k <= (int) lastEffectKind; ++k)
  172. {
  173. const auto kind = (EffectKind) k;
  174. if (kind == EffectKind::plugin || kind == EffectKind::patcher || kind == EffectKind::hardwareInsert)
  175. continue;
  176. for (int variant = 0; variant < 5; ++variant)
  177. {
  178. auto slot = makeEffectSlot (kind);
  179. if (variant > 0)
  180. for (auto& v : slot.params)
  181. v = next();
  182. auto fx = makeEffect (slot);
  183. if (fx == nullptr)
  184. continue;
  185. fx->prepare (44100.0);
  186. const auto before = fx->latencySamples();
  187. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  188. l[10] = r[10] = 0.25f;
  189. for (int done = 0; done < 1024; done += 512)
  190. fx->process (l.data() + done, r.data() + done, 512, slot.params, 120.0);
  191. const auto after = fx->latencySamples();
  192. if (before != after)
  193. std::printf (" %s (settings %d): %d before the first block, %d after\n", effectInfo (kind).name, variant, before, after);
  194. CHECK (before == after);
  195. ++checked;
  196. }
  197. }
  198. CHECK (checked > 500);
  199. }
  200. TEST (pdc188e_patcher_with_a_limiter_aligns_to_the_sample)
  201. {
  202. using namespace lynx;
  203. const auto slot = pdc188e::multibandSidechain (pdc188e::limiter());
  204. auto graph = patcher::makeEffect (slot, 44100.0, patcher::builtInFactories (44100.0));
  205. graph->prepare (44100.0);
  206. auto alone = makeEffect (pdc188e::limiter());
  207. alone->prepare (44100.0);
  208. const auto lookahead = alone->latencySamples();
  209. const auto reported = graph->latencySamples();
  210. // the impulse leaves the Patcher once (the dry path waits for the wet one), as late as the Patcher reports
  211. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  212. l[64] = r[64] = 0.25f;
  213. for (int done = 0; done < 8192; done += 512)
  214. graph->process (l.data() + done, r.data() + done, 512, slot.params, 120.0);
  215. int best = 0;
  216. for (int i = 1; i < 8192; ++i)
  217. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  218. best = i;
  219. std::printf (" Multiband Sidechain Patcher: limiter %d, Patcher reports %d, delays %d, dry at the input %.4f\n", lookahead, reported,
  220. best - 64, (double) l[64]);
  221. CHECK (lookahead > 900 && reported == lookahead && best - 64 == reported);
  222. CHECK (std::abs (l[64]) < 1e-6f); // (no undelayed dry copy)
  223. CHECK (graph->latencySamples() == reported && ! patcher::graphOf (graph.get())->latencyStale (&slot.patcher));
  224. // the limiter node switched off in the document: the plan no longer holds, the rack builds the graph again
  225. auto off = slot;
  226. off.patcher.nodes[2].effect.enabled = false;
  227. CHECK (patcher::graphOf (graph.get())->latencyStale (&off.patcher));
  228. CHECK (! patcher::acceptValues (graph.get(), off));
  229. CHECK (patcher::acceptValues (graph.get(), slot));
  230. auto rebuilt = patcher::makeEffect (off, 44100.0, patcher::builtInFactories (44100.0), graph.get());
  231. CHECK (rebuilt->latencySamples() == 0);
  232. }
  233. TEST (pdc188e_track_with_the_patcher_matches_the_bypassed_track)
  234. {
  235. // The reporter's measurement: the track through the Patcher against the same track with the Patcher bypassed,
  236. // offset 0 samples (it was 979)
  237. using namespace lynx;
  238. for (const double rate : { 44100.0, 48000.0 })
  239. {
  240. const auto patch = pdc188e::multibandSidechain (pdc188e::limiter());
  241. auto bypassed = patch;
  242. bypassed.enabled = false;
  243. const auto song = pdc188e::twoTracks (patch, bypassed);
  244. const auto state = RackState::build (song, nullptr, rate, pdc188e::impulseFactories());
  245. const auto through = pdc188e::masterPeak (*state, 0);
  246. const auto plain = pdc188e::masterPeak (*state, 1);
  247. std::printf (" at %.0f Hz: PDC total %d, the Patcher's track peaks at %d, the bypassed one at %d\n", rate, state->pdc.total, through,
  248. plain);
  249. CHECK (state->pdc.total > 900);
  250. CHECK (through == plain);
  251. CHECK (state->currentLatencies() == state->latencies); // (nothing for the mixer to re-plan)
  252. // the same limiter straight on the insert, for reference: compensated too
  253. const auto direct = RackState::build (pdc188e::twoTracks (pdc188e::limiter(), bypassed), nullptr, rate, pdc188e::impulseFactories());
  254. CHECK (direct->pdc.total == state->pdc.total);
  255. CHECK (pdc188e::masterPeak (*direct, 0) == pdc188e::masterPeak (*direct, 1));
  256. }
  257. }
  258. TEST (pdc188e_patcher_replans_when_a_node_reports_its_latency_late)
  259. {
  260. // A node that only reports its latency once it runs (a hosted plugin can): the Patcher's reported latency follows
  261. // it, the mixer sees the change, and the rack's next build plans the graph again with the running node kept
  262. using namespace lynx;
  263. auto patch = makeEffectSlot (EffectKind::patcher);
  264. patch.patcher.nodes.clear();
  265. patch.patcher.wires.clear();
  266. PatchNode late;
  267. late.effect.kind = EffectKind::plugin;
  268. late.effect.plugin.id = "late";
  269. PatchNode dry;
  270. dry.effect = makeEffectSlot (EffectKind::fruityBalance);
  271. patch.patcher.nodes = { late, dry };
  272. patch.patcher.wires = { pdc188e::wire (patchFromHost, 0), pdc188e::wire (patchFromHost, 1), pdc188e::wire (0, patchToHost),
  273. pdc188e::wire (1, patchToHost) };
  274. auto bypassed = patch;
  275. bypassed.enabled = false;
  276. const auto song = pdc188e::twoTracks (patch, bypassed);
  277. auto f = pdc188e::impulseFactories();
  278. f.pluginEffects = [] (const PluginRef&, double) -> std::shared_ptr<Effect> { return std::make_shared<pdc188e::LateReporting> (300); };
  279. const auto first = RackState::build (song, nullptr, 48000.0, f);
  280. CHECK (first->pdc.total == 0); // (the node hasn't reported yet)
  281. pdc188e::masterPeak (*first, 0); // it runs
  282. const auto* graph = patcher::graphOf (first->effects[1][0].get());
  283. CHECK (graph != nullptr && graph->latencyStale() && first->effects[1][0]->latencySamples() == 300);
  284. CHECK (first->currentLatencies() != first->latencies); // the mixer's poll re-plans
  285. const auto next = RackState::build (song, first.get(), 48000.0, f);
  286. CHECK (next->effects[1][0] != first->effects[1][0]); // the graph was built again…
  287. const auto* again = patcher::graphOf (next->effects[1][0].get());
  288. CHECK (again != nullptr && again->effectAt (0) == graph->effectAt (0)); // …keeping the running node
  289. CHECK (next->pdc.total == 300 && next->effects[1][0]->latencySamples() == 300 && ! again->latencyStale());
  290. const auto through = pdc188e::masterPeak (*next, 0);
  291. const auto plain = pdc188e::masterPeak (*next, 1);
  292. std::printf (" late-reporting node: the Patcher's track peaks at %d, the bypassed one at %d\n", through, plain);
  293. CHECK (through == plain);
  294. // nothing moved since: the next build keeps the running graph
  295. const auto third = RackState::build (song, next.get(), 48000.0, f);
  296. CHECK (third->effects[1][0] == next->effects[1][0]);
  297. }
  298. TEST (pdc188e_dynamics_suite_sound_unchanged_by_the_early_latency)
  299. {
  300. // Crest / Prism / Sentry now keep the slot's settings through prepare() (their latency is known before the first
  301. // block). The sound must not move: an instance built with the settings renders bit-identical to one built without
  302. // them that gets them on its first block (what every slot did before)
  303. using namespace lynx;
  304. std::uint32_t seed = 0x188e0d1u;
  305. auto next = [&seed] {
  306. seed ^= seed << 13;
  307. seed ^= seed >> 17;
  308. seed ^= seed << 5;
  309. return (float) (seed >> 8) * (1.0f / 16777216.0f);
  310. };
  311. for (const auto kind : { EffectKind::crestCompressor, EffectKind::prismMultiband, EffectKind::sentryGate })
  312. for (int variant = 0; variant < 6; ++variant)
  313. {
  314. auto slot = makeEffectSlot (kind);
  315. if (variant > 0)
  316. for (auto& v : slot.params)
  317. v = next();
  318. auto early = dyn::make (kind, &slot.params);
  319. auto late = dyn::make (kind, nullptr);
  320. early->prepare (44100.0);
  321. late->prepare (44100.0);
  322. std::vector<float> a (4096), b (4096), c (4096), d (4096);
  323. for (int i = 0; i < 4096; ++i)
  324. {
  325. a[(std::size_t) i] = c[(std::size_t) i] = 0.6f * std::sin ((float) i * 0.05f) * (i % 900 < 450 ? 1.0f : 0.1f);
  326. b[(std::size_t) i] = d[(std::size_t) i] = 0.5f * std::sin ((float) i * 0.031f);
  327. }
  328. for (int done = 0; done < 4096; done += 512)
  329. {
  330. early->process (a.data() + done, b.data() + done, 512, slot.params, 120.0);
  331. late->process (c.data() + done, d.data() + done, 512, slot.params, 120.0);
  332. }
  333. CHECK (early->latencySamples() == late->latencySamples());
  334. CHECK (std::memcmp (a.data(), c.data(), a.size() * sizeof (float)) == 0 && std::memcmp (b.data(), d.data(), b.size() * sizeof (float)) == 0);
  335. }
  336. }