| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- #include "ui/ChannelRackComponent.h"
- #include "ui/AutomationUi.h"
- #include "core/Slicer.h"
- #include "ui/ChannelSettings.h"
- #include "engine/Text.h"
- #include "ui/BrowserComponent.h"
- #include "ui/PluginWindow.h"
- #include "ui/Theme.h"
- namespace lynx
- {
- namespace
- {
- juce::String instrumentName (InstrumentKind k)
- {
- switch (k)
- {
- case InstrumentKind::kick: return "Kick";
- case InstrumentKind::snare: return "Snare";
- case InstrumentKind::hat: return "Hat";
- case InstrumentKind::clap: return "Clap";
- case InstrumentKind::synth: return "Synth";
- case InstrumentKind::sampler: return "Sampler";
- case InstrumentKind::audioClip: return utf8 ("Audio clip");
- case InstrumentKind::plugin: return "Plugin";
- case InstrumentKind::controller: return "Controller";
- case InstrumentKind::sakura: return "Sakura";
- case InstrumentKind::drumpad: return "Drumpad";
- }
- return "?";
- }
- constexpr InstrumentKind builtInKinds[] = { InstrumentKind::kick, InstrumentKind::snare, InstrumentKind::hat,
- InstrumentKind::clap, InstrumentKind::synth };
- void setStep (Pattern& p, int channel, int step, bool on)
- {
- if (p.hasStep (channel, step) != on)
- p.toggleStep (channel, step);
- }
- void drawKnob (juce::Graphics& g, juce::Rectangle<float> r, float value01, bool bipolar, juce::Colour colour)
- {
- const auto size = juce::jmin (r.getWidth(), r.getHeight()) - 6.0f;
- const auto area = r.withSizeKeepingCentre (size, size);
- const auto centre = area.getCentre();
- const auto radius = size / 2.0f;
- constexpr auto start = juce::MathConstants<float>::pi * 1.25f;
- constexpr auto range = juce::MathConstants<float>::pi * 1.5f;
- g.setColour (theme::knobBackground);
- g.fillEllipse (area);
- const auto from = bipolar ? start + range * 0.5f : start;
- const auto to = start + range * value01;
- juce::Path arc;
- arc.addCentredArc (centre.x, centre.y, radius - 1.5f, radius - 1.5f, 0.0f, juce::jmin (from, to), juce::jmax (from, to), true);
- g.setColour (colour);
- g.strokePath (arc, juce::PathStrokeType (2.5f));
- // JUCE angles: 0 = 12 o'clock, clockwise
- const auto length = radius - 3.0f;
- g.setColour (juce::Colours::white);
- g.drawLine (centre.x, centre.y, centre.x + std::sin (to) * length, centre.y - std::cos (to) * length, 1.5f);
- }
- } // namespace
- ChannelRackComponent::ChannelRackComponent (AudioEngine& e)
- : engine (e)
- {
- setWantsKeyboardFocus (false);
- setMouseClickGrabsKeyboardFocus (false);
- engine.addChangeListener (this);
- updateSize();
- startTimerHz (30);
- }
- ChannelRackComponent::~ChannelRackComponent()
- {
- engine.removeChangeListener (this);
- }
- int ChannelRackComponent::numSteps() const
- {
- const auto& song = engine.getSong();
- if (song.patterns.empty())
- return 16;
- const auto& p = song.patterns[(std::size_t) song.currentPattern];
- const auto steps = (int) (p.effectiveLength (song.beatsPerBar) / ticksPerStep);
- return juce::jlimit (16, 512, steps);
- }
- void ChannelRackComponent::updateSize()
- {
- const auto rows = (int) engine.getSong().channels.size() + 1; // +1 = "add channel" row
- setSize (gridLeft() + numSteps() * stepWidth + 8, rows * rowHeight + 8);
- }
- juce::Rectangle<int> ChannelRackComponent::rowBounds (int row) const
- {
- return { 0, 4 + row * rowHeight, getWidth(), rowHeight };
- }
- juce::Rectangle<int> ChannelRackComponent::stepBounds (int row, int step) const
- {
- return { gridLeft() + step * stepWidth + 1, 4 + row * rowHeight + 4, stepWidth - 3, rowHeight - 8 };
- }
- ChannelRackComponent::Area ChannelRackComponent::areaAt (int x, int y, int& row) const
- {
- const auto channels = (int) engine.getSong().channels.size();
- row = (y - 4) / rowHeight;
- if (y < 4 || row > channels)
- return Area::none;
- if (row == channels)
- return x < gridLeft() ? Area::addRow : Area::none;
- if (x < muteWidth)
- return Area::mute;
- if (x < muteWidth + knobWidth)
- return Area::pan;
- if (x < muteWidth + 2 * knobWidth)
- return Area::volume;
- if (x < nameLeft())
- return Area::route;
- if (x < gridLeft())
- return Area::name;
- return Area::grid;
- }
- bool ChannelRackComponent::cellHasNote (int channel, int step) const
- {
- const auto& song = engine.getSong();
- const auto& p = song.patterns[(std::size_t) song.currentPattern];
- const auto from = (std::int64_t) step * ticksPerStep;
- const auto to = from + ticksPerStep;
- for (const auto& n : p.notes)
- if (n.channel == channel && n.start >= from && n.start < to)
- return true;
- return false;
- }
- void ChannelRackComponent::paint (juce::Graphics& g)
- {
- g.fillAll (theme::panel);
- const auto& song = engine.getSong();
- if (song.patterns.empty())
- return;
- const auto& pattern = song.patterns[(std::size_t) song.currentPattern];
- const auto steps = numSteps();
- const auto clip = g.getClipBounds();
- const auto selected = engine.getSelectedChannel();
- g.setFont (juce::FontOptions (14.0f));
- for (int row = 0; row < (int) song.channels.size(); ++row)
- {
- const auto y = 4 + row * rowHeight;
- if (y > clip.getBottom() || y + rowHeight < clip.getY())
- continue;
- const auto& channel = song.channels[(std::size_t) row];
- const auto nameArea = juce::Rectangle<float> ((float) nameLeft(), (float) y + 3, (float) nameWidth, (float) rowHeight - 6);
- g.setColour (theme::muteLed (channel.muted));
- g.fillEllipse ((float) 9, (float) y + rowHeight / 2.0f - 5, 10, 10);
- drawKnob (g, { (float) muteWidth, (float) y, (float) knobWidth, (float) rowHeight }, (channel.pan + 1.0f) / 2.0f,
- true, theme::accentBlue);
- drawKnob (g, { (float) muteWidth + knobWidth, (float) y, (float) knobWidth, (float) rowHeight }, channel.volume,
- false, theme::accentGreen);
- // small box with the mixer insert ("-" = straight to Master), as in FL
- const auto routeArea = juce::Rectangle<float> ((float) (muteWidth + 2 * knobWidth) + 1, (float) y + 7,
- (float) routeWidth - 3, (float) rowHeight - 14);
- g.setColour (theme::knobBackground);
- g.fillRoundedRectangle (routeArea, 3.0f);
- g.setColour (channel.mixerTrack > 0 ? theme::accentOrange : theme::text.withAlpha (0.5f));
- g.setFont (juce::FontOptions (12.0f));
- g.drawText (channel.mixerTrack > 0 ? juce::String (channel.mixerTrack) : juce::String ("-"), routeArea.toNearestInt(),
- juce::Justification::centred);
- g.setFont (juce::FontOptions (14.0f));
- // channel colour (from the .flp or user-picked) blended into the button, as in FL
- auto button = row == selected ? theme::channelButtonSelected : theme::channelButton;
- if (channel.colour != 0)
- button = button.interpolatedWith (juce::Colour (channel.colour), 0.55f);
- g.setColour (button);
- g.fillRoundedRectangle (nameArea, 4.0f);
- if (row == selected)
- {
- g.setColour (theme::accentOrange);
- g.fillRoundedRectangle (nameArea.withWidth (4.0f), 2.0f);
- }
- g.setColour (channel.muted ? juce::Colours::grey : juce::Colours::white);
- g.drawText (juce::String::fromUTF8 (channel.name.c_str()), nameArea.reduced (10, 0).toNearestInt(),
- juce::Justification::centredLeft, true);
- if (channel.instrument == InstrumentKind::audioClip)
- {
- g.setColour (theme::text.withAlpha (0.35f));
- g.drawText (utf8 ("audio clip — plays from the Playlist"), gridLeft() + 4, y, 400, rowHeight, juce::Justification::centredLeft);
- g.setFont (juce::FontOptions (14.0f));
- continue;
- }
- for (int step = 0; step < steps; ++step)
- {
- const auto r = stepBounds (row, step);
- if (r.getRight() < clip.getX() || r.getX() > clip.getRight())
- continue;
- const auto onGrid = pattern.hasStep (row, step);
- const auto any = onGrid || cellHasNote (row, step);
- g.setColour (any ? (onGrid ? theme::stepOn : theme::stepOnOffGrid) : theme::stepOff (step));
- g.fillRoundedRectangle (r.toFloat(), 3.0f);
- }
- }
- const auto addY = 4 + (int) song.channels.size() * rowHeight;
- g.setColour (juce::Colours::white.withAlpha (0.5f));
- g.drawText (utf8 ("+ add channel (or drop an audio file)"), nameLeft() + 2, addY,
- 400, rowHeight, juce::Justification::centredLeft);
- if (playheadStep >= 0 && playheadStep < steps)
- {
- g.setColour (theme::playhead.withAlpha (0.35f));
- g.fillRect (gridLeft() + playheadStep * stepWidth, 0, stepWidth - 2, addY);
- }
- }
- void ChannelRackComponent::mouseDown (const juce::MouseEvent& e)
- {
- int row = 0;
- const auto area = areaAt (e.x, e.y, row);
- const auto& song = engine.getSong();
- switch (area)
- {
- case Area::none:
- return;
- case Area::addRow:
- showAddChannelMenu();
- return;
- case Area::mute:
- if (e.mods.isPopupMenu() || e.mods.isCtrlDown())
- {
- // solo as in FL: only this channel plays; repeating on the already-soloed channel unmutes all
- engine.editSong ([row] (Song& s) {
- const auto alone = ! s.channels[(std::size_t) row].muted
- && std::all_of (s.channels.begin(), s.channels.end(), [&] (const ChannelDef& c) {
- return &c == &s.channels[(std::size_t) row] || c.muted;
- });
- for (std::size_t i = 0; i < s.channels.size(); ++i)
- s.channels[i].muted = alone ? false : i != (std::size_t) row;
- });
- }
- else
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].muted = ! s.channels[(std::size_t) row].muted; });
- return;
- case Area::pan:
- case Area::volume:
- if (e.mods.isPopupMenu())
- {
- juce::PopupMenu menu;
- addAutomationItem (menu, engine, { area == Area::pan ? AutomationTargetKind::channelPan : AutomationTargetKind::channelVolume, row });
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- return;
- }
- knobDrag = area;
- knobRow = row;
- knobStartValue = area == Area::pan ? song.channels[(std::size_t) row].pan : song.channels[(std::size_t) row].volume;
- return;
- case Area::route:
- knobDrag = area;
- knobRow = row;
- routeStartValue = song.channels[(std::size_t) row].mixerTrack;
- return;
- case Area::name:
- engine.setSelectedChannel (row);
- if (e.mods.isPopupMenu())
- showChannelMenu (row);
- else
- engine.sendLiveNote (row, 60, 0.8f, true);
- return;
- case Area::grid:
- {
- const auto step = (e.x - gridLeft()) / stepWidth;
- const auto& pattern = song.patterns[(std::size_t) song.currentPattern];
- if (e.mods.isRightButtonDown())
- paintMode = PaintMode::remove;
- else
- paintMode = pattern.hasStep (row, step) ? PaintMode::remove : PaintMode::add;
- lastPaintedRow = lastPaintedStep = -1;
- applyPaint (e);
- return;
- }
- }
- }
- void ChannelRackComponent::mouseDrag (const juce::MouseEvent& e)
- {
- if (knobDrag != Area::none)
- {
- const auto delta = (float) -e.getDistanceFromDragStartY() / 150.0f;
- const auto row = knobRow;
- if (knobDrag == Area::route)
- {
- const auto tracks = juce::jmax (1, (int) engine.getSong().mixer.size());
- const auto value = juce::jlimit (0, tracks - 1, routeStartValue - e.getDistanceFromDragStartY() / 6);
- if (value != engine.getSong().channels[(std::size_t) row].mixerTrack)
- engine.editSong ([row, value] (Song& s) { s.channels[(std::size_t) row].mixerTrack = value; });
- }
- else if (knobDrag == Area::pan)
- {
- const auto value = juce::jlimit (-1.0f, 1.0f, knobStartValue + delta * 2.0f);
- engine.editSong ([row, value] (Song& s) { s.channels[(std::size_t) row].pan = value; });
- }
- else
- {
- const auto value = juce::jlimit (0.0f, 1.0f, knobStartValue + delta);
- engine.editSong ([row, value] (Song& s) { s.channels[(std::size_t) row].volume = value; });
- }
- return;
- }
- if (paintMode != PaintMode::none)
- applyPaint (e);
- }
- void ChannelRackComponent::mouseUp (const juce::MouseEvent& e)
- {
- int row = 0;
- if (areaAt (e.getMouseDownX(), e.getMouseDownY(), row) == Area::name && ! e.mods.isPopupMenu())
- engine.sendLiveNote (row, 60, 0.0f, false);
- paintMode = PaintMode::none;
- knobDrag = Area::none;
- }
- void ChannelRackComponent::mouseDoubleClick (const juce::MouseEvent& e)
- {
- int row = 0;
- const auto area = areaAt (e.x, e.y, row);
- if (area == Area::pan)
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].pan = 0.0f; });
- else if (area == Area::volume)
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].volume = 0.8f; });
- else if (area == Area::route)
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.nextFreeInsert(); });
- else if (area == Area::name && engine.getSong().channels[(std::size_t) row].instrument == InstrumentKind::plugin)
- openPluginWindow (row);
- else if (area == Area::name && onOpenPianoRoll)
- onOpenPianoRoll();
- }
- void ChannelRackComponent::applyPaint (const juce::MouseEvent& e)
- {
- const auto& song = engine.getSong();
- const auto row = (e.y - 4) / rowHeight;
- const auto step = (e.x - gridLeft()) / stepWidth;
- if (row < 0 || row >= (int) song.channels.size() || step < 0 || step >= numSteps() || e.x < gridLeft()
- || song.channels[(std::size_t) row].instrument == InstrumentKind::audioClip)
- return;
- if (row == lastPaintedRow && step == lastPaintedStep)
- return;
- lastPaintedRow = row;
- lastPaintedStep = step;
- const auto on = paintMode == PaintMode::add;
- engine.editSong ([row, step, on] (Song& s) { setStep (s.patterns[(std::size_t) s.currentPattern], row, step, on); });
- if (on)
- engine.sendLiveNote (row, 60, 0.78f, true);
- }
- void ChannelRackComponent::showChannelMenu (int channel)
- {
- const auto& c = engine.getSong().channels[(std::size_t) channel];
- juce::PopupMenu instruments;
- for (auto kind : builtInKinds)
- instruments.addItem (instrumentName (kind), true, c.instrument == kind, [this, channel, kind] {
- engine.editSong ([channel, kind] (Song& s) { s.channels[(std::size_t) channel].instrument = kind; });
- });
- instruments.addSeparator();
- instruments.addItem ("Sampler (choose file)...", [this, channel] { chooseSample (channel); });
- juce::PopupMenu plugins;
- addPluginItems (plugins, engine, true, [this, channel] (PluginRef ref) {
- engine.editSong ([channel, ref] (Song& s) {
- auto& c = s.channels[(std::size_t) channel];
- c.instrument = InstrumentKind::plugin;
- c.plugin = ref;
- c.name = ref.name;
- });
- openPluginWindow (channel);
- });
- instruments.addSubMenu ("Plugin (VST3/CLAP)", plugins);
- juce::PopupMenu menu;
- menu.addSectionHeader (juce::String::fromUTF8 (c.name.c_str()));
- if (c.instrument == InstrumentKind::sampler)
- {
- menu.addItem (juce::String ("Sample: ") + juce::File (juce::String::fromUTF8 (c.samplePath.c_str())).getFileName(),
- false, false, [] {});
- menu.addItem (utf8 ("Sampler settings (pitch, envelope, reverse)..."),
- [this, channel] { openChannelSettings (engine, channel); });
- if (c.sampler.slices.empty())
- menu.addItem (utf8 ("Slice sample (slicer): one slice per key and a pattern with the loop"), [this, channel] { sliceChannel (channel); });
- else
- menu.addItem (utf8 ("Undo slices (") + juce::String ((int) c.sampler.slices.size()) + ")", [this, channel] {
- engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].sampler.slices.clear(); });
- });
- }
- if (c.instrument == InstrumentKind::synth)
- menu.addItem (utf8 ("Synth settings (oscillators, envelope, filter)..."), [this, channel] { openChannelSettings (engine, channel); });
- if (c.instrument == InstrumentKind::plugin)
- {
- menu.addItem (utf8 ("Open plugin editor"), [this, channel] { openPluginWindow (channel); });
- if (auto* p = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get()))
- addPluginParameterItems (menu, engine, p->getInstance(), { AutomationTargetKind::channelParam, channel, 0, 0 });
- }
- menu.addItem ("Piano Roll", [this] {
- if (onOpenPianoRoll)
- onOpenPianoRoll();
- });
- menu.addSubMenu ("Instrument", instruments);
- {
- // channel arpeggiator
- const auto arp = c.arp;
- auto set = [this, channel] (std::function<void (ArpSettings&)> change) {
- engine.editSong ([channel, change] (Song& s) { change (s.channels[(std::size_t) channel].arp); });
- };
- juce::PopupMenu arpMenu;
- const char* directions[] = { "Off", "Up", "Down", "Up and down", "Random" };
- for (int d = 0; d < 5; ++d)
- arpMenu.addItem (utf8 (directions[d]), true, arp.direction == d, [set, d] { set ([d] (ArpSettings& a) { a.direction = d; }); });
- arpMenu.addSeparator();
- juce::PopupMenu range, speed, gate, repeat;
- for (int o = 1; o <= 4; ++o)
- range.addItem (juce::String (o) + (o == 1 ? " octave" : " octaves"), true, arp.range == o, [set, o] { set ([o] (ArpSettings& a) { a.range = o; }); });
- const std::pair<const char*, float> speeds[] = { { "1/4", 1.0f }, { "1/8", 0.5f }, { "1/8 triplet", 1.0f / 3.0f },
- { "1/16", 0.25f }, { "1/16 triplet", 1.0f / 6.0f }, { "1/32", 0.125f } };
- for (const auto& [name, beats] : speeds)
- speed.addItem (name, true, std::abs (arp.stepBeats - beats) < 1e-4f, [set, b = beats] { set ([b] (ArpSettings& a) { a.stepBeats = b; }); });
- for (int g : { 25, 50, 75, 100 })
- gate.addItem (juce::String (g) + "%", true, std::abs (arp.gate - g / 100.0f) < 1e-4f, [set, g] { set ([g] (ArpSettings& a) { a.gate = g / 100.0f; }); });
- for (int n : { 1, 2, 3, 4 })
- repeat.addItem (juce::String (n) + "x", true, arp.repeat == n, [set, n] { set ([n] (ArpSettings& a) { a.repeat = n; }); });
- arpMenu.addSubMenu (utf8 ("Range"), range);
- arpMenu.addSubMenu ("Speed", speed);
- arpMenu.addSubMenu ("Gate", gate);
- arpMenu.addSubMenu (utf8 ("Repeat"), repeat);
- menu.addSubMenu (arp.enabled() ? utf8 ("Arpeggiator (on)") : utf8 ("Arpeggiator"), arpMenu);
- }
- menu.addItem ("Rename...", [this, channel] { rename (channel); });
- {
- juce::PopupMenu colours;
- colours.addItem (utf8 ("Default"), [this, channel] { engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].colour = 0; }); });
- for (auto c : { 0xff485156u, 0xffa14547u, 0xffa16f45u, 0xffa19d45u, 0xff6ca145u, 0xff45a175u, 0xff45a1a0u, 0xff457ca1u,
- 0xff4f45a1u, 0xff8a5096u, 0xffa1458au })
- {
- juce::Image swatch (juce::Image::ARGB, 16, 16, true);
- juce::Graphics (swatch).fillAll (juce::Colour (c));
- juce::PopupMenu::Item item;
- item.text = "#" + juce::String::toHexString ((juce::int64) (c & 0xFFFFFF)).paddedLeft ('0', 6).toUpperCase();
- item.image = std::make_unique<juce::DrawableImage> (swatch);
- item.action = [this, channel, c] { engine.editSong ([channel, c] (Song& s) { s.channels[(std::size_t) channel].colour = c; }); };
- colours.addItem (std::move (item));
- }
- menu.addSubMenu ("Colour", colours);
- }
- menu.addItem ("Clear notes in this pattern", [this, channel] {
- engine.editSong ([channel] (Song& s) {
- auto& notes = s.patterns[(std::size_t) s.currentPattern].notes;
- notes.erase (std::remove_if (notes.begin(), notes.end(), [channel] (const Note& n) { return n.channel == channel; }),
- notes.end());
- });
- });
- menu.addSeparator();
- menu.addItem ("Delete channel", engine.getSong().channels.size() > 1, false, [this, channel] {
- engine.editSong ([channel] (Song& s) { s.removeChannel (channel); });
- });
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- }
- void ChannelRackComponent::showAddChannelMenu()
- {
- juce::PopupMenu menu;
- for (auto kind : builtInKinds)
- menu.addItem (instrumentName (kind), [this, kind] {
- engine.editSong ([kind] (Song& s) {
- ChannelDef c;
- c.name = instrumentName (kind).toStdString();
- c.instrument = kind;
- c.mixerTrack = s.nextFreeInsert();
- s.channels.push_back (c);
- });
- engine.setSelectedChannel ((int) engine.getSong().channels.size() - 1);
- });
- menu.addSeparator();
- menu.addItem ("Sampler (choose file)...", [this] { chooseSample (-1); });
- juce::PopupMenu plugins;
- addPluginItems (plugins, engine, true, [this] (PluginRef ref) {
- engine.editSong ([ref] (Song& s) {
- ChannelDef c;
- c.name = ref.name;
- c.instrument = InstrumentKind::plugin;
- c.plugin = ref;
- c.mixerTrack = s.nextFreeInsert();
- s.channels.push_back (c);
- });
- const auto added = (int) engine.getSong().channels.size() - 1;
- engine.setSelectedChannel (added);
- openPluginWindow (added); // as in FL: the plugin opens when added
- });
- menu.addSubMenu ("Plugin (VST3/CLAP)", plugins);
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- }
- void ChannelRackComponent::chooseSample (int channel)
- {
- fileChooser = std::make_unique<juce::FileChooser> ("Choose sample", juce::File(), engine.getSampleLibrary().wildcard());
- fileChooser->launchAsync (juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
- [this, channel] (const juce::FileChooser& chooser) {
- if (chooser.getResult().existsAsFile())
- assignSample (channel, chooser.getResult());
- });
- }
- void ChannelRackComponent::assignSample (int channel, const juce::File& file)
- {
- if (engine.getSampleLibrary().load (file.getFullPathName()) == nullptr)
- {
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, "Sample",
- utf8 ("Could not read ") + file.getFileName());
- return;
- }
- const auto path = file.getFullPathName().toStdString();
- const auto name = file.getFileNameWithoutExtension().toStdString();
- engine.editSong ([channel, path, name] (Song& s) {
- const auto isNew = channel < 0 || channel >= (int) s.channels.size();
- const auto freeInsert = s.nextFreeInsert();
- auto& c = isNew ? s.channels.emplace_back() : s.channels[(std::size_t) channel];
- if (isNew)
- c.mixerTrack = freeInsert;
- c.instrument = InstrumentKind::sampler;
- c.samplePath = path;
- c.name = name;
- });
- if (channel < 0)
- engine.setSelectedChannel ((int) engine.getSong().channels.size() - 1);
- }
- void ChannelRackComponent::openPluginWindow (int channel)
- {
- auto instrument = engine.getChannelInstrument (channel);
- if (auto* plugin = dynamic_cast<PluginInstrument*> (instrument.get()))
- {
- PluginWindows::open (instrument, plugin->getInstance(),
- juce::String::fromUTF8 (engine.getSong().channels[(std::size_t) channel].name.c_str()));
- return;
- }
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, "Plugin",
- utf8 ("This plugin could not be loaded (it is not installed or has not been scanned)."));
- }
- void ChannelRackComponent::rename (int channel)
- {
- auto* window = new juce::AlertWindow ("Rename channel", {}, juce::MessageBoxIconType::NoIcon);
- window->addTextEditor ("name", juce::String::fromUTF8 (engine.getSong().channels[(std::size_t) channel].name.c_str()));
- window->addButton ("OK", 1, juce::KeyPress (juce::KeyPress::returnKey));
- window->addButton ("Cancel", 0, juce::KeyPress (juce::KeyPress::escapeKey));
- window->enterModalState (true, juce::ModalCallbackFunction::create ([this, channel, window] (int result) {
- const auto name = window->getTextEditorContents ("name").trim().toStdString();
- if (result == 1 && ! name.empty() && channel < (int) engine.getSong().channels.size())
- engine.editSong ([channel, name] (Song& s) { s.channels[(std::size_t) channel].name = name; });
- }),
- true);
- }
- void ChannelRackComponent::sliceChannel (int channel)
- {
- const auto& song = engine.getSong();
- const auto& c = song.channels[(std::size_t) channel];
- const auto sample = engine.getSampleLibrary().load (juce::String::fromUTF8 (c.samplePath.c_str()));
- if (sample == nullptr || sample->frames() == 0)
- return;
- const auto slices = detectSlices (*sample, 0.5f);
- const auto seconds = (double) sample->frames() / sample->sampleRate;
- const auto ticksPerSecond = song.bpm * ppq / 60.0;
- const auto name = c.name + " (slices)";
- engine.editSong ([&] (Song& s) {
- s.channels[(std::size_t) channel].sampler.slices = slices;
- // pattern that plays the slices in their original order and timing (at the project BPM)
- auto& p = s.patterns.emplace_back();
- p.name = name;
- for (std::size_t i = 0; i < slices.size(); ++i)
- {
- const auto start = slices[i] * seconds;
- const auto end = i + 1 < slices.size() ? slices[i + 1] * seconds : seconds;
- Note n;
- n.start = (std::int64_t) std::llround (start * ticksPerSecond);
- n.length = std::max<std::int64_t> (1, (std::int64_t) std::llround ((end - start) * ticksPerSecond));
- n.key = 60 + (int) i;
- n.channel = channel;
- p.notes.push_back (n);
- }
- p.sortNotes();
- s.currentPattern = (int) s.patterns.size() - 1;
- });
- }
- bool ChannelRackComponent::isInterestedInFileDrag (const juce::StringArray& files)
- {
- for (const auto& f : files)
- if (juce::File (f).hasFileExtension ("wav;aif;aiff;flac;ogg;mp3"))
- return true;
- return false;
- }
- void ChannelRackComponent::filesDropped (const juce::StringArray& files, int x, int y)
- {
- int row = 0;
- const auto area = areaAt (x, y, row);
- auto target = (area == Area::none || area == Area::addRow) ? -1 : row;
- for (const auto& f : files)
- {
- const juce::File file (f);
- if (! file.hasFileExtension ("wav;aif;aiff;flac;ogg;mp3"))
- continue;
- assignSample (target, file);
- target = -1; // the remaining files become new channels
- }
- }
- void ChannelRackComponent::changeListenerCallback (juce::ChangeBroadcaster*)
- {
- updateSize();
- repaint();
- }
- void ChannelRackComponent::timerCallback()
- {
- auto& transport = engine.getTransport();
- auto step = -1;
- if (transport.playing.load() && engine.getSong().mode == PlayMode::pattern)
- {
- const auto& song = engine.getSong();
- const auto& p = song.patterns[(std::size_t) song.currentPattern];
- const auto length = (double) p.effectiveLength (song.beatsPerBar);
- step = (int) (std::fmod (transport.positionTicks.load(), length) / ticksPerStep);
- }
- if (step != playheadStep)
- {
- auto dirty = [this] (int s) {
- if (s >= 0)
- repaint (gridLeft() + s * stepWidth, 0, stepWidth, getHeight());
- };
- dirty (playheadStep);
- playheadStep = step;
- dirty (playheadStep);
- }
- }
- bool ChannelRackComponent::isInterestedInDragSource (const SourceDetails& details)
- {
- return details.description.toString() == BrowserComponent::dragDescription;
- }
- void ChannelRackComponent::itemDropped (const SourceDetails& details)
- {
- if (auto* tree = dynamic_cast<juce::FileTreeComponent*> (details.sourceComponent.get()))
- if (tree->getSelectedFile().existsAsFile())
- filesDropped ({ tree->getSelectedFile().getFullPathName() }, details.localPosition.x, details.localPosition.y);
- }
- } // namespace lynx
|