Mod190oxTests.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Round 190ox: modulators stage 2 (research 190oe P9) — the new sources against reference curves, renders where they
  2. // drive a target (an envelope follower on an insert's level, an ADSR on a channel's notes, keytrack, a macro and its
  3. // automation), hosted plugin parameters as targets (clamp (base + depth × LFO), the base from an automation), the .flp
  4. // export's automation clips, and stage 1's modulators unchanged.
  5. namespace
  6. {
  7. // the song's master volume as the output level of a constant input, block by block from tick 0 (`blocks` of 256)
  8. std::vector<float> mod190oxLevels (const lynx::Song& song, int blocks, const std::vector<float>* input = nullptr,
  9. const lynx::RackState::Factories* factories = nullptr)
  10. {
  11. auto state = factories != nullptr ? lynx::RackState::build (song, nullptr, 48000.0, *factories) : lynx::RackState::build (song, nullptr, 48000.0);
  12. lynx::RackRenderer renderer;
  13. renderer.prepare (48000.0, 512);
  14. lynx::LiveInput live;
  15. renderer.setLiveInput (&live);
  16. std::vector<float> a (256, 0.5f), l (256), r (256);
  17. live.channels[0] = a.data();
  18. live.channels[1] = a.data();
  19. live.numChannels = 2;
  20. const auto tps = song.bpm * lynx::ppq / 60.0 / 48000.0;
  21. std::vector<float> out;
  22. for (int b = 0; b < blocks; ++b)
  23. {
  24. if (input != nullptr)
  25. for (int i = 0; i < 256; ++i)
  26. a[(std::size_t) i] = (std::size_t) (b * 256 + i) < input->size() ? (*input)[(std::size_t) (b * 256 + i)] : 0.0f;
  27. renderer.process (state.get(), true, b * 256 * tps, 256, l.data(), r.data());
  28. out.push_back (std::abs (l[255]));
  29. }
  30. return out;
  31. }
  32. lynx::Song mod190oxSong()
  33. {
  34. auto song = lynx::Song::makeDefault();
  35. song.channels.clear();
  36. song.playlist.clear();
  37. song.mode = lynx::PlayMode::song;
  38. song.bpm = 120.0;
  39. song.mixer = lynx::Song::defaultMixer (2);
  40. song.mixer[0].inputChannel = 0;
  41. song.mixer[0].monitor = lynx::InputMonitor::automatic;
  42. song.mixer[0].volume = 0.3f;
  43. return song;
  44. }
  45. // a stand-in for a hosted plugin effect: it records the parameter values the host sends
  46. struct Mod190oxProbe final : lynx::Effect
  47. {
  48. void prepare (double) noexcept override {}
  49. void reset() noexcept override {}
  50. void process (float*, float*, int, const lynx::EffectParams&, double) noexcept override {}
  51. void setParameter (int index, float value) noexcept override
  52. {
  53. if (index == 0)
  54. last = value, ++sent;
  55. }
  56. float last = -1.0f;
  57. int sent = 0;
  58. };
  59. } // namespace
  60. TEST (mod190ox_steps_random_reference)
  61. {
  62. auto steps = lynx::Modulator::make (lynx::ModKind::steps);
  63. CHECK (steps.steps.size() == 8 && steps.beats == 0.25 && steps.deterministic());
  64. steps.steps = { 1.0f, -1.0f, 0.5f };
  65. CHECK (steps.stepOutput (0.2) == 1.0f && steps.stepOutput (1.5) == -1.0f && steps.stepOutput (2.9) == 0.5f);
  66. CHECK (steps.stepOutput (3.1) == 1.0f); // (it loops)
  67. steps.glide = 0.5f; // the second half of a step slides to the next value: half way at 3/4, S-shaped
  68. CHECK (steps.stepOutput (0.4) == 1.0f && std::abs (steps.stepOutput (0.75) - 0.0f) < 1.0e-5f && std::abs (steps.stepOutput (0.999) + 1.0f) < 1.0e-3f);
  69. steps.unipolar = true;
  70. CHECK (steps.stepOutput (0.2) == 1.0f && std::abs (steps.stepOutput (1.2) - 0.0f) < 1.0e-6f);
  71. auto random = lynx::Modulator::make (lynx::ModKind::random);
  72. const auto a = random.stepOutput (5.1), b = random.stepOutput (5.9), c = random.stepOutput (6.1);
  73. CHECK (a == b && a != c && a >= -1.0f && a <= 1.0f); // sample and hold: one value a step, the same for the same step
  74. lynx::Modulator again = random;
  75. CHECK (again.stepOutput (5.5) == a); // (by song position, whatever played before)
  76. random.smooth = 1.0f; // smooth: continuous over the step's edge
  77. CHECK (std::abs (random.stepOutput (6.0 - 1.0e-6) - random.stepOutput (6.0)) < 1.0e-3f);
  78. random.sync = false;
  79. CHECK (! random.deterministic() && lynx::Modulator::make (lynx::ModKind::macro).deterministic());
  80. CHECK (! lynx::Modulator::make (lynx::ModKind::follower).deterministic());
  81. CHECK (std::string (lynx::Modulator::kindName (lynx::ModKind::envelope)) == "ADSR envelope");
  82. }
  83. TEST (mod190ox_envelope_and_follower_reference)
  84. {
  85. auto env = lynx::Modulator::make (lynx::ModKind::envelope);
  86. env.attack = 10.0f;
  87. env.decay = 100.0f;
  88. env.sustain = 0.5f;
  89. env.release = 50.0f;
  90. lynx::modenv::State s;
  91. // attack: linear, 1 in 10 ms (here 1 ms steps)
  92. lynx::modenv::envelope (env, s, true, true, 0.001);
  93. CHECK (std::abs (s.level - 0.1f) < 1.0e-5f && s.stage == 1);
  94. for (int k = 0; k < 4; ++k)
  95. lynx::modenv::envelope (env, s, true, false, 0.001);
  96. CHECK (std::abs (s.level - 0.5f) < 1.0e-4f);
  97. for (int k = 0; k < 5; ++k)
  98. lynx::modenv::envelope (env, s, true, false, 0.001);
  99. CHECK (s.level == 1.0f && s.stage == 2);
  100. // decay: exponential towards the sustain, time constant 100 ms
  101. lynx::modenv::envelope (env, s, true, false, 0.1);
  102. CHECK (std::abs (s.level - (0.5f + 0.5f * std::exp (-1.0f))) < 1.0e-4f);
  103. for (int k = 0; k < 100; ++k)
  104. lynx::modenv::envelope (env, s, true, false, 0.05);
  105. CHECK (std::abs (s.level - 0.5f) < 1.0e-3f);
  106. // release: towards 0 with 50 ms; then idle
  107. lynx::modenv::envelope (env, s, false, false, 0.05);
  108. CHECK (s.stage == 4 && std::abs (s.level - 0.5f * std::exp (-1.0f)) < 2.0e-3f);
  109. for (int k = 0; k < 40; ++k)
  110. lynx::modenv::envelope (env, s, false, false, 0.05);
  111. CHECK (s.level == 0.0f && s.stage == 0);
  112. // a new note retriggers from where it is
  113. lynx::modenv::envelope (env, s, true, true, 0.005);
  114. CHECK (s.stage == 1 && std::abs (s.level - 0.5f) < 1.0e-4f);
  115. auto f = lynx::Modulator::make (lynx::ModKind::follower);
  116. f.attack = 10.0f;
  117. f.release = 100.0f;
  118. f.gain = 6.0f; // (+6 dB: 0.25 reads ~0.5)
  119. lynx::modenv::State fs;
  120. lynx::modenv::follow (f, fs, 0.25f, 0.01);
  121. const auto in = 0.25f * std::pow (10.0f, 6.0f / 20.0f);
  122. CHECK (std::abs (fs.level - in * (1.0f - std::exp (-1.0f))) < 1.0e-4f); // one attack time constant
  123. for (int k = 0; k < 50; ++k)
  124. lynx::modenv::follow (f, fs, 0.25f, 0.01);
  125. CHECK (std::abs (fs.level - in) < 1.0e-4f);
  126. const auto top = fs.level;
  127. lynx::modenv::follow (f, fs, 0.0f, 0.1);
  128. CHECK (std::abs (fs.level - top * std::exp (-1.0f)) < 1.0e-4f); // one release time constant
  129. }
  130. TEST (mod190ox_follower_render_drives_a_target)
  131. {
  132. // insert 1 takes the input (a burst, then silence); the follower on it drives the Master's volume
  133. auto song = mod190oxSong();
  134. song.mixer[0].inputChannel = -1;
  135. song.mixer[1].inputChannel = 0;
  136. song.mixer[1].monitor = lynx::InputMonitor::on;
  137. auto f = lynx::Modulator::make (lynx::ModKind::follower);
  138. f.name = "Follower 1";
  139. f.host = { lynx::AutomationTargetKind::mixerVolume, 1, 0, 0 };
  140. f.attack = 5.0f;
  141. f.release = 30.0f;
  142. f.routes = { { { lynx::AutomationTargetKind::mixerVolume, 0, 0, 0 }, 0.5f } };
  143. song.modulators.push_back (f);
  144. const auto state = lynx::RackState::build (song, nullptr, 48000.0);
  145. CHECK (state->modStateful && state->modFeeds.size() == 1 && state->modFeeds[0].track == 1 && state->followedTracks.size() >= 2
  146. && state->followedTracks[1] == 1);
  147. std::vector<float> burst (256 * 120, 0.0f);
  148. for (int i = 0; i < 256 * 40; ++i)
  149. burst[(std::size_t) i] = 0.5f; // 40 blocks loud, then silence
  150. auto plain = song;
  151. plain.modulators.clear();
  152. const auto ref = mod190oxLevels (plain, 120, &burst);
  153. const auto mod = mod190oxLevels (song, 120, &burst);
  154. // while loud: the Master is louder than without the follower; after the release it is back
  155. CHECK (mod[35] > ref[35] * 1.3f);
  156. CHECK (ref[100] < 1.0e-6f && mod[100] < 1.0e-6f); // (silence stays silence)
  157. // the Master level just after the burst falls back as the follower releases (30 ms ≈ 6 blocks)
  158. std::vector<float> steady (256 * 120, 0.5f);
  159. const auto follow = mod190oxLevels (song, 120, &steady);
  160. CHECK (follow[60] > ref[35] * 1.3f);
  161. }
  162. TEST (mod190ox_envelope_keytrack_velocity_from_notes)
  163. {
  164. // a silent channel's notes (one bar at 1 beat, key 72, velocity 0.8) drive the Master's volume
  165. auto song = mod190oxSong();
  166. lynx::ChannelDef c;
  167. c.name = "Notes";
  168. c.instrument = lynx::InstrumentKind::sampler;
  169. c.volume = 0.0f;
  170. song.channels.push_back (c);
  171. song.patterns.clear();
  172. song.patterns.emplace_back();
  173. lynx::Note n;
  174. n.channel = 0;
  175. n.key = 72;
  176. n.velocity = 0.8f;
  177. n.start = lynx::ppq;
  178. n.length = lynx::ppq;
  179. song.patterns[0].notes.push_back (n);
  180. lynx::Clip clip;
  181. clip.pattern = 0;
  182. clip.length = 8 * lynx::ppq;
  183. song.playlist.push_back (clip);
  184. auto env = lynx::Modulator::make (lynx::ModKind::envelope);
  185. env.name = "Envelope 1";
  186. env.host = { lynx::AutomationTargetKind::channelVolume, 0, 0, 0 };
  187. env.attack = 1.0f;
  188. env.decay = 1.0f;
  189. env.sustain = 1.0f;
  190. env.release = 20.0f;
  191. env.routes = { { { lynx::AutomationTargetKind::mixerVolume, 0, 0, 0 }, 0.5f } };
  192. song.modulators.push_back (env);
  193. // one beat = 24000 samples at 120 BPM (~94 blocks)
  194. const auto levels = mod190oxLevels (song, 300);
  195. const auto before = levels[60], during = levels[150], after = levels[290];
  196. CHECK (during > before * 1.5f);
  197. CHECK (std::abs (after - before) < before * 0.05f);
  198. // keytrack: key 72 = +0.2 of ±1; velocity: 0.8 (unipolar)
  199. song.modulators[0] = lynx::Modulator::make (lynx::ModKind::keytrack);
  200. song.modulators[0].host = { lynx::AutomationTargetKind::channelVolume, 0, 0, 0 };
  201. song.modulators[0].routes = { { { lynx::AutomationTargetKind::mixerVolume, 0, 0, 0 }, 1.0f } };
  202. const auto key = mod190oxLevels (song, 300);
  203. song.modulators[0].kind = lynx::ModKind::velocity;
  204. song.modulators[0].unipolar = true;
  205. song.modulators[0].routes[0].depth = 0.5f;
  206. const auto vel = mod190oxLevels (song, 300);
  207. // both 0 before the first note, the note's value after it (they hold the last note)
  208. CHECK (std::abs (key[60] - before) < before * 0.02f && key[290] > before * 1.2f);
  209. CHECK (std::abs (vel[60] - before) < before * 0.02f && vel[290] > key[290]); // (0.3 + 0.4 against 0.3 + 0.2)
  210. }
  211. TEST (mod190ox_macro_and_its_automation)
  212. {
  213. auto song = mod190oxSong();
  214. auto m = lynx::Modulator::make (lynx::ModKind::macro);
  215. m.name = "Macro 1";
  216. m.macro = 0.0f;
  217. m.routes = { { { lynx::AutomationTargetKind::mixerVolume, 0, 0, 0 }, 0.5f } };
  218. song.modulators.push_back (m);
  219. const lynx::AutomationTarget knob { lynx::AutomationTargetKind::modulatorMacro, 0, 0, 0 };
  220. CHECK (song.targetValue (knob) == 0.0f && song.setTargetValue (knob, 0.6f) && song.modulators[0].macro == 0.6f);
  221. CHECK (! lynx::Modulator::canModulate (song, knob)); // (a modulator doesn't drive another)
  222. const auto at06 = mod190oxLevels (song, 8).back();
  223. song.modulators[0].macro = 0.0f;
  224. const auto at0 = mod190oxLevels (song, 8).back();
  225. CHECK (at06 > at0 * 1.3f);
  226. // an automation clip of the knob drives it (and its targets) instead of the saved value
  227. lynx::Automation a;
  228. a.target = knob;
  229. a.points = { { 0, 0.6f, 0.0f }, { 16 * lynx::ppq, 0.6f, 0.0f } };
  230. song.automations.push_back (a);
  231. lynx::Clip clip;
  232. clip.kind = lynx::ClipKind::automation;
  233. clip.automation = 0;
  234. clip.length = 16 * lynx::ppq;
  235. song.playlist.push_back (clip);
  236. CHECK (std::abs (mod190oxLevels (song, 8).back() - at06) < at06 * 0.02f);
  237. // deleting the modulator unlinks what pointed at its knob
  238. song.removeModulator (0);
  239. CHECK (song.modulators.empty() && song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  240. }
  241. TEST (mod190ox_hosted_plugin_parameter)
  242. {
  243. // a hosted effect on insert 1 (a probe standing in), its parameter 0 automated at 0.4 and modulated by a square
  244. // LFO of depth 0.3: the plugin gets clamp (0.4 ± 0.3), the automation's own binding sends nothing
  245. auto song = mod190oxSong();
  246. auto slot = lynx::makeEffectSlot (lynx::EffectKind::plugin);
  247. slot.plugin.id = "probe";
  248. slot.plugin.name = "Probe";
  249. song.mixer[1].effects.push_back (slot);
  250. const lynx::AutomationTarget param { lynx::AutomationTargetKind::effectParam, 1, 0, 0 };
  251. CHECK (lynx::Modulator::isHostedTarget (song, param) && lynx::Modulator::canModulate (song, param));
  252. CHECK (! lynx::Modulator::canModulate (song, { lynx::AutomationTargetKind::effectParam, 1, 0, lynx::midictl::ccBase + 1 }));
  253. lynx::Modulator lfo;
  254. lfo.name = "LFO 1";
  255. lfo.shape = lynx::ModShape::square;
  256. lfo.beats = 4.0;
  257. lfo.routes = { { param, 0.3f, 0.7f } };
  258. song.modulators.push_back (lfo);
  259. auto probe = std::make_shared<Mod190oxProbe>();
  260. lynx::RackState::Factories factories;
  261. factories.pluginEffects = [probe] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Effect> {
  262. return ref.id == "probe" ? probe : nullptr;
  263. };
  264. {
  265. const auto state = lynx::RackState::build (song, nullptr, 48000.0, factories);
  266. CHECK (state->modRoutes.size() == 1 && state->modRoutes[0].hosted && state->modRoutes[0].base == 0.7f);
  267. }
  268. // no automation: around the route's own base, clamped (0.7 + 0.3 = 1.0 on the high half, 0.4 on the low)
  269. mod190oxLevels (song, 4, nullptr, &factories);
  270. CHECK (std::abs (probe->last - 1.0f) < 1.0e-6f);
  271. lynx::Automation a;
  272. a.target = param;
  273. a.points = { { 0, 0.4f, 0.0f }, { 16 * lynx::ppq, 0.4f, 0.0f } };
  274. song.automations.push_back (a);
  275. lynx::Clip clip;
  276. clip.kind = lynx::ClipKind::automation;
  277. clip.automation = 0;
  278. clip.length = 16 * lynx::ppq;
  279. song.playlist.push_back (clip);
  280. {
  281. const auto state = lynx::RackState::build (song, nullptr, 48000.0, factories);
  282. const auto bound = std::count_if (state->pluginParams.begin(), state->pluginParams.end(), [] (const auto& b) { return b.track == 1 && b.param == 0; });
  283. CHECK (bound == 1 && state->modRoutes.size() == 1 && state->modRoutes[0].source == 0);
  284. }
  285. probe->last = -1.0f;
  286. mod190oxLevels (song, 20, nullptr, &factories); // first half of the bar: +1
  287. CHECK (std::abs (probe->last - 0.7f) < 1.0e-5f);
  288. // the second half (a bar is 96000 samples at 120 BPM): 0.4 − 0.3
  289. probe->last = -1.0f;
  290. {
  291. auto state = lynx::RackState::build (song, nullptr, 48000.0, factories);
  292. lynx::RackRenderer renderer;
  293. renderer.prepare (48000.0, 512);
  294. std::vector<float> l (256), r (256);
  295. const auto tps = song.bpm * lynx::ppq / 60.0 / 48000.0;
  296. for (int b = 0; b < 4; ++b)
  297. renderer.process (state.get(), true, (60000 + b * 256) * tps, 256, l.data(), r.data());
  298. }
  299. CHECK (std::abs (probe->last - 0.1f) < 1.0e-5f);
  300. // a route without a known base and nothing automating the parameter is not bound (the plugin keeps its value)
  301. song.automations.clear();
  302. song.playlist.clear();
  303. song.modulators[0].routes[0].base = -1.0f;
  304. CHECK (lynx::RackState::build (song, nullptr, 48000.0, factories)->modRoutes.empty());
  305. }
  306. TEST (mod190ox_stage1_unchanged_and_lfo_only_costs_nothing_new)
  307. {
  308. // stage 1's LFO modulators render exactly as before: the same output as the formula, no stateful work
  309. auto song = mod190oxSong();
  310. lynx::Modulator m;
  311. m.shape = lynx::ModShape::triangle;
  312. m.beats = 1.0;
  313. m.routes = { { { lynx::AutomationTargetKind::mixerVolume, 0, 0, 0 }, 0.25f } };
  314. song.modulators.push_back (m);
  315. const auto state = lynx::RackState::build (song, nullptr, 48000.0);
  316. CHECK (! state->modStateful && state->followedTracks.empty() && state->modRoutes.size() == 1 && ! state->modRoutes[0].hosted);
  317. CHECK (m.kind == lynx::ModKind::lfo && m.host.kind == lynx::AutomationTargetKind::none && m.steps.empty());
  318. }
  319. TEST (mod190ox_flp_export_bakes_what_fl_can_play)
  320. {
  321. auto song = mod190oxSong();
  322. song.mixer = lynx::Song::defaultMixer (4);
  323. song.playlist.clear();
  324. lynx::Clip pc; // a pattern clip so the song has a length
  325. pc.pattern = 0;
  326. pc.length = 16 * lynx::ppq;
  327. song.patterns.resize (1);
  328. song.playlist.push_back (pc);
  329. lynx::Modulator tri; // a synced triangle LFO on insert 1's pan: FL's clip LFO, exact
  330. tri.name = "LFO 1";
  331. tri.shape = lynx::ModShape::triangle;
  332. tri.beats = 2.0;
  333. tri.routes = { { { lynx::AutomationTargetKind::mixerPan, 1, 0, 0 }, 0.25f } };
  334. song.modulators.push_back (tri);
  335. auto steps = lynx::Modulator::make (lynx::ModKind::steps); // steps on insert 2's volume: points, Hold
  336. steps.name = "Steps 1";
  337. steps.routes = { { { lynx::AutomationTargetKind::mixerVolume, 2, 0, 0 }, 0.2f } };
  338. song.modulators.push_back (steps);
  339. auto macro = lynx::Modulator::make (lynx::ModKind::macro); // a macro: a flat clip
  340. macro.name = "Macro 1";
  341. macro.macro = 0.5f;
  342. macro.routes = { { { lynx::AutomationTargetKind::mixerVolume, 3, 0, 0 }, 0.2f } };
  343. song.modulators.push_back (macro);
  344. auto follower = lynx::Modulator::make (lynx::ModKind::follower); // lost
  345. follower.name = "Follower 1";
  346. follower.routes = { { { lynx::AutomationTargetKind::mixerPan, 2, 0, 0 }, 0.5f } };
  347. song.modulators.push_back (follower);
  348. auto baked = song;
  349. const auto result = lynx::modexport::bakeForFl (baked);
  350. CHECK (result.clips == 3 && result.lost.size() == 1 && baked.modulators.empty() && baked.automations.size() == 3);
  351. // the LFO clip plays what the modulator played (LYNX plays FL's clip LFO as FL does)
  352. const auto& lfoClip = baked.automations[0];
  353. CHECK (lfoClip.lfo.on && lfoClip.lfo.add && lfoClip.lfo.level == 64 && lfoClip.target == tri.routes[0].target);
  354. const auto base = song.modulationBase (tri.routes[0].target);
  355. for (const auto beat : { 0.1, 0.5, 0.9, 1.3, 1.77, 3.2, 7.5 })
  356. {
  357. const auto tick = beat * lynx::ppq;
  358. const auto want = std::clamp (base + 0.25f * tri.output (beat / 2.0), 0.0f, 1.0f);
  359. CHECK (std::abs (lfoClip.playAt (tick, tick) - want) < 2.0e-3f);
  360. }
  361. // the steps clip: Hold points with the step values
  362. const auto& stepClip = baked.automations[1];
  363. const auto stepBase = song.modulationBase (steps.routes[0].target);
  364. for (const auto step : { 0.5, 1.5, 5.5, 12.25 })
  365. CHECK (std::abs (stepClip.valueAt (step * 0.25 * lynx::ppq) - std::clamp (stepBase + 0.2f * steps.stepOutput (step), 0.0f, 1.0f)) < 1.0e-5f);
  366. // the macro: base + 0.2 × 0.5 (unipolar)
  367. CHECK (std::abs (baked.automations[2].valueAt (lynx::ppq) - std::clamp (song.modulationBase (macro.routes[0].target) + 0.1f, 0.0f, 1.0f)) < 1.0e-6f);
  368. // each on a free track over the song
  369. CHECK (std::count_if (baked.playlist.begin(), baked.playlist.end(), [] (const lynx::Clip& c) { return c.kind == lynx::ClipKind::automation; }) == 3);
  370. // the whole export: notes about it, the follower dropped by name
  371. lynx::flpexport::Report report;
  372. lynx::flpexport::toProject (song, {}, report);
  373. CHECK (std::any_of (report.dropped.begin(), report.dropped.end(), [] (const std::string& d) { return d.find ("Follower 1") != std::string::npos; }));
  374. CHECK (std::any_of (report.notes.begin(), report.notes.end(), [] (const std::string& d) { return d.find ("3 route(s)") != std::string::npos; }));
  375. CHECK (std::abs (lynx::modexport::sineTension()) <= 128);
  376. }
  377. TEST (mod190ox_targets_follow_channel_and_insert_moves)
  378. {
  379. auto song = mod190oxSong();
  380. song.mixer = lynx::Song::defaultMixer (3);
  381. song.channels.resize (2);
  382. auto f = lynx::Modulator::make (lynx::ModKind::envelope);
  383. f.host = { lynx::AutomationTargetKind::channelVolume, 1, 0, 0 };
  384. f.input = { lynx::AutomationTargetKind::channelVolume, 1, 0, 0 };
  385. song.modulators.push_back (f);
  386. song.removeChannel (0); // (channel 1 becomes 0)
  387. CHECK (song.modulators[0].host.index == 0 && song.modulators[0].input.index == 0);
  388. song.removeChannel (0);
  389. CHECK (song.modulators[0].host.kind == lynx::AutomationTargetKind::none);
  390. }
  391. // Round 190ox: the cost of a block with modulators (run under `perf stat -e instructions` with
  392. // LYNX_MOD190OX_PERF=none|automation|lfo|follower|envelope|steps|eight): 60 s of a 2 Hz pulse through insert 1's Filter
  393. // whose cutoff is automated / modulated, rendered in blocks of 256. Without the variable the test does nothing.
  394. TEST (mod190ox_perf_cpu)
  395. {
  396. const char* variant = std::getenv ("LYNX_MOD190OX_PERF");
  397. if (variant == nullptr)
  398. return;
  399. const std::string v = variant;
  400. auto song = mod190oxSong();
  401. song.mixer[0].inputChannel = -1;
  402. song.mixer[1].inputChannel = 0;
  403. song.mixer[1].monitor = lynx::InputMonitor::on;
  404. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::filter));
  405. const lynx::AutomationTarget cutoff { lynx::AutomationTargetKind::effectParam, 1, 0, 1 }; // (the Filter's Cutoff)
  406. lynx::Clip clip;
  407. clip.kind = lynx::ClipKind::automation;
  408. clip.automation = 0;
  409. clip.length = 1000 * lynx::ppq;
  410. if (v == "automation")
  411. {
  412. lynx::Automation a;
  413. a.target = cutoff;
  414. a.points = { { 0, 0.2f, 0.0f }, { 1000 * lynx::ppq, 0.9f, 0.0f } };
  415. song.automations.push_back (a);
  416. song.playlist.push_back (clip);
  417. }
  418. const auto route = std::vector<lynx::ModRoute> { { cutoff, 0.3f } };
  419. const auto addKind = [&] (lynx::ModKind kind) {
  420. auto m = lynx::Modulator::make (kind);
  421. m.host = { lynx::AutomationTargetKind::mixerVolume, 1, 0, 0 };
  422. m.routes = route;
  423. song.modulators.push_back (m);
  424. };
  425. if (v == "lfo")
  426. addKind (lynx::ModKind::lfo);
  427. else if (v == "follower")
  428. addKind (lynx::ModKind::follower);
  429. else if (v == "envelope")
  430. addKind (lynx::ModKind::envelope);
  431. else if (v == "steps")
  432. addKind (lynx::ModKind::steps);
  433. else if (v == "eight")
  434. for (int k = 0; k < lynx::modKindCount; ++k)
  435. addKind ((lynx::ModKind) k);
  436. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  437. lynx::RackRenderer renderer;
  438. renderer.prepare (48000.0, 512);
  439. lynx::LiveInput live;
  440. renderer.setLiveInput (&live);
  441. std::vector<float> a (256), l (256), r (256);
  442. live.channels[0] = a.data();
  443. live.channels[1] = a.data();
  444. live.numChannels = 2;
  445. const auto tps = song.bpm * lynx::ppq / 60.0 / 48000.0;
  446. double sum = 0.0;
  447. const auto t0 = std::chrono::steady_clock::now();
  448. for (int b = 0; b < 48000 * 60 / 256; ++b)
  449. {
  450. for (int i = 0; i < 256; ++i)
  451. a[(std::size_t) i] = ((b * 256 + i) % 24000) < 2400 ? 0.8f * std::sin ((float) i * 0.05f) : 0.0f;
  452. renderer.process (state.get(), true, b * 256 * tps, 256, l.data(), r.data());
  453. sum += std::abs (l[17]);
  454. }
  455. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - t0).count();
  456. std::printf ("mod190ox_perf %s: %.1f ms for 60 s (%d modulator(s), %d route(s)) sum %.3f\n", variant, ms, (int) song.modulators.size(),
  457. (int) state->modRoutes.size(), sum);
  458. CHECK (sum >= 0.0);
  459. }