| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944 |
- #include "ui/VfxEditors.h"
- #include <algorithm>
- #include <array>
- #include "core/Vfx.h"
- #include "engine/Text.h"
- #include "ui/I18n.h"
- #include "ui/Theme.h"
- #include "ui/ToolWindow.h"
- #include "ui/ValueControl.h"
- namespace lynx
- {
- namespace
- {
- juce::String keyName (int key)
- {
- return juce::MidiMessage::getMidiNoteName (std::clamp (key, 0, 127), true, true, 4);
- }
- juce::String pitchClassName (int pc)
- {
- static const char* names[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
- return names[((pc % 12) + 12) % 12];
- }
- // A small knob with LYNX's control contract (drag, Ctrl = fine, wheel, Alt / middle-click / double-click reset,
- // right-click: type a value / reset)
- class Knob final : public juce::Component
- {
- public:
- Knob (ui::ValueSpec s, std::function<double()> get, std::function<void (double)> set)
- : spec (std::move (s)), getter (std::move (get)), setter (std::move (set))
- {
- setRepaintsOnMouseActivity (true);
- ui::setHint (*this, spec.name, true, false);
- }
- void paint (juce::Graphics& g) override
- {
- const auto value = getter();
- const auto v = juce::jlimit (0.0, 1.0, (value - spec.minimum) / juce::jmax (1e-9, spec.maximum - spec.minimum));
- auto area = getLocalBounds().toFloat();
- const auto label = area.removeFromBottom (14.0f);
- const auto valueArea = area.removeFromBottom (13.0f);
- const auto size = juce::jmin (area.getWidth(), area.getHeight()) - 4.0f;
- const auto box = juce::Rectangle<float> (size, size).withCentre (area.getCentre());
- constexpr float sweep = juce::MathConstants<float>::pi * 1.5f;
- g.setColour (theme::colour::bg4);
- g.fillEllipse (box.reduced (4.0f));
- juce::Path track, arc;
- const auto radius = size * 0.5f - 1.5f;
- track.addCentredArc (box.getCentreX(), box.getCentreY(), radius, radius, 0.0f, -sweep * 0.5f, sweep * 0.5f, true);
- g.setColour (theme::colour::knobTrack);
- g.strokePath (track, juce::PathStrokeType (3.0f));
- // bipolar knobs light from the centre
- const auto bipolar = spec.minimum < 0.0 && spec.maximum > 0.0;
- const auto from = bipolar ? 0.5 : 0.0;
- arc.addCentredArc (box.getCentreX(), box.getCentreY(), radius, radius, 0.0f, -sweep * 0.5f + sweep * (float) std::min (from, v),
- -sweep * 0.5f + sweep * (float) std::max (from, v), true);
- g.setColour (isMouseOverOrDragging() ? theme::colour::accent : theme::colour::knobArc);
- g.strokePath (arc, juce::PathStrokeType (3.0f));
- const auto angle = -sweep * 0.5f + sweep * (float) v;
- const auto tip = box.getCentre() + juce::Point<float> (std::sin (angle), -std::cos (angle)) * (radius - 5.0f);
- g.drawLine (box.getCentreX(), box.getCentreY(), tip.x, tip.y, 2.0f);
- g.setColour (theme::colour::textPrimary);
- g.setFont (theme::font (theme::type::caption));
- g.drawText (spec.text (value), valueArea, juce::Justification::centred);
- g.setColour (theme::colour::textSecondary);
- g.drawText (caption, label, juce::Justification::centred);
- }
- void mouseDown (const juce::MouseEvent& e) override
- {
- if (e.mods.isPopupMenu())
- {
- ui::showValueMenu (*this, nullptr, spec, getter(), [this] (double v) { change (v); });
- return;
- }
- if (ui::isResetClick (e))
- {
- change (spec.defaultValue);
- return;
- }
- drag.begin (e, getter());
- }
- void mouseDrag (const juce::MouseEvent& e) override
- {
- if (drag.isActive())
- change (drag.update (e, spec));
- }
- void mouseUp (const juce::MouseEvent&) override { drag.end(); }
- void mouseDoubleClick (const juce::MouseEvent&) override { change (spec.defaultValue); }
- void mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& w) override
- {
- if (const auto v = ui::wheelValue (getter(), e, w, spec))
- change (*v);
- }
- juce::String caption;
- private:
- void change (double v)
- {
- v = juce::jlimit (spec.minimum, spec.maximum, v);
- if (spec.interval > 0.0)
- v = spec.minimum + std::round ((v - spec.minimum) / spec.interval) * spec.interval;
- setter (v);
- repaint();
- }
- ui::ValueSpec spec;
- std::function<double()> getter;
- std::function<void (double)> setter;
- ui::ValueDrag drag;
- };
- ui::ValueSpec intSpec (const juce::String& name, int lo, int hi, int def, std::function<juce::String (double)> text = nullptr)
- {
- ui::ValueSpec s;
- s.name = name;
- s.minimum = lo;
- s.maximum = hi;
- s.defaultValue = def;
- s.interval = 1.0;
- s.pixelsForRange = std::clamp ((double) (hi - lo) * 6.0, 120.0, 300.0);
- if (text)
- {
- s.toText = text;
- s.fromText = ui::inverseOf (text, lo, hi);
- }
- else
- s.toText = [] (double v) { return juce::String ((int) std::lround (v)); };
- return s;
- }
- // What every body gets: the plugin's FL state and a way to change it
- class Body : public juce::Component
- {
- public:
- using Edit = std::function<void (std::vector<std::uint8_t>&)>;
- std::function<void (Edit)> edit;
- virtual void refresh (const std::vector<std::uint8_t>& fl) = 0;
- virtual int preferredHeight() const { return 300; }
- virtual int preferredWidth() const { return 700; }
- };
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Key Mapper: inputs (one octave) down, outputs across; the controls above
- class KeyMapperBody final : public Body
- {
- public:
- static constexpr int lowest = -12, columns = 36; // outputs: an octave below the row's octave to an octave above (FL's range)
- KeyMapperBody()
- {
- enable.setButtonText (tr ("Enable"));
- enable.onClick = [this] { change ([on = enable.getToggleState()] (vfx::KeyMapperSettings& s) { s.enabled = on; }); };
- addAndMakeVisible (enable);
- for (const auto& [id, text] : std::vector<std::pair<int, juce::String>> {
- { 1, tr ("All") }, { 2, tr ("All (gain-compensated)") }, { 3, tr ("Random") }, { 4, tr ("Cycle up") }, { 5, tr ("Cycle down") } })
- multimode.addItem (text, id);
- multimode.onChange = [this] { change ([m = multimode.getSelectedId() - 1] (vfx::KeyMapperSettings& s) { s.multimode = std::max (0, m); }); };
- ui::setHint (multimode, tr ("Multimode: how an input mapped to several notes plays them"));
- addAndMakeVisible (multimode);
- multimodeLabel.setText (tr ("Multimode"), juce::dontSendNotification);
- multimodeLabel.setFont (theme::font (theme::type::small));
- addAndMakeVisible (multimodeLabel);
- offset.caption = tr ("Offset");
- baseKey.caption = tr ("Base key");
- slide.caption = tr ("Pitch slide");
- addAndMakeVisible (offset);
- addAndMakeVisible (baseKey);
- addAndMakeVisible (slide);
- }
- void refresh (const std::vector<std::uint8_t>& fl) override
- {
- settings = vfx::parseKeyMapper (fl).value_or (vfx::defaultKeyMapper());
- enable.setToggleState (settings.enabled, juce::dontSendNotification);
- multimode.setSelectedId (std::clamp (settings.multimode, 0, 4) + 1, juce::dontSendNotification);
- repaint();
- offset.repaint();
- baseKey.repaint();
- slide.repaint();
- }
- int preferredHeight() const override { return 64 + 12 * 22 + 30; }
- int preferredWidth() const override { return 64 + columns * 15 + 16; }
- void resized() override
- {
- auto top = getLocalBounds().reduced (8, 4).removeFromTop (60);
- enable.setBounds (top.removeFromLeft (90).withSizeKeepingCentre (90, 24));
- top.removeFromLeft (8);
- offset.setBounds (top.removeFromLeft (64));
- baseKey.setBounds (top.removeFromLeft (64));
- slide.setBounds (top.removeFromLeft (64));
- top.removeFromLeft (16);
- auto mm = top.removeFromLeft (200);
- multimodeLabel.setBounds (mm.removeFromTop (22));
- multimode.setBounds (mm.removeFromTop (24));
- }
- void paint (juce::Graphics& g) override
- {
- const auto grid = gridArea();
- g.setColour (theme::colour::bg0);
- g.fillRect (grid);
- const auto cw = grid.getWidth() / (float) columns, rh = grid.getHeight() / 12.0f;
- // output columns: the octaves and the black keys, labelled every C
- for (int c = 0; c < columns; ++c)
- {
- const auto value = lowest + c;
- const auto pc = ((value % 12) + 12) % 12;
- const auto black = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
- const juce::Rectangle<float> col (grid.getX() + (float) c * cw, grid.getY(), cw, grid.getHeight());
- if (value >= 0 && value < 12) // the row's own octave, brighter (as FL draws it)
- {
- g.setColour (theme::colour::bg2);
- g.fillRect (col);
- }
- if (black)
- {
- g.setColour (theme::colour::bg0.withAlpha (0.55f));
- g.fillRect (col);
- }
- g.setColour (pc == 0 ? theme::colour::borderStrong : theme::colour::border);
- g.drawVerticalLine ((int) col.getX(), grid.getY(), grid.getBottom());
- if (pc == 0)
- {
- g.setColour (theme::colour::textSecondary);
- g.setFont (theme::font (theme::type::caption));
- const auto octave = value / 12;
- const auto name = pitchClassName (settings.baseKey);
- g.drawText (octave == 0 ? name : (octave > 0 ? name + " +" + juce::String (octave) : name + " " + juce::String (octave)),
- juce::Rectangle<float> (col.getX() + 2.0f, grid.getBottom() + 2.0f, 40.0f, 14.0f), juce::Justification::centredLeft);
- }
- }
- // input rows (top = B, bottom = C, as a keyboard on its side)
- for (int r = 0; r < 12; ++r)
- {
- const auto pc = 11 - r;
- const juce::Rectangle<float> row (grid.getX(), grid.getY() + (float) r * rh, grid.getWidth(), rh);
- g.setColour (theme::colour::border);
- g.drawHorizontalLine ((int) row.getY(), grid.getX(), grid.getRight());
- const auto black = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
- const juce::Rectangle<float> label (4.0f, row.getY(), grid.getX() - 8.0f, rh);
- g.setColour (black ? theme::colour::bg4 : theme::colour::bg6);
- g.fillRoundedRectangle (label.reduced (1.0f, 1.5f), 2.0f);
- g.setColour (theme::colour::textPrimary);
- g.setFont (theme::font (theme::type::small));
- g.drawText (pitchClassName (pc + settings.baseKey), label.reduced (6.0f, 0.0f), juce::Justification::centredLeft);
- // the unchanged note (a faint diagonal)
- g.setColour (theme::colour::bg5.withAlpha (0.55f));
- g.fillRect (cell (pc, pc).reduced (cw * 0.35f, rh * 0.35f));
- for (const auto value : settings.rows[(std::size_t) pc])
- {
- const auto c = cell (pc, value);
- if (c.isEmpty())
- continue;
- g.setColour (settings.enabled ? theme::colour::accent : theme::colour::textDisabled);
- g.fillRoundedRectangle (c.reduced (1.5f), 2.5f);
- }
- }
- g.setColour (theme::colour::borderStrong);
- g.drawRect (grid);
- if (hover.has_value())
- {
- g.setColour (theme::colour::textPrimary.withAlpha (0.5f));
- g.drawRect (cell (hover->first, hover->second), 1.0f);
- }
- }
- void mouseMove (const juce::MouseEvent& e) override
- {
- auto now = cellAt (e.position);
- if (now != hover)
- {
- hover = now;
- if (hover)
- ui::setHint (*this, tr ("{0} plays {1} - left-click: add / remove, right-click: only this note, middle-click or Alt+click: reset the row",
- { pitchClassName (hover->first + settings.baseKey), outputName (hover->first, hover->second) }));
- repaint();
- }
- }
- void mouseExit (const juce::MouseEvent&) override
- {
- hover.reset();
- repaint();
- }
- void mouseDown (const juce::MouseEvent& e) override
- {
- if (const auto grid = gridArea(); e.position.x < grid.getX() && e.position.y >= grid.getY() && e.position.y < grid.getBottom())
- {
- // a click on a row's label: back to the note itself
- const auto pc = 11 - std::clamp ((int) ((e.position.y - grid.getY()) / (grid.getHeight() / 12.0f)), 0, 11);
- change ([pc] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { pc }; });
- return;
- }
- const auto at = cellAt (e.position);
- if (! at)
- return;
- const auto [pc, value] = *at;
- if (e.mods.isMiddleButtonDown() || e.mods.isAltDown())
- change ([pc] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { pc }; });
- else if (e.mods.isPopupMenu())
- change ([pc, value] (vfx::KeyMapperSettings& s) { s.rows[(std::size_t) pc] = { value }; });
- else
- change ([pc, value] (vfx::KeyMapperSettings& s) {
- auto& row = s.rows[(std::size_t) pc];
- if (auto it = std::find (row.begin(), row.end(), value); it != row.end())
- row.erase (it);
- else
- row.push_back (value); // (FL keeps the order: Cycle and Random follow it)
- });
- }
- private:
- juce::Rectangle<float> gridArea() const
- {
- return getLocalBounds().toFloat().withTrimmedTop (68.0f).withTrimmedLeft (56.0f).withTrimmedRight (8.0f).withTrimmedBottom (20.0f);
- }
- juce::Rectangle<float> cell (int pc, int value) const
- {
- const auto grid = gridArea();
- const auto c = value - lowest;
- if (c < 0 || c >= columns)
- return {};
- const auto cw = grid.getWidth() / (float) columns, rh = grid.getHeight() / 12.0f;
- return { grid.getX() + (float) c * cw, grid.getY() + (float) (11 - pc) * rh, cw, rh };
- }
- std::optional<std::pair<int, int>> cellAt (juce::Point<float> p) const
- {
- const auto grid = gridArea();
- if (! grid.contains (p))
- return std::nullopt;
- const auto c = (int) ((p.x - grid.getX()) / (grid.getWidth() / (float) columns));
- const auto r = (int) ((p.y - grid.getY()) / (grid.getHeight() / 12.0f));
- return std::make_pair (11 - std::clamp (r, 0, 11), lowest + std::clamp (c, 0, columns - 1));
- }
- juce::String outputName (int pc, int value) const
- {
- const auto shift = value - pc;
- return pitchClassName (value + settings.baseKey) + " (" + (shift > 0 ? "+" : "") + juce::String (shift) + ")";
- }
- void change (std::function<void (vfx::KeyMapperSettings&)> f)
- {
- if (edit)
- edit ([f] (std::vector<std::uint8_t>& fl) {
- auto s = vfx::parseKeyMapper (fl).value_or (vfx::defaultKeyMapper());
- f (s);
- fl = vfx::writeKeyMapper (s);
- });
- }
- vfx::KeyMapperSettings settings = vfx::defaultKeyMapper();
- juce::ToggleButton enable;
- juce::ComboBox multimode;
- juce::Label multimodeLabel;
- Knob offset { intSpec (tr ("Offset (transpose)"), -12, 12, 0, [] (double v) { return (v > 0 ? "+" : "") + juce::String ((int) std::lround (v)); }),
- [this] { return (double) settings.offset; },
- [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.offset = (int) std::lround (v); }); } };
- 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)); }),
- [this] { return (double) settings.baseKey; },
- [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.baseKey = (int) std::lround (v); }); } };
- Knob slide { intSpec (tr ("Pitch slide: the new notes start at the played pitch and glide to their own (in steps)"), 0, 192, 0,
- [] (double v) { const auto u = (int) std::lround (v); return juce::String (u / 48) + ":" + juce::String (u % 48).paddedLeft ('0', 2); }),
- [this] { return (double) settings.slide; },
- [this] (double v) { change ([v] (vfx::KeyMapperSettings& s) { s.slide = (int) std::lround (v); }); } };
- std::optional<std::pair<int, int>> hover;
- };
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Color Mapper: per note colour, the voice output it goes to and the colour it leaves with
- class ColorMapperBody final : public Body
- {
- public:
- ColorMapperBody()
- {
- for (int c = 0; c < 16; ++c)
- {
- auto& port = ports[(std::size_t) c];
- auto& colour = colours[(std::size_t) c];
- for (int k = 0; k < 16; ++k)
- {
- port.addItem (tr ("Voice {0}", { juce::String (k + 1) }), k + 1);
- colour.addItem (tr ("Colour {0}", { juce::String (k + 1) }), k + 1);
- }
- 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); }); };
- 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); }); };
- ui::setHint (port, tr ("The voice output the notes of colour {0} go to", { juce::String (c + 1) }));
- ui::setHint (colour, tr ("The colour the notes of colour {0} leave with", { juce::String (c + 1) }));
- addAndMakeVisible (port);
- addAndMakeVisible (colour);
- }
- }
- void refresh (const std::vector<std::uint8_t>& fl) override
- {
- settings = vfx::parseColorMapper (fl).value_or (vfx::defaultColorMapper());
- for (int c = 0; c < 16; ++c)
- {
- ports[(std::size_t) c].setSelectedId (settings.port[(std::size_t) c] + 1, juce::dontSendNotification);
- colours[(std::size_t) c].setSelectedId (settings.colour[(std::size_t) c] + 1, juce::dontSendNotification);
- }
- repaint();
- }
- int preferredHeight() const override { return 28 + 8 * 28 + 8; }
- void resized() override
- {
- auto area = getLocalBounds().reduced (10, 4).withTrimmedTop (24);
- const auto half = area.getWidth() / 2;
- for (int c = 0; c < 16; ++c)
- {
- auto column = c < 8 ? area.withWidth (half - 10) : area.withTrimmedLeft (half + 10);
- auto row = column.withHeight (26).translated (0, (c % 8) * 28);
- row.removeFromLeft (70);
- ports[(std::size_t) c].setBounds (row.removeFromLeft ((row.getWidth() - 8) / 2));
- row.removeFromLeft (8);
- colours[(std::size_t) c].setBounds (row);
- }
- }
- void paint (juce::Graphics& g) override
- {
- auto area = getLocalBounds().reduced (10, 4);
- const auto half = area.getWidth() / 2;
- g.setFont (theme::font (theme::type::caption));
- g.setColour (theme::colour::textSecondary);
- for (const auto x : { area.getX(), area.getX() + half + 10 })
- {
- const auto w = half - 10 - 70;
- g.drawText (theme::caps (tr ("Note colour")), x, area.getY(), 70, 18, juce::Justification::centredLeft);
- g.drawText (theme::caps (tr ("Voice output")), x + 70, area.getY(), (w - 8) / 2, 18, juce::Justification::centredLeft);
- g.drawText (theme::caps (tr ("Colour")), x + 70 + (w - 8) / 2 + 8, area.getY(), (w - 8) / 2, 18, juce::Justification::centredLeft);
- }
- area.removeFromTop (24);
- for (int c = 0; c < 16; ++c)
- {
- auto column = c < 8 ? area.withWidth (half - 10) : area.withTrimmedLeft (half + 10);
- auto row = column.withHeight (26).translated (0, (c % 8) * 28).removeFromLeft (64).toFloat();
- g.setColour (theme::noteGroupHue (c));
- g.fillRoundedRectangle (row.removeFromLeft (26.0f).reduced (3.0f), 3.0f);
- g.setColour (theme::colour::textPrimary);
- g.setFont (theme::font (theme::type::small));
- g.drawText (juce::String (c + 1), row.reduced (4.0f, 0.0f), juce::Justification::centredLeft);
- }
- }
- private:
- void change (std::function<void (vfx::ColorMapperSettings&)> f)
- {
- if (edit)
- edit ([f] (std::vector<std::uint8_t>& fl) {
- auto s = vfx::parseColorMapper (fl).value_or (vfx::defaultColorMapper());
- f (s);
- fl = vfx::writeColorMapper (s);
- });
- }
- vfx::ColorMapperSettings settings = vfx::defaultColorMapper();
- std::array<juce::ComboBox, 16> ports, colours;
- };
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Level Scaler: a tab per voice property, its curve's controls on the left, the curve on the right
- class LevelScalerBody final : public Body
- {
- public:
- using L = vfx::LevelScalerSettings;
- LevelScalerBody()
- {
- 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") };
- for (int t = 0; t < L::tabCount; ++t)
- {
- auto& b = tabs[(std::size_t) t];
- b.setButtonText (tr (names[t]));
- b.setClickingTogglesState (false);
- b.setColour (juce::TextButton::buttonOnColourId, theme::colour::accentMuted);
- b.onClick = [this, t] { change ([t] (L& st) { st.selectedTab = t; }); };
- addAndMakeVisible (b);
- }
- auto knob = [this] (Knob& k, const juce::String& caption) {
- k.caption = caption;
- addAndMakeVisible (k);
- };
- knob (center, tr ("Center"));
- knob (multiply, tr ("Multiply"));
- knob (offset, tr ("Offset"));
- knob (tension, tr ("Tension"));
- knob (humanize, tr ("Humanize"));
- mode.addItem (tr ("Subtract"), 1);
- mode.addItem (tr ("Spread"), 2);
- mode.addItem (tr ("Add"), 3);
- mode.onChange = [this] { set (L::humanizeMode, mode.getSelectedId() - 1); };
- ui::setHint (mode, tr ("Humanize mode: the random part goes below the value, around it or above it"));
- addAndMakeVisible (mode);
- reset.setButtonText (tr ("Reset"));
- ui::setHint (reset, tr ("Reset all controls of this property"));
- reset.onClick = [this] { change ([] (L& st) { vfx::resetLevelScalerTab (st, st.selectedTab); }); };
- addAndMakeVisible (reset);
- }
- void refresh (const std::vector<std::uint8_t>& fl) override
- {
- settings = vfx::parseLevelScaler (fl).value_or (vfx::defaultLevelScaler());
- for (int t = 0; t < L::tabCount; ++t)
- tabs[(std::size_t) t].setToggleState (t == settings.selectedTab, juce::dontSendNotification);
- mode.setSelectedId (std::clamp (settings.at (settings.selectedTab, L::humanizeMode), 0, 2) + 1, juce::dontSendNotification);
- for (auto* k : { ¢er, &multiply, &offset, &tension, &humanize })
- k->repaint();
- repaint();
- }
- int preferredHeight() const override { return 360; }
- int preferredWidth() const override { return 700; }
- void resized() override
- {
- auto area = getLocalBounds().reduced (12, 6);
- auto top = area.removeFromTop (28);
- for (auto& b : tabs)
- b.setBounds (top.removeFromLeft (92).reduced (2, 0));
- area.removeFromTop (8);
- auto left = area.removeFromLeft (230);
- auto row1 = left.removeFromTop (92);
- for (auto* k : { ¢er, &multiply, &offset })
- k->setBounds (row1.removeFromLeft (76));
- auto row2 = left.removeFromTop (92);
- for (auto* k : { &tension, &humanize })
- k->setBounds (row2.removeFromLeft (76));
- auto side = row2.reduced (2, 14);
- mode.setBounds (side.removeFromTop (26));
- left.removeFromTop (8);
- reset.setBounds (left.removeFromTop (26).withWidth (100));
- graph = area.reduced (10, 0).toFloat();
- }
- void paint (juce::Graphics& g) override
- {
- if (graph.isEmpty())
- return;
- const auto tab = settings.selectedTab;
- const auto bipolar = tab >= L::finePitch;
- const auto lo = bipolar ? -1.0 : 0.0, hi = 1.0;
- g.setColour (theme::colour::bg0);
- g.fillRoundedRectangle (graph, 4.0f);
- g.setColour (theme::colour::border);
- for (int k = 1; k < 8; ++k)
- {
- g.drawVerticalLine ((int) (graph.getX() + graph.getWidth() * (float) k / 8.0f), graph.getY(), graph.getBottom());
- g.drawHorizontalLine ((int) (graph.getY() + graph.getHeight() * (float) k / 8.0f), graph.getX(), graph.getRight());
- }
- g.setColour (theme::colour::textDisabled);
- g.setFont (theme::font (theme::type::title, true));
- g.drawText (tabs[(std::size_t) tab].getButtonText(), graph.reduced (12.0f, 8.0f), juce::Justification::topLeft);
- auto point = [&] (double u, double y) {
- return juce::Point<float> (graph.getX() + (float) u * graph.getWidth(), graph.getBottom() - (float) ((y - lo) / (hi - lo)) * graph.getHeight());
- };
- g.setColour (theme::colour::borderStrong);
- g.drawLine (juce::Line<float> (point (0.0, lo), point (1.0, hi)), 1.0f);
- juce::Path curve, upper, lower;
- constexpr int steps = 160;
- for (int k = 0; k <= steps; ++k)
- {
- const auto u = (double) k / steps;
- const auto x = lo + u * (hi - lo);
- const auto p = point (u, vfx::levelScale (settings, tab, x, lo, hi, 0.5));
- const auto a = point (u, vfx::levelScale (settings, tab, x, lo, hi, 0.0));
- const auto b = point (u, vfx::levelScale (settings, tab, x, lo, hi, 1.0));
- if (k == 0)
- {
- curve.startNewSubPath (p);
- lower.startNewSubPath (a);
- upper.startNewSubPath (b);
- }
- else
- {
- curve.lineTo (p);
- lower.lineTo (a);
- upper.lineTo (b);
- }
- }
- if (settings.at (tab, L::humanize) > 0)
- {
- juce::Path band (upper);
- for (int k = steps; k >= 0; --k)
- {
- const auto u = (double) k / steps;
- band.lineTo (point (u, vfx::levelScale (settings, tab, lo + u * (hi - lo), lo, hi, 0.0)));
- }
- band.closeSubPath();
- g.setColour (theme::colour::accent.withAlpha (0.16f));
- g.fillPath (band);
- g.setColour (theme::colour::accent.withAlpha (0.45f));
- g.strokePath (upper, juce::PathStrokeType (1.0f));
- g.strokePath (lower, juce::PathStrokeType (1.0f));
- }
- g.setColour (theme::colour::accent);
- g.strokePath (curve, juce::PathStrokeType (2.2f));
- g.setColour (theme::colour::borderStrong);
- g.drawRoundedRectangle (graph, 4.0f, 1.0f);
- g.setColour (theme::colour::textSecondary);
- g.setFont (theme::font (theme::type::caption));
- g.drawText (tr ("in"), graph.withTrimmedTop (graph.getHeight() - 16.0f).reduced (6.0f, 0.0f), juce::Justification::centredRight);
- g.drawText (tr ("out"), graph.reduced (6.0f, 4.0f).withHeight (14.0f), juce::Justification::centredRight);
- }
- private:
- void change (std::function<void (L&)> f)
- {
- if (edit)
- edit ([f] (std::vector<std::uint8_t>& fl) {
- auto s = vfx::parseLevelScaler (fl).value_or (vfx::defaultLevelScaler());
- f (s);
- fl = vfx::writeLevelScaler (s);
- });
- }
- void set (int field, double v)
- {
- change ([field, v] (L& st) { st.at (st.selectedTab, field) = (int) std::lround (v); });
- }
- double get (int field) const { return settings.at (settings.selectedTab, field); }
- static juce::String percent (double v) { return juce::String (v * 100.0, 0) + "%"; }
- L settings = vfx::defaultLevelScaler();
- std::array<juce::TextButton, L::tabCount> tabs;
- Knob center { intSpec (tr ("Center: the input level the curve turns around"), 0, 8192, 0, [] (double v) { return percent (v / 8192.0); }),
- [this] { return get (L::center); }, [this] (double v) { set (L::center, v); } };
- Knob multiply { intSpec (tr ("Multiply: the slope around the center (negative inverts)"), -16384, 16384, 8192,
- [] (double v) { return juce::String::charToString (0xd7) + juce::String (v / 8192.0, 2); }),
- [this] { return get (L::multiply); }, [this] (double v) { set (L::multiply, v); } };
- Knob offset { intSpec (tr ("Offset: moves every value up or down"), -8192, 8192, 0,
- [] (double v) { return (v > 0 ? "+" : "") + percent (v / 8192.0); }),
- [this] { return get (L::offset); }, [this] (double v) { set (L::offset, v); } };
- Knob tension { intSpec (tr ("Tension: bends the curve (positive rises fast, negative starts slow)"), -8192, 8192, 0,
- [] (double v) { return (v > 0 ? "+" : "") + percent (v / 8192.0); }),
- [this] { return get (L::tension); }, [this] (double v) { set (L::tension, v); } };
- Knob humanize { intSpec (tr ("Humanize: a random amount for each note"), 0, 16384, 0, [] (double v) { return percent (v / 16384.0); }),
- [this] { return get (L::humanize); }, [this] (double v) { set (L::humanize, v); } };
- juce::ComboBox mode;
- juce::TextButton reset;
- juce::Rectangle<float> graph;
- };
- // A kind LYNX does not edit yet: says so (its notes pass through)
- class PendingBody final : public Body
- {
- public:
- void refresh (const std::vector<std::uint8_t>&) override {}
- int preferredHeight() const override { return 80; }
- void paint (juce::Graphics& g) override
- {
- g.setColour (theme::colour::textSecondary);
- g.setFont (theme::font (theme::type::body));
- g.drawFittedText (tr ("LYNX keeps this plugin's settings (from FL Studio) and passes its notes through unchanged."),
- getLocalBounds().reduced (14, 8), juce::Justification::topLeft, 3);
- }
- };
- std::unique_ptr<Body> makeBody (vfx::Kind kind)
- {
- switch (kind)
- {
- case vfx::Kind::keyMapper: return std::make_unique<KeyMapperBody>();
- case vfx::Kind::colorMapper: return std::make_unique<ColorMapperBody>();
- case vfx::Kind::levelScaler: return std::make_unique<LevelScalerBody>();
- default: break;
- }
- return std::make_unique<PendingBody>();
- }
- juce::String description (vfx::Kind kind)
- {
- switch (kind)
- {
- case vfx::Kind::keyMapper: return tr ("Maps each played note to other notes: transpose, scales, chords");
- case vfx::Kind::colorMapper: return tr ("Sends each note colour to its own voice output");
- case vfx::Kind::envelope: return tr ("Envelopes and LFOs on the notes' velocity, pitch, pan and Mod X / Y");
- case vfx::Kind::keyboardSplitter: return tr ("Splits the keyboard and the velocity into zones, one output each");
- case vfx::Kind::levelScaler: return tr ("Reshapes the notes' velocity, release, fine pitch, pan and Mod X / Y");
- case vfx::Kind::sequencer: return tr ("Plays step patterns and arpeggios from the held notes");
- case vfx::Kind::script: return tr ("FL's Python note scripts");
- }
- return {};
- }
- // ---------------------------------------------------------------------------------------------------------------
- class VfxEditor final : public juce::Component, private juce::ChangeListener
- {
- public:
- VfxEditor (AudioEngine& e, std::function<ChannelDef* (Song&)> l, int c) : engine (e), locate (std::move (l)), channel (c)
- {
- const auto s = state();
- kind = s ? s->kind : vfx::Kind::keyMapper;
- body = makeBody (kind);
- body->edit = [this] (Body::Edit change) { editState ([change] (vfx::State& st) { change (st.fl); }); };
- addAndMakeVisible (*body);
- presetButton.onClick = [this] { showPresetMenu(); };
- ui::setHint (presetButton, tr ("Presets: LYNX's own and, with FL Studio installed, FL's factory presets"));
- addAndMakeVisible (presetButton);
- for (auto* b : { &prev, &next })
- {
- addAndMakeVisible (b);
- b->setWantsKeyboardFocus (false);
- }
- prev.onClick = [this] { stepPreset (-1); };
- next.onClick = [this] { stepPreset (1); };
- // on a Channel Rack channel: the MIDI port its outputs go out on
- port.setSliderStyle (juce::Slider::IncDecButtons);
- port.setRange (0, 255, 1);
- port.setTextBoxStyle (juce::Slider::TextBoxLeft, false, 48, 22);
- port.onValueChange = [this] { editState ([v = (int) port.getValue()] (vfx::State& st) { st.port = v; }); };
- ui::setHint (port, tr ("Output port: output 1 goes out on this MIDI port, output 2 on the next one…; the plugins and Patchers "
- "that take that port play it"));
- portLabel.setText (tr ("Output port"), juce::dontSendNotification);
- portLabel.setFont (theme::font (theme::type::small));
- connect.setButtonText (tr ("Send to a plugin..."));
- connect.onClick = [this] { showConnectMenu(); };
- ui::setHint (connect, tr ("Sets a plugin channel's MIDI input port to this port, so it plays what this channel sends"));
- for (juce::Component* comp : { (juce::Component*) &port, (juce::Component*) &portLabel, (juce::Component*) &connect })
- {
- addAndMakeVisible (comp);
- comp->setVisible (channel >= 0);
- }
- engine.addChangeListener (this);
- refresh();
- setSize (std::max (620, body->preferredWidth()), 44 + (channel >= 0 ? 34 : 0) + body->preferredHeight());
- }
- ~VfxEditor() override { engine.removeChangeListener (this); }
- void paint (juce::Graphics& g) override
- {
- g.fillAll (theme::colour::bg2);
- auto top = getLocalBounds().removeFromTop (40).reduced (14, 0);
- g.setColour (theme::colour::textPrimary);
- g.setFont (theme::font (theme::type::title, true));
- g.drawText (vfx::flName (kind), top.removeFromLeft (220), juce::Justification::centredLeft);
- g.setColour (theme::colour::textSecondary);
- g.setFont (theme::font (theme::type::caption));
- g.drawText (description (kind), top.withTrimmedRight (300), juce::Justification::centredLeft);
- g.setColour (theme::colour::border);
- g.drawHorizontalLine (40, 0.0f, (float) getWidth());
- }
- void resized() override
- {
- auto area = getLocalBounds();
- auto top = area.removeFromTop (40).reduced (10, 7);
- next.setBounds (top.removeFromRight (26));
- presetButton.setBounds (top.removeFromRight (210));
- prev.setBounds (top.removeFromRight (26));
- area.removeFromTop (4);
- if (channel >= 0)
- {
- auto row = area.removeFromTop (30).reduced (14, 2);
- portLabel.setBounds (row.removeFromLeft (90));
- port.setBounds (row.removeFromLeft (130));
- row.removeFromLeft (10);
- connect.setBounds (row.removeFromLeft (170));
- }
- body->setBounds (area);
- }
- private:
- std::optional<vfx::State> state() const
- {
- const auto* c = locate (const_cast<Song&> (engine.getSong()));
- return c != nullptr ? vfx::readState (c->pluginState) : std::nullopt;
- }
- void editState (std::function<void (vfx::State&)> change)
- {
- engine.editSong ([this, change] (Song& song) {
- auto* c = locate (song);
- if (c == nullptr || c->instrument != InstrumentKind::vfx)
- return;
- auto s = vfx::readState (c->pluginState);
- if (! s)
- return;
- change (*s);
- c->pluginState = vfx::writeState (*s);
- });
- }
- struct PresetEntry
- {
- juce::String folder, name;
- std::vector<std::uint8_t> fl; // LYNX's own
- juce::File file; // FL's
- };
- std::vector<PresetEntry> presets() const
- {
- std::vector<PresetEntry> out;
- for (auto& p : vfx::builtInPresets (kind))
- out.push_back ({ juce::String::fromUTF8 (p.folder.c_str()), juce::String::fromUTF8 (p.name.c_str()), std::move (p.fl), {} });
- const auto roots = findFlStudioRoots();
- if (! roots.data.empty())
- {
- const auto base = juce::File (juce::String::fromUTF8 (roots.data.c_str())).getChildFile ("Patches/Plugin presets/Effects").getChildFile (vfx::flName (kind));
- auto files = base.findChildFiles (juce::File::findFiles, true, "*.fst");
- std::sort (files.begin(), files.end(), [] (const juce::File& a, const juce::File& b) { return a.getFullPathName().compareNatural (b.getFullPathName()) < 0; });
- for (const auto& f : files)
- {
- const auto folder = f.getParentDirectory() == base ? juce::String() : f.getParentDirectory().getFileName();
- out.push_back ({ "FL Studio" + (folder.isEmpty() ? juce::String() : " / " + folder), f.getFileNameWithoutExtension(), {}, f });
- }
- }
- return out;
- }
- void apply (const PresetEntry& p)
- {
- auto fl = p.fl;
- if (p.file != juce::File())
- {
- juce::MemoryBlock data;
- if (! p.file.loadFileAsData (data))
- return;
- const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
- const auto state = vfx::stateFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), kind);
- if (! state)
- return;
- fl = *state;
- }
- presetName = p.name;
- editState ([fl] (vfx::State& st) { st.fl = fl; });
- presetButton.setButtonText (presetName);
- }
- void showPresetMenu()
- {
- const auto all = presets();
- juce::PopupMenu menu;
- menu.addItem (tr ("Default"), [this] {
- presetName = tr ("Default");
- editState ([k = kind] (vfx::State& st) { st.fl = vfx::defaultState (k); });
- presetButton.setButtonText (presetName);
- });
- std::map<juce::String, juce::PopupMenu> folders;
- std::vector<juce::String> order;
- for (std::size_t i = 0; i < all.size(); ++i)
- {
- const auto folder = all[i].folder.isEmpty() ? tr ("Presets") : all[i].folder;
- if (! folders.count (folder))
- order.push_back (folder);
- folders[folder].addItem (all[i].name, [this, entry = all[i]] { apply (entry); });
- }
- for (const auto& f : order)
- menu.addSubMenu (f, folders[f]);
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&presetButton));
- }
- void stepPreset (int delta)
- {
- const auto all = presets();
- if (all.empty())
- return;
- int at = -1;
- for (int i = 0; i < (int) all.size(); ++i)
- if (all[(std::size_t) i].name == presetName)
- at = i;
- at = at < 0 ? (delta > 0 ? 0 : (int) all.size() - 1) : (at + delta + (int) all.size()) % (int) all.size();
- apply (all[(std::size_t) at]);
- }
- void showConnectMenu()
- {
- juce::PopupMenu menu;
- const auto& song = engine.getSong();
- const auto s = state();
- const auto p = s ? s->port : 0;
- for (int c = 0; c < (int) song.channels.size(); ++c)
- if (song.channels[(std::size_t) c].instrument == InstrumentKind::plugin)
- menu.addItem (juce::String::fromUTF8 (song.channels[(std::size_t) c].name.c_str()), true,
- song.channels[(std::size_t) c].plugin.midiInPort == p, [this, c, p] {
- engine.editSong ([c, p] (Song& song) {
- auto& in = song.channels[(std::size_t) c].plugin.midiInPort;
- in = in == p ? -1 : p;
- });
- });
- if (menu.getNumItems() == 0)
- menu.addItem (tr ("(no plugin channel in the Channel Rack)"), false, false, [] {});
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&connect));
- }
- void refresh()
- {
- const auto s = state();
- if (! s)
- return;
- body->refresh (s->fl);
- port.setValue (s->port, juce::dontSendNotification);
- if (presetName.isEmpty())
- presetButton.setButtonText (tr ("Presets"));
- }
- void changeListenerCallback (juce::ChangeBroadcaster*) override { refresh(); }
- AudioEngine& engine;
- std::function<ChannelDef* (Song&)> locate;
- int channel;
- vfx::Kind kind;
- std::unique_ptr<Body> body;
- juce::TextButton presetButton, prev { juce::String::fromUTF8 ("\xe2\x97\x80") }, next { juce::String::fromUTF8 ("\xe2\x96\xb6") };
- juce::String presetName;
- juce::Slider port;
- juce::Label portLabel;
- juce::TextButton connect;
- };
- } // namespace
- std::unique_ptr<juce::Component> makeVfxEditor (AudioEngine& engine, std::function<ChannelDef* (Song&)> locate, int channel)
- {
- return std::make_unique<VfxEditor> (engine, std::move (locate), channel);
- }
- std::unique_ptr<juce::Component> makeVfxEditor (AudioEngine& engine, int channel)
- {
- return makeVfxEditor (
- engine,
- [channel] (Song& song) -> ChannelDef* {
- return channel >= 0 && channel < (int) song.channels.size() ? &song.channels[(std::size_t) channel] : nullptr;
- },
- channel);
- }
- } // namespace lynx
|