WaveTravellerEditor.cpp 29 KB

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