PianoRollScript190Tests.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // --- Piano roll scripts (core/PianoRollScript.h) - round 190as (REA_rodada190as-pyscript.md)
  2. #include <filesystem>
  3. #include <fstream>
  4. #include <sstream>
  5. #include "core/KeyMap.h"
  6. #include "core/PianoRollScript.h"
  7. namespace prscript190
  8. {
  9. // a pattern with a C major arpeggio on channel 0 (4 notes, one per beat) and two notes of channel 1
  10. inline lynx::Pattern pattern()
  11. {
  12. lynx::Pattern p;
  13. const int keys[] = { 60, 64, 67, 72 };
  14. for (int i = 0; i < 4; ++i)
  15. {
  16. lynx::Note n;
  17. n.channel = 0;
  18. n.key = keys[i];
  19. n.start = (std::int64_t) i * lynx::ppq;
  20. n.length = lynx::ppq / 2;
  21. n.velocity = 0.5f + 0.125f * (float) i;
  22. p.notes.push_back (n);
  23. }
  24. for (int i = 0; i < 2; ++i)
  25. {
  26. lynx::Note n;
  27. n.channel = 1;
  28. n.key = 36;
  29. n.start = (std::int64_t) i * 2 * lynx::ppq + 10;
  30. n.length = 100;
  31. n.group = 3;
  32. p.notes.push_back (n);
  33. }
  34. p.sortNotes();
  35. return p;
  36. }
  37. inline std::vector<int> channelIndices (const lynx::Pattern& p, int channel)
  38. {
  39. std::vector<int> out;
  40. for (std::size_t i = 0; i < p.notes.size(); ++i)
  41. if (p.notes[i].channel == channel)
  42. out.push_back ((int) i);
  43. return out;
  44. }
  45. struct Result
  46. {
  47. lynx::prscript::Outcome outcome;
  48. std::vector<int> selected;
  49. int forms = 0;
  50. };
  51. // runs a script to its end on channel 0 of `p`, answering every form with its defaults changed by `set`
  52. inline Result runAll (const std::string& src, lynx::Pattern& p, const std::vector<int>& selection = {}, const std::map<std::string, double>& set = {},
  53. double limitMs = 20000.0, std::int64_t timeFrom = 0, std::int64_t timeTo = 0)
  54. {
  55. using namespace lynx::prscript;
  56. Result r;
  57. std::vector<Form> answers;
  58. for (int round = 0; round < 8; ++round)
  59. {
  60. auto score = scoreOf (p, 0, selection, 4, 4, timeFrom, timeTo, lynx::Note {});
  61. r.outcome = run (src, score, answers, limitMs);
  62. if (r.outcome.status != Outcome::Status::needsForm)
  63. {
  64. if (r.outcome.status == Outcome::Status::done)
  65. r.selected = apply (p, 0, score);
  66. r.forms = (int) answers.size();
  67. return r;
  68. }
  69. auto f = r.outcome.form;
  70. for (auto& c : f.controls)
  71. if (const auto it = set.find (c.name); it != set.end())
  72. c.value = it->second;
  73. answers.push_back (f);
  74. }
  75. return r;
  76. }
  77. inline bool done (const Result& r)
  78. {
  79. if (r.outcome.status != lynx::prscript::Outcome::Status::done)
  80. std::printf (" script: %s\n", r.outcome.error.c_str());
  81. return r.outcome.status == lynx::prscript::Outcome::Status::done;
  82. }
  83. inline bool failedWith (const Result& r, const char* text)
  84. {
  85. const bool ok = r.outcome.status == lynx::prscript::Outcome::Status::failed && r.outcome.error.find (text) != std::string::npos;
  86. if (! ok)
  87. std::printf (" expected an error with '%s', got status %d: %s\n", text, (int) r.outcome.status, r.outcome.error.c_str());
  88. return ok;
  89. }
  90. } // namespace prscript190
  91. TEST (prscript190_reference_example_and_what_survives)
  92. {
  93. // the example of FL's reference: every note muted; the other channel and LYNX-only note data stay
  94. auto p = prscript190::pattern();
  95. for (auto& n : p.notes)
  96. if (n.channel == 0)
  97. n.chance = 0.25f;
  98. const auto before = p;
  99. const auto r = prscript190::runAll ("from flpianoroll import *\n"
  100. "for i in range(score.noteCount) :\n"
  101. " score.getNote(i).muted = True\n",
  102. p);
  103. CHECK (prscript190::done (r));
  104. CHECK (r.forms == 0);
  105. CHECK (p.notes.size() == before.notes.size());
  106. for (std::size_t i = 0; i < p.notes.size(); ++i)
  107. {
  108. CHECK (p.notes[i].muted == (p.notes[i].channel == 0));
  109. CHECK (p.notes[i].key == before.notes[i].key && p.notes[i].start == before.notes[i].start);
  110. CHECK (p.notes[i].chance == before.notes[i].chance);
  111. CHECK (p.notes[i].velocity == before.notes[i].velocity);
  112. }
  113. }
  114. TEST (prscript190_score_values_and_units)
  115. {
  116. // PPQ and the time signature, FL's units (pan 0..1 with 0.5 = centre, pitchofs = fine pitch), Note() defaults
  117. auto p = prscript190::pattern();
  118. p.notes[0].pan = -1.0f;
  119. p.notes[0].finePitch = -30.0f;
  120. p.notes[0].modX = 0.25f;
  121. p.notes[0].colour = 5;
  122. p.notes[0].slide = true;
  123. const auto r = prscript190::runAll ("import flpianoroll as flp\n"
  124. "s = flp.score\n"
  125. "n = s.getNote(0)\n"
  126. "print(s.PPQ, s.tsnum, s.tsden, s.noteCount, s.markerCount, s.length)\n"
  127. "print(n.number, n.time, n.length, n.pan, n.pitchofs, n.fcut, n.color, n.slide, n.porta, n.selected, n.group)\n"
  128. "d = flp.Note()\n"
  129. "print(d.number, d.time, d.length, d.pan, d.velocity, d.release, d.color, d.pitchofs, d.repeats, d.muted)\n"
  130. "print(round(d.fcut * 255), round(d.fres * 255))\n"
  131. "print(s.getNote(99), s.getNote(-1), s.getTimelineSelection(), s.getNextFreeGroupIndex())\n"
  132. "print(s.snap_root_note, s.snap_scale_helper)\n",
  133. p);
  134. CHECK (prscript190::done (r));
  135. const auto expected = std::to_string (lynx::ppq) + " 4 4 4 0 " + std::to_string (3 * lynx::ppq + lynx::ppq / 2) + "\n"
  136. "60 0 " + std::to_string (lynx::ppq / 2) + " 0.0 -30 0.25 5 True False False 0\n"
  137. "0 0 0 0.5 0.78125 0.5 0 0 0 False\n"
  138. "128 128\n"
  139. "None None (0, -1) 1\n"
  140. "0 0,0,0,0,0,0,0,0,0,0,0,0\n";
  141. if (r.outcome.log != expected)
  142. std::printf (" log:\n%s", r.outcome.log.c_str());
  143. CHECK (r.outcome.log == expected);
  144. }
  145. TEST (prscript190_live_notes_add_clone_delete)
  146. {
  147. // getNote() hands out the live note, addNote() takes the object itself (FL: changes after the call count, which
  148. // FL's Note repeater script relies on); clone() copies; deleteNote() of a bad index raises
  149. auto p = prscript190::pattern();
  150. const auto r = prscript190::runAll ("import flpianoroll as flp\n"
  151. "a = flp.score.getNote(1)\n"
  152. "print(a is flp.score.getNote(1))\n"
  153. "c = a.clone()\n"
  154. "print(c is a, c.number == a.number, c.time == a.time)\n"
  155. "flp.score.addNote(c)\n"
  156. "c.number = a.number + 12\n" // after addNote
  157. "c.velocity = 2.5\n" // out of range: clamped when stored
  158. "c.pan = -3\n"
  159. "n = flp.Note()\n"
  160. "n.number = 48\n"
  161. "n.time = 100\n"
  162. "n.length = 50\n"
  163. "flp.score.addNote(n)\n"
  164. "print(flp.score.noteCount)\n"
  165. "flp.score.deleteNote(0)\n"
  166. "print(flp.score.noteCount, flp.score.getNote(0) is a)\n"
  167. "try:\n"
  168. " flp.score.deleteNote(77)\n"
  169. "except Exception as e:\n"
  170. " print('error', e)\n",
  171. p);
  172. CHECK (prscript190::done (r));
  173. CHECK (r.outcome.log == "True\nFalse True True\n6\n5 True\nerror Invalid note index\n");
  174. const auto mine = prscript190::channelIndices (p, 0);
  175. CHECK (mine.size() == 5);
  176. int found = 0;
  177. for (const int i : mine)
  178. {
  179. const auto& n = p.notes[(std::size_t) i];
  180. CHECK (n.key != 60); // the deleted note
  181. if (n.key == 76)
  182. {
  183. ++found;
  184. CHECK (n.start == lynx::ppq && n.velocity == 1.0f && n.pan == -1.0f);
  185. }
  186. if (n.key == 48)
  187. {
  188. ++found;
  189. CHECK (n.start == 100 && n.length == 50 && n.velocity == 0.78125f && n.pan == 0.0f && n.release == 0.5f);
  190. }
  191. }
  192. CHECK (found == 2);
  193. CHECK (std::is_sorted (p.notes.begin(), p.notes.end(), [] (const lynx::Note& a, const lynx::Note& b) { return a.start < b.start; }));
  194. CHECK (prscript190::channelIndices (p, 1).size() == 2);
  195. }
  196. TEST (prscript190_selection_limits_what_the_script_sees)
  197. {
  198. // with notes selected the script sees only those (FL: the visible flag of the score, REA §3); clearNotes() without
  199. // True removes the still-selected ones, and the notes left selected come back as the selection
  200. auto p = prscript190::pattern();
  201. const auto mine = prscript190::channelIndices (p, 0);
  202. const std::vector<int> selection { mine[1], mine[2] };
  203. auto q = p;
  204. auto r = prscript190::runAll ("import flpianoroll as flp\n"
  205. "print(flp.score.noteCount, [flp.score.getNote(i).number for i in range(flp.score.noteCount)])\n"
  206. "print([flp.score.getNote(i).selected for i in range(flp.score.noteCount)])\n"
  207. "flp.score.getNote(0).selected = False\n"
  208. "n = flp.Note()\n"
  209. "n.number = 50\n"
  210. "n.length = 10\n"
  211. "flp.score.addNote(n)\n"
  212. "flp.score.clearNotes()\n" // removes only note 67 (still selected)
  213. "print(flp.score.noteCount)\n"
  214. "n.selected = True\n",
  215. q, selection);
  216. CHECK (prscript190::done (r));
  217. CHECK (r.outcome.log == "2 [64, 67]\n[True, True]\n2\n");
  218. CHECK (prscript190::channelIndices (q, 0).size() == 4); // 60, 64, 72 + the new 50
  219. CHECK (r.selected.size() == 1);
  220. if (r.selected.size() == 1)
  221. CHECK (q.notes[(std::size_t) r.selected[0]].key == 50);
  222. for (const auto& n : q.notes)
  223. CHECK (n.key != 67);
  224. // without a selection: all of the channel's notes, clearNotes() removes them all, the other channel stays
  225. q = p;
  226. r = prscript190::runAll ("import flpianoroll as flp\nprint(flp.score.noteCount)\nflp.score.clearNotes(False)\nprint(flp.score.noteCount)\n", q);
  227. CHECK (prscript190::done (r));
  228. CHECK (r.outcome.log == "4\n0\n");
  229. CHECK (q.notes.size() == 2 && q.notes[0].channel == 1);
  230. // getNextFreeGroupIndex() also counts the groups of the notes the script does not see (channel 1 uses group 3)
  231. q = p;
  232. q.notes[(std::size_t) mine[0]].group = 1;
  233. q.notes[(std::size_t) mine[1]].group = 2;
  234. r = prscript190::runAll ("import flpianoroll as flp\nprint(flp.score.getNextFreeGroupIndex())\n", q);
  235. CHECK (r.outcome.log == "4\n");
  236. }
  237. TEST (prscript190_members_are_typed_as_in_fl)
  238. {
  239. // FL's Note members are typed slots: an int member refuses a float, a bool member anything but a bool
  240. auto p = prscript190::pattern();
  241. const auto before = p;
  242. CHECK (prscript190::failedWith (prscript190::runAll ("import flpianoroll as flp\nflp.score.getNote(0).time = 1.5\n", p), "TypeError"));
  243. CHECK (prscript190::failedWith (prscript190::runAll ("import flpianoroll as flp\nflp.score.getNote(0).selected = 1\n", p), "TypeError"));
  244. CHECK (prscript190::failedWith (prscript190::runAll ("import flpianoroll as flp\nflp.score.getNote(0).velocity = 'x'\n", p), "TypeError"));
  245. CHECK (prscript190::failedWith (prscript190::runAll ("import flpianoroll as flp\nflp.score.addNote(5)\n", p), "TypeError"));
  246. CHECK (p == before); // a failed script changes nothing
  247. // an int into a float member is fine
  248. CHECK (prscript190::done (prscript190::runAll ("import flpianoroll as flp\nflp.score.getNote(0).velocity = 1\n", p)));
  249. CHECK (p.notes[0].velocity == 1.0f);
  250. }
  251. TEST (prscript190_markers_and_timeline_selection)
  252. {
  253. auto p = prscript190::pattern();
  254. lynx::Marker a;
  255. a.tick = lynx::ppq;
  256. a.name = "Verse 'one'";
  257. p.markers.push_back (a);
  258. lynx::Marker sig;
  259. sig.tick = 8 * lynx::ppq;
  260. sig.numerator = 3;
  261. sig.denominator = 4;
  262. sig.action = lynx::Marker::timeSignature;
  263. p.markers.push_back (sig);
  264. auto q = p;
  265. auto r = prscript190::runAll ("import flpianoroll as flp\n"
  266. "s = flp.score\n"
  267. "print(s.markerCount, s.getMarker(0).name, s.getMarker(0).time, s.getMarker(0).mode)\n"
  268. "print(s.getMarker(1).mode, s.getMarker(1).tsnum, s.getMarker(1).tsden, s.getMarker(5))\n"
  269. "m = flp.Marker()\n"
  270. "print(m.time, m.name == '', m.mode, m.tsnum, m.tsden)\n"
  271. "m.name = 'Drop'\n"
  272. "m.time = 2 * s.PPQ\n"
  273. "s.addMarker(m)\n"
  274. "s.deleteMarker(0)\n"
  275. "print(s.getTimelineSelection())\n"
  276. "s.setTimelineSelection(10, 500)\n",
  277. q, {}, {}, 20000.0, 960, 1920);
  278. CHECK (prscript190::done (r));
  279. CHECK (r.outcome.log == "2 Verse 'one' 960 0\n8 3 4 None\n0 True 0 4 4\n(960, 1920)\n");
  280. CHECK (q.markers.size() == 2);
  281. if (q.markers.size() == 2)
  282. {
  283. CHECK (q.markers[0].name == "Drop" && q.markers[0].tick == 2 * lynx::ppq && q.markers[0].action == 0);
  284. CHECK (q.markers[1].isSignature() && q.markers[1].numerator == 3);
  285. }
  286. // clearMarkers(): only the markers inside the notes' time range (FL); clearMarkers(True): all of them
  287. q = p;
  288. r = prscript190::runAll ("import flpianoroll as flp\nflp.score.clearMarkers()\nprint(flp.score.markerCount)\n", q);
  289. CHECK (r.outcome.log == "1\n");
  290. CHECK (q.markers.size() == 1 && q.markers[0].isSignature());
  291. q = p;
  292. r = prscript190::runAll ("import flpianoroll as flp\nflp.score.clear(True)\nprint(flp.score.markerCount, flp.score.noteCount)\n", q);
  293. CHECK (r.outcome.log == "0 0\n");
  294. CHECK (q.markers.empty() && q.notes.size() == 2);
  295. }
  296. TEST (prscript190_preview_style_form_and_messages)
  297. {
  298. // createDialog() + apply(form): the first run stops for the form, the next applies it with the answers
  299. const std::string src = "import flpianoroll as flp\n"
  300. "def createDialog():\n"
  301. " form = flp.ScriptDialog('Transpose', 'Moves the notes.')\n"
  302. " form.AddInputKnobInt('Semitones', 2, -24, 24)\n"
  303. " form.addInputCheckbox('Mute', False)\n"
  304. " form.AddInputCombo('Mode', 'a,b,c', 1)\n"
  305. " return form\n"
  306. "def apply(form):\n"
  307. " for i in range(flp.score.noteCount):\n"
  308. " flp.score.getNote(i).number += form.GetInputValue('Semitones')\n"
  309. " flp.score.getNote(i).muted = form.getInputValue('Mute')\n"
  310. " flp.Utils.ShowMessage('mode ' + str(form.GetInputValue('Mode')))\n"
  311. " flp.Utils.log('logged')\n";
  312. auto p = prscript190::pattern();
  313. auto score = lynx::prscript::scoreOf (p, 0, {}, 4, 4, 0, 0, lynx::Note {});
  314. const auto first = lynx::prscript::run (src, score, {});
  315. CHECK (first.status == lynx::prscript::Outcome::Status::needsForm);
  316. CHECK (first.form.title == "Transpose" && first.form.description == "Moves the notes." && first.form.controls.size() == 3);
  317. if (first.form.controls.size() == 3)
  318. {
  319. CHECK (first.form.controls[0].name == "Semitones" && first.form.controls[0].value == 2.0 && first.form.controls[0].minimum == -24.0);
  320. CHECK (first.form.controls[2].options.size() == 3 && first.form.controls[2].value == 1.0);
  321. }
  322. const auto r = prscript190::runAll (src, p, {}, { { "Semitones", 7.0 }, { "Mute", 1.0 } });
  323. CHECK (prscript190::done (r));
  324. CHECK (r.forms == 1);
  325. CHECK (r.outcome.messages.size() == 1 && r.outcome.messages[0] == "mode 1");
  326. CHECK (r.outcome.log == "logged\n");
  327. const auto mine = prscript190::channelIndices (p, 0);
  328. CHECK (p.notes[(std::size_t) mine[0]].key == 67 && p.notes[(std::size_t) mine[0]].muted);
  329. // FL's messages for a createDialog() that returns no ScriptDialog
  330. CHECK (prscript190::failedWith (prscript190::runAll ("def createDialog():\n return 5\ndef apply(form):\n pass\n", p),
  331. "createDialog function needs to return a ScriptDialog instance"));
  332. CHECK (prscript190::failedWith (prscript190::runAll ("def createDialog():\n pass\ndef apply(form):\n pass\n", p), "no dialog was created in createDialog"));
  333. // a script that shows its own dialog (Execute) works the same way
  334. const auto own = prscript190::runAll ("from flpianoroll import *\n"
  335. "form = ScriptDialog('Own', '')\n"
  336. "form.addInputKnob('Velocity', 0.5, 0, 1)\n"
  337. "if form.execute():\n"
  338. " for i in range(score.noteCount):\n"
  339. " score.getNote(i).velocity = form.getInputValue('Velocity')\n",
  340. p, {}, { { "Velocity", 0.25 } });
  341. CHECK (prscript190::done (own) && own.forms == 1);
  342. CHECK (p.notes[(std::size_t) mine[1]].velocity == 0.25f);
  343. }
  344. TEST (prscript190_timeout_and_sandbox)
  345. {
  346. auto p = prscript190::pattern();
  347. const auto before = p;
  348. // a script that never ends is stopped
  349. CHECK (prscript190::failedWith (prscript190::runAll ("import flpianoroll as flp\nwhile True:\n pass\n", p, {}, {}, 150.0), "TimeoutError"));
  350. // no files, no programs, no network, no file reader
  351. for (const char* src : { "open('/etc/passwd')\n", "open('lynx-test-file.txt', 'w')\n", "import os\nos.system('echo hi')\n", "import socket\n",
  352. "import subprocess\n", "import urllib\n", "__import__('os')\n",
  353. "import flpianoroll as flp\nflp.score.loadScoreFromFile('x.fsc')\n" })
  354. {
  355. const auto r = prscript190::runAll (src, p);
  356. CHECK (r.outcome.status == lynx::prscript::Outcome::Status::failed);
  357. if (r.outcome.status != lynx::prscript::Outcome::Status::failed)
  358. std::printf (" ran: %s", src);
  359. }
  360. CHECK (! std::filesystem::exists ("lynx-test-file.txt"));
  361. CHECK (p == before);
  362. // and the interpreter is fine afterwards
  363. CHECK (prscript190::done (prscript190::runAll ("import flpianoroll as flp\nflp.score.getNote(0).muted = True\n", p)));
  364. }
  365. TEST (prscript190_lynx_scripts_run)
  366. {
  367. for (const auto& b : lynx::prscript::builtins())
  368. {
  369. auto p = prscript190::pattern();
  370. const auto before = p;
  371. const auto r = prscript190::runAll (b.source, p);
  372. CHECK (prscript190::done (r));
  373. CHECK (! (p == before));
  374. if (p == before)
  375. std::printf (" %s changed nothing\n", b.name);
  376. }
  377. // Octave layers: one copy per note, an octave up
  378. auto p = prscript190::pattern();
  379. CHECK (prscript190::done (prscript190::runAll (lynx::prscript::builtins()[1].source, p)));
  380. CHECK (prscript190::channelIndices (p, 0).size() == 8);
  381. // Mirror in time: the last note's end becomes the first note's start
  382. p = prscript190::pattern();
  383. CHECK (prscript190::done (prscript190::runAll (lynx::prscript::builtins()[2].source, p)));
  384. const auto mine = prscript190::channelIndices (p, 0);
  385. CHECK (p.notes[(std::size_t) mine[0]].key == 72 && p.notes[(std::size_t) mine[0]].start == 0);
  386. }
  387. TEST (prscript190_fl_scripts_run_unmodified)
  388. {
  389. // the scripts FL Studio ships, read from the installation (never copied): each runs to its end with its own
  390. // default form values and does what its name says
  391. if (realFlpDir.empty())
  392. return;
  393. const auto dir = std::filesystem::path (realFlpDir).parent_path() / "System" / "Config" / "Piano roll scripts";
  394. if (! std::filesystem::is_directory (dir))
  395. {
  396. std::printf (" (no Piano roll scripts in this FL installation: skipped)\n");
  397. return;
  398. }
  399. auto read = [&dir] (const char* name) {
  400. std::ifstream in (dir / name, std::ios::binary);
  401. std::stringstream s;
  402. s << in.rdbuf();
  403. return s.str();
  404. };
  405. int ran = 0;
  406. {
  407. const auto src = read ("Euclidean.pyscript");
  408. CHECK (! src.empty());
  409. auto p = prscript190::pattern();
  410. const auto r = prscript190::runAll (src, p);
  411. CHECK (prscript190::done (r) && r.forms == 1);
  412. // 8 + 13 + 18 onsets over 32 steps of a 16th, added to the 4 notes
  413. CHECK (prscript190::channelIndices (p, 0).size() == 4 + 8 + 13 + 18);
  414. ran += prscript190::done (r);
  415. }
  416. {
  417. const auto src = read ("Humanize.pyscript");
  418. auto p = prscript190::pattern();
  419. const auto before = p;
  420. const auto r = prscript190::runAll (src, p, {}, { { "Seed", 3.0 } });
  421. CHECK (prscript190::done (r) && r.forms == 1);
  422. CHECK (p.notes.size() == before.notes.size());
  423. CHECK (! (p == before));
  424. auto q = before; // the same seed gives the same result
  425. CHECK (prscript190::done (prscript190::runAll (src, q, {}, { { "Seed", 3.0 } })));
  426. CHECK (p == q);
  427. ran += prscript190::done (r);
  428. }
  429. {
  430. const auto src = read ("Note repeater sprinkler.pyscript");
  431. auto p = prscript190::pattern();
  432. const auto r = prscript190::runAll (src, p);
  433. CHECK (prscript190::done (r) && r.forms == 1);
  434. CHECK (prscript190::channelIndices (p, 0).size() == 4 * (1 + 3)); // 3 repeats of each note
  435. ran += prscript190::done (r);
  436. }
  437. {
  438. const auto src = read ("Select by velocity and more.pyscript");
  439. auto p = prscript190::pattern();
  440. // durations up to 100 ticks at the script's defaults: make two notes that short
  441. const auto mine = prscript190::channelIndices (p, 0);
  442. p.notes[(std::size_t) mine[0]].length = 50;
  443. p.notes[(std::size_t) mine[3]].length = 80;
  444. const auto r = prscript190::runAll (src, p);
  445. CHECK (prscript190::done (r) && r.forms == 1);
  446. CHECK (r.selected.size() == 2);
  447. ran += prscript190::done (r);
  448. }
  449. {
  450. const auto src = read ("Arpeggiator.pyscript");
  451. auto p = prscript190::pattern();
  452. for (auto& n : p.notes) // a held chord
  453. if (n.channel == 0)
  454. {
  455. n.start = 0;
  456. n.length = 2 * lynx::ppq;
  457. }
  458. const auto r = prscript190::runAll (src, p, {}, { { "Seed", 5.0 }, { "Note Prob", 1.0 } });
  459. CHECK (prscript190::done (r) && r.forms == 1);
  460. // 16th steps over two beats: 8 notes, each a 16th long, walking up the chord
  461. const auto mine = prscript190::channelIndices (p, 0);
  462. CHECK (mine.size() == 8);
  463. for (const int i : mine)
  464. CHECK (p.notes[(std::size_t) i].length == lynx::ppq / 4);
  465. ran += prscript190::done (r);
  466. }
  467. std::printf (" FL Studio's own Piano roll scripts run unmodified: %d of 5\n", ran);
  468. }
  469. TEST (prscript190_run_last_script_key)
  470. {
  471. // FL's key for Tools > Scripts > Run last script again (live capture): Ctrl+Alt+Y in the Piano roll
  472. using namespace lynx::keys;
  473. KeyMap map;
  474. CHECK (map.find ({ 'Y', ctrl | alt }, Context::pianoRoll, false) == "pianoRoll.runLastScript");
  475. CHECK (map.find ({ 'Y', alt }, Context::pianoRoll, false) == "pianoRoll.flip");
  476. CHECK (map.conflicts().empty());
  477. }