VfxScript165Tests.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Round 165d: VFX Script runs Python (core/Python.h, core/VfxScript.h)
  2. #include "core/Python.h"
  3. #include "core/VfxScript.h"
  4. namespace vfxs165
  5. {
  6. using lynx::vfx::Event;
  7. std::vector<std::uint8_t> state (const std::string& source, const std::vector<std::pair<std::string, std::string>>& params = {})
  8. {
  9. auto s = lynx::vfx::defaultScript();
  10. s.setSource (source);
  11. if (! params.empty())
  12. s.setParameters (params);
  13. return lynx::vfx::writeScript (s);
  14. }
  15. struct Timed
  16. {
  17. long at = 0; // absolute sample
  18. Event e;
  19. };
  20. // Plays `total` samples in blocks of n (the song playing from tick 0 at `bpm`, 44.1 kHz); `in` at absolute samples
  21. std::vector<Timed> play (lynx::vfx::Processor& p, std::vector<Timed> in, long total, int n = 512, bool playing = true, double bpm = 120.0)
  22. {
  23. std::vector<Timed> out;
  24. lynx::vfx::Transport t;
  25. t.sampleRate = 44100.0;
  26. t.bpm = bpm;
  27. t.playing = playing;
  28. std::size_t next = 0;
  29. for (long start = 0; start < total; start += n)
  30. {
  31. std::vector<Event> block;
  32. while (next < in.size() && in[next].at < start + n)
  33. {
  34. auto e = in[next++].e;
  35. e.offset = (int) (in[next - 1].at - start);
  36. block.push_back (e);
  37. }
  38. t.ticks = (double) start * bpm * 960.0 / (60.0 * t.sampleRate);
  39. lynx::vfx::EventList list;
  40. p.process (block.data(), (int) block.size(), n, t, list);
  41. list.sortByOffset();
  42. for (int k = 0; k < list.size(); ++k)
  43. out.push_back ({ start + list[k].offset, list[k] });
  44. }
  45. return out;
  46. }
  47. Timed on (long at, int key, float velocity = 0.8f, int colour = 0)
  48. {
  49. return { at, vfx164::on (key, 0, colour, velocity) };
  50. }
  51. Timed off (long at, int key, int colour = 0)
  52. {
  53. return { at, vfx164::off (key, 0, colour) };
  54. }
  55. std::vector<int> keysOn (const std::vector<Timed>& out)
  56. {
  57. std::vector<int> keys;
  58. for (const auto& o : out)
  59. if (o.e.type == Event::Type::on)
  60. keys.push_back (o.e.voice.key);
  61. return keys;
  62. }
  63. const char* passThrough = R"(
  64. import flvfx as vfx
  65. class Child(vfx.Voice):
  66. parent = None
  67. def onTriggerVoice(v):
  68. for shift in (0, 4, 7):
  69. c = Child(v)
  70. c.parent = v
  71. c.note += shift
  72. c.trigger()
  73. def onReleaseVoice(v):
  74. for c in list(vfx.context.voices):
  75. if c.parent is v:
  76. c.release()
  77. )";
  78. } // namespace vfxs165
  79. TEST (vfx_script_runs_python_on_the_notes)
  80. {
  81. using namespace lynx::vfx;
  82. auto p = makeProcessor (Kind::script, vfxs165::state (vfxs165::passThrough));
  83. const auto out = vfxs165::play (*p, { vfxs165::on (100, 60, 0.5f), vfxs165::off (3000, 60) }, 4096);
  84. // three notes on at the note's sample, three offs at the release's
  85. CHECK (out.size() == 6);
  86. if (out.size() != 6)
  87. return;
  88. CHECK ((vfxs165::keysOn (out) == std::vector<int> { 60, 64, 67 }));
  89. for (int k = 0; k < 3; ++k)
  90. {
  91. CHECK (out[(std::size_t) k].at == 100 && std::abs (out[(std::size_t) k].e.voice.velocity - 0.5f) < 1e-6f);
  92. CHECK (out[(std::size_t) k + 3].at == 3000 && out[(std::size_t) k + 3].e.type == Event::Type::off);
  93. }
  94. CHECK (out[3].e.id == out[0].e.id);
  95. }
  96. TEST (vfx_script_ticks_play_notes_at_their_sample)
  97. {
  98. using namespace lynx::vfx;
  99. // an eighth-note pulse (48 of 96 ticks), each half as long, from the song's start
  100. const char* src = R"(
  101. import flvfx as vfx
  102. def onTick():
  103. if vfx.context.isPlaying and vfx.context.ticks % 48 == 0:
  104. v = vfx.Voice()
  105. v.note = 72
  106. v.length = 24
  107. v.trigger()
  108. )";
  109. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  110. auto out = vfxs165::play (*p, {}, 44100);
  111. out.erase (std::remove_if (out.begin(), out.end(), [] (const vfxs165::Timed& o) { return o.at >= 44100; }), out.end());
  112. // 120 bpm: a tick is 44100 · 60 / (120 · 96) = 229.6875 samples; an eighth 11025
  113. std::vector<long> ons, offs;
  114. for (const auto& o : out)
  115. (o.e.type == Event::Type::on ? ons : offs).push_back (o.at);
  116. if (ons != std::vector<long> { 0, 11025, 22050, 33075 })
  117. {
  118. for (const auto& o : out)
  119. std::printf (" %s at %ld key %d\n", o.e.type == Event::Type::on ? "on" : "off", o.at, o.e.voice.key);
  120. }
  121. CHECK ((ons == std::vector<long> { 0, 11025, 22050, 33075 }));
  122. // (24 ticks = 5512.5 samples: the half sample rounds either way)
  123. const long expectedOffs[] = { 5513, 16538, 27563, 38588 };
  124. CHECK (offs.size() == 4);
  125. for (std::size_t k = 0; k < offs.size() && k < 4; ++k)
  126. CHECK (std::abs (offs[k] - expectedOffs[k]) <= 1);
  127. CHECK (p->busy());
  128. }
  129. TEST (vfx_script_controls_values_and_output_controllers)
  130. {
  131. using namespace lynx::vfx;
  132. const char* src = R"(
  133. import flvfx as vfx
  134. vfx.addOutputController('Level', 0.25)
  135. def onTick():
  136. f = vfx.context.form
  137. vfx.setOutputController('Level', f.getInputValue('Main: Amount'))
  138. vfx.setOutputController('Steps', f.getInputValue('Steps') / 10.0)
  139. vfx.setOutputController('Mode', 1.0 if f.GetInputValue('Mode') == 2 and f.getInputValue('Name') == 'abc' else 0.0)
  140. def createDialog():
  141. form = vfx.ScriptDialog('Title', 'What it does')
  142. form.addGroup('Main')
  143. form.addInputKnob('Amount', 0.5, -1, 1, hint='the amount')
  144. form.endGroup()
  145. form.addInputKnobInt('Steps', 3, 1, 8)
  146. form.AddInputCombo('Mode', 'One,Two,Three', 0)
  147. form.addInputCheckbox('On', 1)
  148. form.addInputText('Name', 'x')
  149. vfx.addOutputController('Steps', 0)
  150. vfx.addOutputController('Mode', 0)
  151. return form
  152. )";
  153. auto settings = defaultScript();
  154. settings.setSource (src);
  155. const auto info = script::describe (settings);
  156. CHECK (info->compiled);
  157. if (! info->compiled)
  158. {
  159. std::printf (" %s\n", info->error.c_str());
  160. return;
  161. }
  162. CHECK (info->title == "Title" && info->description == "What it does");
  163. CHECK (info->controls.size() == 5);
  164. CHECK (info->controls[0].name == "Main: Amount" && info->controls[0].type == script::Control::Type::knob && info->controls[0].hint == "the amount");
  165. CHECK (info->controls[2].options == (std::vector<std::string> { "One", "Two", "Three" }));
  166. CHECK ((info->outputs == std::vector<std::string> { "Level", "Steps", "Mode" }));
  167. // FL's encodings: a knob as 2^18-normalized, an int / combo as the number, a text as it is
  168. settings.setParameters ({ { "Main: Amount", "196608" }, { "Steps", "7" }, { "Mode", "2" }, { "On", "0" }, { "Name", "abc" } });
  169. auto v = script::valuesOf (*info, settings);
  170. CHECK (std::abs (v.numbers[0] - 0.5) < 1e-9 && v.numbers[1] == 7.0 && v.numbers[2] == 2.0 && v.numbers[3] == 0.0 && v.texts[4] == "abc");
  171. auto again = settings;
  172. script::writeValues (again, *info, v);
  173. CHECK (again.parameters() == settings.parameters());
  174. CHECK (script::encodeValue (info->controls[0], 0.0, {}) == "131072"); // (0 in −1..1: the middle)
  175. auto p = makeProcessor (Kind::script, writeScript (settings));
  176. CHECK (std::abs (p->controlOutput (0) - 0.25f) < 1e-6f); // (the default, set at the top level)
  177. vfxs165::play (*p, {}, 2048);
  178. CHECK (std::abs (p->controlOutput (0) - 0.5f) < 1e-6f);
  179. CHECK (std::abs (p->controlOutput (1) - 0.7f) < 1e-6f);
  180. CHECK (p->controlOutput (2) == 1.0f);
  181. // an automation input (Patcher cable) moves a control: 0..1 over its range
  182. p->setParameter (0, 0.0f);
  183. p->setParameter (1, 1.0f);
  184. vfxs165::play (*p, {}, 1024);
  185. CHECK (p->controlOutput (0) == 0.0f); // (−1, clamped to the output's 0..1)
  186. CHECK (std::abs (p->controlOutput (1) - 0.8f) < 1e-6f);
  187. }
  188. TEST (vfx_script_errors_are_reported_and_never_hang)
  189. {
  190. using namespace lynx::vfx;
  191. // a syntax error: the line and the message, as FL's editor shows them
  192. const auto bad = script::describe (std::string ("import flvfx as vfx\ndef onTick(:\n pass\n"));
  193. CHECK (! bad->compiled && bad->error.find ("SyntaxError") != std::string::npos && bad->error.find ("line 2") != std::string::npos);
  194. // a runtime error in a callback: logged once, then counted; the script keeps running
  195. State st;
  196. st.kind = Kind::script;
  197. st.tag = 0x165d0001;
  198. const char* src = R"(
  199. import flvfx as vfx
  200. print('hello from the script')
  201. n = 0
  202. def onTick():
  203. global n
  204. n += 1
  205. if n % 2 == 0:
  206. raise ValueError('even tick')
  207. vfx.setOutputController('n', min(1.0, n / 1000.0))
  208. vfx.addOutputController('n', 0)
  209. )";
  210. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  211. p->setTag (st.tag);
  212. vfxs165::play (*p, {}, 8192);
  213. const auto monitor = script::monitorOf (st.tag);
  214. CHECK (monitor != nullptr);
  215. if (monitor == nullptr)
  216. return;
  217. const auto log = monitor->log();
  218. CHECK (log.find ("hello from the script") != std::string::npos);
  219. CHECK (log.find ("ValueError: even tick") != std::string::npos);
  220. CHECK (log.find ("the same error again") != std::string::npos);
  221. CHECK (monitor->status().empty());
  222. CHECK (p->controlOutput (0) > 0.01f);
  223. // an endless loop: interrupted, the instance stops and says why; the audio thread never waits long
  224. script::setRealtime (true);
  225. auto loop = makeProcessor (Kind::script, vfxs165::state ("import flvfx as vfx\ndef onTriggerVoice(v):\n while True:\n try:\n pass\n except:\n pass\n"));
  226. loop->setTag (0x165d0002);
  227. const auto begin = std::chrono::steady_clock::now();
  228. double worst = 0.0;
  229. std::vector<double> times;
  230. for (int b = 0; b < 400; ++b)
  231. {
  232. lynx::vfx::EventList out;
  233. lynx::vfx::Transport t;
  234. auto e = vfx164::on (60);
  235. const auto before = std::chrono::steady_clock::now();
  236. loop->process (&e, b == 0 ? 1 : 0, 512, t, out);
  237. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - before).count();
  238. worst = std::max (worst, ms);
  239. times.push_back (ms);
  240. std::this_thread::sleep_for (std::chrono::milliseconds (2));
  241. }
  242. script::setRealtime (false);
  243. const auto m2 = script::monitorOf (0x165d0002);
  244. // (round 167e: the limit is 250 ms of the Python thread's CPU time, so on a busy machine it may come after the
  245. // 400 blocks: waited for, up to the 1 s wall-clock backstop and some)
  246. for (int w = 0; w < 300 && m2 != nullptr && m2->status().find ("took too long") == std::string::npos; ++w)
  247. std::this_thread::sleep_for (std::chrono::milliseconds (10));
  248. CHECK (m2 != nullptr && m2->status().find ("took too long") != std::string::npos);
  249. std::sort (times.begin(), times.end());
  250. const auto p95 = times[times.size() * 95 / 100];
  251. std::printf (" endless loop: stopped; block wait 95th percentile %.2f ms, worst %.2f ms, %.0f ms in all\n", p95, worst,
  252. std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - begin).count());
  253. // the wait is bounded by the budget (2 ms at most); the worst block also carries the OS scheduler's noise on a
  254. // loaded machine (a test thread has no real-time priority)
  255. CHECK (p95 < 2.5);
  256. CHECK (worst < 50.0);
  257. }
  258. TEST (vfx_script_memory_runaway_is_stopped)
  259. {
  260. using namespace lynx::vfx;
  261. // a script that keeps allocating is stopped at LYNX's heap limit (768 MB), with a message; the memory comes back
  262. auto p = makeProcessor (Kind::script, vfxs165::state ("import flvfx as vfx\nkeep = []\ndef onTick():\n while True:\n try:\n keep.append([0] * 4000000)\n except:\n pass\n"));
  263. p->setTag (0x165d0005);
  264. vfxs165::play (*p, {}, 1024);
  265. auto m = script::monitorOf (0x165d0005);
  266. CHECK (m != nullptr && m->status().find ("more than 768 MB") != std::string::npos);
  267. if (m != nullptr)
  268. std::printf (" %s\n", m->status().c_str());
  269. m.reset();
  270. p.reset(); // (its VM is released: the next script starts with a small heap)
  271. auto q = makeProcessor (Kind::script, vfxs165::state ("import flvfx as vfx\nbig = [0] * 1000000\ndef onTick():\n vfx.setOutputController('ok', 1)\nvfx.addOutputController('ok', 0)\n"));
  272. vfxs165::play (*q, {}, 512);
  273. CHECK (q->controlOutput (0) == 1.0f);
  274. }
  275. TEST (vfx_script_voice_changes_reach_the_sounding_note)
  276. {
  277. using namespace lynx::vfx;
  278. const char* src = R"(
  279. import flvfx as vfx
  280. def onTriggerVoice(v):
  281. c = vfx.Voice(v)
  282. c.parent = v
  283. c.trigger()
  284. def onTick():
  285. for c in vfx.context.voices:
  286. c.copyFrom(c.parent)
  287. c.note += 0.5
  288. c.pan = -1
  289. )";
  290. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  291. const auto out = vfxs165::play (*p, { vfxs165::on (0, 60) }, 1024);
  292. bool pitch = false, pan = false;
  293. for (const auto& o : out)
  294. if (o.e.type == Event::Type::prop)
  295. {
  296. pitch = pitch || (o.e.prop == Prop::pitch && std::abs (o.e.value - 50.0f) < 1e-3f);
  297. pan = pan || (o.e.prop == Prop::pan && o.e.value == -1.0f);
  298. }
  299. CHECK (pitch && pan);
  300. CHECK (! out.empty() && out[0].e.type == Event::Type::on && out[0].e.voice.key == 60);
  301. }
  302. TEST (vfx_script_python_has_what_fl_scripts_use)
  303. {
  304. using namespace lynx::vfx;
  305. // CPython features pocketpy lacks that FL's factory scripts use: LYNX adds or patches them
  306. const char* src = "import flvfx as vfx\nimport re, random, sys, math, time\n"
  307. "text = '''two lines \\\nas one'''\n"
  308. "class Mine(vfx.Voice):\n def __init__(self):\n super().__init__()\n self.extra: int = 3\n"
  309. "def check():\n"
  310. " assert re.sub(r'choices\\((.*?)\\)( |$)', r'choices(\\1)[0] ', 'choices([1,2],k=1)') == 'choices([1,2],k=1)[0] '\n"
  311. " assert re.findall(r'\\d+', 'a1b22c333') == ['1', '22', '333']\n"
  312. " m = re.match(r'(?P<w>[a-z]+)(\\d)', 'ab7x')\n"
  313. " assert m.group('w') == 'ab' and m.group(2) == '7' and m.span() == (0, 3)\n"
  314. " assert re.split(r',\\s*', 'a, b,c') == ['a', 'b', 'c']\n"
  315. " assert pow(2, 10, 1000) == 24 and pow(2, 3) == 8\n"
  316. " assert sys.float_info.epsilon < 1e-15\n"
  317. " random.seed(1.5)\n"
  318. " g = [random.gauss(0, 1) for i in range(2000)]\n"
  319. " assert abs(sum(g) / len(g)) < 0.15\n"
  320. " assert next((x for x in [1, 5, 9] if x > 4), None) == 5\n"
  321. " assert sum(x * x for x in range(4)) == 14\n"
  322. " assert text == 'two lines as one'\n"
  323. " assert Mine().extra == 3 and Mine().note == 60.0\n"
  324. " assert time.time_ns() > 0\n"
  325. " assert '42'.isdigit() and not '4a'.isdigit() and 'ab'.isalpha() and 'a1'.isalnum() and ' '.isspace()\n"
  326. " assert 'AB'.isupper() and 'ab'.islower() and 'hello world'.title() == 'Hello World'\n"
  327. " assert 'a,b,c'.rsplit(',', 1) == ['a,b', 'c'] and 'a.b.c'.rpartition('.') == ('a.b', '.', 'c')\n"
  328. " assert 'abcabc'.rfind('b') == 4 and 'ab'.center(6, '*') == '**ab**' and 'x'.capitalize() == 'X'\n"
  329. " vfx.setOutputController('ok', 1)\n"
  330. "vfx.addOutputController('ok', 0)\n"
  331. "def onTick():\n check()\n";
  332. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  333. p->setTag (0x165d0003);
  334. vfxs165::play (*p, {}, 512);
  335. CHECK (p->controlOutput (0) == 1.0f);
  336. if (p->controlOutput (0) != 1.0f)
  337. if (const auto m = script::monitorOf (0x165d0003))
  338. std::printf (" %s\n", m->log().c_str());
  339. // the sandbox: no files, no processes
  340. auto q = makeProcessor (Kind::script, vfxs165::state ("import flvfx as vfx\nimport os\n"));
  341. q->setTag (0x165d0004);
  342. const auto m = script::monitorOf (0x165d0004);
  343. CHECK (m != nullptr && m->status().find ("ImportError") != std::string::npos);
  344. CHECK (! script::describe (std::string ("x = open('a.txt', 'w')\n"))->compiled);
  345. }
  346. TEST (vfx_script_late_answers_play_at_the_next_block)
  347. {
  348. using namespace lynx::vfx;
  349. // a script slower than the real-time budget: its note comes later, at the start of the first block after it
  350. // answered. (Round 167e: the script took 6 ms against the 2 ms budget and the test looked 30 ms later. On a busy
  351. // machine the test thread could be preempted inside its wait for longer than the script (the answer was then on
  352. // time, in the first block) or the Python thread could answer after the 30 ms: the margins, not LYNX, were flaky.
  353. // The script now takes 40 ms (20x the budget, well inside the 250 ms real-time limit) and the blocks go on until
  354. // the answer has come.)
  355. const char* src = R"(
  356. import flvfx as vfx, time
  357. def onTriggerVoice(v):
  358. t = time.time() + 0.04
  359. while time.time() < t:
  360. pass
  361. c = vfx.Voice(v)
  362. c.trigger()
  363. )";
  364. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  365. script::setRealtime (true);
  366. lynx::vfx::Transport t;
  367. lynx::vfx::EventList first, later;
  368. auto e = vfx164::on (60, 100);
  369. p->process (&e, 1, 512, t, first);
  370. int blocks = 0;
  371. for (; blocks < 400 && later.size() == 0; ++blocks)
  372. {
  373. std::this_thread::sleep_for (std::chrono::milliseconds (5));
  374. p->process (nullptr, 0, 512, t, later);
  375. }
  376. script::setRealtime (false);
  377. CHECK (first.size() == 0);
  378. CHECK (later.size() == 1 && later[0].offset == 0 && later[0].voice.key == 60);
  379. // offline (export) it waits: the note is at its sample
  380. lynx::vfx::EventList exact;
  381. auto p2 = makeProcessor (Kind::script, vfxs165::state (src));
  382. p2->process (&e, 1, 512, t, exact);
  383. CHECK (exact.size() == 1 && exact[0].offset == 100);
  384. }
  385. TEST (vfx_script_runs_fl_factory_scripts)
  386. {
  387. using namespace lynx::vfx;
  388. if (realFlpDir.empty())
  389. return;
  390. namespace fs = std::filesystem;
  391. const auto dir = fs::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / "VFX Script";
  392. if (! fs::is_directory (dir))
  393. return;
  394. auto load = [&] (const fs::path& file) -> std::optional<ScriptSettings> {
  395. std::ifstream in (file, std::ios::binary);
  396. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), {});
  397. const auto st = stateFromFlPreset (bytes, Kind::script);
  398. return st ? parseScript (*st) : std::nullopt;
  399. };
  400. int files = 0, compiled = 0;
  401. double slowest = 0.0;
  402. std::string slowestName;
  403. for (const auto& entry : fs::recursive_directory_iterator (dir))
  404. {
  405. if (entry.path().extension() != ".fst")
  406. continue;
  407. const auto s = load (entry.path());
  408. if (! s)
  409. continue;
  410. ++files;
  411. const auto info = script::describe (*s);
  412. if (info->compiled)
  413. ++compiled;
  414. else
  415. std::printf (" %s: %s\n", entry.path().filename().string().c_str(), info->error.c_str());
  416. // each one runs 2 s with a held chord and the song playing, without an error
  417. auto p = makeProcessor (Kind::script, writeScript (*s));
  418. p->setTag (0x165d1000 + (std::uint32_t) files);
  419. const auto begin = std::chrono::steady_clock::now();
  420. vfxs165::play (*p, { vfxs165::on (0, 60), vfxs165::on (0, 64), vfxs165::on (0, 67), vfxs165::off (44100, 60), vfxs165::off (44100, 64), vfxs165::off (44100, 67) },
  421. 88200);
  422. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - begin).count();
  423. if (ms > slowest)
  424. slowest = ms, slowestName = entry.path().filename().string();
  425. if (const auto m = script::monitorOf (0x165d1000 + (std::uint32_t) files))
  426. if (m->log().find ("Error") != std::string::npos)
  427. std::printf (" %s ran with errors:\n%s\n", entry.path().filename().string().c_str(), m->log().c_str());
  428. }
  429. std::printf (" %d of %d FL scripts compile; slowest: %s, 2 s of audio in %.1f ms\n", compiled, files, slowestName.c_str(), slowest);
  430. CHECK (files >= 80 && compiled == files);
  431. // FL's Snap To Scale (spec S6): Enable, C major, Down: 61 → 60, 63 → 62; Up: 61 → 62
  432. if (auto s = load (dir / "Snap To Scale.fst"))
  433. {
  434. const auto info = script::describe (*s);
  435. auto v = script::valuesOf (*info, *s);
  436. auto set = [&] (const char* name, double value) {
  437. if (const auto i = info->index (name); i >= 0)
  438. v.numbers[(std::size_t) i] = value;
  439. };
  440. set ("Snap to Scale: Enable", 1);
  441. set ("Snap to Scale: Key", 0);
  442. set ("Snap to Scale: Scale", 0);
  443. set ("Snap to Scale: Snap Direction", 1);
  444. script::writeValues (*s, *info, v);
  445. auto p = makeProcessor (Kind::script, writeScript (*s));
  446. auto keys = vfxs165::keysOn (vfxs165::play (*p, { vfxs165::on (0, 61), vfxs165::on (10, 63), vfxs165::on (20, 60) }, 512));
  447. CHECK ((keys == std::vector<int> { 60, 62, 60 }));
  448. set ("Snap to Scale: Snap Direction", 0);
  449. script::writeValues (*s, *info, v);
  450. p->configure (writeScript (*s));
  451. keys = vfxs165::keysOn (vfxs165::play (*p, { vfxs165::on (0, 61) }, 512));
  452. CHECK ((keys == std::vector<int> { 62 }));
  453. }
  454. // FL's Arpeggiator (Up, 2 per beat, 0.75 long): C E G held → C E G C… every 48 ticks
  455. if (auto s = load (dir / "Arpeggiator.fst"))
  456. {
  457. auto p = makeProcessor (Kind::script, writeScript (*s));
  458. const auto out = vfxs165::play (*p, { vfxs165::on (0, 60), vfxs165::on (0, 64), vfxs165::on (0, 67) }, 44100);
  459. std::vector<int> keys;
  460. std::vector<long> at;
  461. for (const auto& o : out)
  462. if (o.e.type == Event::Type::on)
  463. keys.push_back (o.e.voice.key), at.push_back (o.at);
  464. CHECK (keys.size() >= 4 && keys[0] == 60 && keys[1] == 64 && keys[2] == 67 && keys[3] == 60);
  465. CHECK (at.size() >= 2 && at[1] - at[0] == 11025);
  466. }
  467. // ... synced, Speed 3 (as in FL's render, round 165d): the arpeggio stops when the held notes are released
  468. if (auto s = load (dir / "Arpeggiator.fst"))
  469. {
  470. const auto info = script::describe (*s);
  471. auto v = script::valuesOf (*info, *s);
  472. for (auto [name, value] : { std::pair { "Sync", 1.0 }, { "Speed", 3.0 } })
  473. if (const auto i = info->index (name); i >= 0)
  474. v.numbers[(std::size_t) i] = value;
  475. script::writeValues (*s, *info, v);
  476. auto p = makeProcessor (Kind::script, writeScript (*s));
  477. const auto out = vfxs165::play (*p, { vfxs165::on (5512, 60), vfxs165::on (5512, 64), vfxs165::on (6890, 67), vfxs165::off (97387, 60),
  478. vfxs165::off (97387, 64), vfxs165::off (98765, 67) },
  479. 176400);
  480. long last = 0;
  481. int ons = 0;
  482. for (const auto& o : out)
  483. if (o.e.type == Event::Type::on)
  484. last = o.at, ++ons;
  485. if (last > 99000)
  486. std::printf (" synced arpeggio: %d notes, the last at %ld\n", ons, last);
  487. CHECK (ons >= 10 && last <= 99000);
  488. }
  489. // FL's Note Formula (spec S7): "note + 12", also for a note at the render's first sample (FL has ticked before)
  490. if (auto s = load (dir / "Note Formula Examples" / "1 - Raise the pitch by one octave.fst"))
  491. {
  492. auto p = makeProcessor (Kind::script, writeScript (*s));
  493. auto keys = vfxs165::keysOn (vfxs165::play (*p, { vfxs165::on (300, 60) }, 2048));
  494. CHECK ((keys == std::vector<int> { 72 }));
  495. auto first = makeProcessor (Kind::script, writeScript (*s));
  496. keys = vfxs165::keysOn (vfxs165::play (*first, { vfxs165::on (0, 60) }, 2048));
  497. CHECK ((keys == std::vector<int> { 72 }));
  498. }
  499. }