VfxEditors.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #include "ui/VfxEditors.h"
  2. #include <algorithm>
  3. #include <array>
  4. #include "core/Vfx.h"
  5. #include "engine/Text.h"
  6. #include "ui/I18n.h"
  7. #include "ui/Theme.h"
  8. #include "ui/ToolWindow.h"
  9. #include "ui/ValueControl.h"
  10. namespace lynx
  11. {
  12. namespace
  13. {
  14. juce::String keyName (int key)
  15. {
  16. return juce::MidiMessage::getMidiNoteName (std::clamp (key, 0, 127), true, true, 4);
  17. }
  18. juce::String pitchClassName (int pc)
  19. {
  20. static const char* names[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  21. return names[((pc % 12) + 12) % 12];
  22. }
  23. // A small knob with LYNX's control contract (drag, Ctrl = fine, wheel, Alt / middle-click / double-click reset,
  24. // right-click: type a value / reset)
  25. class Knob final : public juce::Component
  26. {
  27. public:
  28. Knob (ui::ValueSpec s, std::function<double()> get, std::function<void (double)> set)
  29. : spec (std::move (s)), getter (std::move (get)), setter (std::move (set))
  30. {
  31. setRepaintsOnMouseActivity (true);
  32. ui::setHint (*this, spec.name, true, false);
  33. }
  34. void paint (juce::Graphics& g) override
  35. {
  36. const auto value = getter();
  37. const auto v = juce::jlimit (0.0, 1.0, (value - spec.minimum) / juce::jmax (1e-9, spec.maximum - spec.minimum));
  38. auto area = getLocalBounds().toFloat();
  39. const auto label = area.removeFromBottom (14.0f);
  40. const auto valueArea = area.removeFromBottom (13.0f);
  41. const auto size = juce::jmin (area.getWidth(), area.getHeight()) - 4.0f;
  42. const auto box = juce::Rectangle<float> (size, size).withCentre (area.getCentre());
  43. constexpr float sweep = juce::MathConstants<float>::pi * 1.5f;
  44. g.setColour (theme::colour::bg4);
  45. g.fillEllipse (box.reduced (4.0f));
  46. juce::Path track, arc;
  47. const auto radius = size * 0.5f - 1.5f;
  48. track.addCentredArc (box.getCentreX(), box.getCentreY(), radius, radius, 0.0f, -sweep * 0.5f, sweep * 0.5f, true);
  49. g.setColour (theme::colour::knobTrack);
  50. g.strokePath (track, juce::PathStrokeType (3.0f));
  51. // bipolar knobs light from the centre
  52. const auto bipolar = spec.minimum < 0.0 && spec.maximum > 0.0;
  53. const auto from = bipolar ? 0.5 : 0.0;
  54. arc.addCentredArc (box.getCentreX(), box.getCentreY(), radius, radius, 0.0f, -sweep * 0.5f + sweep * (float) std::min (from, v),
  55. -sweep * 0.5f + sweep * (float) std::max (from, v), true);
  56. g.setColour (isMouseOverOrDragging() ? theme::colour::accent : theme::colour::knobArc);
  57. g.strokePath (arc, juce::PathStrokeType (3.0f));
  58. const auto angle = -sweep * 0.5f + sweep * (float) v;
  59. const auto tip = box.getCentre() + juce::Point<float> (std::sin (angle), -std::cos (angle)) * (radius - 5.0f);
  60. g.drawLine (box.getCentreX(), box.getCentreY(), tip.x, tip.y, 2.0f);
  61. g.setColour (theme::colour::textPrimary);
  62. g.setFont (theme::font (theme::type::caption));
  63. g.drawText (spec.text (value), valueArea, juce::Justification::centred);
  64. g.setColour (theme::colour::textSecondary);
  65. g.drawText (caption, label, juce::Justification::centred);
  66. }
  67. void mouseDown (const juce::MouseEvent& e) override
  68. {
  69. if (e.mods.isPopupMenu())
  70. {
  71. ui::showValueMenu (*this, nullptr, spec, getter(), [this] (double v) { change (v); });
  72. return;
  73. }
  74. if (ui::isResetClick (e))
  75. {
  76. change (spec.defaultValue);
  77. return;
  78. }
  79. drag.begin (e, getter());
  80. }
  81. void mouseDrag (const juce::MouseEvent& e) override
  82. {
  83. if (drag.isActive())
  84. change (drag.update (e, spec));
  85. }
  86. void mouseUp (const juce::MouseEvent&) override { drag.end(); }
  87. void mouseDoubleClick (const juce::MouseEvent&) override { change (spec.defaultValue); }
  88. void mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& w) override
  89. {
  90. if (const auto v = ui::wheelValue (getter(), e, w, spec))
  91. change (*v);
  92. }
  93. juce::String caption;
  94. private:
  95. void change (double v)
  96. {
  97. v = juce::jlimit (spec.minimum, spec.maximum, v);
  98. if (spec.interval > 0.0)
  99. v = spec.minimum + std::round ((v - spec.minimum) / spec.interval) * spec.interval;
  100. setter (v);
  101. repaint();
  102. }
  103. ui::ValueSpec spec;
  104. std::function<double()> getter;
  105. std::function<void (double)> setter;
  106. ui::ValueDrag drag;
  107. };
  108. ui::ValueSpec intSpec (const juce::String& name, int lo, int hi, int def, std::function<juce::String (double)> text = nullptr)
  109. {
  110. ui::ValueSpec s;
  111. s.name = name;
  112. s.minimum = lo;
  113. s.maximum = hi;
  114. s.defaultValue = def;
  115. s.interval = 1.0;
  116. s.pixelsForRange = std::clamp ((double) (hi - lo) * 6.0, 120.0, 300.0);
  117. if (text)
  118. {
  119. s.toText = text;
  120. s.fromText = ui::inverseOf (text, lo, hi);
  121. }
  122. else
  123. s.toText = [] (double v) { return juce::String ((int) std::lround (v)); };
  124. return s;
  125. }
  126. // What every body gets: the plugin's FL state and a way to change it
  127. class Body : public juce::Component
  128. {
  129. public:
  130. using Edit = std::function<void (std::vector<std::uint8_t>&)>;
  131. std::function<void (Edit)> edit;
  132. virtual void refresh (const std::vector<std::uint8_t>& fl) = 0;
  133. virtual int preferredHeight() const { return 300; }
  134. virtual int preferredWidth() const { return 700; }
  135. };
  136. // ---------------------------------------------------------------------------------------------------------------
  137. // VFX Key Mapper: inputs (one octave) down, outputs across; the controls above
  138. class KeyMapperBody final : public Body
  139. {
  140. public:
  141. static constexpr int lowest = -12, columns = 36; // outputs: an octave below the row's octave to an octave above (FL's range)
  142. KeyMapperBody()
  143. {
  144. enable.setButtonText (tr ("Enable"));
  145. enable.onClick = [this] { change ([on = enable.getToggleState()] (vfx::KeyMapperSettings& s) { s.enabled = on; }); };
  146. addAndMakeVisible (enable);
  147. for (const auto& [id, text] : std::vector<std::pair<int, juce::String>> {
  148. { 1, tr ("All") }, { 2, tr ("All (gain-compensated)") }, { 3, tr ("Random") }, { 4, tr ("Cycle up") }, { 5, tr ("Cycle down") } })
  149. multimode.addItem (text, id);
  150. multimode.onChange = [this] { change ([m = multimode.getSelectedId() - 1] (vfx::KeyMapperSettings& s) { s.multimode = std::max (0, m); }); };
  151. ui::setHint (multimode, tr ("Multimode: how an input mapped to several notes plays them"));
  152. addAndMakeVisible (multimode);
  153. multimodeLabel.setText (tr ("Multimode"), juce::dontSendNotification);
  154. multimodeLabel.setFont (theme::font (theme::type::small));
  155. addAndMakeVisible (multimodeLabel);
  156. offset.caption = tr ("Offset");
  157. baseKey.caption = tr ("Base key");
  158. slide.caption = tr ("Pitch slide");
  159. addAndMakeVisible (offset);
  160. addAndMakeVisible (baseKey);
  161. addAndMakeVisible (slide);
  162. }
  163. void refresh (const std::vector<std::uint8_t>& fl) override
  164. {
  165. settings = vfx::parseKeyMapper (fl).value_or (vfx::defaultKeyMapper());
  166. enable.setToggleState (settings.enabled, juce::dontSendNotification);
  167. multimode.setSelectedId (std::clamp (settings.multimode, 0, 4) + 1, juce::dontSendNotification);
  168. repaint();
  169. offset.repaint();
  170. baseKey.repaint();
  171. slide.repaint();
  172. }
  173. int preferredHeight() const override { return 64 + 12 * 22 + 30; }
  174. int preferredWidth() const override { return 64 + columns * 15 + 16; }
  175. void resized() override
  176. {
  177. auto top = getLocalBounds().reduced (8, 4).removeFromTop (60);
  178. enable.setBounds (top.removeFromLeft (90).withSizeKeepingCentre (90, 24));
  179. top.removeFromLeft (8);
  180. offset.setBounds (top.removeFromLeft (64));
  181. baseKey.setBounds (top.removeFromLeft (64));
  182. slide.setBounds (top.removeFromLeft (64));
  183. top.removeFromLeft (16);
  184. auto mm = top.removeFromLeft (200);
  185. multimodeLabel.setBounds (mm.removeFromTop (22));
  186. multimode.setBounds (mm.removeFromTop (24));
  187. }
  188. void paint (juce::Graphics& g) override
  189. {
  190. const auto grid = gridArea();
  191. g.setColour (theme::colour::bg0);
  192. g.fillRect (grid);
  193. const auto cw = grid.getWidth() / (float) columns, rh = grid.getHeight() / 12.0f;
  194. // output columns: the octaves and the black keys, labelled every C
  195. for (int c = 0; c < columns; ++c)
  196. {
  197. const auto value = lowest + c;
  198. const auto pc = ((value % 12) + 12) % 12;
  199. const auto black = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
  200. const juce::Rectangle<float> col (grid.getX() + (float) c * cw, grid.getY(), cw, grid.getHeight());
  201. if (value >= 0 && value < 12) // the row's own octave, brighter (as FL draws it)
  202. {
  203. g.setColour (theme::colour::bg2);
  204. g.fillRect (col);
  205. }
  206. if (black)
  207. {
  208. g.setColour (theme::colour::bg0.withAlpha (0.55f));
  209. g.fillRect (col);
  210. }
  211. g.setColour (pc == 0 ? theme::colour::borderStrong : theme::colour::border);
  212. g.drawVerticalLine ((int) col.getX(), grid.getY(), grid.getBottom());
  213. if (pc == 0)
  214. {
  215. g.setColour (theme::colour::textSecondary);
  216. g.setFont (theme::font (theme::type::caption));
  217. const auto octave = value / 12;
  218. const auto name = pitchClassName (settings.baseKey);
  219. g.drawText (octave == 0 ? name : (octave > 0 ? name + " +" + juce::String (octave) : name + " " + juce::String (octave)),
  220. juce::Rectangle<float> (col.getX() + 2.0f, grid.getBottom() + 2.0f, 40.0f, 14.0f), juce::Justification::centredLeft);
  221. }
  222. }
  223. // input rows (top = B, bottom = C, as a keyboard on its side)
  224. for (int r = 0; r < 12; ++r)
  225. {
  226. const auto pc = 11 - r;
  227. const juce::Rectangle<float> row (grid.getX(), grid.getY() + (float) r * rh, grid.getWidth(), rh);
  228. g.setColour (theme::colour::border);
  229. g.drawHorizontalLine ((int) row.getY(), grid.getX(), grid.getRight());
  230. const auto black = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
  231. const juce::Rectangle<float> label (4.0f, row.getY(), grid.getX() - 8.0f, rh);
  232. g.setColour (black ? theme::colour::bg4 : theme::colour::bg6);
  233. g.fillRoundedRectangle (label.reduced (1.0f, 1.5f), 2.0f);
  234. g.setColour (theme::colour::textPrimary);
  235. g.setFont (theme::font (theme::type::small));
  236. g.drawText (pitchClassName (pc + settings.baseKey), label.reduced (6.0f, 0.0f), juce::Justification::centredLeft);
  237. // the unchanged note (a faint diagonal)
  238. g.setColour (theme::colour::bg5.withAlpha (0.55f));
  239. g.fillRect (cell (pc, pc).reduced (cw * 0.35f, rh * 0.35f));
  240. for (const auto value : settings.rows[(std::size_t) pc])
  241. {
  242. const auto c = cell (pc, value);
  243. if (c.isEmpty())
  244. continue;
  245. g.setColour (settings.enabled ? theme::colour::accent : theme::colour::textDisabled);
  246. g.fillRoundedRectangle (c.reduced (1.5f), 2.5f);
  247. }
  248. }
  249. g.setColour (theme::colour::borderStrong);
  250. g.drawRect (grid);
  251. if (hover.has_value())
  252. {
  253. g.setColour (theme::colour::textPrimary.withAlpha (0.5f));
  254. g.drawRect (cell (hover->first, hover->second), 1.0f);
  255. }
  256. }
  257. void mouseMove (const juce::MouseEvent& e) override
  258. {
  259. auto now = cellAt (e.position);
  260. if (now != hover)
  261. {
  262. hover = now;
  263. if (hover)
  264. ui::setHint (*this, tr ("{0} plays {1} - left-click: add / remove, right-click: only this note, middle-click or Alt+click: reset the row",
  265. { pitchClassName (hover->first + settings.baseKey), outputName (hover->first, hover->second) }));
  266. repaint();
  267. }
  268. }
  269. void mouseExit (const juce::MouseEvent&) override
  270. {
  271. hover.reset();
  272. repaint();
  273. }
  274. void mouseDown (const juce::MouseEvent& e) override
  275. {
  276. if (const auto grid = gridArea(); e.position.x < grid.getX() && e.position.y >= grid.getY() && e.position.y < grid.getBottom())
  277. {
  278. // a click on a row's label: back to the note itself
  279. const auto pc = 11 - std::clamp ((int) ((e.position.y - grid.getY()) / (grid.getHeight() / 12.0f)), 0, 11);
  280. change ([pc] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { pc }; });
  281. return;
  282. }
  283. const auto at = cellAt (e.position);
  284. if (! at)
  285. return;
  286. const auto [pc, value] = *at;
  287. if (e.mods.isMiddleButtonDown() || e.mods.isAltDown())
  288. change ([pc] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { pc }; });
  289. else if (e.mods.isPopupMenu())
  290. change ([pc, value] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { value }; });
  291. else
  292. change ([pc, value] (vfx::KeyMapperSettings& s) {
  293. auto& row = s.rows[(std::size_t) pc];
  294. if (auto it = std::find (row.begin(), row.end(), value); it != row.end())
  295. row.erase (it);
  296. else
  297. row.push_back (value); // (FL keeps the order: Cycle and Random follow it)
  298. });
  299. }
  300. private:
  301. juce::Rectangle<float> gridArea() const
  302. {
  303. return getLocalBounds().toFloat().withTrimmedTop (68.0f).withTrimmedLeft (56.0f).withTrimmedRight (8.0f).withTrimmedBottom (20.0f);
  304. }
  305. juce::Rectangle<float> cell (int pc, int value) const
  306. {
  307. const auto grid = gridArea();
  308. const auto c = value - lowest;
  309. if (c < 0 || c >= columns)
  310. return {};
  311. const auto cw = grid.getWidth() / (float) columns, rh = grid.getHeight() / 12.0f;
  312. return { grid.getX() + (float) c * cw, grid.getY() + (float) (11 - pc) * rh, cw, rh };
  313. }
  314. std::optional<std::pair<int, int>> cellAt (juce::Point<float> p) const
  315. {
  316. const auto grid = gridArea();
  317. if (! grid.contains (p))
  318. return std::nullopt;
  319. const auto c = (int) ((p.x - grid.getX()) / (grid.getWidth() / (float) columns));
  320. const auto r = (int) ((p.y - grid.getY()) / (grid.getHeight() / 12.0f));
  321. return std::make_pair (11 - std::clamp (r, 0, 11), lowest + std::clamp (c, 0, columns - 1));
  322. }
  323. juce::String outputName (int pc, int value) const
  324. {
  325. const auto shift = value - pc;
  326. return pitchClassName (value + settings.baseKey) + " (" + (shift > 0 ? "+" : "") + juce::String (shift) + ")";
  327. }
  328. void change (std::function<void (vfx::KeyMapperSettings&)> f)
  329. {
  330. if (edit)
  331. edit ([f] (std::vector<std::uint8_t>& fl) {
  332. auto s = vfx::parseKeyMapper (fl).value_or (vfx::defaultKeyMapper());
  333. f (s);
  334. fl = vfx::writeKeyMapper (s);
  335. });
  336. }
  337. vfx::KeyMapperSettings settings = vfx::defaultKeyMapper();
  338. juce::ToggleButton enable;
  339. juce::ComboBox multimode;
  340. juce::Label multimodeLabel;
  341. Knob offset { intSpec (tr ("Offset (transpose)"), -12, 12, 0, [] (double v) { return (v > 0 ? "+" : "") + juce::String ((int) std::lround (v)); }),
  342. [this] { return (double) settings.offset; },
  343. [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.offset = (int) std::lround (v); }); } };
  344. Knob baseKey { intSpec (tr ("Base key: the note the rows and their octaves start on"), 0, 11, 0, [] (double v) { return pitchClassName ((int) std::lround (v)); }),
  345. [this] { return (double) settings.baseKey; },
  346. [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.baseKey = (int) std::lround (v); }); } };
  347. Knob slide { intSpec (tr ("Pitch slide: the new notes start at the played pitch and glide to their own (in steps)"), 0, 192, 0,
  348. [] (double v) { const auto u = (int) std::lround (v); return juce::String (u / 48) + ":" + juce::String (u % 48).paddedLeft ('0', 2); }),
  349. [this] { return (double) settings.slide; },
  350. [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.slide = (int) std::lround (v); }); } };
  351. std::optional<std::pair<int, int>> hover;
  352. };
  353. // ---------------------------------------------------------------------------------------------------------------
  354. // VFX Color Mapper: per note colour, the voice output it goes to and the colour it leaves with
  355. class ColorMapperBody final : public Body
  356. {
  357. public:
  358. ColorMapperBody()
  359. {
  360. for (int c = 0; c < 16; ++c)
  361. {
  362. auto& port = ports[(std::size_t) c];
  363. auto& colour = colours[(std::size_t) c];
  364. for (int k = 0; k < 16; ++k)
  365. {
  366. port.addItem (tr ("Voice {0}", { juce::String (k + 1) }), k + 1);
  367. colour.addItem (tr ("Colour {0}", { juce::String (k + 1) }), k + 1);
  368. }
  369. port.onChange = [this, c] { change ([c, v = ports[(std::size_t) c].getSelectedId() - 1] (vfx::ColorMapperSettings& s) { s.port[(std::size_t) c] = std::max (0, v); }); };
  370. colour.onChange = [this, c] { change ([c, v = colours[(std::size_t) c].getSelectedId() - 1] (vfx::ColorMapperSettings& s) { s.colour[(std::size_t) c] = std::max (0, v); }); };
  371. ui::setHint (port, tr ("The voice output the notes of colour {0} go to", { juce::String (c + 1) }));
  372. ui::setHint (colour, tr ("The colour the notes of colour {0} leave with", { juce::String (c + 1) }));
  373. addAndMakeVisible (port);
  374. addAndMakeVisible (colour);
  375. }
  376. }
  377. void refresh (const std::vector<std::uint8_t>& fl) override
  378. {
  379. settings = vfx::parseColorMapper (fl).value_or (vfx::defaultColorMapper());
  380. for (int c = 0; c < 16; ++c)
  381. {
  382. ports[(std::size_t) c].setSelectedId (settings.port[(std::size_t) c] + 1, juce::dontSendNotification);
  383. colours[(std::size_t) c].setSelectedId (settings.colour[(std::size_t) c] + 1, juce::dontSendNotification);
  384. }
  385. repaint();
  386. }
  387. int preferredHeight() const override { return 28 + 8 * 28 + 8; }
  388. void resized() override
  389. {
  390. auto area = getLocalBounds().reduced (10, 4).withTrimmedTop (24);
  391. const auto half = area.getWidth() / 2;
  392. for (int c = 0; c < 16; ++c)
  393. {
  394. auto column = c < 8 ? area.withWidth (half - 10) : area.withTrimmedLeft (half + 10);
  395. auto row = column.withHeight (26).translated (0, (c % 8) * 28);
  396. row.removeFromLeft (70);
  397. ports[(std::size_t) c].setBounds (row.removeFromLeft ((row.getWidth() - 8) / 2));
  398. row.removeFromLeft (8);
  399. colours[(std::size_t) c].setBounds (row);
  400. }
  401. }
  402. void paint (juce::Graphics& g) override
  403. {
  404. auto area = getLocalBounds().reduced (10, 4);
  405. const auto half = area.getWidth() / 2;
  406. g.setFont (theme::font (theme::type::caption));
  407. g.setColour (theme::colour::textSecondary);
  408. for (const auto x : { area.getX(), area.getX() + half + 10 })
  409. {
  410. const auto w = half - 10 - 70;
  411. g.drawText (theme::caps (tr ("Note colour")), x, area.getY(), 70, 18, juce::Justification::centredLeft);
  412. g.drawText (theme::caps (tr ("Voice output")), x + 70, area.getY(), (w - 8) / 2, 18, juce::Justification::centredLeft);
  413. g.drawText (theme::caps (tr ("Colour")), x + 70 + (w - 8) / 2 + 8, area.getY(), (w - 8) / 2, 18, juce::Justification::centredLeft);
  414. }
  415. area.removeFromTop (24);
  416. for (int c = 0; c < 16; ++c)
  417. {
  418. auto column = c < 8 ? area.withWidth (half - 10) : area.withTrimmedLeft (half + 10);
  419. auto row = column.withHeight (26).translated (0, (c % 8) * 28).removeFromLeft (64).toFloat();
  420. g.setColour (theme::noteGroupHue (c));
  421. g.fillRoundedRectangle (row.removeFromLeft (26.0f).reduced (3.0f), 3.0f);
  422. g.setColour (theme::colour::textPrimary);
  423. g.setFont (theme::font (theme::type::small));
  424. g.drawText (juce::String (c + 1), row.reduced (4.0f, 0.0f), juce::Justification::centredLeft);
  425. }
  426. }
  427. private:
  428. void change (std::function<void (vfx::ColorMapperSettings&)> f)
  429. {
  430. if (edit)
  431. edit ([f] (std::vector<std::uint8_t>& fl) {
  432. auto s = vfx::parseColorMapper (fl).value_or (vfx::defaultColorMapper());
  433. f (s);
  434. fl = vfx::writeColorMapper (s);
  435. });
  436. }
  437. vfx::ColorMapperSettings settings = vfx::defaultColorMapper();
  438. std::array<juce::ComboBox, 16> ports, colours;
  439. };
  440. // ---------------------------------------------------------------------------------------------------------------
  441. // VFX Level Scaler: a tab per voice property, its curve's controls on the left, the curve on the right
  442. class LevelScalerBody final : public Body
  443. {
  444. public:
  445. using L = vfx::LevelScalerSettings;
  446. LevelScalerBody()
  447. {
  448. static const char* names[] = { TR_NOOP ("Velocity"), TR_NOOP ("Release"), TR_NOOP ("Fine pitch"), TR_NOOP ("Pan"), TR_NOOP ("Mod X"), TR_NOOP ("Mod Y") };
  449. for (int t = 0; t < L::tabCount; ++t)
  450. {
  451. auto& b = tabs[(std::size_t) t];
  452. b.setButtonText (tr (names[t]));
  453. b.setClickingTogglesState (false);
  454. b.setColour (juce::TextButton::buttonOnColourId, theme::colour::accentMuted);
  455. b.onClick = [this, t] { change ([t] (L& st) { st.selectedTab = t; }); };
  456. addAndMakeVisible (b);
  457. }
  458. auto knob = [this] (Knob& k, const juce::String& caption) {
  459. k.caption = caption;
  460. addAndMakeVisible (k);
  461. };
  462. knob (center, tr ("Center"));
  463. knob (multiply, tr ("Multiply"));
  464. knob (offset, tr ("Offset"));
  465. knob (tension, tr ("Tension"));
  466. knob (humanize, tr ("Humanize"));
  467. mode.addItem (tr ("Subtract"), 1);
  468. mode.addItem (tr ("Spread"), 2);
  469. mode.addItem (tr ("Add"), 3);
  470. mode.onChange = [this] { set (L::humanizeMode, mode.getSelectedId() - 1); };
  471. ui::setHint (mode, tr ("Humanize mode: the random part goes below the value, around it or above it"));
  472. addAndMakeVisible (mode);
  473. reset.setButtonText (tr ("Reset"));
  474. ui::setHint (reset, tr ("Reset all controls of this property"));
  475. reset.onClick = [this] { change ([] (L& st) { vfx::resetLevelScalerTab (st, st.selectedTab); }); };
  476. addAndMakeVisible (reset);
  477. }
  478. void refresh (const std::vector<std::uint8_t>& fl) override
  479. {
  480. settings = vfx::parseLevelScaler (fl).value_or (vfx::defaultLevelScaler());
  481. for (int t = 0; t < L::tabCount; ++t)
  482. tabs[(std::size_t) t].setToggleState (t == settings.selectedTab, juce::dontSendNotification);
  483. mode.setSelectedId (std::clamp (settings.at (settings.selectedTab, L::humanizeMode), 0, 2) + 1, juce::dontSendNotification);
  484. for (auto* k : { &center, &multiply, &offset, &tension, &humanize })
  485. k->repaint();
  486. repaint();
  487. }
  488. int preferredHeight() const override { return 360; }
  489. int preferredWidth() const override { return 700; }
  490. void resized() override
  491. {
  492. auto area = getLocalBounds().reduced (12, 6);
  493. auto top = area.removeFromTop (28);
  494. for (auto& b : tabs)
  495. b.setBounds (top.removeFromLeft (92).reduced (2, 0));
  496. area.removeFromTop (8);
  497. auto left = area.removeFromLeft (230);
  498. auto row1 = left.removeFromTop (92);
  499. for (auto* k : { &center, &multiply, &offset })
  500. k->setBounds (row1.removeFromLeft (76));
  501. auto row2 = left.removeFromTop (92);
  502. for (auto* k : { &tension, &humanize })
  503. k->setBounds (row2.removeFromLeft (76));
  504. auto side = row2.reduced (2, 14);
  505. mode.setBounds (side.removeFromTop (26));
  506. left.removeFromTop (8);
  507. reset.setBounds (left.removeFromTop (26).withWidth (100));
  508. graph = area.reduced (10, 0).toFloat();
  509. }
  510. void paint (juce::Graphics& g) override
  511. {
  512. if (graph.isEmpty())
  513. return;
  514. const auto tab = settings.selectedTab;
  515. const auto bipolar = tab >= L::finePitch;
  516. const auto lo = bipolar ? -1.0 : 0.0, hi = 1.0;
  517. g.setColour (theme::colour::bg0);
  518. g.fillRoundedRectangle (graph, 4.0f);
  519. g.setColour (theme::colour::border);
  520. for (int k = 1; k < 8; ++k)
  521. {
  522. g.drawVerticalLine ((int) (graph.getX() + graph.getWidth() * (float) k / 8.0f), graph.getY(), graph.getBottom());
  523. g.drawHorizontalLine ((int) (graph.getY() + graph.getHeight() * (float) k / 8.0f), graph.getX(), graph.getRight());
  524. }
  525. g.setColour (theme::colour::textDisabled);
  526. g.setFont (theme::font (theme::type::title, true));
  527. g.drawText (tabs[(std::size_t) tab].getButtonText(), graph.reduced (12.0f, 8.0f), juce::Justification::topLeft);
  528. auto point = [&] (double u, double y) {
  529. return juce::Point<float> (graph.getX() + (float) u * graph.getWidth(), graph.getBottom() - (float) ((y - lo) / (hi - lo)) * graph.getHeight());
  530. };
  531. g.setColour (theme::colour::borderStrong);
  532. g.drawLine (juce::Line<float> (point (0.0, lo), point (1.0, hi)), 1.0f);
  533. juce::Path curve, upper, lower;
  534. constexpr int steps = 160;
  535. for (int k = 0; k <= steps; ++k)
  536. {
  537. const auto u = (double) k / steps;
  538. const auto x = lo + u * (hi - lo);
  539. const auto p = point (u, vfx::levelScale (settings, tab, x, lo, hi, 0.5));
  540. const auto a = point (u, vfx::levelScale (settings, tab, x, lo, hi, 0.0));
  541. const auto b = point (u, vfx::levelScale (settings, tab, x, lo, hi, 1.0));
  542. if (k == 0)
  543. {
  544. curve.startNewSubPath (p);
  545. lower.startNewSubPath (a);
  546. upper.startNewSubPath (b);
  547. }
  548. else
  549. {
  550. curve.lineTo (p);
  551. lower.lineTo (a);
  552. upper.lineTo (b);
  553. }
  554. }
  555. if (settings.at (tab, L::humanize) > 0)
  556. {
  557. juce::Path band (upper);
  558. for (int k = steps; k >= 0; --k)
  559. {
  560. const auto u = (double) k / steps;
  561. band.lineTo (point (u, vfx::levelScale (settings, tab, lo + u * (hi - lo), lo, hi, 0.0)));
  562. }
  563. band.closeSubPath();
  564. g.setColour (theme::colour::accent.withAlpha (0.16f));
  565. g.fillPath (band);
  566. g.setColour (theme::colour::accent.withAlpha (0.45f));
  567. g.strokePath (upper, juce::PathStrokeType (1.0f));
  568. g.strokePath (lower, juce::PathStrokeType (1.0f));
  569. }
  570. g.setColour (theme::colour::accent);
  571. g.strokePath (curve, juce::PathStrokeType (2.2f));
  572. g.setColour (theme::colour::borderStrong);
  573. g.drawRoundedRectangle (graph, 4.0f, 1.0f);
  574. g.setColour (theme::colour::textSecondary);
  575. g.setFont (theme::font (theme::type::caption));
  576. g.drawText (tr ("in"), graph.withTrimmedTop (graph.getHeight() - 16.0f).reduced (6.0f, 0.0f), juce::Justification::centredRight);
  577. g.drawText (tr ("out"), graph.reduced (6.0f, 4.0f).withHeight (14.0f), juce::Justification::centredRight);
  578. }
  579. private:
  580. void change (std::function<void (L&)> f)
  581. {
  582. if (edit)
  583. edit ([f] (std::vector<std::uint8_t>& fl) {
  584. auto s = vfx::parseLevelScaler (fl).value_or (vfx::defaultLevelScaler());
  585. f (s);
  586. fl = vfx::writeLevelScaler (s);
  587. });
  588. }
  589. void set (int field, double v)
  590. {
  591. change ([field, v] (L& st) { st.at (st.selectedTab, field) = (int) std::lround (v); });
  592. }
  593. double get (int field) const { return settings.at (settings.selectedTab, field); }
  594. static juce::String percent (double v) { return juce::String (v * 100.0, 0) + "%"; }
  595. L settings = vfx::defaultLevelScaler();
  596. std::array<juce::TextButton, L::tabCount> tabs;
  597. Knob center { intSpec (tr ("Center: the input level the curve turns around"), 0, 8192, 0, [] (double v) { return percent (v / 8192.0); }),
  598. [this] { return get (L::center); }, [this] (double v) { set (L::center, v); } };
  599. Knob multiply { intSpec (tr ("Multiply: the slope around the center (negative inverts)"), -16384, 16384, 8192,
  600. [] (double v) { return juce::String::charToString (0xd7) + juce::String (v / 8192.0, 2); }),
  601. [this] { return get (L::multiply); }, [this] (double v) { set (L::multiply, v); } };
  602. Knob offset { intSpec (tr ("Offset: moves every value up or down"), -8192, 8192, 0,
  603. [] (double v) { return (v > 0 ? "+" : "") + percent (v / 8192.0); }),
  604. [this] { return get (L::offset); }, [this] (double v) { set (L::offset, v); } };
  605. Knob tension { intSpec (tr ("Tension: bends the curve (positive rises fast, negative starts slow)"), -8192, 8192, 0,
  606. [] (double v) { return (v > 0 ? "+" : "") + percent (v / 8192.0); }),
  607. [this] { return get (L::tension); }, [this] (double v) { set (L::tension, v); } };
  608. Knob humanize { intSpec (tr ("Humanize: a random amount for each note"), 0, 16384, 0, [] (double v) { return percent (v / 16384.0); }),
  609. [this] { return get (L::humanize); }, [this] (double v) { set (L::humanize, v); } };
  610. juce::ComboBox mode;
  611. juce::TextButton reset;
  612. juce::Rectangle<float> graph;
  613. };
  614. // A kind LYNX does not edit yet: says so (its notes pass through)
  615. class PendingBody final : public Body
  616. {
  617. public:
  618. void refresh (const std::vector<std::uint8_t>&) override {}
  619. int preferredHeight() const override { return 80; }
  620. void paint (juce::Graphics& g) override
  621. {
  622. g.setColour (theme::colour::textSecondary);
  623. g.setFont (theme::font (theme::type::body));
  624. g.drawFittedText (tr ("LYNX keeps this plugin's settings (from FL Studio) and passes its notes through unchanged."),
  625. getLocalBounds().reduced (14, 8), juce::Justification::topLeft, 3);
  626. }
  627. };
  628. std::unique_ptr<Body> makeBody (vfx::Kind kind)
  629. {
  630. switch (kind)
  631. {
  632. case vfx::Kind::keyMapper: return std::make_unique<KeyMapperBody>();
  633. case vfx::Kind::colorMapper: return std::make_unique<ColorMapperBody>();
  634. case vfx::Kind::levelScaler: return std::make_unique<LevelScalerBody>();
  635. default: break;
  636. }
  637. return std::make_unique<PendingBody>();
  638. }
  639. juce::String description (vfx::Kind kind)
  640. {
  641. switch (kind)
  642. {
  643. case vfx::Kind::keyMapper: return tr ("Maps each played note to other notes: transpose, scales, chords");
  644. case vfx::Kind::colorMapper: return tr ("Sends each note colour to its own voice output");
  645. case vfx::Kind::envelope: return tr ("Envelopes and LFOs on the notes' velocity, pitch, pan and Mod X / Y");
  646. case vfx::Kind::keyboardSplitter: return tr ("Splits the keyboard and the velocity into zones, one output each");
  647. case vfx::Kind::levelScaler: return tr ("Reshapes the notes' velocity, release, fine pitch, pan and Mod X / Y");
  648. case vfx::Kind::sequencer: return tr ("Plays step patterns and arpeggios from the held notes");
  649. case vfx::Kind::script: return tr ("FL's Python note scripts");
  650. }
  651. return {};
  652. }
  653. // ---------------------------------------------------------------------------------------------------------------
  654. class VfxEditor final : public juce::Component, private juce::ChangeListener
  655. {
  656. public:
  657. VfxEditor (AudioEngine& e, std::function<ChannelDef* (Song&)> l, int c) : engine (e), locate (std::move (l)), channel (c)
  658. {
  659. const auto s = state();
  660. kind = s ? s->kind : vfx::Kind::keyMapper;
  661. body = makeBody (kind);
  662. body->edit = [this] (Body::Edit change) { editState ([change] (vfx::State& st) { change (st.fl); }); };
  663. addAndMakeVisible (*body);
  664. presetButton.onClick = [this] { showPresetMenu(); };
  665. ui::setHint (presetButton, tr ("Presets: LYNX's own and, with FL Studio installed, FL's factory presets"));
  666. addAndMakeVisible (presetButton);
  667. for (auto* b : { &prev, &next })
  668. {
  669. addAndMakeVisible (b);
  670. b->setWantsKeyboardFocus (false);
  671. }
  672. prev.onClick = [this] { stepPreset (-1); };
  673. next.onClick = [this] { stepPreset (1); };
  674. // on a Channel Rack channel: the MIDI port its outputs go out on
  675. port.setSliderStyle (juce::Slider::IncDecButtons);
  676. port.setRange (0, 255, 1);
  677. port.setTextBoxStyle (juce::Slider::TextBoxLeft, false, 48, 22);
  678. port.onValueChange = [this] { editState ([v = (int) port.getValue()] (vfx::State& st) { st.port = v; }); };
  679. ui::setHint (port, tr ("Output port: output 1 goes out on this MIDI port, output 2 on the next one…; the plugins and Patchers "
  680. "that take that port play it"));
  681. portLabel.setText (tr ("Output port"), juce::dontSendNotification);
  682. portLabel.setFont (theme::font (theme::type::small));
  683. connect.setButtonText (tr ("Send to a plugin..."));
  684. connect.onClick = [this] { showConnectMenu(); };
  685. ui::setHint (connect, tr ("Sets a plugin channel's MIDI input port to this port, so it plays what this channel sends"));
  686. for (juce::Component* comp : { (juce::Component*) &port, (juce::Component*) &portLabel, (juce::Component*) &connect })
  687. {
  688. addAndMakeVisible (comp);
  689. comp->setVisible (channel >= 0);
  690. }
  691. engine.addChangeListener (this);
  692. refresh();
  693. setSize (std::max (620, body->preferredWidth()), 44 + (channel >= 0 ? 34 : 0) + body->preferredHeight());
  694. }
  695. ~VfxEditor() override { engine.removeChangeListener (this); }
  696. void paint (juce::Graphics& g) override
  697. {
  698. g.fillAll (theme::colour::bg2);
  699. auto top = getLocalBounds().removeFromTop (40).reduced (14, 0);
  700. g.setColour (theme::colour::textPrimary);
  701. g.setFont (theme::font (theme::type::title, true));
  702. g.drawText (vfx::flName (kind), top.removeFromLeft (220), juce::Justification::centredLeft);
  703. g.setColour (theme::colour::textSecondary);
  704. g.setFont (theme::font (theme::type::caption));
  705. g.drawText (description (kind), top.withTrimmedRight (300), juce::Justification::centredLeft);
  706. g.setColour (theme::colour::border);
  707. g.drawHorizontalLine (40, 0.0f, (float) getWidth());
  708. }
  709. void resized() override
  710. {
  711. auto area = getLocalBounds();
  712. auto top = area.removeFromTop (40).reduced (10, 7);
  713. next.setBounds (top.removeFromRight (26));
  714. presetButton.setBounds (top.removeFromRight (210));
  715. prev.setBounds (top.removeFromRight (26));
  716. area.removeFromTop (4);
  717. if (channel >= 0)
  718. {
  719. auto row = area.removeFromTop (30).reduced (14, 2);
  720. portLabel.setBounds (row.removeFromLeft (90));
  721. port.setBounds (row.removeFromLeft (130));
  722. row.removeFromLeft (10);
  723. connect.setBounds (row.removeFromLeft (170));
  724. }
  725. body->setBounds (area);
  726. }
  727. private:
  728. std::optional<vfx::State> state() const
  729. {
  730. const auto* c = locate (const_cast<Song&> (engine.getSong()));
  731. return c != nullptr ? vfx::readState (c->pluginState) : std::nullopt;
  732. }
  733. void editState (std::function<void (vfx::State&)> change)
  734. {
  735. engine.editSong ([this, change] (Song& song) {
  736. auto* c = locate (song);
  737. if (c == nullptr || c->instrument != InstrumentKind::vfx)
  738. return;
  739. auto s = vfx::readState (c->pluginState);
  740. if (! s)
  741. return;
  742. change (*s);
  743. c->pluginState = vfx::writeState (*s);
  744. });
  745. }
  746. struct PresetEntry
  747. {
  748. juce::String folder, name;
  749. std::vector<std::uint8_t> fl; // LYNX's own
  750. juce::File file; // FL's
  751. };
  752. std::vector<PresetEntry> presets() const
  753. {
  754. std::vector<PresetEntry> out;
  755. for (auto& p : vfx::builtInPresets (kind))
  756. out.push_back ({ juce::String::fromUTF8 (p.folder.c_str()), juce::String::fromUTF8 (p.name.c_str()), std::move (p.fl), {} });
  757. const auto roots = findFlStudioRoots();
  758. if (! roots.data.empty())
  759. {
  760. const auto base = juce::File (juce::String::fromUTF8 (roots.data.c_str())).getChildFile ("Patches/Plugin presets/Effects").getChildFile (vfx::flName (kind));
  761. auto files = base.findChildFiles (juce::File::findFiles, true, "*.fst");
  762. std::sort (files.begin(), files.end(), [] (const juce::File& a, const juce::File& b) { return a.getFullPathName().compareNatural (b.getFullPathName()) < 0; });
  763. for (const auto& f : files)
  764. {
  765. const auto folder = f.getParentDirectory() == base ? juce::String() : f.getParentDirectory().getFileName();
  766. out.push_back ({ "FL Studio" + (folder.isEmpty() ? juce::String() : " / " + folder), f.getFileNameWithoutExtension(), {}, f });
  767. }
  768. }
  769. return out;
  770. }
  771. void apply (const PresetEntry& p)
  772. {
  773. auto fl = p.fl;
  774. if (p.file != juce::File())
  775. {
  776. juce::MemoryBlock data;
  777. if (! p.file.loadFileAsData (data))
  778. return;
  779. const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
  780. const auto state = vfx::stateFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), kind);
  781. if (! state)
  782. return;
  783. fl = *state;
  784. }
  785. presetName = p.name;
  786. editState ([fl] (vfx::State& st) { st.fl = fl; });
  787. presetButton.setButtonText (presetName);
  788. }
  789. void showPresetMenu()
  790. {
  791. const auto all = presets();
  792. juce::PopupMenu menu;
  793. menu.addItem (tr ("Default"), [this] {
  794. presetName = tr ("Default");
  795. editState ([k = kind] (vfx::State& st) { st.fl = vfx::defaultState (k); });
  796. presetButton.setButtonText (presetName);
  797. });
  798. std::map<juce::String, juce::PopupMenu> folders;
  799. std::vector<juce::String> order;
  800. for (std::size_t i = 0; i < all.size(); ++i)
  801. {
  802. const auto folder = all[i].folder.isEmpty() ? tr ("Presets") : all[i].folder;
  803. if (! folders.count (folder))
  804. order.push_back (folder);
  805. folders[folder].addItem (all[i].name, [this, entry = all[i]] { apply (entry); });
  806. }
  807. for (const auto& f : order)
  808. menu.addSubMenu (f, folders[f]);
  809. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&presetButton));
  810. }
  811. void stepPreset (int delta)
  812. {
  813. const auto all = presets();
  814. if (all.empty())
  815. return;
  816. int at = -1;
  817. for (int i = 0; i < (int) all.size(); ++i)
  818. if (all[(std::size_t) i].name == presetName)
  819. at = i;
  820. at = at < 0 ? (delta > 0 ? 0 : (int) all.size() - 1) : (at + delta + (int) all.size()) % (int) all.size();
  821. apply (all[(std::size_t) at]);
  822. }
  823. void showConnectMenu()
  824. {
  825. juce::PopupMenu menu;
  826. const auto& song = engine.getSong();
  827. const auto s = state();
  828. const auto p = s ? s->port : 0;
  829. for (int c = 0; c < (int) song.channels.size(); ++c)
  830. if (song.channels[(std::size_t) c].instrument == InstrumentKind::plugin)
  831. menu.addItem (juce::String::fromUTF8 (song.channels[(std::size_t) c].name.c_str()), true,
  832. song.channels[(std::size_t) c].plugin.midiInPort == p, [this, c, p] {
  833. engine.editSong ([c, p] (Song& song) {
  834. auto& in = song.channels[(std::size_t) c].plugin.midiInPort;
  835. in = in == p ? -1 : p;
  836. });
  837. });
  838. if (menu.getNumItems() == 0)
  839. menu.addItem (tr ("(no plugin channel in the Channel Rack)"), false, false, [] {});
  840. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&connect));
  841. }
  842. void refresh()
  843. {
  844. const auto s = state();
  845. if (! s)
  846. return;
  847. body->refresh (s->fl);
  848. port.setValue (s->port, juce::dontSendNotification);
  849. if (presetName.isEmpty())
  850. presetButton.setButtonText (tr ("Presets"));
  851. }
  852. void changeListenerCallback (juce::ChangeBroadcaster*) override { refresh(); }
  853. AudioEngine& engine;
  854. std::function<ChannelDef* (Song&)> locate;
  855. int channel;
  856. vfx::Kind kind;
  857. std::unique_ptr<Body> body;
  858. juce::TextButton presetButton, prev { juce::String::fromUTF8 ("\xe2\x97\x80") }, next { juce::String::fromUTF8 ("\xe2\x96\xb6") };
  859. juce::String presetName;
  860. juce::Slider port;
  861. juce::Label portLabel;
  862. juce::TextButton connect;
  863. };
  864. } // namespace
  865. std::unique_ptr<juce::Component> makeVfxEditor (AudioEngine& engine, std::function<ChannelDef* (Song&)> locate, int channel)
  866. {
  867. return std::make_unique<VfxEditor> (engine, std::move (locate), channel);
  868. }
  869. std::unique_ptr<juce::Component> makeVfxEditor (AudioEngine& engine, int channel)
  870. {
  871. return makeVfxEditor (
  872. engine,
  873. [channel] (Song& song) -> ChannelDef* {
  874. return channel >= 0 && channel < (int) song.channels.size() ? &song.channels[(std::size_t) channel] : nullptr;
  875. },
  876. channel);
  877. }
  878. } // namespace lynx