| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886 |
- #include "ui/ChannelRackComponent.h"
- #include "ui/AutomationUi.h"
- #include "core/NoteTools.h"
- #include "ui/ToolWindow.h"
- #include "core/Slicer.h"
- #include "ui/ChannelSettings.h"
- #include "engine/Text.h"
- #include "ui/Dialogs.h"
- #include "ui/HintBar.h"
- #include "ui/I18n.h"
- #include "ui/BrowserComponent.h"
- #include "ui/PluginWindow.h"
- #include "ui/Theme.h"
- #include "ui/AudioEditor.h"
- namespace lynx
- {
- namespace
- {
- // English name (used for new channel names stored in the song)
- 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 "Audio clip";
- case InstrumentKind::plugin: return "Plugin";
- case InstrumentKind::controller: return "Controller";
- case InstrumentKind::sakura: return "Sakura";
- case InstrumentKind::drumpad: return "Drumpad";
- case InstrumentKind::boobass: return "BooBass";
- case InstrumentKind::drumsynth: return "DrumSynth Live";
- case InstrumentKind::harmless: return "Harmless";
- case InstrumentKind::sytrus: return "Sytrus";
- case InstrumentKind::harmor: return "Harmor";
- case InstrumentKind::gms: return "GMS";
- case InstrumentKind::morphine: return "Morphine";
- case InstrumentKind::ogun: return "Ogun";
- case InstrumentKind::minisynth: return "MiniSynth";
- case InstrumentKind::dx10: return "Fruity DX10";
- case InstrumentKind::kepler: return "Kepler";
- case InstrumentKind::poizone: return "PoiZone";
- case InstrumentKind::fruitkick: return "Fruit Kick";
- case InstrumentKind::bassdrum: return "BassDrum";
- case InstrumentKind::toxic: return "Toxic Biohazard";
- case InstrumentKind::threeosc: return "3x Osc";
- case InstrumentKind::autogun: return "Autogun";
- case InstrumentKind::transistorbass: return "Transistor Bass";
- case InstrumentKind::simsynth: return "SimSynth";
- case InstrumentKind::wasp: return "Wasp";
- case InstrumentKind::plucked: return "Plucked!";
- case InstrumentKind::beepmap: return "BeepMap";
- case InstrumentKind::sawer: return "Sawer";
- case InstrumentKind::flkeys: return "FL Keys";
- case InstrumentKind::granulizer: return "Fruity Granulizer";
- case InstrumentKind::drumaxx: return "Drumaxx";
- case InstrumentKind::slayer: return "FL Slayer";
- case InstrumentKind::fruitydance: return "Fruity Dance";
- case InstrumentKind::directwave: return "DirectWave";
- }
- return "?";
- }
- // name shown in menus (translated)
- juce::String instrumentLabel (InstrumentKind k)
- {
- switch (k)
- {
- case InstrumentKind::kick: return tr ("Kick");
- case InstrumentKind::snare: return tr ("Snare");
- case InstrumentKind::hat: return tr ("Hat");
- case InstrumentKind::clap: return tr ("Clap");
- case InstrumentKind::synth: return tr ("Synth");
- case InstrumentKind::sampler: return tr ("Sampler");
- case InstrumentKind::audioClip: return tr ("Audio clip");
- case InstrumentKind::controller: return tr ("Controller");
- default: return instrumentName (k);
- }
- }
- 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);
- }
- juce::String channelName (const Song& song, int row)
- {
- return row >= 0 && row < (int) song.channels.size() ? juce::String::fromUTF8 (song.channels[(std::size_t) row].name.c_str()) : juce::String();
- }
- } // namespace
- bool ChannelRackComponent::keepEditorsOpen = false;
- ChannelRackComponent::ChannelRackComponent (AudioEngine& e)
- : engine (e)
- {
- setWantsKeyboardFocus (false);
- setMouseClickGrabsKeyboardFocus (false);
- engine.addChangeListener (this);
- previews.setSender ([this] (int channel, int key, float velocity, bool on) { engine.sendLiveNote (channel, key, velocity, on); });
- updateSize();
- startTimerHz (30);
- }
- ChannelRackComponent::~ChannelRackComponent()
- {
- previews.releaseAll();
- engine.removeChangeListener (this);
- }
- void ChannelRackComponent::visibilityChanged()
- {
- if (! isShowing())
- previews.releaseAll(); // hidden mid-gesture: nothing keeps sounding
- }
- 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 + (graphRow() >= 0 ? laneHeight : 0));
- }
- int ChannelRackComponent::graphRow() const
- {
- const auto count = (int) engine.getSong().channels.size();
- return graphLanes && count > 0 ? juce::jlimit (0, count - 1, engine.getSelectedChannel()) : -1;
- }
- int ChannelRackComponent::rowY (int row) const
- {
- const auto lane = graphRow();
- return 4 + row * rowHeight + (lane >= 0 && row > lane ? laneHeight : 0);
- }
- int ChannelRackComponent::rowAtY (int y) const
- {
- if (y < 4)
- return -1;
- if (const auto lane = graphRow(); lane >= 0)
- {
- const auto laneTop = rowY (lane) + rowHeight;
- if (y >= laneTop && y < laneTop + laneHeight)
- return lane;
- if (y >= laneTop + laneHeight)
- y -= laneHeight;
- }
- return (y - 4) / rowHeight;
- }
- juce::Rectangle<int> ChannelRackComponent::laneBounds() const
- {
- const auto lane = graphRow();
- return lane < 0 ? juce::Rectangle<int>() : juce::Rectangle<int> (0, rowY (lane) + rowHeight, getWidth(), laneHeight);
- }
- juce::Rectangle<int> ChannelRackComponent::rowBounds (int row) const
- {
- return { 0, rowY (row), getWidth(), rowHeight };
- }
- juce::Rectangle<int> ChannelRackComponent::stepBounds (int row, int step) const
- {
- return { gridLeft() + step * stepWidth + 1, rowY (row) + 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 = rowAtY (y);
- if (row < 0 || row > channels)
- return Area::none;
- if (laneBounds().contains (x, y))
- return x < gridLeft() ? Area::laneLevels : Area::lane;
- 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 < selectLeft())
- return Area::name;
- if (x < gridLeft())
- return Area::select;
- 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)
- {
- using namespace theme::colour;
- g.fillAll (bg2);
- 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 primary = engine.getSelectedChannel();
- const auto& selection = engine.getSelectedChannels();
- g.setFont (theme::font (theme::type::body));
- for (int row = 0; row < (int) song.channels.size(); ++row)
- {
- const auto y = rowY (row);
- if (y > clip.getBottom() || y + rowHeight < clip.getY())
- continue;
- const auto& channel = song.channels[(std::size_t) row];
- const auto selected = std::binary_search (selection.begin(), selection.end(), row);
- const auto nameArea = juce::Rectangle<float> ((float) nameLeft(), (float) y + 3, (float) nameWidth, (float) rowHeight - 6);
- if (selected) // selected rows are subtly lifted
- {
- g.setColour (bg3);
- g.fillRect (0, y + 1, getWidth(), rowHeight - 2);
- }
- const auto hovered = [this, row] (Area a) { return hoverRow == row && hoverArea == a; };
- const auto led = juce::Rectangle<float> (9.0f, (float) y + rowHeight / 2.0f - 4.5f, 9.0f, 9.0f);
- g.setColour (theme::muteLed (channel.muted));
- g.fillEllipse (led);
- if (hovered (Area::mute)) // hover ring
- {
- g.setColour (textSecondary.withAlpha (0.8f));
- g.drawEllipse (led.expanded (2.5f), 1.0f);
- }
- const auto touched = knobDrag != Area::none && knobRow == row;
- theme::drawKnob (g, controlBounds (Area::pan, row).toFloat().reduced (1.0f, 2.0f), (channel.pan + 1.0f) / 2.0f, true,
- touched && knobDrag == Area::pan ? accent : info, hovered (Area::pan) || (touched && knobDrag == Area::pan));
- theme::drawKnob (g, controlBounds (Area::volume, row).toFloat().reduced (1.0f, 2.0f), channel.volume, false,
- touched && knobDrag == Area::volume ? accent : knobArc, hovered (Area::volume) || (touched && knobDrag == Area::volume));
- // small box with the mixer insert ("-" = straight to Master)
- const auto routeArea = controlBounds (Area::route, row).toFloat();
- g.setColour (hovered (Area::route) || (touched && knobDrag == Area::route) ? bg4 : bg0);
- g.fillRoundedRectangle (routeArea, theme::radius::sm);
- if (hovered (Area::route))
- {
- g.setColour (borderStrong);
- g.drawRoundedRectangle (routeArea.reduced (0.5f), theme::radius::sm, 1.0f);
- }
- g.setColour (channel.mixerTrack > 0 ? textPrimary : textDisabled);
- g.setFont (theme::font (theme::type::caption, true));
- g.drawText (channel.mixerTrack > 0 ? juce::String (channel.mixerTrack) : juce::String ("-"), routeArea.toNearestInt(),
- juce::Justification::centred);
- g.setFont (theme::font (theme::type::body));
- // name button: neutral surface with the channel colour (from the .flp or user-picked) as a strip
- g.setColour (row == primary ? bg6 : hovered (Area::name) ? bg5 : bg4);
- g.fillRoundedRectangle (nameArea, theme::radius::md);
- if (row == primary)
- {
- g.setColour (accent);
- g.drawRoundedRectangle (nameArea.reduced (0.5f), theme::radius::md, 1.0f);
- }
- g.setColour (channel.colour != 0 ? theme::tame (juce::Colour (channel.colour)) : bg6.brighter (0.2f));
- g.fillRoundedRectangle (nameArea.withWidth (4.0f).reduced (0.0f, 3.0f).translated (3.0f, 0.0f), 1.5f);
- g.setColour (channel.muted ? textDisabled : textPrimary);
- g.drawText (juce::String::fromUTF8 (channel.name.c_str()), nameArea.withTrimmedLeft (14).withTrimmedRight (6).toNearestInt(),
- juce::Justification::centredLeft, true);
- // selector: a slim pill, lit when the row is selected
- const auto pill = juce::Rectangle<float> ((float) selectLeft() + 3.0f, (float) y + 7.0f, (float) selectWidth - 6.0f, (float) rowHeight - 14.0f);
- g.setColour (selected ? (hovered (Area::select) ? accentHover : accent) : hovered (Area::select) ? textDisabled : ledOff);
- g.fillRoundedRectangle (pill, 3.0f);
- if (channel.instrument == InstrumentKind::audioClip)
- {
- g.setColour (textDisabled);
- g.setFont (theme::font (theme::type::small));
- g.drawText (tr ("audio clip — plays from the Playlist"), gridLeft() + 4, y, 400, rowHeight, juce::Justification::centredLeft);
- g.setFont (theme::font (theme::type::body));
- 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);
- const auto hoverStepHere = hoverArea == Area::grid && hoverRow == row && hoverStep == step && paintMode == PaintMode::none;
- const auto fill = any ? (onGrid ? stepOn : stepNote) : theme::stepOff (step);
- g.setColour (hoverStepHere ? fill.brighter (any ? 0.15f : 0.35f) : fill);
- g.fillRoundedRectangle (r.toFloat(), theme::radius::sm);
- if (hoverStepHere)
- {
- g.setColour (textSecondary.withAlpha (0.7f));
- g.drawRoundedRectangle (r.toFloat().reduced (0.5f), theme::radius::sm, 1.0f);
- }
- }
- }
- paintLane (g);
- const auto addY = rowY ((int) song.channels.size());
- {
- const auto addArea = juce::Rectangle<float> ((float) nameLeft(), (float) addY + 3, (float) nameWidth, (float) rowHeight - 6);
- const float dash[] = { 3.0f, 3.0f };
- juce::Path outline, dashed;
- outline.addRoundedRectangle (addArea.reduced (0.5f), theme::radius::md);
- juce::PathStrokeType (1.0f).createDashedStroke (dashed, outline, dash, 2);
- const auto addHovered = hoverArea == Area::addRow;
- g.setColour (addHovered ? textSecondary : borderStrong);
- g.fillPath (dashed);
- g.setColour (addHovered ? textPrimary : textSecondary);
- g.setFont (theme::font (theme::type::small));
- g.drawText (tr ("+ add channel"), addArea.toNearestInt(), juce::Justification::centred);
- g.setColour (textDisabled);
- g.drawText (tr ("or drop an audio file here"), gridLeft() + 4, addY, 400, rowHeight, juce::Justification::centredLeft);
- }
- if (playheadStep >= 0 && playheadStep < steps)
- {
- g.setColour (playhead.withAlpha (0.28f));
- g.fillRoundedRectangle (juce::Rectangle<float> ((float) (gridLeft() + playheadStep * stepWidth), 2.0f, (float) stepWidth - 2.0f,
- (float) addY - 2.0f), theme::radius::sm);
- }
- }
- // --- the control contract for the knobs and the route box ----------------------------------------
- ui::ValueSpec ChannelRackComponent::specFor (Area area, int row) const
- {
- const auto name = channelName (engine.getSong(), row);
- if (area == Area::pan)
- {
- auto spec = ui::panSpec (name + " - " + tr ("Pan"));
- spec.target = AutomationTarget { AutomationTargetKind::channelPan, row };
- return spec;
- }
- if (area == Area::volume)
- {
- auto spec = ui::percentSpec (name + " - " + tr ("Volume"), 0.78);
- spec.target = AutomationTarget { AutomationTargetKind::channelVolume, row };
- return spec;
- }
- // route box: the mixer insert, as a number
- ui::ValueSpec spec;
- spec.name = name + " - " + tr ("Mixer insert");
- spec.maximum = juce::jmax (0, (int) engine.getSong().mixer.size() - 1);
- spec.interval = 1.0;
- spec.pixelsForRange = 6.0 * juce::jmax (1.0, spec.maximum);
- spec.toText = [] (double v) { return v < 0.5 ? tr ("Master") : tr ("Insert {0}", { juce::String (juce::roundToInt (v)) }); };
- spec.fromText = [] (const juce::String& t) -> std::optional<double> {
- if (t.trim().startsWithIgnoreCase ("m"))
- return 0.0;
- const auto digits = t.retainCharacters ("0123456789");
- return digits.isEmpty() ? std::nullopt : std::optional<double> (digits.getDoubleValue());
- };
- return spec;
- }
- double ChannelRackComponent::valueOf (Area area, int row) const
- {
- const auto& c = engine.getSong().channels[(std::size_t) row];
- return area == Area::pan ? c.pan : area == Area::volume ? c.volume : (double) c.mixerTrack;
- }
- void ChannelRackComponent::setValue (Area area, int row, double value)
- {
- if (row < 0 || row >= (int) engine.getSong().channels.size())
- return;
- if (area == Area::pan)
- engine.editSong ([row, v = (float) value] (Song& s) { s.channels[(std::size_t) row].pan = v; });
- else if (area == Area::volume)
- engine.editSong ([row, v = (float) value] (Song& s) { s.channels[(std::size_t) row].volume = v; });
- else if (area == Area::route)
- {
- const auto track = juce::roundToInt (value);
- if (track != engine.getSong().channels[(std::size_t) row].mixerTrack)
- engine.editSong ([row, track] (Song& s) { s.channels[(std::size_t) row].mixerTrack = track; });
- }
- }
- juce::Rectangle<int> ChannelRackComponent::controlBounds (Area area, int row) const
- {
- const auto y = rowY (row);
- switch (area)
- {
- case Area::pan: return { muteWidth, y, knobWidth, rowHeight };
- case Area::volume: return { muteWidth + knobWidth, y, knobWidth, rowHeight };
- case Area::route: return { muteWidth + 2 * knobWidth + 1, y + 7, routeWidth - 3, rowHeight - 14 };
- case Area::lane:
- {
- const auto lane = laneBounds();
- const auto step = juce::jmax (0, hoverStep);
- return { gridLeft() + step * stepWidth, lane.getY() + 6, stepWidth, lane.getHeight() - 12 };
- }
- default: return {};
- }
- }
- void ChannelRackComponent::showTip (Area area, int row, bool transient)
- {
- if (row < 0 || row >= (int) engine.getSong().channels.size())
- return;
- const auto spec = specFor (area, row);
- const auto text = spec.text (valueOf (area, row));
- ui::postHint (ui::valueHint (spec, valueOf (area, row), ui::valueGestures()));
- if (transient)
- ui::flashValueTip (*this, controlBounds (area, row), text);
- else
- ui::showValueTip (*this, controlBounds (area, row), text);
- }
- void ChannelRackComponent::setHover (Area area, int row, int step)
- {
- if (area == hoverArea && row == hoverRow && step == hoverStep)
- return;
- auto dirty = [this] (Area a, int r) {
- if (a == Area::lane || a == Area::laneLevels)
- repaint (laneBounds());
- else if (a == Area::addRow)
- repaint (rowBounds ((int) engine.getSong().channels.size()));
- else if (r >= 0)
- repaint (rowBounds (r));
- };
- dirty (hoverArea, hoverRow);
- hoverArea = area;
- hoverRow = row;
- hoverStep = step;
- dirty (hoverArea, hoverRow);
- }
- void ChannelRackComponent::mouseMove (const juce::MouseEvent& e)
- {
- int row = 0;
- const auto area = areaAt (e.x, e.y, row);
- const auto step = area == Area::grid || area == Area::lane ? (e.x - gridLeft()) / stepWidth : -1;
- setHover (area, row, step >= 0 && step < numSteps() ? step : -1);
- }
- void ChannelRackComponent::mouseExit (const juce::MouseEvent&)
- {
- setHover (Area::none, -1, -1);
- }
- void ChannelRackComponent::setSwingMixOfSelected (int channel)
- {
- const auto& song = engine.getSong();
- if (channel < 0 || channel >= (int) song.channels.size())
- return;
- auto spec = ui::percentSpec (channelName (song, channel) + " - " + tr ("Swing mix"), 1.0);
- const auto rows = engine.getSelectedChannels();
- ui::typeValue (*this, spec, song.channels[(std::size_t) channel].swingMix, [this, rows, channel] (double v) {
- engine.editSong ([rows, channel, v] (Song& s) {
- auto targets = rows;
- if (std::find (targets.begin(), targets.end(), channel) == targets.end())
- targets = { channel };
- for (auto r : targets)
- if (r >= 0 && r < (int) s.channels.size())
- s.channels[(std::size_t) r].swingMix = (float) v;
- });
- });
- }
- // --- selection -----------------------------------------------------------------------------------
- void ChannelRackComponent::selectRows (std::vector<int> rows, int primary)
- {
- engine.setChannelSelection (std::move (rows), primary);
- repaint();
- }
- void ChannelRackComponent::clickSelector (int row, const juce::ModifierKeys& mods)
- {
- ChannelSelection selection;
- selection.rows = engine.getSelectedChannels();
- selection.anchor = selectionAnchor;
- if (mods.isShiftDown())
- selection.extendTo (row);
- else if (mods.isCtrlDown() || mods.isPopupMenu())
- selection.toggle (row);
- else
- selection.selectOnly (row);
- selectionAnchor = selection.anchor;
- const auto primary = selection.contains (row) ? row : selection.rows.front();
- selectRows (selection.rows, primary);
- }
- void ChannelRackComponent::selectRelative (int delta, bool extend)
- {
- const auto count = (int) engine.getSong().channels.size();
- if (count == 0)
- return;
- const auto row = juce::jlimit (0, count - 1, engine.getSelectedChannel() + delta);
- if (extend)
- {
- ChannelSelection selection;
- selection.anchor = selectionAnchor;
- selection.extendTo (row);
- selectRows (selection.rows, row);
- }
- else
- {
- selectionAnchor = row;
- selectRows ({ row }, row);
- }
- }
- void ChannelRackComponent::selectAll()
- {
- ChannelSelection selection;
- selection.selectAll ((int) engine.getSong().channels.size());
- selectRows (selection.rows, engine.getSelectedChannel());
- }
- bool ChannelRackComponent::moveSelected (int delta)
- {
- auto rows = engine.getSelectedChannels();
- const auto primary = engine.getSelectedChannel();
- const auto primaryIndex = (int) (std::find (rows.begin(), rows.end(), primary) - rows.begin());
- bool moved = false;
- std::vector<int> after;
- engine.editSong ([&] (Song& s) {
- after = rows;
- moved = moveChannels (s, after, delta);
- });
- if (! moved)
- return false;
- const auto newPrimary = primaryIndex < (int) after.size() ? after[(std::size_t) primaryIndex] : after.front();
- openEditorRow = -1;
- selectionAnchor = juce::jlimit (0, (int) engine.getSong().channels.size() - 1, selectionAnchor + delta);
- selectRows (after, newPrimary);
- return true;
- }
- void ChannelRackComponent::cloneSelected()
- {
- auto rows = engine.getSelectedChannels();
- std::vector<int> clones;
- engine.editSong ([&] (Song& s) {
- // from the bottom up, so earlier clones don't shift the rows still to clone
- for (auto it = rows.rbegin(); it != rows.rend(); ++it)
- cloneChannel (s, *it);
- });
- // each clone sits right below its original: original i (sorted) ends at rows[i] + i, its clone one below
- for (std::size_t i = 0; i < rows.size(); ++i)
- clones.push_back (rows[i] + (int) i + 1);
- if (! clones.empty())
- selectRows (clones, clones.front());
- }
- void ChannelRackComponent::deleteSelected()
- {
- const auto rows = engine.getSelectedChannels();
- const auto count = (int) engine.getSong().channels.size();
- if (rows.empty() || (int) rows.size() >= count) // at least one channel stays
- return;
- for (auto r : rows)
- closeEditor (r);
- engine.editSong ([rows] (Song& s) { removeChannels (s, rows); });
- openEditorRow = -1;
- const auto row = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().channels.size() - 1), rows.front());
- selectRows ({ row }, row);
- }
- void ChannelRackComponent::sendSelectedToInserts()
- {
- const auto rows = engine.getSelectedChannels();
- engine.editSong ([rows] (Song& s) { sendChannelsToFreeInserts (s, rows); });
- }
- void ChannelRackComponent::toggleMute (int channel)
- {
- if (channel >= 0 && channel < (int) engine.getSong().channels.size())
- engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].muted = ! s.channels[(std::size_t) channel].muted; });
- }
- void ChannelRackComponent::solo (int row)
- {
- if (row < 0 || row >= (int) engine.getSong().channels.size())
- return;
- // 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;
- });
- }
- // --- channel editors ---------------------------------------------------------------------------
- bool ChannelRackComponent::isEditorOpen (int channel)
- {
- if (channel < 0 || channel >= (int) engine.getSong().channels.size())
- return false;
- if (engine.getSong().channels[(std::size_t) channel].instrument == InstrumentKind::plugin)
- {
- if (auto* plugin = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get()))
- return PluginWindows::isOpen (plugin->getInstance());
- return false;
- }
- return isChannelSettingsOpen (channel);
- }
- void ChannelRackComponent::closeEditor (int channel)
- {
- if (channel < 0 || channel >= (int) engine.getSong().channels.size())
- return;
- if (engine.getSong().channels[(std::size_t) channel].instrument == InstrumentKind::plugin)
- {
- if (auto* plugin = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get()))
- PluginWindows::close (plugin->getInstance());
- }
- else
- closeChannelSettings (channel);
- }
- void ChannelRackComponent::toggleEditor (int channel, bool keepOthers)
- {
- if (channel < 0 || channel >= (int) engine.getSong().channels.size())
- return;
- const auto kind = engine.getSong().channels[(std::size_t) channel].instrument;
- if (kind != InstrumentKind::plugin && ! hasChannelSettings (kind))
- return; // built-in drums and audio clips have no editor window: the click only selects
- if (isEditorOpen (channel))
- {
- closeEditor (channel);
- if (openEditorRow == channel)
- openEditorRow = -1;
- return;
- }
- if (! keepOthers && ! keepEditorsOpen && openEditorRow != channel)
- closeEditor (openEditorRow);
- if (kind == InstrumentKind::plugin)
- openPluginWindow (channel);
- else
- openChannelSettings (engine, channel);
- openEditorRow = channel;
- }
- // --- step graph lanes (docs/WORKFLOW_FL.md P2, C4) --------------------------------------------------
- juce::Rectangle<int> ChannelRackComponent::levelButtonBounds (int level) const
- {
- const auto lane = laneBounds();
- const auto width = (gridLeft() - 24) / 3;
- return { 12 + (level % 3) * width, lane.getY() + 8 + (level / 3) * 24, width - 4, 20 };
- }
- void ChannelRackComponent::paintLane (juce::Graphics& g)
- {
- using namespace theme::colour;
- const auto lane = laneBounds();
- if (lane.isEmpty() || ! g.getClipBounds().intersects (lane))
- return;
- const auto& song = engine.getSong();
- const auto& pattern = song.patterns[(std::size_t) song.currentPattern];
- const auto channel = graphRow();
- const auto level = (notetools::Level) graphLevel;
- g.setColour (bg0);
- g.fillRect (lane);
- g.setColour (border);
- g.drawHorizontalLine (lane.getY(), 0.0f, (float) getWidth());
- g.drawHorizontalLine (lane.getBottom() - 1, 0.0f, (float) getWidth());
- for (int l = 0; l < notetools::levelCount; ++l)
- {
- const auto r = levelButtonBounds (l).toFloat();
- g.setColour (l == graphLevel ? accent : bg4);
- g.fillRoundedRectangle (r, theme::radius::sm);
- g.setColour (l == graphLevel ? onAccent : textSecondary);
- g.setFont (theme::font (theme::type::caption, l == graphLevel));
- g.drawFittedText (tr (notetools::levelName ((notetools::Level) l)), r.toNearestInt(), juce::Justification::centred, 1, 0.8f);
- }
- g.setColour (textDisabled);
- g.setFont (theme::font (theme::type::caption));
- g.drawFittedText (tr ("Drag: set · Right-drag: ramp · Alt+click: reset"),
- juce::Rectangle<int> (12, lane.getBottom() - 20, gridLeft() - 24, 16), juce::Justification::centredLeft, 1, 0.8f);
- const auto top = (float) lane.getY() + 6.0f, bottom = (float) lane.getBottom() - 6.0f;
- const auto bipolar = notetools::levelIsBipolar (level);
- const auto base = bipolar ? (top + bottom) * 0.5f : bottom;
- for (int step = 0; step < numSteps(); ++step)
- {
- const auto x = (float) (gridLeft() + step * stepWidth + 1);
- const auto w = (float) stepWidth - 3.0f;
- if (step % 4 == 0 && step > 0)
- {
- g.setColour (step % 16 == 0 ? gridBar : gridBeat);
- g.drawVerticalLine ((int) x - 2, top, bottom);
- }
- if (hoverArea == Area::lane && hoverStep == step)
- {
- g.setColour (bg3);
- g.fillRect (juce::Rectangle<float> (x, top, w, bottom - top));
- }
- const auto notes = notesInStep (pattern, channel, step);
- if (notes.empty())
- {
- g.setColour (theme::stepOff (step));
- g.fillRect (x + w * 0.5f - 1.0f, base - 1.0f, 2.0f, 2.0f);
- continue;
- }
- const auto v = notetools::levelValue (pattern.notes[(std::size_t) notes.front()], level);
- const auto y = bottom - v * (bottom - top);
- g.setColour ((level == notetools::Level::velocity ? accent : info).withAlpha (0.85f));
- g.fillRect (juce::Rectangle<float> (x + 2.0f, juce::jmin (y, base), w - 4.0f, juce::jmax (2.0f, std::abs (base - y))));
- g.setColour (textPrimary);
- g.fillRect (x + 1.0f, y - 1.0f, w - 2.0f, 2.0f);
- }
- if (bipolar)
- {
- g.setColour (gridBar);
- g.drawHorizontalLine ((int) base, (float) gridLeft(), (float) getWidth());
- }
- }
- float ChannelRackComponent::laneValueAt (int y) const
- {
- const auto lane = laneBounds();
- const auto top = (float) lane.getY() + 6.0f, bottom = (float) lane.getBottom() - 6.0f;
- return juce::jlimit (0.0f, 1.0f, (bottom - (float) y) / juce::jmax (1.0f, bottom - top));
- }
- void ChannelRackComponent::setLaneValue (int step, float value)
- {
- const auto channel = graphRow();
- const auto level = (notetools::Level) graphLevel;
- engine.editSong ([channel, step, value, level] (Song& s) {
- auto& p = s.patterns[(std::size_t) s.currentPattern];
- for (auto i : notesInStep (p, channel, step))
- notetools::setLevelValue (p.notes[(std::size_t) i], level, value);
- });
- }
- void ChannelRackComponent::applyLane (const juce::MouseEvent& e)
- {
- const auto steps = numSteps();
- const auto step = juce::jlimit (0, steps - 1, (e.x - gridLeft()) / stepWidth);
- const auto value = laneValueAt (e.y);
- if (e.mods.isRightButtonDown()) // a straight ramp from the press to here
- {
- const auto from = juce::jmin (laneDownStep, step), to = juce::jmax (laneDownStep, step);
- const auto channel = graphRow();
- const auto level = (notetools::Level) graphLevel;
- const auto v0 = laneDownStep <= step ? laneDownValue : value, v1 = laneDownStep <= step ? value : laneDownValue;
- engine.editSong ([=] (Song& s) {
- auto& p = s.patterns[(std::size_t) s.currentPattern];
- for (int st = from; st <= to; ++st)
- {
- const auto t = to > from ? (float) (st - from) / (float) (to - from) : 0.0f;
- for (auto i : notesInStep (p, channel, st))
- notetools::setLevelValue (p.notes[(std::size_t) i], level, v0 + (v1 - v0) * t);
- }
- });
- return;
- }
- setLaneValue (step, value);
- }
- void ChannelRackComponent::toggleGraphLanes()
- {
- graphLanes = ! graphLanes;
- updateSize();
- repaint();
- }
- // --- fills, rotation and organisation ------------------------------------------------------------
- void ChannelRackComponent::rotateSelected (int steps)
- {
- const auto rows = engine.getSelectedChannels();
- engine.editSong ([rows, steps] (Song& s) {
- auto& p = s.patterns[(std::size_t) s.currentPattern];
- const auto length = patternSteps (s, p);
- for (auto r : rows)
- rotateSteps (p, r, steps, length);
- });
- }
- void ChannelRackComponent::fillSelected (int every)
- {
- const auto rows = engine.getSelectedChannels();
- engine.editSong ([rows, every] (Song& s) {
- auto& p = s.patterns[(std::size_t) s.currentPattern];
- const auto length = patternSteps (s, p);
- for (auto r : rows)
- if (r < (int) s.channels.size() && s.channels[(std::size_t) r].instrument != InstrumentKind::audioClip)
- fillEverySteps (p, r, every, length);
- });
- }
- void ChannelRackComponent::sortChannels (ChannelSort by)
- {
- engine.editSong ([by] (Song& s) { permuteChannels (s, channelSortOrder (s, by)); });
- openEditorRow = -1;
- selectRows ({ 0 }, 0);
- }
- void ChannelRackComponent::selectUnused()
- {
- const auto unused = unusedChannels (engine.getSong());
- if (unused.empty())
- {
- juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Select unused channels"),
- tr ("Every channel is used by a pattern or a Playlist clip."));
- return;
- }
- selectRows (unused, unused.front());
- }
- int ChannelRackComponent::addChannel (ChannelDef c, bool openEditor)
- {
- int index = 0;
- engine.editSong ([&] (Song& s) {
- c.mixerTrack = s.nextFreeInsert();
- s.channels.push_back (c);
- index = (int) s.channels.size() - 1;
- });
- selectionAnchor = index;
- selectRows ({ index }, index);
- if (openEditor && c.instrument == InstrumentKind::plugin)
- {
- openPluginWindow (index);
- openEditorRow = index;
- }
- return index;
- }
- bool ChannelRackComponent::loadSampleIntoSelected (const juce::File& file)
- {
- const auto row = engine.getSelectedChannel();
- const auto& song = engine.getSong();
- if (row < 0 || row >= (int) song.channels.size() || ! file.hasFileExtension ("wav;aif;aiff;flac;ogg;mp3"))
- return false;
- const auto kind = song.channels[(std::size_t) row].instrument;
- // only channels that play a sample (or the built-in drums, which become samplers) take a file
- if (kind != InstrumentKind::sampler && kind != InstrumentKind::kick && kind != InstrumentKind::snare && kind != InstrumentKind::hat
- && kind != InstrumentKind::clap)
- return false;
- assignSample (row, file);
- return true;
- }
- namespace
- {
- // Euclidean fill: pulses spread evenly over a length, rotated; previewed live on the pattern.
- class EuclideanPanel final : public juce::Component
- {
- public:
- EuclideanPanel (AudioEngine& e, int c) : engine (e), channel (c)
- {
- const auto& song = engine.getSong();
- patternIndex = song.currentPattern;
- originals = song.patterns[(std::size_t) patternIndex].notes;
- steps = patternSteps (song, song.patterns[(std::size_t) patternIndex]);
- auto setup = [this] (juce::Slider& s, juce::Label& l, const juce::String& name, double lo, double hi, double v) {
- l.setText (name, juce::dontSendNotification);
- addAndMakeVisible (l);
- s.setSliderStyle (juce::Slider::LinearHorizontal);
- s.setTextBoxStyle (juce::Slider::TextBoxRight, false, 50, 20);
- s.setRange (lo, hi, 1.0);
- s.setValue (v, juce::dontSendNotification);
- s.onValueChange = [this] { preview(); };
- addAndMakeVisible (s);
- };
- setup (pulses, pulsesLabel, tr ("Pulses"), 0, 32, 5);
- setup (length, lengthLabel, tr ("Length (steps)"), 1, 32, 16);
- setup (rotation, rotationLabel, tr ("Rotation"), -16, 16, 0);
- title.setText (tr ("Euclidean fill: {0}", { juce::String::fromUTF8 (song.channels[(std::size_t) channel].name.c_str()) }), juce::dontSendNotification);
- title.setFont (theme::font (theme::type::title, true));
- addAndMakeVisible (title);
- for (auto* b : { &accept, &cancel })
- addAndMakeVisible (b);
- accept.setColour (juce::TextButton::buttonColourId, theme::colour::accent);
- accept.onClick = [this] {
- restore();
- const auto rhythm = current();
- engine.editSong ([this, rhythm] (Song& s) { setStepRhythm (s.patterns[(std::size_t) patternIndex], channel, rhythm, steps); });
- done = true;
- closeToolWindowOf (*this);
- };
- cancel.onClick = [this] { closeToolWindowOf (*this); };
- setSize (380, 330);
- preview();
- }
- ~EuclideanPanel() override
- {
- if (! done)
- restore();
- }
- void paint (juce::Graphics& g) override
- {
- g.fillAll (theme::colour::bg2);
- // the rhythm as a ring of steps, the first step at the top
- const auto rhythm = current();
- const auto ring = juce::Rectangle<float> (0, 150, (float) getWidth(), 120).withSizeKeepingCentre (110, 110);
- const auto n = (int) rhythm.size();
- for (int i = 0; i < n; ++i)
- {
- const auto angle = juce::MathConstants<float>::twoPi * (float) i / (float) n;
- const auto centre = ring.getCentre() + juce::Point<float> (std::sin (angle), -std::cos (angle)) * 50.0f;
- const auto dot = juce::Rectangle<float> (12, 12).withCentre (centre);
- g.setColour (rhythm[(std::size_t) i] ? theme::colour::stepOn : theme::colour::stepOff);
- g.fillEllipse (dot);
- if (i == 0)
- {
- g.setColour (theme::colour::textPrimary);
- g.drawEllipse (dot.expanded (2.0f), 1.0f);
- }
- }
- }
- void resized() override
- {
- auto area = getLocalBounds().reduced (12, 10);
- title.setBounds (area.removeFromTop (24));
- area.removeFromTop (6);
- for (auto [s, l] : { std::pair { &pulses, &pulsesLabel }, std::pair { &length, &lengthLabel }, std::pair { &rotation, &rotationLabel } })
- {
- auto row = area.removeFromTop (30);
- l->setBounds (row.removeFromLeft (120));
- s->setBounds (row);
- }
- auto buttons = area.removeFromBottom (28);
- accept.setBounds (buttons.removeFromRight (100));
- buttons.removeFromRight (8);
- cancel.setBounds (buttons.removeFromRight (90));
- }
- private:
- std::vector<bool> current() const
- {
- return euclideanRhythm ((int) pulses.getValue(), (int) length.getValue(), (int) rotation.getValue());
- }
- void restore()
- {
- if (patternIndex < (int) engine.getSong().patterns.size())
- engine.editSong ([this] (Song& s) { s.patterns[(std::size_t) patternIndex].notes = originals; }, false);
- }
- void preview()
- {
- const auto rhythm = current();
- if (patternIndex < (int) engine.getSong().patterns.size())
- engine.editSong ([this, rhythm] (Song& s) {
- auto& p = s.patterns[(std::size_t) patternIndex];
- p.notes = originals;
- setStepRhythm (p, channel, rhythm, steps);
- }, false);
- repaint();
- }
- AudioEngine& engine;
- int channel, patternIndex = 0, steps = 16;
- std::vector<Note> originals;
- bool done = false;
- juce::Label title, pulsesLabel, lengthLabel, rotationLabel;
- juce::Slider pulses, length, rotation;
- juce::TextButton accept { tr ("Accept") }, cancel { tr ("Cancel") };
- };
- } // namespace
- std::unique_ptr<juce::Component> ChannelRackComponent::makeEuclideanPanel (int channel)
- {
- return std::make_unique<EuclideanPanel> (engine, juce::jlimit (0, juce::jmax (0, (int) engine.getSong().channels.size() - 1), channel));
- }
- void ChannelRackComponent::showEuclideanFill (int channel)
- {
- if (channel >= 0 && channel < (int) engine.getSong().channels.size())
- openToolWindow (makeEuclideanPanel (channel), tr ("Euclidean fill"), "euclidean");
- }
- // --- mouse -------------------------------------------------------------------------------------
- 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::laneLevels:
- for (int l = 0; l < notetools::levelCount; ++l)
- if (levelButtonBounds (l).contains (e.getPosition()))
- {
- graphLevel = l;
- repaint();
- }
- return;
- case Area::lane:
- {
- const auto step = juce::jlimit (0, numSteps() - 1, (e.x - gridLeft()) / stepWidth);
- if (e.mods.isAltDown() || e.mods.isMiddleButtonDown())
- {
- setLaneValue (step, notetools::levelDefault ((notetools::Level) graphLevel));
- return;
- }
- laneDownStep = step;
- laneDownValue = laneValueAt (e.y);
- applyLane (e);
- return;
- }
- case Area::mute:
- if (e.mods.isPopupMenu() || e.mods.isCtrlDown())
- solo (row);
- else
- toggleMute (row);
- return;
- case Area::pan:
- case Area::volume:
- case Area::route:
- {
- const auto spec = specFor (area, row);
- if (e.mods.isPopupMenu())
- {
- juce::PopupMenu menu;
- menu.addSectionHeader (spec.name + ": " + spec.text (valueOf (area, row)));
- ui::addValueItems (menu, *this, &engine, spec, valueOf (area, row), [this, area, row] (double v) { setValue (area, row, v); });
- if (area == Area::route)
- {
- menu.addSeparator();
- menu.addItem (tr ("Next free insert"), [this, row] {
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.nextFreeInsert(); });
- });
- menu.addItem (tr ("Send the selected channels to free inserts") + "\tCtrl+L", [this] { sendSelectedToInserts(); });
- if (onOpenMixerInsert)
- menu.addItem (tr ("Show this insert in the Mixer"), [this, row] {
- if (row < (int) engine.getSong().channels.size())
- onOpenMixerInsert (engine.getSong().channels[(std::size_t) row].mixerTrack);
- });
- }
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- return;
- }
- if (ui::isResetClick (e))
- {
- setValue (area, row, spec.defaultValue);
- showTip (area, row, true);
- return;
- }
- knobDrag = area;
- knobRow = row;
- drag.begin (e, valueOf (area, row));
- showTip (area, row, false);
- repaint();
- return;
- }
- case Area::name:
- if (e.mods.isPopupMenu())
- {
- if (! std::binary_search (engine.getSelectedChannels().begin(), engine.getSelectedChannels().end(), row))
- clickSelector (row, {});
- else
- selectRows (engine.getSelectedChannels(), row);
- showChannelMenu (row);
- return;
- }
- if (e.mods.isShiftDown())
- {
- clickSelector (row, e.mods);
- return;
- }
- if (! std::binary_search (engine.getSelectedChannels().begin(), engine.getSelectedChannels().end(), row)
- || engine.getSelectedChannels().size() > 1)
- {
- selectionAnchor = row;
- selectRows ({ row }, row);
- }
- if (e.mods.isCtrlDown()) // audition without opening anything
- {
- previews.press (row, 60, 0.8f); // stops on release
- return;
- }
- // the editor opens on release, unless the button is dragged (that reorders the channels)
- nameDownRow = row;
- nameDragged = false;
- nameDownAlt = e.mods.isAltDown();
- nameDragY = e.position.y;
- return;
- case Area::select:
- clickSelector (row, e.mods);
- if (e.mods.isPopupMenu()) // right-drag adds the rows it crosses
- {
- selectDownRow = row;
- selectDragAdds = true;
- selectDragBase = engine.getSelectedChannels();
- }
- else if (! e.mods.isShiftDown() && ! e.mods.isCtrlDown()) // left-drag selects a range
- {
- selectDownRow = row;
- selectDragAdds = false;
- }
- 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 && drag.isActive())
- {
- setValue (knobDrag, knobRow, drag.update (e, specFor (knobDrag, knobRow)));
- showTip (knobDrag, knobRow, false);
- return;
- }
- if (laneDownStep >= 0)
- {
- applyLane (e);
- hoverStep = juce::jlimit (0, numSteps() - 1, (e.x - gridLeft()) / stepWidth);
- hoverArea = Area::lane;
- const auto v = laneValueAt (e.y);
- const auto bipolar = notetools::levelIsBipolar ((notetools::Level) graphLevel);
- const auto shown = juce::roundToInt ((bipolar ? v * 2.0f - 1.0f : v) * 100.0f);
- const auto valueText = (bipolar && shown > 0 ? "+" : "") + juce::String (shown) + "%";
- const auto levelText = tr (notetools::levelName ((notetools::Level) graphLevel));
- ui::showValueTip (*this, controlBounds (Area::lane, graphRow()), levelText + ": " + valueText);
- ui::Hint live;
- live.text = channelName (engine.getSong(), graphRow()) + " - " + levelText;
- live.value = valueText;
- ui::postHint (live);
- repaint (laneBounds());
- return;
- }
- if (nameDownRow >= 0)
- {
- if (! nameDragged && std::abs (e.position.y - nameDragY) < 4.0f)
- return;
- nameDragged = true;
- setMouseCursor (juce::MouseCursor::UpDownResizeCursor);
- const auto count = (int) engine.getSong().channels.size();
- const auto target = juce::jlimit (0, count - 1, rowAtY (e.y));
- // move the selection one row at a time until the grabbed channel is under the mouse
- for (int guard = 0; guard < count && engine.getSelectedChannel() != target; ++guard)
- if (! moveSelected (target < engine.getSelectedChannel() ? -1 : 1))
- break;
- return;
- }
- if (selectDownRow >= 0)
- {
- const auto count = (int) engine.getSong().channels.size();
- const auto row = juce::jlimit (0, juce::jmax (0, count - 1), rowAtY (e.y));
- std::vector<int> rows;
- for (int r = juce::jmin (selectDownRow, row); r <= juce::jmax (selectDownRow, row); ++r)
- rows.push_back (r);
- if (selectDragAdds)
- {
- rows.insert (rows.end(), selectDragBase.begin(), selectDragBase.end());
- std::sort (rows.begin(), rows.end());
- rows.erase (std::unique (rows.begin(), rows.end()), rows.end());
- }
- else
- selectionAnchor = selectDownRow;
- if (rows != engine.getSelectedChannels())
- selectRows (rows, row);
- return;
- }
- if (paintMode != PaintMode::none)
- applyPaint (e);
- }
- void ChannelRackComponent::mouseUp (const juce::MouseEvent& e)
- {
- previews.releaseAll(); // the audition (Ctrl+click) and the steps' previews stop with the button
- if (nameDownRow >= 0 && ! nameDragged && e.mouseWasClicked())
- toggleEditor (nameDownRow, nameDownAlt);
- if (nameDragged)
- setMouseCursor (juce::MouseCursor::NormalCursor);
- nameDownRow = -1;
- nameDragged = false;
- selectDownRow = -1;
- selectDragBase.clear();
- ui::hideValueTip();
- ui::clearPostedHint();
- paintMode = PaintMode::none;
- laneDownStep = -1;
- if (knobDrag != Area::none)
- repaint();
- knobDrag = Area::none;
- drag.end();
- }
- void ChannelRackComponent::mouseDoubleClick (const juce::MouseEvent& e)
- {
- int row = 0;
- const auto area = areaAt (e.x, e.y, row);
- if (area == Area::pan || area == Area::volume)
- {
- setValue (area, row, specFor (area, row).defaultValue);
- showTip (area, row, true);
- }
- else if (area == Area::route)
- {
- if (onOpenMixerInsert)
- onOpenMixerInsert (engine.getSong().channels[(std::size_t) row].mixerTrack);
- else
- engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.nextFreeInsert(); });
- }
- else if (area == Area::select && ! e.mods.isPopupMenu())
- selectAll();
- else if (area == Area::name && ! e.mods.isAnyModifierKeyDown() && onOpenPianoRoll)
- onOpenPianoRoll();
- }
- void ChannelRackComponent::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
- {
- int row = 0;
- const auto area = areaAt (e.x, e.y, row);
- if (area == Area::pan || area == Area::volume || area == Area::route)
- {
- if (const auto v = ui::wheelValue (valueOf (area, row), e, wheel, specFor (area, row)))
- {
- setValue (area, row, *v);
- showTip (area, row, true);
- }
- return;
- }
- if ((area == Area::name || area == Area::select) && e.mods.isShiftDown()) // Shift+wheel moves the channel
- {
- const auto delta = (wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1;
- selectRows ({ row }, row);
- moveSelected (delta);
- return;
- }
- juce::Component::mouseWheelMove (e, wheel); // scroll the viewport
- }
- ui::Hint ChannelRackComponent::hintAt (juce::Point<int> p)
- {
- int row = 0;
- const auto area = areaAt (p.x, p.y, row);
- const auto& song = engine.getSong();
- ui::Hint h;
- if (row >= 0 && row < (int) song.channels.size() && (area == Area::pan || area == Area::volume || area == Area::route))
- {
- const auto active = knobDrag != Area::none && drag.isActive();
- const auto shownArea = active ? knobDrag : area;
- const auto shownRow = active ? knobRow : row;
- h = ui::valueHint (specFor (shownArea, shownRow), valueOf (shownArea, shownRow),
- shownArea == Area::route ? tr ("[LMB] drag or [Wheel] routes [2xLMB] show in the Mixer [RMB] next free insert")
- : ui::valueGestures());
- return h;
- }
- const auto name = channelName (song, row);
- switch (area)
- {
- case Area::mute:
- h.text = name + " - " + tr ("Click: mute. Ctrl+click or right-click: solo");
- break;
- case Area::name:
- h.text = name + " - " + tr ("[LMB] open/close the editor [Alt]+[LMB] keep others open [Ctrl]+[LMB] audition [2xLMB] Piano Roll [LMB] drag up/down reorders [RMB] channel menu");
- h.hasMenu = true;
- break;
- case Area::select:
- h.text = name + " - " + tr ("Select: [LMB] one [Ctrl]+[LMB] or [RMB] add/remove [Shift]+[LMB] range [LMB] drag range [RMB] drag adds [2xLMB] all [Alt]+[Up]/[Down] moves the selection");
- break;
- case Area::grid:
- h.text = name + " - " + tr ("Steps: [LMB] drag draws (starting on a lit step erases) [RMB] drag erases");
- break;
- case Area::addRow:
- h.text = tr ("Add a channel (or drop audio files on the rack)");
- break;
- case Area::laneLevels:
- h.text = tr ("Graph lane: choose what the bars edit (Ctrl+K hides the lane)");
- break;
- case Area::lane:
- h.text = name + " - " + tr ("Graph lane: drag sets the steps, right-drag draws a ramp, Alt+click resets");
- break;
- default:
- break;
- }
- return h;
- }
- void ChannelRackComponent::applyPaint (const juce::MouseEvent& e)
- {
- const auto& song = engine.getSong();
- const auto row = rowAtY (e.y);
- const auto step = (e.x - gridLeft()) / stepWidth;
- if (laneBounds().contains (e.getPosition()))
- return;
- 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) // the new step sounds while the button is held (the previous one stops)
- {
- previews.releaseAll();
- previews.press (row, 60, 0.78f);
- }
- }
- // --- menus -------------------------------------------------------------------------------------
- void ChannelRackComponent::showChannelMenu (int channel)
- {
- const auto& c = engine.getSong().channels[(std::size_t) channel];
- juce::PopupMenu instruments;
- for (auto kind : builtInKinds)
- instruments.addItem (instrumentLabel (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 (tr ("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 (tr ("Plugin (VST/VST3/CLAP)"), plugins);
- juce::PopupMenu menu;
- menu.addSectionHeader (juce::String::fromUTF8 (c.name.c_str()));
- if (c.instrument == InstrumentKind::sampler)
- {
- menu.addItem (tr ("Sample: {0}", { juce::File (juce::String::fromUTF8 (c.samplePath.c_str())).getFileName() }),
- false, false, [] {});
- menu.addItem (tr ("Sampler settings (pitch, envelope, reverse)..."),
- [this, channel] { openChannelSettings (engine, channel); });
- menu.addItem (tr ("Edit the sample in the audio editor..."), ! c.samplePath.empty(), false, [this, channel] { openAudioEditorForChannel (engine, channel); });
- if (c.sampler.slices.empty())
- menu.addItem (tr ("Slice sample (slicer): one slice per key and a pattern with the loop"), [this, channel] { sliceChannel (channel); });
- else
- menu.addItem (tr ("Undo slices ({0})", { juce::String ((int) c.sampler.slices.size()) }), [this, channel] {
- engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].sampler.slices.clear(); });
- });
- }
- else if (c.instrument == InstrumentKind::synth)
- menu.addItem (tr ("Synth settings (oscillators, envelope, filter)..."), [this, channel] { openChannelSettings (engine, channel); });
- else if (hasChannelSettings (c.instrument))
- menu.addItem (tr ("Channel settings..."), [this, channel] { openChannelSettings (engine, channel); });
- if (c.instrument == InstrumentKind::plugin)
- {
- menu.addItem (tr ("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 (tr ("Piano Roll"), [this] {
- if (onOpenPianoRoll)
- onOpenPianoRoll();
- });
- menu.addSubMenu (tr ("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[] = { TR_NOOP ("Off"), TR_NOOP ("Up"), TR_NOOP ("Down"), TR_NOOP ("Up and down"), TR_NOOP ("Random") };
- for (int d = 0; d < 5; ++d)
- arpMenu.addItem (tr (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 (o == 1 ? tr ("1 octave") : tr ("{0} octaves", { juce::String (o) }), 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 }, { TR_NOOP ("1/8 triplet"), 1.0f / 3.0f },
- { "1/16", 0.25f }, { TR_NOOP ("1/16 triplet"), 1.0f / 6.0f }, { "1/32", 0.125f } };
- for (const auto& [name, beats] : speeds)
- speed.addItem (tr (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 (tr ("Range"), range);
- arpMenu.addSubMenu (tr ("Speed"), speed);
- arpMenu.addSubMenu (tr ("Gate"), gate);
- arpMenu.addSubMenu (tr ("Repeat"), repeat);
- menu.addSubMenu (arp.enabled() ? tr ("Arpeggiator (on)") : tr ("Arpeggiator"), arpMenu);
- }
- menu.addItem (tr ("Rename and color...") + "\tF2", [this, channel] { rename (channel); });
- {
- juce::PopupMenu colours;
- colours.addItem (tr ("Default"), [this, channel] { engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].colour = 0; }); });
- for (auto colour : lynxPalette())
- {
- juce::Image swatch (juce::Image::ARGB, 16, 16, true);
- juce::Graphics (swatch).fillAll (juce::Colour (colour));
- juce::PopupMenu::Item item;
- item.text = "#" + juce::String::toHexString ((juce::int64) (colour & 0xFFFFFF)).paddedLeft ('0', 6).toUpperCase();
- item.image = std::make_unique<juce::DrawableImage> (swatch);
- item.isTicked = c.colour == colour;
- item.action = [this, channel, colour] { engine.editSong ([channel, colour] (Song& s) { s.channels[(std::size_t) channel].colour = colour; }); };
- colours.addItem (std::move (item));
- }
- menu.addSubMenu (tr ("Color"), colours);
- }
- {
- juce::PopupMenu steps;
- steps.addItem (tr ("Fill every 2 steps"), [this] { fillSelected (2); });
- steps.addItem (tr ("Fill every 4 steps"), [this] { fillSelected (4); });
- steps.addItem (tr ("Fill every 8 steps"), [this] { fillSelected (8); });
- steps.addItem (tr ("Euclidean fill..."), [this, channel] { showEuclideanFill (channel); });
- steps.addSeparator();
- steps.addItem (tr ("Rotate left") + "\tCtrl+Shift+Left", [this] { rotateSelected (-1); });
- steps.addItem (tr ("Rotate right") + "\tCtrl+Shift+Right", [this] { rotateSelected (1); });
- steps.addSeparator();
- steps.addItem (tr ("Graph lanes (velocity, pan, pitch...)") + "\tCtrl+K", true, graphLanes, [this] { toggleGraphLanes(); });
- steps.addSeparator();
- steps.addItem (tr ("Swing mix ({0})...", { juce::String (juce::roundToInt (c.swingMix * 100.0f)) + "%" }),
- [this, channel] { setSwingMixOfSelected (channel); });
- menu.addSubMenu (tr ("Steps"), steps);
- }
- menu.addItem (tr ("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());
- });
- });
- // organisation (acts on the selection)
- const auto selected = (int) engine.getSelectedChannels().size();
- const auto count = (int) engine.getSong().channels.size();
- menu.addSeparator();
- menu.addItem (selected > 1 ? tr ("Clone {0} channels", { juce::String (selected) }) : tr ("Clone channel"), [this] { cloneSelected(); });
- {
- juce::PopupMenu insert;
- addInstrumentItems (insert, channel);
- menu.addSubMenu (tr ("Insert channel here"), insert);
- }
- menu.addItem (tr ("Move up") + "\tAlt+Up", engine.getSelectedChannels().front() > 0, false, [this] { moveSelected (-1); });
- menu.addItem (tr ("Move down") + "\tAlt+Down", engine.getSelectedChannels().back() < count - 1, false, [this] { moveSelected (1); });
- menu.addItem (tr ("Send to free mixer inserts") + "\tCtrl+L", [this] { sendSelectedToInserts(); });
- {
- juce::PopupMenu sort;
- sort.addItem (tr ("By name"), [this] { sortChannels (ChannelSort::name); });
- sort.addItem (tr ("By color"), [this] { sortChannels (ChannelSort::colour); });
- sort.addItem (tr ("By mixer insert"), [this] { sortChannels (ChannelSort::insert); });
- sort.addItem (tr ("By type"), [this] { sortChannels (ChannelSort::type); });
- menu.addSubMenu (tr ("Sort channels"), sort);
- }
- menu.addItem (tr ("Select unused channels"), [this] { selectUnused(); });
- menu.addSeparator();
- if (selected > 1)
- menu.addItem (tr ("Delete {0} channels", { juce::String (selected) }) + "\tAlt+Del", selected < count, false, [this] { deleteSelected(); });
- else
- menu.addItem (tr ("Delete channel") + "\tAlt+Del", count > 1, false, [this, channel] {
- closeEditor (channel);
- engine.editSong ([channel] (Song& s) { s.removeChannel (channel); });
- openEditorRow = -1;
- });
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- }
- void ChannelRackComponent::addInstrumentItems (juce::PopupMenu& menu, int insertAt)
- {
- // a new channel of `kind`, at the end (insertAt < 0) or at a row
- auto add = [this, insertAt] (ChannelDef c) {
- int index = 0;
- engine.editSong ([&] (Song& s) {
- c.mixerTrack = s.nextFreeInsert();
- index = insertAt < 0 ? (s.channels.push_back (c), (int) s.channels.size() - 1) : insertChannel (s, insertAt, c);
- });
- selectionAnchor = index;
- selectRows ({ index }, index);
- return index;
- };
- for (auto kind : builtInKinds)
- menu.addItem (instrumentLabel (kind), [add, kind] {
- ChannelDef c;
- c.name = instrumentName (kind).toStdString();
- c.instrument = kind;
- add (c);
- });
- menu.addSeparator();
- if (insertAt < 0)
- menu.addItem (tr ("Sampler (choose file)..."), [this] { chooseSample (-1); });
- juce::PopupMenu plugins;
- addPluginItems (plugins, engine, true, [this, add] (PluginRef ref) {
- ChannelDef c;
- c.name = ref.name;
- c.instrument = InstrumentKind::plugin;
- c.plugin = ref;
- const auto added = add (c);
- openPluginWindow (added); // the plugin opens when added
- openEditorRow = added;
- });
- menu.addSubMenu (tr ("Plugin (VST/VST3/CLAP)"), plugins);
- }
- void ChannelRackComponent::showAddChannelMenu (int insertAt)
- {
- juce::PopupMenu menu;
- addInstrumentItems (menu, insertAt);
- menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
- }
- void ChannelRackComponent::chooseSample (int channel)
- {
- fileChooser = std::make_unique<juce::FileChooser> (tr ("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, tr ("Sample"),
- tr ("Could not read {0}", { 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, tr ("Plugin"),
- tr ("This plugin could not be loaded (it is not installed or has not been scanned)."));
- }
- void ChannelRackComponent::rename (int channel)
- {
- const auto& c = engine.getSong().channels[(std::size_t) channel];
- showNameAndColourDialog (tr ("Rename channel"), juce::String::fromUTF8 (c.name.c_str()), c.colour,
- [this, channel] (const juce::String& name, std::uint32_t colour) {
- if (channel < (int) engine.getSong().channels.size())
- engine.editSong ([channel, n = name.toStdString(), colour] (Song& s) {
- s.channels[(std::size_t) channel].name = n;
- s.channels[(std::size_t) channel].colour = colour;
- });
- });
- }
- 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 BrowserComponent::isBrowserDrag (details);
- }
- void ChannelRackComponent::itemDropped (const SourceDetails& details)
- {
- if (const auto file = BrowserComponent::draggedFile (details); file.existsAsFile())
- filesDropped ({ file.getFullPathName() }, details.localPosition.x, details.localPosition.y);
- }
- } // namespace lynx
|