| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- #include "ui/SimpleWorkflow.h"
- #include <algorithm>
- #include "core/LoopStarter.h"
- #include "core/MidiImport.h"
- #include "engine/AudioEngine.h"
- #include "engine/MidiFileIo.h"
- #include "ui/FileChoosers.h"
- #include "ui/I18n.h"
- #include "ui/LoopStarterWindow.h"
- #include "ui/QuickPick.h"
- #include "ui/Theme.h"
- namespace lynx
- {
- namespace
- {
- using simpletracks::Kind;
- // --- the tiles' pictures: LYNX's own simple line drawings in a 1×1 box ------------------------------------------------
- enum class Glyph { voice, instrument, drums, sampler, guitar, import, loops };
- juce::Path glyphPath (Glyph glyph)
- {
- juce::Path p;
- switch (glyph)
- {
- case Glyph::voice: // a microphone on its stand
- p.addRoundedRectangle (0.36f, 0.08f, 0.28f, 0.48f, 0.14f);
- p.startNewSubPath (0.24f, 0.44f);
- p.cubicTo (0.24f, 0.78f, 0.76f, 0.78f, 0.76f, 0.44f);
- p.startNewSubPath (0.5f, 0.72f);
- p.lineTo (0.5f, 0.9f);
- p.startNewSubPath (0.34f, 0.9f);
- p.lineTo (0.66f, 0.9f);
- break;
- case Glyph::instrument: // three octave keys
- p.addRectangle (0.1f, 0.22f, 0.8f, 0.56f);
- for (float x : { 0.3f, 0.5f, 0.7f })
- {
- p.startNewSubPath (x, 0.22f);
- p.lineTo (x, 0.78f);
- }
- for (float x : { 0.25f, 0.45f, 0.65f })
- p.addRectangle (x, 0.22f, 0.1f, 0.3f);
- break;
- case Glyph::drums: // a drum with two sticks
- p.addEllipse (0.16f, 0.4f, 0.68f, 0.2f);
- p.startNewSubPath (0.16f, 0.5f);
- p.lineTo (0.16f, 0.78f);
- p.quadraticTo (0.5f, 0.94f, 0.84f, 0.78f);
- p.lineTo (0.84f, 0.5f);
- p.startNewSubPath (0.28f, 0.1f);
- p.lineTo (0.46f, 0.42f);
- p.startNewSubPath (0.72f, 0.1f);
- p.lineTo (0.54f, 0.42f);
- break;
- case Glyph::sampler: // a waveform over a key
- {
- const float h[] { 0.1f, 0.24f, 0.4f, 0.18f, 0.32f, 0.12f, 0.06f };
- for (int i = 0; i < 7; ++i)
- {
- const auto x = 0.14f + 0.12f * (float) i;
- p.startNewSubPath (x, 0.42f - h[i]);
- p.lineTo (x, 0.42f + h[i]);
- }
- p.addRectangle (0.14f, 0.7f, 0.72f, 0.16f);
- break;
- }
- case Glyph::guitar: // a body and a neck
- p.addEllipse (0.1f, 0.5f, 0.34f, 0.34f);
- p.addEllipse (0.22f, 0.36f, 0.26f, 0.26f);
- p.startNewSubPath (0.38f, 0.56f);
- p.lineTo (0.84f, 0.1f);
- p.addEllipse (0.24f, 0.62f, 0.08f, 0.08f);
- break;
- case Glyph::import: // an arrow into a tray
- p.startNewSubPath (0.5f, 0.1f);
- p.lineTo (0.5f, 0.6f);
- p.startNewSubPath (0.32f, 0.44f);
- p.lineTo (0.5f, 0.62f);
- p.lineTo (0.68f, 0.44f);
- p.startNewSubPath (0.14f, 0.62f);
- p.lineTo (0.14f, 0.86f);
- p.lineTo (0.86f, 0.86f);
- p.lineTo (0.86f, 0.62f);
- break;
- case Glyph::loops: // a circle of arrows
- p.addCentredArc (0.5f, 0.5f, 0.32f, 0.32f, 0.0f, 0.4f, 5.6f, true);
- p.startNewSubPath (0.62f, 0.12f);
- p.lineTo (0.7f, 0.22f);
- p.lineTo (0.56f, 0.28f);
- break;
- }
- return p;
- }
- void drawGlyph (juce::Graphics& g, Glyph glyph, juce::Rectangle<float> box, juce::Colour tint)
- {
- g.setColour (tint.withAlpha (0.2f));
- g.fillRoundedRectangle (box, theme::radius::lg);
- g.setColour (tint.withAlpha (0.55f));
- g.drawRoundedRectangle (box.reduced (0.5f), theme::radius::lg, 1.0f);
- const auto inner = box.reduced (box.getWidth() * 0.18f);
- auto path = glyphPath (glyph);
- path.applyTransform (juce::AffineTransform::scale (inner.getWidth(), inner.getHeight()).translated (inner.getX(), inner.getY()));
- g.setColour (theme::colour::textPrimary);
- g.strokePath (path, juce::PathStrokeType (1.6f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
- }
- // A tile: picture, name, one line of help. Clicking anywhere on it runs `onClick`.
- class Tile final : public juce::Component,
- public juce::SettableTooltipClient
- {
- public:
- Tile (Glyph g, juce::Colour c, juce::String n, juce::String h) : glyph (g), tint (c), name (std::move (n)), help (std::move (h))
- {
- setWantsKeyboardFocus (true);
- setMouseCursor (juce::MouseCursor::PointingHandCursor);
- setTitle (name);
- setDescription (help);
- setTooltip (help);
- }
- std::function<void()> onClick;
- // the part of the tile on the right that holds other controls (the Guitar | Bass switch): the text stops before it
- int rightReserve = 0;
- void paint (juce::Graphics& g) override
- {
- auto r = getLocalBounds().toFloat().reduced (1.0f);
- const auto hover = isMouseOver (true), down = isMouseButtonDown();
- g.setColour (down ? theme::colour::bg5 : hover ? theme::colour::bg4 : theme::colour::bg3);
- g.fillRoundedRectangle (r, theme::radius::lg);
- g.setColour (hasKeyboardFocus (false) ? theme::colour::accent : hover ? theme::colour::borderStrong : theme::colour::border);
- g.drawRoundedRectangle (r.reduced (0.5f), theme::radius::lg, 1.0f);
- const auto side = juce::jmin (44.0f, r.getHeight() - 16.0f);
- drawGlyph (g, glyph, { r.getX() + 10.0f, r.getCentreY() - side / 2.0f, side, side }, tint);
- auto text = getLocalBounds().withTrimmedLeft ((int) side + 22).withTrimmedRight (8 + rightReserve).reduced (0, 8);
- g.setColour (theme::colour::textPrimary);
- g.setFont (theme::font (theme::type::title, true));
- g.drawFittedText (name, text.removeFromTop (20), juce::Justification::centredLeft, 1, 0.85f);
- g.setColour (theme::colour::textSecondary);
- g.setFont (theme::font (theme::type::small));
- g.drawFittedText (help, text, juce::Justification::topLeft, 2, 0.9f);
- }
- void mouseEnter (const juce::MouseEvent&) override { repaint(); }
- void mouseExit (const juce::MouseEvent&) override { repaint(); }
- void mouseDown (const juce::MouseEvent&) override { repaint(); }
- void mouseUp (const juce::MouseEvent& e) override
- {
- repaint();
- if (e.mouseWasClicked() && getLocalBounds().contains (e.getPosition()) && onClick)
- onClick();
- }
- bool keyPressed (const juce::KeyPress& key) override
- {
- if ((key == juce::KeyPress::returnKey || key == juce::KeyPress::spaceKey) && onClick)
- {
- onClick();
- return true;
- }
- return false;
- }
- void focusGained (FocusChangeType) override { repaint(); }
- void focusLost (FocusChangeType) override { repaint(); }
- private:
- Glyph glyph;
- juce::Colour tint;
- juce::String name, help;
- };
- juce::Colour kindColour (Kind kind) { return juce::Colour (simpletracks::defaultColour (kind)); }
- // The name a new track of `kind` takes, in the UI's language
- std::string trackNameFor (Kind kind)
- {
- switch (kind)
- {
- case Kind::voice: return tr ("Voice").toStdString();
- case Kind::instrument: return tr ("Instrument").toStdString();
- case Kind::drums: return tr ("Drums").toStdString();
- case Kind::sampler: return tr ("Sampler").toStdString();
- case Kind::guitar: return tr ("Guitar").toStdString();
- case Kind::bass: return trc ("@instrument: Bass").toStdString();
- case Kind::audio: break;
- }
- return tr ("Audio").toStdString();
- }
- } // namespace
- // --- the chooser: the tiles, then (after Drums) the starter beats -----------------------------------------------------
- class SimpleWorkflow::Chooser final : public juce::Component
- {
- public:
- explicit Chooser (SimpleWorkflow& w)
- : owner (w),
- voice (Glyph::voice, kindColour (Kind::voice), tr ("Voice / audio"), tr ("Record your voice or any sound from a microphone.")),
- instrument (Glyph::instrument, kindColour (Kind::instrument), tr ("Instrument"), tr ("Play a sound with the computer keys, the mouse or a MIDI keyboard.")),
- drums (Glyph::drums, kindColour (Kind::drums), tr ("Drums"), tr ("Make a beat on a step grid, or start from a starter beat.")),
- sampler (Glyph::sampler, kindColour (Kind::sampler), tr ("Sampler"), tr ("Turn any sound into an instrument you play on keys.")),
- guitar (Glyph::guitar, kindColour (Kind::guitar), tr ("Guitar / bass"), tr ("Plug in a guitar or a bass and play it through the track's effects.")),
- import (Glyph::import, kindColour (Kind::audio), tr ("Import"), tr ("Bring in audio or MIDI files: one track each.")),
- loops (Glyph::loops, kindColour (Kind::audio), tr ("Loops"), tr ("Browse loops and drag them onto the timeline."))
- {
- setWantsKeyboardFocus (true);
- title.setText (tr ("New track"), juce::dontSendNotification);
- title.setFont (theme::font (theme::type::display, true));
- title.setJustificationType (juce::Justification::centred);
- footer.setText (tr ("Each track gets its own channel and mixer track."), juce::dontSendNotification);
- footer.setFont (theme::font (theme::type::small));
- footer.setColour (juce::Label::textColourId, theme::colour::textSecondary);
- footer.setJustificationType (juce::Justification::centred);
- close.setButtonText (juce::String::fromUTF8 ("\xc3\x97"));
- close.setTooltip (tr ("Close"));
- close.onClick = [this] { owner.closeByHand(); };
- voice.onClick = [this] { owner.addAudio (Kind::voice); };
- instrument.onClick = [this] { owner.chooseInstrument (instrument); };
- drums.onClick = [this] { showStarterPage(); };
- sampler.onClick = [this] { owner.chooseSample(); };
- guitar.onClick = [this] { owner.addAudio (bassButton.getToggleState() ? Kind::bass : Kind::guitar); };
- import.onClick = [this] { owner.importFiles(); };
- loops.onClick = [this] {
- if (owner.host.showLoops)
- owner.host.showLoops();
- };
- guitar.rightReserve = 168;
- guitarButton.setButtonText (tr ("Guitar"));
- bassButton.setButtonText (trc ("@instrument: Bass"));
- for (auto* b : { &guitarButton, &bassButton })
- {
- b->setClickingTogglesState (true);
- b->setRadioGroupId (1184);
- guitar.addAndMakeVisible (*b);
- }
- guitarButton.setToggleState (true, juce::dontSendNotification);
- for (auto* c : std::initializer_list<juce::Component*> { &title, &close, &voice, &instrument, &drums, &sampler, &guitar, &import, &loops, &footer })
- addAndMakeVisible (*c);
- // the starter-beat page
- starterTitle.setText (tr ("Starter beat"), juce::dontSendNotification);
- starterTitle.setFont (theme::font (theme::type::display, true));
- starterTitle.setJustificationType (juce::Justification::centred);
- back.setButtonText (juce::String::fromUTF8 ("\xe2\x80\xb9 ") + tr ("Back"));
- back.onClick = [this] { showTilesPage(); };
- useTempo.setButtonText (tr ("Use the suggested tempo"));
- useTempo.setToggleState (true, juce::dontSendNotification);
- addDrums.setButtonText (tr ("Add drums"));
- addDrums.onClick = [this] {
- const auto picked = std::find_if (genreButtons.begin(), genreButtons.end(), [] (const auto& b) { return b->getToggleState(); });
- const auto index = picked == genreButtons.end() ? 0 : (int) (picked - genreButtons.begin());
- owner.addDrums (index == 0 ? std::string() : genreIds[(std::size_t) index], index != 0, useTempo.getToggleState());
- };
- for (auto* c : std::initializer_list<juce::Component*> { &starterTitle, &back, &useTempo, &addDrums })
- addChildComponent (*c);
- }
- void showStarterPage()
- {
- if (genreButtons.empty())
- {
- // Empty, then Loop Starter's genres (FL's when installed, else LYNX's own) with their suggested tempo
- genreIds.push_back ({});
- genreButtons.push_back (std::make_unique<juce::TextButton> (tr ("Empty grid")));
- if (const auto* config = loopstarterui::sources().config)
- for (const auto* genre : config->listed())
- {
- genreIds.push_back (genre->id);
- genreButtons.push_back (std::make_unique<juce::TextButton> (juce::String::fromUTF8 (genre->label.c_str()) + " "
- + tr ("{0} BPM", { juce::String (juce::roundToInt (genre->defaultTempo)) })));
- }
- for (auto& b : genreButtons)
- {
- b->setClickingTogglesState (true);
- b->setRadioGroupId (2184);
- addChildComponent (*b);
- }
- genreButtons[genreButtons.size() > 1 ? 1 : 0]->setToggleState (true, juce::dontSendNotification);
- }
- starterPage = true;
- updatePage();
- }
- void showTilesPage()
- {
- starterPage = false;
- updatePage();
- }
- void paint (juce::Graphics& g) override
- {
- g.fillAll (theme::colour::bg0.withAlpha (0.72f)); // the timeline stays faintly visible behind
- const auto c = card().toFloat();
- g.setColour (juce::Colours::black.withAlpha (0.35f));
- g.fillRoundedRectangle (c.translated (0.0f, 4.0f).expanded (2.0f), theme::radius::lg + 2.0f);
- g.setColour (theme::colour::bg2);
- g.fillRoundedRectangle (c, theme::radius::lg);
- g.setColour (theme::colour::borderStrong);
- g.drawRoundedRectangle (c.reduced (0.5f), theme::radius::lg, 1.0f);
- g.setColour (theme::colour::accent);
- g.fillRect (c.getX() + 24.0f, c.getY() + 52.0f, c.getWidth() - 48.0f, 1.0f);
- }
- void resized() override
- {
- auto c = card().reduced (20, 14);
- auto top = c.removeFromTop (34);
- close.setBounds (top.removeFromRight (28).withSizeKeepingCentre (28, 28));
- back.setBounds (top.removeFromLeft (90).withSizeKeepingCentre (90, 28));
- title.setBounds (top);
- starterTitle.setBounds (top);
- c.removeFromTop (16);
- footer.setBounds (c.removeFromBottom (20));
- c.removeFromBottom (6);
- // tiles: two columns, then Guitar / bass across, then Import and Loops
- const auto gap = 10;
- const auto rowH = juce::jlimit (56, 78, (c.getHeight() - 4 * gap) / 5);
- auto tiles = c;
- auto row = [&] { auto r = tiles.removeFromTop (rowH); tiles.removeFromTop (gap); return r; };
- auto pair = [&] (juce::Component& a, juce::Component& b) {
- auto r = row();
- a.setBounds (r.removeFromLeft ((r.getWidth() - gap) / 2));
- r.removeFromLeft (gap);
- b.setBounds (r);
- };
- pair (voice, instrument);
- pair (drums, sampler);
- auto g = row();
- guitar.setBounds (g);
- pair (import, loops);
- auto sw = guitar.getLocalBounds().removeFromRight (guitar.rightReserve).reduced (8, 0).withSizeKeepingCentre (guitar.rightReserve - 16, 28);
- guitarButton.setBounds (sw.removeFromLeft (sw.getWidth() / 2));
- bassButton.setBounds (sw);
- // the starter-beat page: genres in three columns, the tempo switch and Add
- auto s = card().reduced (20, 14).withTrimmedTop (50);
- auto bottom = s.removeFromBottom (34);
- addDrums.setBounds (bottom.removeFromRight (140));
- useTempo.setBounds (bottom.removeFromLeft (bottom.getWidth() - 10));
- s.removeFromBottom (10);
- const auto columns = 3, buttonH = 32;
- const auto w = (s.getWidth() - (columns - 1) * gap) / columns;
- for (int i = 0; i < (int) genreButtons.size(); ++i)
- genreButtons[(std::size_t) i]->setBounds (s.getX() + (i % columns) * (w + gap), s.getY() + (i / columns) * (buttonH + 8), w, buttonH);
- }
- bool keyPressed (const juce::KeyPress& key) override
- {
- if (key == juce::KeyPress::escapeKey)
- {
- if (starterPage)
- showTilesPage();
- else
- owner.closeByHand();
- return true;
- }
- return false;
- }
- // clicks on the dimmed area outside the card close the chooser
- void mouseUp (const juce::MouseEvent& e) override
- {
- if (e.mouseWasClicked() && ! card().contains (e.getPosition()))
- owner.closeByHand();
- }
- private:
- juce::Rectangle<int> card() const
- {
- const auto w = juce::jmin (680, getWidth() - 24), h = juce::jmin (520, getHeight() - 24);
- return getLocalBounds().withSizeKeepingCentre (juce::jmax (320, w), juce::jmax (300, h));
- }
- void updatePage()
- {
- for (auto* c : std::initializer_list<juce::Component*> { &title, &voice, &instrument, &drums, &sampler, &guitar, &import, &loops, &footer })
- c->setVisible (! starterPage);
- for (auto* c : std::initializer_list<juce::Component*> { &starterTitle, &back, &useTempo, &addDrums })
- c->setVisible (starterPage);
- for (auto& b : genreButtons)
- b->setVisible (starterPage);
- resized();
- repaint();
- }
- SimpleWorkflow& owner;
- juce::Label title, footer, starterTitle;
- juce::TextButton close, back, addDrums, guitarButton, bassButton;
- juce::ToggleButton useTempo;
- Tile voice, instrument, drums, sampler, guitar, import, loops;
- std::vector<std::unique_ptr<juce::TextButton>> genreButtons;
- std::vector<std::string> genreIds;
- bool starterPage = false;
- friend class SimpleWorkflow;
- };
- // --- the workflow ------------------------------------------------------------------------------------------------------
- namespace
- {
- // "+ Add track" in the Playlist's title bar
- class AddTrackButton final : public juce::TextButton
- {
- public:
- AddTrackButton() : juce::TextButton ("+ " + tr ("Add track"))
- {
- setTooltip (tr ("Add a track: voice, instrument, drums, sampler, guitar or bass, or import files"));
- }
- };
- } // namespace
- SimpleWorkflow::SimpleWorkflow (AudioEngine& e, Host h) : engine (e), host (std::move (h))
- {
- instances.fetch_add (1, std::memory_order_relaxed);
- auto button = std::make_unique<AddTrackButton>();
- button->onClick = [this] { showChooser(); };
- addButton = std::move (button);
- }
- SimpleWorkflow::~SimpleWorkflow()
- {
- if (active)
- left();
- }
- void SimpleWorkflow::entered()
- {
- active = true;
- if (host.setTitleAccessory)
- host.setTitleAccessory (addButton.get(), 110);
- songChanged();
- }
- void SimpleWorkflow::left()
- {
- active = false;
- hideChooser();
- if (host.setTitleAccessory)
- host.setTitleAccessory (nullptr, 0);
- }
- void SimpleWorkflow::songChanged()
- {
- if (! active)
- return;
- const auto& song = engine.getSong();
- if (simpletracks::isEmptySong (song))
- {
- if (song.projectId != dismissedFor && ! isChooserShowing())
- showChooser();
- }
- else if (isChooserShowing() && ! chooser->starterPage)
- hideChooser(); // (something was added some other way: an undo, a drop, a loaded project)
- }
- void SimpleWorkflow::showChooser()
- {
- if (chooser == nullptr)
- chooser = std::make_unique<Chooser> (*this);
- chooser->showTilesPage();
- if (host.setOverlay)
- host.setOverlay (chooser.get());
- if (chooser->isShowing())
- chooser->grabKeyboardFocus();
- }
- void SimpleWorkflow::hideChooser()
- {
- if (chooser != nullptr && chooser->getParentComponent() != nullptr && host.setOverlay)
- host.setOverlay (nullptr);
- }
- bool SimpleWorkflow::isChooserShowing() const noexcept { return chooser != nullptr && chooser->getParentComponent() != nullptr; }
- void SimpleWorkflow::closeByHand()
- {
- dismissedFor = engine.getSong().projectId;
- hideChooser();
- }
- juce::Component* SimpleWorkflow::chooserForSnapshot()
- {
- if (chooser == nullptr)
- chooser = std::make_unique<Chooser> (*this);
- return chooser.get();
- }
- void SimpleWorkflow::showStarterPageForSnapshot()
- {
- chooserForSnapshot();
- chooser->showStarterPage();
- }
- bool SimpleWorkflow::add (simpletracks::Request request)
- {
- // tried on a copy first, so a refused track leaves no undo step behind
- auto trial = engine.getSong();
- if (const auto r = simpletracks::addTrack (trial, request); ! r.ok())
- {
- const auto text = r.error == simpletracks::Error::mixerFull ? tr ("There are no available mixer tracks.")
- : r.error == simpletracks::Error::noFreeTrack ? tr ("No free Playlist track is left.")
- : tr ("This sound can't be played as an instrument.");
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("New track"), text);
- return false;
- }
- simpletracks::Result r;
- engine.editSong ([&] (Song& s) { r = simpletracks::addTrack (s, request); });
- if (! r.ok())
- return false;
- if (simpletracks::isAudioKind (request.kind) && request.kind != Kind::audio && r.insert > 0)
- engine.setInsertArmed (r.insert, true);
- if (! r.channels.empty() && ! simpletracks::isAudioKind (request.kind))
- engine.setSelectedChannel (r.channels.front());
- hideChooser();
- if (host.trackAdded)
- host.trackAdded (r.track);
- return true;
- }
- void SimpleWorkflow::addAudio (Kind kind)
- {
- simpletracks::Request q;
- q.kind = kind;
- q.name = trackNameFor (kind);
- q.input = 0; // the device's first input, mono (a microphone or an instrument cable)
- q.inputStereo = false;
- add (std::move (q));
- }
- void SimpleWorkflow::chooseInstrument (juce::Component& tile)
- {
- // the Channel Rack's own generator list (the same instruments, plugins and Soundfonts)
- juce::PopupMenu menu;
- quickpick::addGeneratorMenu (
- menu, engine,
- [this] (ChannelDef c) {
- simpletracks::Request q;
- q.kind = c.instrument == InstrumentKind::sampler ? Kind::sampler : Kind::instrument;
- q.name = trackNameFor (q.kind);
- q.channel = std::move (c);
- add (std::move (q));
- },
- [this] { chooseSample(); }, nullptr, chooser.get());
- quickpick::showMenu (menu, juce::PopupMenu::Options().withTargetComponent (&tile), {});
- }
- void SimpleWorkflow::chooseSample()
- {
- fileChooser = ui::makeFileChooser (tr ("Choose sample"), juce::File(), engine.getSampleLibrary().wildcard());
- ui::launchChooser (fileChooser, juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
- [this] (const juce::FileChooser& fc) {
- const auto file = fc.getResult();
- if (! file.existsAsFile())
- return;
- if (engine.getSampleLibrary().load (file.getFullPathName()) == nullptr)
- {
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Could not import"),
- tr ("Could not read {0}", { file.getFileName() }));
- return;
- }
- ChannelDef c;
- c.instrument = InstrumentKind::sampler;
- c.samplePath = file.getFullPathName().toStdString();
- simpletracks::Request q;
- q.kind = Kind::sampler;
- q.name = file.getFileNameWithoutExtension().toStdString();
- q.channel = std::move (c);
- add (std::move (q));
- });
- }
- void SimpleWorkflow::importFiles()
- {
- fileChooser = ui::makeFileChooser (tr ("Import audio or MIDI"), juce::File(), engine.getSampleLibrary().wildcard() + ";*.mid;*.midi");
- ui::launchChooser (fileChooser,
- juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles | juce::FileBrowserComponent::canSelectMultipleItems,
- [this] (const juce::FileChooser& fc) {
- juce::StringArray failed;
- for (const auto& file : fc.getResults())
- {
- if (file.hasFileExtension ("mid;midi"))
- {
- // one instrument track (LYNX's synth) per MIDI track with notes, from the song's start
- juce::String error;
- const auto midi = MidiFileIo::parseFile (file, error);
- if (! midi)
- {
- failed.add (file.getFileName());
- continue;
- }
- midiimport::Options options;
- bool any = false;
- for (auto& group : midiimport::groups (*midi, options))
- {
- if (group.notes.empty())
- continue;
- simpletracks::Request q;
- q.kind = Kind::instrument;
- q.name = group.name.empty() ? file.getFileNameWithoutExtension().toStdString() : group.name;
- ChannelDef synth;
- synth.instrument = InstrumentKind::synth;
- q.channel = synth;
- Pattern notes;
- notes.notes = std::move (group.notes);
- q.pattern = std::move (notes);
- any = add (std::move (q)) || any;
- }
- if (! any)
- failed.add (file.getFileName());
- continue;
- }
- const auto data = engine.getSampleLibrary().load (file.getFullPathName());
- if (data == nullptr)
- {
- failed.add (file.getFileName());
- continue;
- }
- // the file's length in ticks at the project's tempo (as a drop on the Playlist)
- const auto seconds = (double) data->frames() / data->sampleRate;
- simpletracks::Request q;
- q.kind = Kind::audio;
- q.name = file.getFileNameWithoutExtension().toStdString();
- q.audioFile = file.getFullPathName().toStdString();
- q.audioLength = juce::jmax<std::int64_t> (ppq / 4, (std::int64_t) (seconds * engine.getSong().bpm / 60.0 * ppq));
- add (std::move (q));
- }
- if (! failed.isEmpty())
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Could not import"),
- tr ("Could not read {0}", { failed.joinIntoString (", ") }));
- });
- }
- void SimpleWorkflow::addDrums (const std::string& genre, bool steps, bool useTempo)
- {
- // a Loop Starter plan for the genre (FL's configuration and presets when installed, else LYNX's own); an empty grid
- // takes the default genre's kit without its steps
- const auto& sources = loopstarterui::sources();
- if (sources.config == nullptr)
- return;
- const auto id = genre.empty() ? sources.config->defaultGenre : genre;
- const auto plan = loopstarter::generate (sources, id, (std::uint64_t) juce::Random::getSystemRandom().nextInt64());
- simpletracks::Request q;
- q.kind = Kind::drums;
- q.name = trackNameFor (Kind::drums);
- q.kit = loopstarter::kitChannels (plan);
- auto pattern = loopstarter::kitPattern (plan);
- if (! steps)
- {
- pattern.notes.clear();
- pattern.lengthTicks = 16 * ticksPerStep;
- }
- q.pattern = std::move (pattern);
- if (q.kit.empty())
- return;
- if (useTempo && steps)
- q.bpm = plan.bpm; // (in the same undo step as the track)
- add (std::move (q));
- }
- } // namespace lynx
|