WaveTravellerEditor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #include "ui/WaveTravellerEditor.h"
  2. #include "ui/FileChoosers.h"
  3. #include "ui/PluginSkin.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. #include "core/FlpImport.h"
  7. #include "core/WaveTraveller.h"
  8. #include "engine/AudioEngine.h"
  9. #include "platform/Paths.h"
  10. #include "ui/ControllerKnob.h"
  11. #include "ui/I18n.h"
  12. #include "ui/Theme.h"
  13. #include "ui/ValueControl.h"
  14. namespace lynx
  15. {
  16. namespace
  17. {
  18. using wavetrav::Patch;
  19. using wavetrav::Point;
  20. // the segment types FL's table builder knows (core/WaveTraveller.cpp buildTable); our names
  21. struct SegmentType
  22. {
  23. int type;
  24. const char* name;
  25. };
  26. constexpr SegmentType segmentTypes[] = {
  27. { 1, "Linear" }, { 2, "Smooth (cosine)" }, { 3, "Bezier" }, { 4, "Sine" }, { 5, "Ease in" }, { 6, "Tension" }, { 8, "Silence (zero)" },
  28. };
  29. juce::String keyName (int key)
  30. {
  31. static const char* const names[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  32. return juce::String (names[key % 12]) + juce::String (key / 12);
  33. }
  34. bool customised (const Patch& p)
  35. {
  36. static const auto d = wavetrav::defaultPatch();
  37. auto samePoints = [] (const std::vector<Point>& a, const std::vector<Point>& b) {
  38. return a.size() == b.size() && std::equal (a.begin(), a.end(), b.begin(), [] (const Point& x, const Point& y) {
  39. return x.x == y.x && x.y == y.y && x.type == y.type;
  40. });
  41. };
  42. return ! p.name.empty() || ! samePoints (p.wave, d.wave) || ! samePoints (p.vol, d.vol) || p.speedBeats != d.speedBeats
  43. || p.absSpeed != d.absSpeed || p.attackMs != d.attackMs || p.releaseMs != d.releaseMs || p.start != d.start || p.end != d.end
  44. || p.splineType != d.splineType || p.mutes.size() != d.mutes.size();
  45. }
  46. class WaveTravellerEditor;
  47. // ---------------------------------------------------------------------------------------------------------------
  48. // The curve of the selected key (position or volume), its points and the mute switches below it
  49. class CurveView final : public juce::Component
  50. {
  51. public:
  52. std::function<Patch*()> patch;
  53. std::function<void()> changed; // after an edit of the patch
  54. bool volume = false;
  55. void paint (juce::Graphics& g) override
  56. {
  57. { // round 170j: a display well of the page's art (Themed), else the plain dark field
  58. const skin::StyleScope styleScope (skin::resolve (skin::Style::themed, skin::Art::waveTraveller), skin::Art::waveTraveller);
  59. if (skin::style() == skin::Style::themed)
  60. skin::well (g, getLocalBounds().toFloat(), theme::radius::md, juce::Colours::transparentBlack);
  61. else
  62. g.fillAll (theme::colour::bg0);
  63. }
  64. auto* p = patch ? patch() : nullptr;
  65. if (p == nullptr)
  66. return;
  67. const auto area = curveArea();
  68. g.setColour (theme::colour::border);
  69. for (int i = 1; i < 8; ++i)
  70. {
  71. const auto x = area.getX() + area.getWidth() * (float) i / 8.0f;
  72. g.drawVerticalLine ((int) x, area.getY(), area.getBottom());
  73. }
  74. for (int i = 1; i < 4; ++i)
  75. g.drawHorizontalLine ((int) (area.getY() + area.getHeight() * (float) i / 4.0f), area.getX(), area.getRight());
  76. // the table the voices read (what FL builds from the points)
  77. const auto* table = volume ? p->volTable.data() : p->waveTable.data();
  78. const auto n = volume ? p->volN : p->waveN;
  79. if (n > 1)
  80. {
  81. juce::Path curve;
  82. for (int i = 0; i < n; ++i)
  83. {
  84. const auto pt = toScreen ((float) i / (float) (n - 1), table[(std::size_t) i]);
  85. if (i == 0)
  86. curve.startNewSubPath (pt);
  87. else
  88. curve.lineTo (pt);
  89. }
  90. g.setColour (volume ? theme::colour::automation : theme::colour::accent);
  91. g.strokePath (curve, juce::PathStrokeType (1.6f));
  92. }
  93. const auto& pts = points (*p);
  94. for (std::size_t i = 0; i < pts.size(); ++i)
  95. {
  96. const auto c = toScreen (pts[i].x, pts[i].y);
  97. g.setColour ((int) i == hover || (int) i == dragging ? theme::colour::textPrimary : theme::colour::knobArc);
  98. g.fillEllipse (juce::Rectangle<float> (8.0f, 8.0f).withCentre (c));
  99. }
  100. // the mute switches: muted spans shaded
  101. const auto strip = muteArea();
  102. g.setColour (theme::colour::bg3);
  103. g.fillRect (strip);
  104. auto ms = p->mutes;
  105. std::stable_sort (ms.begin(), ms.end(), [] (const wavetrav::Mute& a, const wavetrav::Mute& b) { return a.at < b.at; });
  106. for (std::size_t i = 0; i < ms.size(); ++i)
  107. {
  108. const auto from = strip.getX() + strip.getWidth() * std::clamp (ms[i].at, 0.0f, 1.0f);
  109. const auto to = i + 1 < ms.size() ? strip.getX() + strip.getWidth() * std::clamp (ms[i + 1].at, 0.0f, 1.0f) : strip.getRight();
  110. if (! ms[i].on)
  111. {
  112. g.setColour (theme::colour::accentMuted);
  113. g.fillRect (juce::Rectangle<float> (from, strip.getY(), std::max (0.0f, to - from), strip.getHeight()));
  114. }
  115. g.setColour (theme::colour::textSecondary);
  116. g.drawVerticalLine ((int) from, strip.getY(), strip.getBottom());
  117. }
  118. g.setColour (theme::colour::textSecondary);
  119. g.setFont (theme::font (theme::type::caption));
  120. g.drawText (tr ("Mutes (click: switch, right click: remove)"), strip.reduced (6.0f, 0.0f), juce::Justification::centredLeft);
  121. g.setColour (theme::colour::border);
  122. g.drawRect (getLocalBounds());
  123. }
  124. void mouseMove (const juce::MouseEvent& e) override
  125. {
  126. const auto h = pointAt (e.position);
  127. if (h != hover)
  128. {
  129. hover = h;
  130. repaint();
  131. }
  132. }
  133. void mouseExit (const juce::MouseEvent&) override
  134. {
  135. hover = -1;
  136. repaint();
  137. }
  138. void mouseDown (const juce::MouseEvent& e) override
  139. {
  140. auto* p = patch ? patch() : nullptr;
  141. if (p == nullptr)
  142. return;
  143. if (muteArea().contains (e.position))
  144. {
  145. muteClick (*p, e);
  146. return;
  147. }
  148. const auto i = pointAt (e.position);
  149. if (e.mods.isPopupMenu())
  150. {
  151. if (i >= 0)
  152. pointMenu (i);
  153. return;
  154. }
  155. dragging = i;
  156. }
  157. void mouseDrag (const juce::MouseEvent& e) override
  158. {
  159. auto* p = patch ? patch() : nullptr;
  160. if (p == nullptr || dragging < 0)
  161. return;
  162. auto& pts = points (*p);
  163. if (dragging >= (int) pts.size())
  164. return;
  165. auto v = fromScreen (e.position);
  166. auto& q = pts[(std::size_t) dragging];
  167. // the ends stay at 0 and 1; the others between their neighbours
  168. if (dragging == 0)
  169. v.x = 0.0f;
  170. else if (dragging == (int) pts.size() - 1)
  171. v.x = 1.0f;
  172. else
  173. v.x = std::clamp (v.x, pts[(std::size_t) dragging - 1].x, pts[(std::size_t) dragging + 1].x);
  174. q.x = v.x;
  175. q.y = std::clamp (v.y, 0.0f, 1.0f);
  176. edited (*p);
  177. }
  178. void mouseUp (const juce::MouseEvent&) override { dragging = -1; }
  179. void mouseDoubleClick (const juce::MouseEvent& e) override
  180. {
  181. auto* p = patch ? patch() : nullptr;
  182. if (p == nullptr || ! curveArea().contains (e.position))
  183. return;
  184. auto& pts = points (*p);
  185. const auto i = pointAt (e.position);
  186. if (i > 0 && i < (int) pts.size() - 1) // double click on a point removes it
  187. {
  188. pts.erase (pts.begin() + i);
  189. edited (*p);
  190. return;
  191. }
  192. const auto v = fromScreen (e.position);
  193. const auto at = std::upper_bound (pts.begin(), pts.end(), v.x, [] (float x, const Point& q) { return x < q.x; });
  194. if (at == pts.begin() || at == pts.end())
  195. return;
  196. auto q = wavetrav::newPoint (v.x, std::clamp (v.y, 0.0f, 1.0f));
  197. q.type = (at - 1)->type; // the new point continues the segment it splits
  198. pts.insert (at, q);
  199. edited (*p);
  200. }
  201. private:
  202. juce::Rectangle<float> curveArea() const { return getLocalBounds().toFloat().withTrimmedBottom (22.0f).reduced (8.0f, 8.0f); }
  203. juce::Rectangle<float> muteArea() const { return getLocalBounds().toFloat().removeFromBottom (20.0f).reduced (8.0f, 2.0f); }
  204. juce::Point<float> toScreen (float x, float y) const
  205. {
  206. const auto a = curveArea();
  207. return { a.getX() + a.getWidth() * x, a.getBottom() - a.getHeight() * y };
  208. }
  209. juce::Point<float> fromScreen (juce::Point<float> s) const
  210. {
  211. const auto a = curveArea();
  212. return { std::clamp ((s.x - a.getX()) / std::max (1.0f, a.getWidth()), 0.0f, 1.0f), (a.getBottom() - s.y) / std::max (1.0f, a.getHeight()) };
  213. }
  214. std::vector<Point>& points (Patch& p) const { return volume ? p.vol : p.wave; }
  215. int pointAt (juce::Point<float> s) const
  216. {
  217. auto* p = patch ? patch() : nullptr;
  218. if (p == nullptr)
  219. return -1;
  220. const auto& pts = points (*p);
  221. int best = -1;
  222. float bestD = 7.0f;
  223. for (std::size_t i = 0; i < pts.size(); ++i)
  224. if (const auto d = toScreen (pts[i].x, pts[i].y).getDistanceFrom (s); d < bestD)
  225. best = (int) i, bestD = d;
  226. return best;
  227. }
  228. void edited (Patch& p)
  229. {
  230. p.rebuild();
  231. repaint();
  232. if (changed)
  233. changed();
  234. }
  235. void pointMenu (int index)
  236. {
  237. auto* p = patch ? patch() : nullptr;
  238. if (p == nullptr)
  239. return;
  240. const auto& q = points (*p)[(std::size_t) index];
  241. juce::PopupMenu m, types, tension;
  242. for (const auto& t : segmentTypes)
  243. types.addItem (100 + t.type, tr (t.name), true, q.type == t.type);
  244. for (int i = 1; i <= 9; ++i)
  245. tension.addItem (200 + i, juce::String ((double) i / 10.0, 1), true, std::abs (q.tension - (float) i / 10.0f) < 1e-4f);
  246. m.addSubMenu (tr ("Segment type"), types);
  247. m.addSubMenu (tr ("Tension"), tension, q.type == 6);
  248. m.addSeparator();
  249. const auto last = (int) points (*p).size() - 1;
  250. m.addItem (1, tr ("Delete point"), index > 0 && index < last);
  251. juce::Component::SafePointer<CurveView> self (this);
  252. m.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition(), [self, index] (int r) {
  253. if (self == nullptr || r == 0)
  254. return;
  255. auto* p = self->patch ? self->patch() : nullptr;
  256. if (p == nullptr)
  257. return;
  258. auto& pts = self->points (*p);
  259. if (index >= (int) pts.size())
  260. return;
  261. if (r == 1)
  262. pts.erase (pts.begin() + index);
  263. else if (r >= 200)
  264. pts[(std::size_t) index].tension = (float) (r - 200) / 10.0f;
  265. else if (r >= 100)
  266. pts[(std::size_t) index].type = r - 100;
  267. self->edited (*p);
  268. });
  269. }
  270. void muteClick (Patch& p, const juce::MouseEvent& e)
  271. {
  272. const auto strip = muteArea();
  273. const auto at = std::clamp ((e.position.x - strip.getX()) / std::max (1.0f, strip.getWidth()), 0.0f, 1.0f);
  274. if (e.mods.isPopupMenu())
  275. {
  276. int best = -1;
  277. float bestD = 6.0f;
  278. for (std::size_t i = 0; i < p.mutes.size(); ++i)
  279. if (const auto d = std::abs (strip.getX() + strip.getWidth() * p.mutes[i].at - e.position.x); d < bestD)
  280. best = (int) i, bestD = d;
  281. if (best >= 0)
  282. {
  283. p.mutes.erase (p.mutes.begin() + best);
  284. edited (p);
  285. }
  286. return;
  287. }
  288. // the state there, switched from here on
  289. bool on = true;
  290. float from = -1.0f;
  291. for (const auto& m : p.mutes)
  292. if (m.at <= at && m.at >= from)
  293. on = m.on, from = m.at;
  294. p.mutes.push_back ({ ! on, at });
  295. std::stable_sort (p.mutes.begin(), p.mutes.end(), [] (const wavetrav::Mute& a, const wavetrav::Mute& b) { return a.at < b.at; });
  296. edited (p);
  297. }
  298. int hover = -1, dragging = -1;
  299. };
  300. // ---------------------------------------------------------------------------------------------------------------
  301. // The 120 keys (C0 … B9): the one whose patch the editor shows; keys with a patch of their own are marked
  302. class KeyStrip final : public juce::Component
  303. {
  304. public:
  305. std::function<int()> selected;
  306. std::function<bool (int)> marked;
  307. std::function<void (int)> select;
  308. void paint (juce::Graphics& g) override
  309. {
  310. g.fillAll (theme::colour::bg1);
  311. const auto w = (float) getWidth() / (float) wavetrav::keyCount;
  312. const auto sel = selected ? selected() : -1;
  313. for (int k = 0; k < wavetrav::keyCount; ++k)
  314. {
  315. const auto black = juce::String ("010100101010").substring (k % 12, k % 12 + 1) == "1";
  316. auto r = juce::Rectangle<float> ((float) k * w, 0.0f, w, (float) getHeight()).reduced (0.5f, 0.0f);
  317. g.setColour (k == sel ? theme::colour::accent : black ? theme::colour::bg3 : theme::colour::bg5);
  318. g.fillRect (black ? r.withTrimmedBottom ((float) getHeight() * 0.35f) : r);
  319. if (marked && marked (k))
  320. {
  321. g.setColour (k == sel ? theme::colour::onAccent : theme::colour::ledOn);
  322. g.fillEllipse (juce::Rectangle<float> (4.0f, 4.0f).withCentre ({ r.getCentreX(), (float) getHeight() - 5.0f }));
  323. }
  324. if (k % 12 == 0)
  325. {
  326. g.setColour (theme::colour::textSecondary);
  327. g.setFont (theme::font (theme::type::caption));
  328. g.drawText (keyName (k), juce::Rectangle<float> (r.getX() + 1.0f, 0.0f, w * 4.0f, 12.0f), juce::Justification::left);
  329. }
  330. }
  331. }
  332. void mouseDown (const juce::MouseEvent& e) override { pick (e); }
  333. void mouseDrag (const juce::MouseEvent& e) override { pick (e); }
  334. private:
  335. void pick (const juce::MouseEvent& e)
  336. {
  337. const auto k = std::clamp ((int) (e.position.x / std::max (1.0f, (float) getWidth()) * (float) wavetrav::keyCount), 0, wavetrav::keyCount - 1);
  338. if (select && (! selected || selected() != k))
  339. select (k);
  340. }
  341. };
  342. // ---------------------------------------------------------------------------------------------------------------
  343. class WaveTravellerEditor final : public juce::Component,
  344. public juce::FileDragAndDropTarget,
  345. private juce::ChangeListener
  346. {
  347. public:
  348. WaveTravellerEditor (AudioEngine& e, std::function<ChannelDef* (Song&)> l, int c) : engine (e), locate (std::move (l)), channel (c)
  349. {
  350. juce::ignoreUnused (channel);
  351. auto button = [this] (juce::TextButton& b, const juce::String& text, const juce::String& hint, std::function<void()> click) {
  352. b.setButtonText (text);
  353. ui::setHint (b, hint);
  354. b.onClick = std::move (click);
  355. b.setColour (juce::TextButton::buttonOnColourId, theme::colour::accent);
  356. addAndMakeVisible (b);
  357. };
  358. button (loadButton, tr ("Load sample..."), tr ("The sample the curves travel through (or drop a sample here)"), [this] { chooseSample(); });
  359. button (presetButton, tr ("Presets"), tr ("FL Studio's Wave Traveller presets: the curves of this key"), [this] { presetMenu(); });
  360. button (keyButton, tr ("Key"), tr ("Copy or reset this key's patch"), [this] { keyMenu(); });
  361. button (waveButton, tr ("Position"), tr ("Edit the position curve: where the note is in the sample over its time"), [this] { showVolume (false); });
  362. button (volButton, tr ("Volume"), tr ("Edit the volume curve"), [this] { showVolume (true); });
  363. waveButton.setRadioGroupId (1);
  364. volButton.setRadioGroupId (1);
  365. waveButton.setClickingTogglesState (true);
  366. volButton.setClickingTogglesState (true);
  367. waveButton.setToggleState (true, juce::dontSendNotification);
  368. mode.addItem (tr ("Tempo based spline"), 1);
  369. mode.addItem (tr ("Absolute speed based spline"), 2);
  370. mode.onChange = [this] {
  371. const auto absolute = mode.getSelectedId() == 2;
  372. edit ([absolute] (Patch& p) { p.splineType = absolute ? 1 : 0; });
  373. };
  374. ui::setHint (mode, tr ("Speed mode: the curve lasts a number of beats, or plays the sample at a speed factor"));
  375. addAndMakeVisible (mode);
  376. sampleLabel.setFont (theme::font (theme::type::body));
  377. sampleLabel.setColour (juce::Label::textColourId, theme::colour::textPrimary);
  378. addAndMakeVisible (sampleLabel);
  379. keyLabel.setFont (theme::font (theme::type::body));
  380. keyLabel.setColour (juce::Label::textColourId, theme::colour::accent);
  381. keyLabel.setJustificationType (juce::Justification::centredRight);
  382. addAndMakeVisible (keyLabel);
  383. auto knob = [this] (std::unique_ptr<ctlui::Knob>& k, ui::ValueSpec spec, const juce::String& shortName, std::function<double (const Patch&)> get,
  384. std::function<void (Patch&, double)> set) {
  385. k = std::make_unique<ctlui::Knob> (&engine, std::move (spec), [this, get] { return get (current()); },
  386. [this, set] (double v) { edit ([set, v] (Patch& p) { set (p, v); }); });
  387. k->shortName = shortName;
  388. addAndMakeVisible (*k);
  389. };
  390. auto spec = [] (const juce::String& name, double lo, double hi, double def, std::function<juce::String (double)> text) {
  391. ui::ValueSpec s;
  392. s.name = name;
  393. s.minimum = lo, s.maximum = hi, s.defaultValue = def;
  394. s.toText = std::move (text);
  395. return s;
  396. };
  397. const auto beats = [] (double v) { return juce::String (v, 3) + " " + tr ("beats"); };
  398. const auto factor = [] (double v) { return juce::String (v, 2) + "x"; };
  399. const auto ms = [] (double v) { return juce::String (v, 1) + " ms"; };
  400. const auto percent = [] (double v) { return juce::String (v * 100.0, 1) + "%"; };
  401. knob (speedTempo, spec (tr ("Speed (beats the curve lasts)"), 0.125, 8.0, 4.0, beats), tr ("Speed"),
  402. [] (const Patch& p) { return (double) p.speedBeats; }, [] (Patch& p, double v) { p.speedBeats = (float) v; });
  403. knob (speedAbs, spec (tr ("Speed (factor of the sample's own speed)"), 0.25, 4.0, 1.0, factor), tr ("Speed"),
  404. [] (const Patch& p) { return (double) p.absSpeed; }, [] (Patch& p, double v) { p.absSpeed = (float) v; });
  405. knob (attack, spec (tr ("Attack"), 1.0, 500.0, 1.0, ms), tr ("Attack"), [] (const Patch& p) { return (double) p.attackMs; },
  406. [] (Patch& p, double v) { p.attackMs = (float) v; });
  407. knob (release, spec (tr ("Release"), 1.0, 1000.0, 1.0, ms), tr ("Release"), [] (const Patch& p) { return (double) p.releaseMs; },
  408. [] (Patch& p, double v) { p.releaseMs = (float) v; });
  409. knob (start, spec (tr ("Start (the sample's part the curve spans)"), 0.0, 1.0, 0.0, percent), tr ("Start"),
  410. [] (const Patch& p) { return (double) p.start; }, [] (Patch& p, double v) { p.start = (float) v; });
  411. knob (end, spec (tr ("End (the sample's part the curve spans)"), 0.0, 1.0, 1.0, percent), tr ("End"),
  412. [] (const Patch& p) { return (double) p.end; }, [] (Patch& p, double v) { p.end = (float) v; });
  413. curve.patch = [this] { return &state.patches[(std::size_t) std::clamp (state.selectedKey, 0, wavetrav::keyCount - 1)]; };
  414. curve.changed = [this] { commit(); };
  415. ui::setHint (curve, tr ("Drag a point; double click adds or removes a point; right click: segment type"));
  416. addAndMakeVisible (curve);
  417. keys.selected = [this] { return state.selectedKey; };
  418. keys.marked = [this] (int k) { return customised (state.patches[(std::size_t) k]); };
  419. keys.select = [this] (int k) {
  420. state.selectedKey = k;
  421. commit (false);
  422. };
  423. ui::setHint (keys, tr ("The key whose patch is shown: each key plays its own curves"));
  424. addAndMakeVisible (keys);
  425. engine.addChangeListener (this);
  426. reload();
  427. setSize (760, 470);
  428. }
  429. ~WaveTravellerEditor() override { engine.removeChangeListener (this); }
  430. void paint (juce::Graphics& g) override // round 170j: the art's canvas (Themed), else the plain panel
  431. {
  432. const skin::StyleScope styleScope (skin::resolve (skin::Style::themed, skin::Art::waveTraveller), skin::Art::waveTraveller);
  433. if (skin::style() == skin::Style::themed)
  434. skin::canvas (g, getLocalBounds().toFloat(), skin::artAccent (skin::Art::waveTraveller));
  435. else
  436. g.fillAll (theme::colour::bg2);
  437. }
  438. void resized() override
  439. {
  440. auto r = getLocalBounds().reduced (8);
  441. auto top = r.removeFromTop (28);
  442. loadButton.setBounds (top.removeFromLeft (120));
  443. top.removeFromLeft (6);
  444. presetButton.setBounds (top.removeFromLeft (90));
  445. top.removeFromLeft (6);
  446. keyButton.setBounds (top.removeFromLeft (60));
  447. top.removeFromLeft (8);
  448. keyLabel.setBounds (top.removeFromRight (150));
  449. sampleLabel.setBounds (top);
  450. r.removeFromTop (6);
  451. auto knobs = r.removeFromTop (74);
  452. const auto w = 72;
  453. mode.setBounds (knobs.removeFromLeft (210).withSizeKeepingCentre (210, 26));
  454. knobs.removeFromLeft (10);
  455. for (auto* k : { speedTempo.get(), speedAbs.get(), attack.get(), release.get(), start.get(), end.get() })
  456. if (k != speedAbs.get())
  457. k->setBounds (knobs.removeFromLeft (w));
  458. speedAbs->setBounds (speedTempo->getBounds());
  459. r.removeFromTop (6);
  460. keys.setBounds (r.removeFromBottom (44));
  461. r.removeFromBottom (6);
  462. auto tabs = r.removeFromTop (24);
  463. waveButton.setBounds (tabs.removeFromLeft (100));
  464. tabs.removeFromLeft (4);
  465. volButton.setBounds (tabs.removeFromLeft (100));
  466. r.removeFromTop (4);
  467. curve.setBounds (r);
  468. }
  469. bool isInterestedInFileDrag (const juce::StringArray& files) override
  470. {
  471. return files.size() == 1 && juce::File (files[0]).hasFileExtension ("wav;aif;aiff;flac;ogg;mp3");
  472. }
  473. void filesDropped (const juce::StringArray& files, int, int) override
  474. {
  475. if (! files.isEmpty())
  476. setSample (files[0]);
  477. }
  478. private:
  479. const ChannelDef* def() const
  480. {
  481. return locate ? locate (const_cast<Song&> (engine.getSong())) : nullptr;
  482. }
  483. Patch& current() { return state.patches[(std::size_t) std::clamp (state.selectedKey, 0, wavetrav::keyCount - 1)]; }
  484. void reload()
  485. {
  486. const auto* d = def();
  487. if (d == nullptr)
  488. return;
  489. lastState = d->pluginState;
  490. if (! wavetrav::parseState (d->pluginState, state))
  491. state = wavetrav::defaultState();
  492. samplePath = d->samplePath;
  493. refresh();
  494. }
  495. void refresh()
  496. {
  497. const auto& p = current();
  498. sampleLabel.setText (samplePath.empty() ? tr ("No sample: the channel is silent (load or drop one)")
  499. : juce::String::fromUTF8 (samplePath.c_str()).fromLastOccurrenceOf ("\\", false, false).fromLastOccurrenceOf ("/", false, false),
  500. juce::dontSendNotification);
  501. keyLabel.setText (tr ("Key") + " " + keyName (state.selectedKey) + (p.name.empty() ? juce::String() : " - " + juce::String::fromUTF8 (p.name.c_str())),
  502. juce::dontSendNotification);
  503. mode.setSelectedId (p.splineType == 1 ? 2 : 1, juce::dontSendNotification);
  504. speedTempo->setVisible (p.splineType != 1);
  505. speedAbs->setVisible (p.splineType == 1);
  506. repaint();
  507. curve.repaint();
  508. keys.repaint();
  509. }
  510. void edit (const std::function<void (Patch&)>& change)
  511. {
  512. change (current());
  513. current().rebuild();
  514. commit();
  515. }
  516. // writes the state in FL's layout into the channel
  517. void commit (bool undoable = true)
  518. {
  519. const auto bytes = wavetrav::writeState (state);
  520. lastState = bytes;
  521. engine.editSong ([l = locate, bytes] (Song& song) {
  522. if (auto* d = l ? l (song) : nullptr)
  523. d->pluginState = bytes;
  524. }, undoable);
  525. refresh();
  526. }
  527. void changeListenerCallback (juce::ChangeBroadcaster*) override
  528. {
  529. const auto* d = def();
  530. if (d != nullptr && (d->pluginState != lastState || d->samplePath != samplePath))
  531. reload();
  532. }
  533. void showVolume (bool v)
  534. {
  535. curve.volume = v;
  536. curve.repaint();
  537. }
  538. void setSample (const juce::String& path)
  539. {
  540. const auto p = path.toStdString();
  541. state.samplePath = p; // (FL's state names the sample too)
  542. samplePath = p;
  543. const auto bytes = wavetrav::writeState (state);
  544. lastState = bytes;
  545. engine.editSong ([l = locate, bytes, p] (Song& song) {
  546. if (auto* d = l ? l (song) : nullptr)
  547. {
  548. d->pluginState = bytes;
  549. d->samplePath = p;
  550. }
  551. });
  552. refresh();
  553. }
  554. void chooseSample()
  555. {
  556. chooser = lynx::ui::makeFileChooser (tr ("Load sample..."), juce::File(), "*.wav;*.aif;*.aiff;*.flac;*.ogg;*.mp3");
  557. juce::Component::SafePointer<WaveTravellerEditor> self (this);
  558. lynx::ui::launchChooser (chooser, juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles, [self] (const juce::FileChooser& fc) {
  559. if (self != nullptr && fc.getResult().existsAsFile())
  560. self->setSample (fc.getResult().getFullPathName());
  561. });
  562. }
  563. // FL's .spln presets, from the user's installation (never copied: read when picked)
  564. void presetMenu()
  565. {
  566. const auto folder = resolveFlPath ("%FLStudioFactoryData%\\Plugins\\Fruity\\Generators\\Wave Traveller\\Presets", flStudioAccess().roots);
  567. presets.clear();
  568. if (! folder.empty())
  569. presets = platform::localFile (folder).findChildFiles (juce::File::findFiles, false, "*.spln");
  570. std::sort (presets.begin(), presets.end(), [] (const juce::File& a, const juce::File& b) { return a.getFileName().compareNatural (b.getFileName()) < 0; });
  571. juce::PopupMenu m;
  572. for (int i = 0; i < presets.size(); ++i)
  573. m.addItem (i + 1, presets[i].getFileNameWithoutExtension());
  574. if (presets.isEmpty())
  575. m.addItem (-1, tr ("No presets found (FL Studio is not installed)"), false);
  576. juce::Component::SafePointer<WaveTravellerEditor> self (this);
  577. m.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&presetButton), [self] (int r) {
  578. if (self == nullptr || r <= 0 || r > self->presets.size())
  579. return;
  580. juce::MemoryBlock data;
  581. if (! self->presets[r - 1].loadFileAsData (data))
  582. return;
  583. const auto* b = static_cast<const std::uint8_t*> (data.getData());
  584. const std::vector<std::uint8_t> bytes (b, b + data.getSize());
  585. auto& p = self->current();
  586. if (wavetrav::applyPreset (bytes, p))
  587. {
  588. p.name = self->presets[r - 1].getFileNameWithoutExtension().toStdString();
  589. self->commit();
  590. }
  591. });
  592. }
  593. void keyMenu()
  594. {
  595. juce::PopupMenu m;
  596. m.addItem (1, tr ("Copy this key's patch to all keys"));
  597. m.addItem (2, tr ("Reset this key's patch"));
  598. juce::Component::SafePointer<WaveTravellerEditor> self (this);
  599. m.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&keyButton), [self] (int r) {
  600. if (self == nullptr || r == 0)
  601. return;
  602. if (r == 1)
  603. {
  604. const auto p = self->current();
  605. for (auto& q : self->state.patches)
  606. q = p;
  607. }
  608. else
  609. self->current() = wavetrav::defaultPatch();
  610. self->commit();
  611. });
  612. }
  613. AudioEngine& engine;
  614. std::function<ChannelDef* (Song&)> locate;
  615. int channel;
  616. wavetrav::State state;
  617. std::vector<std::uint8_t> lastState;
  618. std::string samplePath;
  619. juce::TextButton loadButton, presetButton, keyButton, waveButton, volButton;
  620. juce::ComboBox mode;
  621. juce::Label sampleLabel, keyLabel;
  622. std::unique_ptr<ctlui::Knob> speedTempo, speedAbs, attack, release, start, end;
  623. CurveView curve;
  624. KeyStrip keys;
  625. juce::Array<juce::File> presets;
  626. std::unique_ptr<juce::FileChooser> chooser;
  627. };
  628. } // namespace
  629. std::unique_ptr<juce::Component> makeWaveTravellerEditor (AudioEngine& engine, std::function<ChannelDef* (Song&)> locate, int channel)
  630. {
  631. return std::make_unique<WaveTravellerEditor> (engine, std::move (locate), channel);
  632. }
  633. std::unique_ptr<juce::Component> makeWaveTravellerEditor (AudioEngine& engine, int channel)
  634. {
  635. return makeWaveTravellerEditor (engine, [channel] (Song& s) -> ChannelDef* {
  636. return channel >= 0 && channel < (int) s.channels.size() ? &s.channels[(std::size_t) channel] : nullptr;
  637. }, channel);
  638. }
  639. } // namespace lynx