ModalShot190mc.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // --modal190mc <folder> (round 190mc): the Chord helper (discussion #105) and Transistor Bass's program row
  2. // (discussion #202), drawn offscreen, each at 1x and at 150 % ("@150").
  3. // 1. The Piano roll with Am F G C (a bar each) and the C selected; beside it the Chord helper in the Pattern layout's
  4. // look, then hovering F minor, the dominant key G chosen, the 7th extension; the same in the Clip-launch layout's
  5. // Live look and in the Simple layout's look; the Tools menu with its item.
  6. // 2. Insert: F minor after the C, voice-led; the helper follows the new chord.
  7. // 3. Transistor Bass from FL's "Factory programs" (needs FL Studio: LYNX_FL_INSTALL): the page with FL's program
  8. // row, another program chosen, the PATCH list (FL's right-click), WRITE lit after a knob edit.
  9. // <folder>/checks.txt lists what was checked.
  10. bool runModalShot190mc (const juce::File& folder)
  11. {
  12. using namespace lynx;
  13. juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  14. folder.createDirectory();
  15. juce::StringArray report;
  16. bool allOk = true;
  17. const auto check = [&report, &allOk] (const char* name, bool ok, const juce::String& detail = {}) {
  18. report.add (juce::String (name) + "=" + (ok ? "OK" : "FAIL") + (detail.isEmpty() ? juce::String() : " " + detail));
  19. allOk = allOk && ok;
  20. };
  21. const auto writeImage = [&] (const juce::Image& image, const juce::File& f, const juce::String& name) {
  22. f.deleteFile();
  23. bool ok = false;
  24. if (auto out = f.createOutputStream())
  25. ok = juce::PNGImageFormat().writeImageToStream (image, *out);
  26. check (name.toRawUTF8(), ok && image.getWidth() > 0);
  27. };
  28. const auto shoot = [&] (juce::Component& c, const juce::String& name) {
  29. writeImage (shot::snapshot (c), shot::file (folder.getChildFile (name + ".png")), name);
  30. if (! shot::given())
  31. writeImage (c.createComponentSnapshot (c.getLocalBounds(), true, 1.5f), folder.getChildFile (name + "@150.png"), name + "@150");
  32. };
  33. // the Piano roll and the helper side by side in one picture (the editor left, the helper's window right)
  34. const auto shootPair = [&] (juce::Component& roll, juce::Component& panel, const juce::String& name) {
  35. for (const auto scale : { 1.0f, 1.5f })
  36. {
  37. const auto w = roll.getWidth() + panel.getWidth() + 12, h = juce::jmax (roll.getHeight(), panel.getHeight());
  38. juce::Image image (juce::Image::ARGB, juce::roundToInt ((float) w * scale), juce::roundToInt ((float) h * scale), true);
  39. {
  40. juce::Graphics g (image);
  41. g.fillAll (juce::Colour (0xff101114));
  42. g.drawImageAt (roll.createComponentSnapshot (roll.getLocalBounds(), true, scale), 0, 0);
  43. g.drawImageAt (panel.createComponentSnapshot (panel.getLocalBounds(), true, scale), juce::roundToInt ((float) (roll.getWidth() + 12) * scale), 0);
  44. }
  45. writeImage (image, folder.getChildFile (name + (scale > 1.0f ? "@150.png" : ".png")), name + (scale > 1.0f ? "@150" : ""));
  46. }
  47. };
  48. const auto menuText = [] (const juce::PopupMenu& m) {
  49. juce::StringArray items;
  50. for (juce::PopupMenu::MenuItemIterator it (m); it.next();)
  51. items.add (it.getItem().isSeparator ? juce::String ("-") : it.getItem().text.upToFirstOccurrenceOf ("\t", false, false));
  52. return items;
  53. };
  54. // --- 1. the Chord helper ----------------------------------------------------------------------------------------
  55. {
  56. AudioEngine engine (false);
  57. auto song = Song::makeDefault();
  58. song.currentPattern = 0;
  59. auto& p = song.patterns.front();
  60. p.notes.clear();
  61. const auto bar = 4 * (std::int64_t) ppq;
  62. const std::vector<std::vector<int>> progression { { 57, 60, 64 }, { 53, 57, 60 }, { 55, 59, 62 }, { 60, 64, 67 } }; // Am F G C
  63. for (std::size_t c = 0; c < progression.size(); ++c)
  64. for (const auto key : progression[c])
  65. {
  66. Note n;
  67. n.channel = 0;
  68. n.start = (std::int64_t) c * bar;
  69. n.length = bar;
  70. n.key = key;
  71. p.notes.push_back (n);
  72. }
  73. p.lengthTicks = 6 * bar;
  74. engine.setSong (std::move (song));
  75. engine.setSelectedChannel (0);
  76. engine.dispatchPendingMessages();
  77. const auto selectLast = [&engine] {
  78. std::vector<int> last;
  79. const auto& notes = engine.getSong().patterns.front().notes;
  80. std::int64_t latest = -1;
  81. for (const auto& n : notes)
  82. latest = std::max (latest, n.start);
  83. for (int i = 0; i < (int) notes.size(); ++i)
  84. if (notes[(std::size_t) i].start == latest)
  85. last.push_back (i);
  86. return last;
  87. };
  88. PianoRollComponent roll (engine);
  89. roll.setBounds (0, 0, 820, 440);
  90. roll.setVisible (true);
  91. engine.sendChangeMessage(); // (the editor learns its pattern and channel, as it has long done in the app)
  92. engine.dispatchPendingMessages();
  93. roll.selectMidiToolResult (selectLast());
  94. roll.scrollToNotes (true);
  95. engine.dispatchPendingMessages();
  96. check ("tools_menu_has_chord_helper", menuText (roll.getToolsMenuForSnapshot()).contains (tr ("Chord helper") + "..."));
  97. const auto target = [&roll, &engine] {
  98. ChordHelperPanel::Target t;
  99. t.pattern = engine.getSong().currentPattern;
  100. t.channel = engine.getSelectedChannel();
  101. bool selected = false;
  102. t.selected = roll.midiToolNotes (selected);
  103. return t;
  104. };
  105. const auto selectInRoll = [&roll] (const std::vector<int>& indices) { roll.selectOwnNewNotes (indices); };
  106. for (const auto look : { ChordHelperPanel::Look::fl, ChordHelperPanel::Look::live, ChordHelperPanel::Look::simple })
  107. {
  108. const auto tag = look == ChordHelperPanel::Look::fl ? juce::String ("pattern") : look == ChordHelperPanel::Look::live ? juce::String ("cliplaunch") : juce::String ("simple");
  109. ChordHelperPanel panel (engine, target, selectInRoll, look);
  110. panel.setVisible (true);
  111. const auto& a = panel.analysis();
  112. if (look == ChordHelperPanel::Look::fl)
  113. {
  114. check ("key_c_ionian_from_context", a.valid && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && a.contextChords == 3);
  115. check ("rank1_parallel_c_minor", ! panel.relatedKeys().empty() && panel.relatedKeys().front().scale.mode == modalhelp::Mode::aeolian);
  116. check ("first_borrowed_f_minor_up_a_4th", ! panel.borrowedChords().empty() && panel.borrowedChords().front().chord.root == 5
  117. && panel.borrowedChords().front().move.up == 5);
  118. check ("no_timer_while_idle", ! panel.previewRunning());
  119. const auto runs = panel.computations();
  120. for (int i = 0; i < 20; ++i) // (nothing changed: nothing is computed again)
  121. panel.refresh();
  122. check ("computes_only_on_change", panel.computations() == runs, juce::String (panel.computations()));
  123. }
  124. shootPair (roll, panel, "modal-" + tag + "-01-open");
  125. shoot (panel, "modal-" + tag + "-02-panel");
  126. panel.hoverChordRowForShot (0);
  127. shoot (panel, "modal-" + tag + "-03-hover-fm");
  128. panel.hoverChordRowForShot (-1);
  129. for (int i = 0; i < (int) panel.relatedKeys().size(); ++i)
  130. if (panel.relatedKeys()[(std::size_t) i].relation == modalhelp::Relation::dominant)
  131. panel.selectKeyRow (i);
  132. if (look == ChordHelperPanel::Look::fl)
  133. check ("dominant_key_lends_v_of_v", ! panel.borrowedChords().empty() && panel.borrowedChords().front().numeral == "V/V");
  134. shoot (panel, "modal-" + tag + "-04-dominant-key");
  135. panel.selectKeyRow (0);
  136. panel.setExtension (modalhelp::Extension::seventh);
  137. shoot (panel, "modal-" + tag + "-05-sevenths");
  138. panel.setExtension (modalhelp::Extension::triad);
  139. }
  140. // --- 2. insert F minor after the C ------------------------------------------------------------------------
  141. {
  142. ChordHelperPanel panel (engine, target, selectInRoll, ChordHelperPanel::Look::fl);
  143. const auto before = engine.getSong().patterns.front().notes.size();
  144. check ("insert_fm", panel.insertChordRow (0));
  145. engine.dispatchPendingMessages();
  146. const auto& notes = engine.getSong().patterns.front().notes;
  147. std::vector<int> keys;
  148. for (const auto& n : notes)
  149. if (n.start == 4 * bar)
  150. keys.push_back (n.key);
  151. std::sort (keys.begin(), keys.end());
  152. check ("inserted_voice_led_c_f_ab", notes.size() == before + 3 && keys == std::vector<int> { 60, 65, 68 });
  153. bool selected = false;
  154. check ("new_chord_selected", roll.midiToolNotes (selected).size() == 3 && selected);
  155. check ("helper_follows_new_chord", panel.analysis().chord.root == 5 && panel.analysis().quality == modalhelp::Quality::minor);
  156. roll.scrollToNotes (true);
  157. shootPair (roll, panel, "modal-pattern-06-inserted");
  158. }
  159. }
  160. // --- 3. Transistor Bass's program row --------------------------------------------------------------------------
  161. {
  162. AudioEngine engine (false);
  163. auto song = Song::makeDefault();
  164. const auto& fl = flStudioAccess();
  165. auto tb = natives::makeChannel (InstrumentKind::transistorbass, fl);
  166. check ("tb_channel_needs_fl", tb.has_value(), tb ? juce::String() : juce::String ("no FL Studio: set LYNX_FL_INSTALL"));
  167. if (tb)
  168. {
  169. song.channels.push_back (*tb);
  170. const auto index = (int) song.channels.size() - 1;
  171. engine.setSong (std::move (song));
  172. engine.dispatchPendingMessages();
  173. auto window = makeChannelWindowForShot (engine, index, 0);
  174. window->setVisible (true);
  175. engine.dispatchPendingMessages();
  176. shoot (*window, "tb-01-factory-programs");
  177. const auto value = [&engine, index] (int param) {
  178. return modelparams::rawValue (engine.getSong().channels[(std::size_t) index], param).value_or (-1.0f);
  179. };
  180. check ("tb_program_65_shown", value (tbprog::program) == 65.0f);
  181. {
  182. // round 190nr (discussion #202): the page's "Copy as MIDI" / "Export MIDI..." on FL's factory program
  183. std::int64_t length = 0;
  184. const auto notes = transistorBassSequenceNotes (engine.getSong().channels[(std::size_t) index].pluginState, engine.getSong().bpm, &length);
  185. juce::String keys;
  186. for (const auto& n : notes)
  187. keys << n.key << "@" << (int) n.start << "/" << (int) n.length << (n.velocity >= 0.99f ? "A" : "") << " ";
  188. check ("tb_sequence_as_notes_190nr", ! notes.empty() && length > 0, keys.trim());
  189. }
  190. // another program, as the PROGRAM wheel does
  191. engine.editSong ([index] (Song& s) { modelparams::setRawValue (s.channels[(std::size_t) index], tbprog::program, 36.0f); });
  192. engine.dispatchPendingMessages();
  193. window = makeChannelWindowForShot (engine, index, 0);
  194. window->setVisible (true);
  195. shoot (*window, "tb-02-program-c3");
  196. check ("tb_program_36_patch_36", value (tbprog::program) == 36.0f && value (tbprog::patch) == 36.0f);
  197. // a knob edit: WRITE lights
  198. engine.editSong ([index] (Song& s) { modelparams::setRawValue (s.channels[(std::size_t) index], 2, 0.9f); });
  199. engine.dispatchPendingMessages();
  200. window->repaint();
  201. shoot (*window, "tb-03-write-lit");
  202. check ("tb_write_lit_after_edit", transistorBassBank (engine.getSong().channels[(std::size_t) index].pluginState)->edited);
  203. // the PATCH list (FL's right-click on the number)
  204. if (const auto bank = transistorBassBank (engine.getSong().channels[(std::size_t) index].pluginState))
  205. {
  206. juce::PopupMenu list, more;
  207. for (int i = 0; i < 128; ++i)
  208. (i < 64 ? list : more).addItem (juce::String (i + 1) + " " + (bank->patches[(std::size_t) i].empty() ? tr ("(no name)") : juce::String::fromUTF8 (bank->patches[(std::size_t) i].c_str())),
  209. true, i == bank->patch, [] {});
  210. list.addSeparator();
  211. list.addSubMenu ("65 - 128", more);
  212. saveMenus (juce::StringArray { "Patch" }, std::vector<juce::PopupMenu> { list }, shot::fileName (folder.getChildFile ("tb-04-patch-list.png").getFullPathName()));
  213. check ("tb_patch_names", ! bank->patches[0].empty());
  214. }
  215. }
  216. }
  217. folder.getChildFile ("checks.txt").replaceWithText (report.joinIntoString ("\n") + "\n");
  218. juce::LookAndFeel::setDefaultLookAndFeel (nullptr);
  219. return allOk;
  220. }