ChannelRackComponent.cpp 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020
  1. #include "core/SelectionRename.h"
  2. #include "core/FlSettings.h"
  3. #include <unordered_map>
  4. #include "ui/Motion.h"
  5. #include "core/RackLayout.h"
  6. #include "core/RackFilter.h"
  7. #include "ui/FlMods.h"
  8. #include "ui/EditorTools.h"
  9. #include "core/FruityDance.h"
  10. #include "core/VideoPlayer.h"
  11. #include "ui/ColourPicker.h"
  12. #include "ui/FileChoosers.h"
  13. #include "ui/TrackIcons.h"
  14. #include "core/TrackIcons.h"
  15. #include "ui/PatcherEditor.h"
  16. #include "ui/UiTrace.h"
  17. #include "ui/TextCache.h"
  18. #include "ui/ChannelRackComponent.h"
  19. #include "core/NoteRepeat.h"
  20. #include "ui/PaintAB.h"
  21. #include "ui/ImageCaches.h"
  22. #include "ui/PluginOffTag.h"
  23. #include "ui/SpeechDialog.h"
  24. #include "core/Patcherize.h"
  25. #include "ui/QuickPick.h"
  26. #include "core/ControlSurface.h"
  27. #include "core/VoltageController.h"
  28. #include "core/MixerOps.h"
  29. #include "core/PlaylistOps.h"
  30. #include "ui/AutomationUi.h"
  31. #include "core/NoteNames.h"
  32. #include "core/NoteTools.h"
  33. #include "core/NoteEdit.h"
  34. #include "core/FlSynths.h"
  35. #include "core/MidiControls.h"
  36. #include "ui/ToolWindow.h"
  37. #include "ui/LoopStarterWindow.h"
  38. #include "core/Rack.h"
  39. #include "core/Slicer.h"
  40. #include "ui/ChannelSettings.h"
  41. #include "engine/Text.h"
  42. #include "engine/ProjectFile.h"
  43. #include "platform/Paths.h"
  44. #include "ui/Dialogs.h"
  45. #include "ui/HintBar.h"
  46. #include "ui/ExportDialog.h"
  47. #include "core/StandIns.h"
  48. #include "ui/I18n.h"
  49. #include "ui/SamplerImportUi.h"
  50. #include "ui/LiveRemoteUi.h"
  51. #include "ui/BrowserComponent.h"
  52. #include "ui/PluginWindow.h"
  53. #include "ui/Theme.h"
  54. #include "ui/AudioEditor.h"
  55. #include "ui/AutoSamplerDialog.h"
  56. #include "core/RackOps.h"
  57. #include "core/TransportEdit.h"
  58. #include "core/ChannelBurn.h"
  59. #include "core/ChannelLoops.h"
  60. #include "core/SoundFont.h"
  61. #include "core/Vfx.h"
  62. #include "ui/SoundFontEditor.h"
  63. namespace lynx
  64. {
  65. namespace
  66. {
  67. void sliceChannelToDrumRack (AudioEngine& engine, int channel); // (round 190ec, below)
  68. // English name (used for new channel names stored in the song)
  69. juce::String instrumentName (InstrumentKind k)
  70. {
  71. switch (k)
  72. {
  73. case InstrumentKind::kick: return "Kick";
  74. case InstrumentKind::snare: return "Snare";
  75. case InstrumentKind::hat: return "Hat";
  76. case InstrumentKind::clap: return "Clap";
  77. case InstrumentKind::synth: return "Synth";
  78. case InstrumentKind::sampler: return "Sampler";
  79. case InstrumentKind::audioClip: return "Audio clip";
  80. case InstrumentKind::plugin: return "Plugin";
  81. case InstrumentKind::controller: return "Controller";
  82. case InstrumentKind::sakura: return "Sakura";
  83. case InstrumentKind::drumpad: return "Drumpad";
  84. case InstrumentKind::boobass: return "BooBass";
  85. case InstrumentKind::drumsynth: return "DrumSynth Live";
  86. case InstrumentKind::harmless: return "Harmless";
  87. case InstrumentKind::sytrus: return "Sytrus";
  88. case InstrumentKind::harmor: return "Harmor";
  89. case InstrumentKind::gms: return "GMS";
  90. case InstrumentKind::morphine: return "Morphine";
  91. case InstrumentKind::ogun: return "Ogun";
  92. case InstrumentKind::minisynth: return "MiniSynth";
  93. case InstrumentKind::dx10: return "Fruity DX10";
  94. case InstrumentKind::kepler: return "Kepler";
  95. case InstrumentKind::keplerExo: return "Kepler Exo";
  96. case InstrumentKind::poizone: return "PoiZone";
  97. case InstrumentKind::fruitkick: return "Fruit Kick";
  98. case InstrumentKind::bassdrum: return "BassDrum";
  99. case InstrumentKind::toxic: return "Toxic Biohazard";
  100. case InstrumentKind::threeosc: return "3x Osc";
  101. case InstrumentKind::autogun: return "Autogun";
  102. case InstrumentKind::transistorbass: return "Transistor Bass";
  103. case InstrumentKind::simsynth: return "SimSynth";
  104. case InstrumentKind::wasp: return "Wasp";
  105. case InstrumentKind::plucked: return "Plucked!";
  106. case InstrumentKind::beepmap: return "BeepMap";
  107. case InstrumentKind::sawer: return "Sawer";
  108. case InstrumentKind::flkeys: return "FL Keys";
  109. case InstrumentKind::granulizer: return "Fruity Granulizer";
  110. case InstrumentKind::drumaxx: return "Drumaxx";
  111. case InstrumentKind::slayer: return "FL Slayer";
  112. case InstrumentKind::fruitydance: return "Fruity Dance";
  113. case InstrumentKind::directwave: return "DirectWave";
  114. case InstrumentKind::midiOut: return "MIDI Out";
  115. case InstrumentKind::patcher: return "Patcher";
  116. case InstrumentKind::soundfont: return "LYNX Soundfont";
  117. case InstrumentKind::layer: return "Layer";
  118. case InstrumentKind::dashboard: return "Dashboard";
  119. case InstrumentKind::voltage: return "Voltage Controller";
  120. case InstrumentKind::vfx: return "VFX";
  121. case InstrumentKind::videoplayer: return "Fruity Video Player";
  122. case InstrumentKind::wavetraveller: return "Wave Traveller";
  123. case InstrumentKind::fruityslicer: return "Fruity Slicer";
  124. case InstrumentKind::slicex: return "Slicex";
  125. case InstrumentKind::fpc: return "FPC";
  126. case InstrumentKind::pocketSuperSaw: return "Pocket SuperSaw";
  127. case InstrumentKind::tetra: return "Tetra";
  128. case InstrumentKind::helix: return "Helix";
  129. case InstrumentKind::braid: return "Braid";
  130. case InstrumentKind::lynxModel: return "LYNX Model";
  131. }
  132. return "?";
  133. }
  134. // name shown in menus (translated)
  135. juce::String instrumentLabel (InstrumentKind k)
  136. {
  137. switch (k)
  138. {
  139. case InstrumentKind::kick: return tr ("Kick");
  140. case InstrumentKind::snare: return tr ("Snare");
  141. case InstrumentKind::hat: return tr ("Hat");
  142. case InstrumentKind::clap: return tr ("Clap");
  143. case InstrumentKind::synth: return tr ("Synth");
  144. case InstrumentKind::sampler: return tr ("Sampler");
  145. case InstrumentKind::audioClip: return tr ("Audio clip");
  146. case InstrumentKind::controller: return tr ("Envelope Controller");
  147. case InstrumentKind::midiOut: return tr ("MIDI Out");
  148. case InstrumentKind::layer: return tr ("Layer");
  149. case InstrumentKind::dashboard: return "Dashboard";
  150. case InstrumentKind::voltage: return "Voltage Controller";
  151. default: return instrumentName (k);
  152. }
  153. }
  154. void setStep (Pattern& p, int channel, int step, bool on, bool muteRemoved = false)
  155. {
  156. if (p.hasStep (channel, step) != on)
  157. p.toggleStep (channel, step, muteRemoved);
  158. }
  159. juce::String channelName (const Song& song, int row)
  160. {
  161. return row >= 0 && row < (int) song.channels.size() ? juce::String::fromUTF8 (song.channels[(std::size_t) row].name.c_str()) : juce::String();
  162. }
  163. } // namespace
  164. bool ChannelRackComponent::keepEditorsOpen = false;
  165. bool ChannelRackComponent::selectFirstChannelWithNotes = false;
  166. bool ChannelRackComponent::autoSelectLinked = true;
  167. bool ChannelRackComponent::muteRemovedSteps = false;
  168. bool ChannelRackComponent::nameAlignLeft = false;
  169. ChannelRackComponent::ChannelRackComponent (AudioEngine& e)
  170. : engine (e)
  171. {
  172. setWantsKeyboardFocus (false);
  173. setMouseClickGrabsKeyboardFocus (false);
  174. // paints every pixel (paint() starts with an opaque fillAll): the panel behind it is no longer filled under it on every
  175. // frame (round 190ms, perf: the window frame's fill under the rack was ~2.5 M instructions a whole frame on JUCE's
  176. // software renderer). Linux only: on Direct2D the frame's clip then becomes a rectangle list (a geometry layer), and
  177. // the rack's frame got 2-9 % slower (round 190ms, Windows --paintbench); macOS was not measured.
  178. #if JUCE_LINUX || JUCE_BSD
  179. setOpaque (true);
  180. #endif
  181. engine.addChangeListener (this);
  182. engine.getLevelsBroadcaster().addChangeListener (this);
  183. previews.setSender ([this] (int channel, int key, float velocity, bool on) { engine.sendLiveNote (channel, key, velocity, on); });
  184. updateSize();
  185. showingTimer = std::make_unique<ui::TimerWhileShowing> (*this, static_cast<juce::Timer&> (*this), 30); // (stopped while hidden; round 174f)
  186. }
  187. ChannelRackComponent::~ChannelRackComponent()
  188. {
  189. previews.releaseAll();
  190. engine.getLevelsBroadcaster().removeChangeListener (this);
  191. engine.removeChangeListener (this);
  192. }
  193. void ChannelRackComponent::moved()
  194. {
  195. // the viewport scrolled the rack sideways: the controls column follows the visible left edge
  196. const auto x = juce::jmax (0, -getX());
  197. if (x != stickyX)
  198. {
  199. stickyX = x;
  200. repaint();
  201. }
  202. }
  203. void ChannelRackComponent::visibilityChanged()
  204. {
  205. if (! isShowing())
  206. previews.releaseAll(); // hidden mid-gesture: nothing keeps sounding
  207. }
  208. int ChannelRackComponent::numSteps() const
  209. {
  210. const auto& song = engine.getSong();
  211. if (song.patterns.empty())
  212. return 16;
  213. const auto& p = song.activePattern();
  214. const auto steps = (int) (p.effectiveLength (song.meter()) / ticksPerStep);
  215. return juce::jlimit (16, 512, steps);
  216. }
  217. PreviewSpan ChannelRackComponent::previewSpan() const
  218. {
  219. auto visible = 0;
  220. if (const auto* holder = getParentComponent())
  221. visible = holder->getWidth() - gridLeft() - 8 - (advancedLoopShown() ? loopColumnWidth : 0);
  222. // (at least 8 steps wide: a rack squeezed narrower than that scrolls its miniatures too)
  223. return notePreviewSpan (numSteps() * stepWidth, juce::jmax (visible, 8 * stepWidth), stickyX, completePreview.load (std::memory_order_relaxed));
  224. }
  225. int ChannelRackComponent::shownSteps() const
  226. {
  227. return juce::jmin (512, numSteps() + ghostSteps);
  228. }
  229. bool ChannelRackComponent::isShown (int row) const
  230. {
  231. return rackfilter::shows (engine.getSong(), row, filterGroup);
  232. }
  233. int ChannelRackComponent::visibleIndex (int row) const
  234. {
  235. if (filterGroup == rackfilter::all)
  236. return row;
  237. int index = 0;
  238. for (int r = 0; r < row && r < (int) engine.getSong().channels.size(); ++r)
  239. index += isShown (r) ? 1 : 0;
  240. return index + juce::jmax (0, row - (int) engine.getSong().channels.size());
  241. }
  242. int ChannelRackComponent::shownCount() const
  243. {
  244. return visibleIndex ((int) engine.getSong().channels.size());
  245. }
  246. // round 190ld (DE-37): FL's Page Up / Page Down step the filter groups in the menu's order (All, Unsorted, Audio clips,
  247. // then the project's groups), wrapping round
  248. void ChannelRackComponent::stepFilterGroup (int direction)
  249. {
  250. const auto& song = engine.getSong();
  251. std::vector<int> order { rackfilter::all, rackfilter::unsorted, rackfilter::audioClips };
  252. for (int g = 0; g < (int) song.channelGroups.size(); ++g)
  253. if (! rackfilter::isBuiltinName (song.channelGroups[(std::size_t) g]))
  254. order.push_back (g);
  255. const auto at = std::find (order.begin(), order.end(), filterGroup);
  256. const auto i = at == order.end() ? 0 : (int) (at - order.begin());
  257. const auto n = (int) order.size();
  258. setFilterGroup (order[(std::size_t) (((i + (direction < 0 ? -1 : 1)) % n + n) % n)]);
  259. }
  260. void ChannelRackComponent::setFilterGroup (int group)
  261. {
  262. group = rackfilter::clamp (engine.getSong(), group);
  263. if (group == filterGroup)
  264. return;
  265. filterGroup = group;
  266. updateSize();
  267. repaint();
  268. if (onFilterChanged)
  269. onFilterChanged();
  270. }
  271. juce::String ChannelRackComponent::filterName() const
  272. {
  273. const auto& groups = engine.getSong().channelGroups;
  274. if (filterGroup == rackfilter::audioClips)
  275. return tr ("Audio clips");
  276. if (filterGroup == rackfilter::unsorted)
  277. return tr ("Unsorted");
  278. return filterGroup >= 0 && filterGroup < (int) groups.size() ? juce::String::fromUTF8 (groups[(std::size_t) filterGroup].c_str()) : tr ("All");
  279. }
  280. void ChannelRackComponent::showFilterMenu (juce::Component* target)
  281. {
  282. const auto& song = engine.getSong();
  283. juce::PopupMenu menu;
  284. menu.addItem (tr ("All"), true, filterGroup == rackfilter::all, [this] { setFilterGroup (rackfilter::all); });
  285. // FL's built-in views (round 168t, REA_rodada168t.md §3): the audio clip channels, and the channels in no group
  286. menu.addItem (tr ("Unsorted"), true, filterGroup == rackfilter::unsorted, [this] { setFilterGroup (rackfilter::unsorted); });
  287. menu.addItem (tr ("Audio clips"), true, filterGroup == rackfilter::audioClips, [this] { setFilterGroup (rackfilter::audioClips); });
  288. bool separated = false;
  289. for (int g = 0; g < (int) song.channelGroups.size(); ++g)
  290. {
  291. if (rackfilter::isBuiltinName (song.channelGroups[(std::size_t) g]))
  292. continue; // FL's own "Audio clips" / "Unsorted" groups: the two views above
  293. if (! std::exchange (separated, true))
  294. menu.addSeparator();
  295. menu.addItem (juce::String::fromUTF8 (song.channelGroups[(std::size_t) g].c_str()), true, filterGroup == g, [this, g] { setFilterGroup (g); });
  296. }
  297. menu.addSeparator();
  298. menu.addItem (tr ("Group selected...") + "\tAlt+G", [this] { groupSelected(); });
  299. {
  300. juce::PopupMenu move;
  301. move.addItem (tr ("(no group)"), [this] { moveSelectedToGroup (-1); });
  302. for (int g = 0; g < (int) song.channelGroups.size(); ++g)
  303. move.addItem (juce::String::fromUTF8 (song.channelGroups[(std::size_t) g].c_str()), [this, g] { moveSelectedToGroup (g); });
  304. menu.addSubMenu (tr ("Move selected channels to"), move, ! engine.getSelectedChannels().empty());
  305. }
  306. menu.addItem (tr ("Rename this group..."), filterGroup >= 0, false, [this] {
  307. const auto group = filterGroup;
  308. auto* window = new juce::AlertWindow (tr ("Filter group name"), {}, juce::MessageBoxIconType::NoIcon);
  309. window->addTextEditor ("name", filterName());
  310. window->addButton (tr ("OK"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  311. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  312. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, group, window] (int result) {
  313. const auto name = window->getTextEditorContents ("name").trim().toStdString();
  314. if (result != 1 || name.empty())
  315. return;
  316. engine.editSong ([group, name] (Song& s) {
  317. if (group < (int) s.channelGroups.size())
  318. s.channelGroups[(std::size_t) group] = name;
  319. });
  320. if (onFilterChanged)
  321. onFilterChanged();
  322. }),
  323. true);
  324. });
  325. menu.addItem (tr ("Delete this group (keeps its channels)"), filterGroup >= 0, false, [this] {
  326. const auto group = filterGroup;
  327. setFilterGroup (-1);
  328. engine.editSong ([group] (Song& s) {
  329. if (group >= (int) s.channelGroups.size())
  330. return;
  331. s.channelGroups.erase (s.channelGroups.begin() + group);
  332. for (auto& c : s.channels)
  333. c.group = c.group == group ? -1 : c.group > group ? c.group - 1 : c.group;
  334. });
  335. });
  336. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (target));
  337. }
  338. void ChannelRackComponent::moveSelectedToGroup (int group)
  339. {
  340. engine.editSong ([group, rows = engine.getSelectedChannels()] (Song& s) {
  341. for (auto r : rows)
  342. if (r >= 0 && r < (int) s.channels.size())
  343. s.channels[(std::size_t) r].group = group >= 0 && group < (int) s.channelGroups.size() ? group : -1;
  344. });
  345. updateSize();
  346. repaint();
  347. }
  348. void ChannelRackComponent::groupSelected()
  349. {
  350. // FL: Alt+G asks for the "Filter group name"; the selected channels move into the new group, which is shown
  351. auto* window = new juce::AlertWindow (tr ("Filter group name"), {}, juce::MessageBoxIconType::NoIcon);
  352. window->addTextEditor ("name", tr ("Group {0}", { juce::String ((int) engine.getSong().channelGroups.size() + 1) }));
  353. window->addButton (tr ("OK"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  354. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  355. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, window] (int result) {
  356. const auto name = window->getTextEditorContents ("name").trim().toStdString();
  357. if (result != 1 || name.empty())
  358. return;
  359. const auto group = (int) engine.getSong().channelGroups.size();
  360. engine.editSong ([name, rows = engine.getSelectedChannels()] (Song& s) {
  361. s.channelGroups.push_back (name);
  362. for (auto r : rows)
  363. if (r >= 0 && r < (int) s.channels.size())
  364. s.channels[(std::size_t) r].group = (int) s.channelGroups.size() - 1;
  365. });
  366. setFilterGroup (group);
  367. }),
  368. true);
  369. }
  370. void ChannelRackComponent::updateSize()
  371. {
  372. refreshZipLayout(); // (round 190mk: zipped channels' strips)
  373. const auto rows = shownCount() + 1; // +1 = "add channel" row
  374. // FL's ghost steps: when the rack is wider than the pattern, the free width shows faint steps after its end;
  375. // clicking one adds a step there and makes the pattern longer (issue #11)
  376. ghostSteps = 0;
  377. const auto loopColumn = advancedLoopShown() ? loopColumnWidth : 0; // (FL's advanced looping controls, at the right)
  378. if (const auto* holder = getParentComponent())
  379. ghostSteps = juce::jmax (0, (holder->getWidth() - gridLeft() - 8 - loopColumn) / stepWidth - numSteps());
  380. setSize (gridLeft() + shownSteps() * stepWidth + 8 + loopColumn,
  381. (zipLayout.top.empty() ? rows * rowHeight : zipLayout.total) + 8 + (graphRow() >= 0 ? laneHeight : 0));
  382. }
  383. // --- FL's Channel button width (round 190mk, DE-39) ---------------------------------------------------------------------
  384. void ChannelRackComponent::setButtonWidth (int percent)
  385. {
  386. buttonWidthPercent = percent <= 0 ? 0 : juce::jlimit (100, 400, percent);
  387. if (buttonWidthPercent > 0)
  388. nameWidth = widthForPercent (buttonWidthPercent);
  389. else // Size to fit: the longest name with the button's margins, between 100 % and 400 %
  390. {
  391. const auto font = theme::font (theme::type::body);
  392. float widest = 0.0f;
  393. for (const auto& c : engine.getSong().channels)
  394. widest = juce::jmax (widest, juce::GlyphArrangement::getStringWidth (font, juce::String::fromUTF8 (c.name.c_str())));
  395. nameWidth = juce::jlimit (widthForPercent (100), widthForPercent (400), (int) std::ceil (widest) + 34);
  396. }
  397. updateSize();
  398. repaint();
  399. }
  400. // --- FL's zipped channels (round 190mk, DE-38; capture 190mf A4, core/RackZip.h) ----------------------------------------
  401. void ChannelRackComponent::refreshZipLayout()
  402. {
  403. const auto& channels = engine.getSong().channels;
  404. if (std::none_of (channels.begin(), channels.end(), [] (const ChannelDef& c) { return c.zipped; }))
  405. {
  406. zipLayout = {}; // (no zipped channel: FL's fixed rows, the code paths as before)
  407. return;
  408. }
  409. std::vector<bool> zipped, shown;
  410. zipped.reserve (channels.size());
  411. shown.reserve (channels.size());
  412. for (int r = 0; r < (int) channels.size(); ++r)
  413. {
  414. zipped.push_back (channels[(std::size_t) r].zipped);
  415. shown.push_back (isShown (r));
  416. }
  417. zipLayout = rackzip::layout (zipped, shown, rowHeight);
  418. }
  419. juce::Rectangle<int> ChannelRackComponent::zipBar (int row) const
  420. {
  421. if (! isZipped (row))
  422. return {};
  423. const auto slot = zipLayout.slot[(std::size_t) row];
  424. const auto top = rowY (row), height = rowHeight / 2;
  425. return { stickyX + nameLeft() + slot * (nameWidth + rackzip::barGap), top + (height - rackzip::barHeight) / 2, nameWidth, rackzip::barHeight };
  426. }
  427. int ChannelRackComponent::zipBarAt (int row, int x) const
  428. {
  429. if (! isZipped (row))
  430. return -1;
  431. return rackzip::barAt (zipLayout, zipLayout.first[(std::size_t) row], x, stickyX + nameLeft(), nameWidth);
  432. }
  433. bool ChannelRackComponent::zipStripForSnapshot (int row, juce::Rectangle<int>& strip) const
  434. {
  435. if (! isZipped (row))
  436. return false;
  437. strip = { 0, rowY (row), getWidth(), rowHeight / 2 };
  438. return true;
  439. }
  440. void ChannelRackComponent::paintZipStrips (juce::Graphics& g)
  441. {
  442. const auto& song = engine.getSong();
  443. const auto clip = g.getClipBounds();
  444. for (int row = 0; row < (int) song.channels.size(); ++row)
  445. {
  446. if (! isZipped (row) || ! isShown (row))
  447. continue;
  448. const auto bar = zipBar (row);
  449. if (! bar.intersects (clip.expanded (0, 1)))
  450. continue;
  451. const auto& c = song.channels[(std::size_t) row];
  452. // FL: the bar in the channel's colour, darker at the bottom (its channel buttons' colour when none is set)
  453. const auto top = c.colour != 0 ? c.colour : (std::uint32_t) theme::colour::bg5.getARGB();
  454. const auto r = bar.toFloat();
  455. g.setGradientFill (juce::ColourGradient (juce::Colour (top), r.getX(), r.getY(), juce::Colour (rackzip::barBottom (top)), r.getX(), r.getBottom(), false));
  456. g.fillRect (r);
  457. if (hoverArea == Area::zipped && hoverRow == row)
  458. {
  459. g.setColour (theme::colour::textPrimary.withAlpha (0.6f));
  460. g.drawRect (r.expanded (1.0f), 1.0f);
  461. }
  462. }
  463. }
  464. void ChannelRackComponent::zipSelected()
  465. {
  466. const auto rows = engine.getSelectedChannels();
  467. if (rows.empty())
  468. return;
  469. engine.editSong ([rows] (Song& s) {
  470. for (const auto r : rows)
  471. if (r >= 0 && r < (int) s.channels.size())
  472. s.channels[(std::size_t) r].zipped = true;
  473. });
  474. updateSize();
  475. repaint();
  476. }
  477. void ChannelRackComponent::unzipAll()
  478. {
  479. const auto& channels = engine.getSong().channels;
  480. if (std::none_of (channels.begin(), channels.end(), [] (const ChannelDef& c) { return c.zipped; }))
  481. return;
  482. engine.editSong ([] (Song& s) {
  483. for (auto& c : s.channels)
  484. c.zipped = false;
  485. });
  486. updateSize();
  487. repaint();
  488. }
  489. void ChannelRackComponent::unzip (int row)
  490. {
  491. if (row < 0 || row >= (int) engine.getSong().channels.size() || ! engine.getSong().channels[(std::size_t) row].zipped)
  492. return;
  493. engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].zipped = false; });
  494. updateSize();
  495. repaint();
  496. }
  497. int ChannelRackComponent::graphRow() const
  498. {
  499. const auto count = (int) engine.getSong().channels.size();
  500. const auto row = graphLanes && count > 0 ? juce::jlimit (0, count - 1, engine.getSelectedChannel()) : -1;
  501. return row >= 0 && isShown (row) && ! isZipped (row) ? row : -1;
  502. }
  503. int ChannelRackComponent::rowY (int row) const
  504. {
  505. const auto lane = graphRow();
  506. const auto offset = lane >= 0 && row > lane ? laneHeight : 0;
  507. if (const auto count = (int) zipLayout.top.size() - 1; count >= 0 && row >= 0) // (round 190mk: zipped strips)
  508. return (row <= count ? zipLayout.top[(std::size_t) row] : zipLayout.top[(std::size_t) count] + (row - count) * rowHeight) + offset;
  509. return 4 + visibleIndex (row) * rowHeight + offset;
  510. }
  511. int ChannelRackComponent::rowAtY (int y) const
  512. {
  513. if (y < 4)
  514. return -1;
  515. if (const auto lane = graphRow(); lane >= 0)
  516. {
  517. const auto laneTop = rowY (lane) + rowHeight;
  518. if (y >= laneTop && y < laneTop + laneHeight)
  519. return lane;
  520. if (y >= laneTop + laneHeight)
  521. y -= laneHeight;
  522. }
  523. if (! zipLayout.top.empty()) // (round 190mk: zipped strips; a strip answers with its first row)
  524. return rackzip::rowAt (zipLayout, y, rowHeight);
  525. const auto index = (y - 4) / rowHeight;
  526. if (filterGroup == rackfilter::all)
  527. return index;
  528. // the index-th shown row (the "add channel" row after the last one)
  529. const auto count = (int) engine.getSong().channels.size();
  530. int seen = 0;
  531. for (int r = 0; r < count; ++r)
  532. if (isShown (r) && seen++ == index)
  533. return r;
  534. return count + (index - seen);
  535. }
  536. juce::Rectangle<int> ChannelRackComponent::laneBounds() const
  537. {
  538. const auto lane = graphRow();
  539. return lane < 0 ? juce::Rectangle<int>() : juce::Rectangle<int> (0, rowY (lane) + rowHeight, getWidth(), laneHeight);
  540. }
  541. juce::Rectangle<int> ChannelRackComponent::rowBounds (int row) const
  542. {
  543. return { 0, rowY (row), getWidth(), rowHeight };
  544. }
  545. juce::Rectangle<int> ChannelRackComponent::stepBounds (int row, int step) const
  546. {
  547. // FL's step keycaps (round 190io, FM-11): 15 x 20 at an 18 px pitch (FL: 13 x 20 at 16; a bit wider for the mouse)
  548. return { gridLeft() + step * stepWidth + 1, rowY (row) + 5, stepWidth - 3, rowHeight - 10 };
  549. }
  550. // --- channel looping (FL 20.5+, round 165f-rack) ------------------------------------------------------
  551. int ChannelRackComponent::loopStepsOf (int row) const
  552. {
  553. const auto& song = engine.getSong();
  554. if (song.patterns.empty() || row < 0 || row >= (int) song.channels.size())
  555. return 0;
  556. const auto& p = song.activePattern();
  557. if (! p.loopOn)
  558. return 0;
  559. const auto length = p.effectiveLength (song.meter());
  560. const auto loop = channelloops::loopLength (p, row, channelloops::effectiveSetting (p, row), length, song.meter());
  561. return loop > 0 && loop < length ? (int) (loop / ticksPerStep) : 0;
  562. }
  563. bool ChannelRackComponent::advancedLoopShown() const
  564. {
  565. const auto& song = engine.getSong();
  566. if (song.patterns.empty())
  567. return false;
  568. const auto& p = song.activePattern();
  569. return p.loopOn && p.loopMode == RackLoopMode::advanced;
  570. }
  571. juce::Rectangle<int> ChannelRackComponent::loopBoxBounds (int row) const
  572. {
  573. // the right edge of what the viewport shows (the column stays there while the steps scroll, like the controls)
  574. auto right = getWidth();
  575. if (const auto* holder = getParentComponent())
  576. right = juce::jmin (getWidth(), stickyX + holder->getWidth());
  577. return { right - loopColumnWidth + 3, rowY (row) + 5, loopColumnWidth - 8, rowHeight - 10 };
  578. }
  579. void ChannelRackComponent::paintLoopColumn (juce::Graphics& g)
  580. {
  581. using namespace theme::colour;
  582. const auto& song = engine.getSong();
  583. const auto& p = song.activePattern();
  584. const auto first = loopBoxBounds (0);
  585. g.setColour (bg2);
  586. g.fillRect (first.getX() - 3, 0, loopColumnWidth, getHeight());
  587. g.setColour (border);
  588. g.fillRect (first.getX() - 4, 0, 1, getHeight());
  589. g.setFont (theme::font (theme::type::caption, true));
  590. for (int row = 0; row < (int) song.channels.size(); ++row)
  591. {
  592. if (! isShown (row) || isZipped (row) || song.channels[(std::size_t) row].instrument == InstrumentKind::audioClip)
  593. continue;
  594. const auto box = loopBoxBounds (row);
  595. const auto setting = channelloops::effectiveSetting (p, row);
  596. const auto hovered = (hoverArea == Area::loop && hoverRow == row) || loopDragRow == row;
  597. theme::fillRoundRect (g, box.toFloat(), theme::radius::sm, hovered ? bg4 : bg0);
  598. if (setting != ChannelLoop::dontLoop)
  599. {
  600. g.setColour (accent.withAlpha (hovered ? 0.9f : 0.6f));
  601. g.drawRoundedRectangle (box.toFloat().reduced (0.5f), theme::radius::sm, 1.0f);
  602. }
  603. g.setColour (setting == ChannelLoop::dontLoop ? textDisabled : textPrimary);
  604. const auto text = setting > 0 ? juce::String (setting) : setting == ChannelLoop::dontLoop ? juce::String ("...")
  605. : tr (channelloops::settingText (setting).c_str());
  606. textcache::drawText (g, text, box, juce::Justification::centred, true);
  607. }
  608. g.setFont (theme::font (theme::type::body));
  609. }
  610. void ChannelRackComponent::setLoopPoint (int row, int setting)
  611. {
  612. if (row < 0 || row >= (int) engine.getSong().channels.size())
  613. return;
  614. engine.editSong ([row, setting] (Song& s) {
  615. auto& p = s.activePattern();
  616. auto value = setting;
  617. // FL's "Set to next beat / bar": a fixed loop point, the steps to the boundary after the channel's last note
  618. if (setting == 1000 + ChannelLoop::nextBeat || setting == 1000 + ChannelLoop::nextBar)
  619. value = channelloops::nextBoundarySteps (p, row, setting - 1000, s.meter());
  620. channelloops::setSetting (p, row, value);
  621. // (FL switches the looping on, advanced, when a loop point is set from a channel)
  622. if (! p.loopOn && value != ChannelLoop::dontLoop)
  623. {
  624. p.loopOn = true;
  625. p.loopMode = RackLoopMode::advanced;
  626. }
  627. });
  628. updateSize();
  629. repaint();
  630. }
  631. void ChannelRackComponent::showLoopPointMenu (int row)
  632. {
  633. const auto& song = engine.getSong();
  634. if (row < 0 || row >= (int) song.channels.size())
  635. return;
  636. const auto& p = song.activePattern();
  637. const auto setting = channelloops::effectiveSetting (p, row);
  638. juce::PopupMenu menu;
  639. ui::addItemHeader (menu, tr ("Loop point"), channelName (song, row), {});
  640. menu.addItem (tr ("Don't loop"), true, setting == ChannelLoop::dontLoop, [this, row] { setLoopPoint (row, ChannelLoop::dontLoop); });
  641. menu.addSeparator();
  642. menu.addItem (tr ("Step") + " - " + tr ("loop at the next step"), true, setting == ChannelLoop::nextStep, [this, row] { setLoopPoint (row, ChannelLoop::nextStep); });
  643. menu.addItem (tr ("Beat") + " - " + tr ("loop at the next beat"), true, setting == ChannelLoop::nextBeat, [this, row] { setLoopPoint (row, ChannelLoop::nextBeat); });
  644. menu.addItem (tr ("Bar") + " - " + tr ("loop at the next bar"), true, setting == ChannelLoop::nextBar, [this, row] { setLoopPoint (row, ChannelLoop::nextBar); });
  645. menu.addSeparator();
  646. menu.addItem (tr ("Set to next beat"), [this, row] { setLoopPoint (row, 1000 + ChannelLoop::nextBeat); });
  647. menu.addItem (tr ("Set to next bar"), [this, row] { setLoopPoint (row, 1000 + ChannelLoop::nextBar); });
  648. if (setting > 0)
  649. menu.addItem (tr ("Every {0} steps", { juce::String (setting) }), false, true, [] {});
  650. menu.addSectionHeader (tr ("Tools"));
  651. menu.addItem (tr ("Burn to pattern"), loopStepsOf (row) > 0, false, [this, row] {
  652. engine.editSong ([row] (Song& s) { channelloops::burnChannel (s.activePattern(), row, s.meter()); });
  653. repaint();
  654. });
  655. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withTargetScreenArea (localAreaToGlobal (loopBoxBounds (row))));
  656. }
  657. bool ChannelRackComponent::isLoopingOn() const
  658. {
  659. const auto& song = engine.getSong();
  660. return ! song.patterns.empty() && song.activePattern().loopOn;
  661. }
  662. void ChannelRackComponent::toggleLooping()
  663. {
  664. engine.editSong ([] (Song& s) {
  665. auto& p = s.activePattern();
  666. p.loopOn = ! p.loopOn;
  667. });
  668. updateSize();
  669. repaint();
  670. if (onFilterChanged)
  671. onFilterChanged(); // (the title bar's Loop button)
  672. }
  673. void ChannelRackComponent::burnLoops (bool selectedOnly)
  674. {
  675. const auto rows = engine.getSelectedChannels();
  676. engine.editSong ([rows, selectedOnly] (Song& s) {
  677. auto& p = s.activePattern();
  678. if (selectedOnly)
  679. {
  680. for (auto r : rows)
  681. channelloops::burnChannel (p, r, s.meter());
  682. }
  683. else
  684. channelloops::burnChannels (p, {}, s.meter());
  685. });
  686. repaint();
  687. }
  688. void ChannelRackComponent::showLoopMenu (juce::Component* target)
  689. {
  690. const auto& song = engine.getSong();
  691. if (song.patterns.empty())
  692. return;
  693. const auto& p = song.activePattern();
  694. juce::PopupMenu menu;
  695. menu.addItem (tr ("Looping on"), true, p.loopOn, [this] { toggleLooping(); });
  696. menu.addSectionHeader (tr ("Loop type"));
  697. const std::pair<RackLoopMode, const char*> types[] = { { RackLoopMode::steps, TR_NOOP ("Loop step channels") },
  698. { RackLoopMode::all, TR_NOOP ("Loop all channels") },
  699. { RackLoopMode::advanced, TR_NOOP ("Advanced looping") } };
  700. for (const auto& [mode, name] : types)
  701. menu.addItem (tr (name), true, p.loopMode == mode, [this, mode = mode] {
  702. engine.editSong ([mode] (Song& s) {
  703. auto& q = s.activePattern();
  704. q.loopMode = mode;
  705. q.loopOn = true; // (FL: picking a type switches the looping on)
  706. });
  707. updateSize();
  708. repaint();
  709. if (onFilterChanged)
  710. onFilterChanged();
  711. });
  712. menu.addSectionHeader (tr ("Burn to pattern"));
  713. const auto looping = ! channelloops::loopingChannels (p, song.meter()).empty();
  714. menu.addItem (tr ("Burn selected channels"), looping, false, [this] { burnLoops (true); });
  715. menu.addItem (tr ("Burn all looping channels"), looping, false, [this] { burnLoops (false); });
  716. auto options = juce::PopupMenu::Options();
  717. if (target != nullptr)
  718. options = options.withTargetComponent (target);
  719. menu.showMenuAsync (options);
  720. }
  721. void ChannelRackComponent::burnMidi (int channel, bool toNewPattern)
  722. {
  723. const auto& song = engine.getSong();
  724. if (song.patterns.empty() || channel < 0 || channel >= (int) song.channels.size())
  725. return;
  726. if (channelburn::playedNotes (song, song.currentPattern, channel).empty())
  727. {
  728. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Nothing to burn"),
  729. tr ("The selected channel doesn't contain any notes to burn."));
  730. return;
  731. }
  732. const auto baked = channelburn::whatBurns (song, song.currentPattern, channel);
  733. auto run = [this, channel, toNewPattern] (channelburn::Reset reset) {
  734. engine.editSong ([channel, toNewPattern, reset] (Song& s) { channelburn::burnMidi (s, channel, toNewPattern, reset); });
  735. updateSize();
  736. repaint();
  737. };
  738. if (! (baked.arpeggiator || baked.echo || baked.swing || baked.time))
  739. {
  740. run ({});
  741. return;
  742. }
  743. // FL's "Reset after burning to MIDI": what the burn wrote into the notes is switched off, or it would play twice
  744. struct ResetDialog final : juce::AlertWindow
  745. {
  746. explicit ResetDialog (const channelburn::Baked& baked)
  747. : juce::AlertWindow (tr ("Reset after burning to MIDI"), tr ("Select what you want to reset:"), juce::MessageBoxIconType::QuestionIcon)
  748. {
  749. const std::pair<const char*, bool> items[] = { { TR_NOOP ("Arpeggiator"), baked.arpeggiator }, { TR_NOOP ("Echo delay"), baked.echo },
  750. { TR_NOOP ("Swing mix"), baked.swing }, { TR_NOOP ("Time (shift and gate)"), baked.time } };
  751. for (std::size_t i = 0; i < toggles.size(); ++i)
  752. {
  753. toggles[i].setButtonText (tr (items[i].first));
  754. toggles[i].setToggleState (items[i].second, juce::dontSendNotification);
  755. toggles[i].setEnabled (items[i].second);
  756. toggles[i].setSize (300, 24);
  757. addCustomComponent (&toggles[i]);
  758. }
  759. addButton (tr ("Burn"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  760. addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  761. }
  762. channelburn::Reset reset() const
  763. {
  764. return { toggles[0].getToggleState(), toggles[1].getToggleState(), toggles[2].getToggleState(), toggles[3].getToggleState() };
  765. }
  766. std::array<juce::ToggleButton, 4> toggles;
  767. };
  768. auto* w = new ResetDialog (baked);
  769. w->enterModalState (true, juce::ModalCallbackFunction::create ([w, run] (int result) {
  770. if (result == 1)
  771. run (w->reset());
  772. }), true);
  773. }
  774. ChannelRackComponent::Area ChannelRackComponent::areaAt (int x, int y, int& row) const
  775. {
  776. const auto channels = (int) engine.getSong().channels.size();
  777. row = rowAtY (y);
  778. const auto rackX = x;
  779. if (x < stickyX + gridLeft()) // the controls column (painted at the visible left edge)
  780. x -= stickyX;
  781. if (row < 0 || row > channels)
  782. return Area::none;
  783. if (isZipped (row)) // round 190mk (DE-38): a zipped strip; on a bar, that bar's channel
  784. {
  785. if (const auto bar = zipBarAt (row, rackX); bar >= 0)
  786. {
  787. row = bar;
  788. return Area::zipped;
  789. }
  790. return Area::none;
  791. }
  792. if (laneBounds().contains (x, y))
  793. return x < gridLeft() ? Area::laneLevels : Area::lane;
  794. if (row == channels) // only the "+ add channel" box opens the menu (issue #116: as FL's + button)
  795. return racklayout::addChannelBox (nameLeft(), nameWidth, rowY (row), rowHeight).contains (x, y) ? Area::addRow : Area::none;
  796. if (advancedLoopShown() && x >= loopBoxBounds (row).getX() - 3) // the column covers the steps under it
  797. return loopBoxBounds (row).expanded (2, 3).contains (x, y) ? Area::loop : Area::none;
  798. if (x < muteWidth)
  799. return Area::mute;
  800. if (x < muteWidth + knobWidth)
  801. return Area::pan;
  802. if (x < muteWidth + 2 * knobWidth)
  803. return Area::volume;
  804. if (x < nameLeft())
  805. return Area::route;
  806. if (x < selectLeft())
  807. return Area::name;
  808. if (x < gridLeft())
  809. return Area::select;
  810. return Area::grid;
  811. }
  812. bool ChannelRackComponent::cellHasNote (int channel, int step) const
  813. {
  814. const auto& song = engine.getSong();
  815. const auto& p = song.activePattern();
  816. const auto from = (std::int64_t) step * ticksPerStep;
  817. const auto to = from + ticksPerStep;
  818. for (const auto& n : p.notes) // (a muted note leaves its step looking off, as in FL: round 169f, FL session)
  819. if (n.channel == channel && n.start >= from && n.start < to && ! n.muted)
  820. return true;
  821. return false;
  822. }
  823. bool ChannelRackComponent::channelHitsNow (int row) const
  824. {
  825. if (playheadStep < 0 || playheadStep >= numSteps())
  826. return false;
  827. const auto& song = engine.getSong();
  828. const auto& p = song.activePattern();
  829. const auto loopSteps = loopStepsOf (row);
  830. const auto step = loopSteps > 0 && playheadStep >= loopSteps ? playheadStep % loopSteps : playheadStep;
  831. return p.hasStep (row, step) || cellHasNote (row, step);
  832. }
  833. // Eight idle step keycaps (two groups of four, `a` then `b`) on the row's own background `bg`, from one opaque cached
  834. // tile (round 190io): an opaque tile is copied, not blended, by the software renderer, so a row of idle steps costs about
  835. // what the old flat tiles did although twice as many rows show steps now (the audio clip channels, FM-12). Drawn straight
  836. // where the scale is not whole.
  837. static void drawIdleStepStrip (juce::Graphics& g, juce::Rectangle<int> firstCell, int pitch, juce::Colour a, juce::Colour b, juce::Colour bg)
  838. {
  839. const auto bounds = firstCell.withWidth (7 * pitch + firstCell.getWidth());
  840. if (! g.clipRegionIntersects (bounds))
  841. return;
  842. const auto paintInto = [&] (juce::Graphics& k, bool withBackground) {
  843. if (withBackground)
  844. {
  845. k.setColour (bg);
  846. k.fillRect (juce::Rectangle<int> (bounds.getWidth(), bounds.getHeight()));
  847. }
  848. for (int i = 0; i < 8; ++i)
  849. theme::drawKeycap (k, juce::Rectangle<int> (i * pitch, 0, firstCell.getWidth(), firstCell.getHeight()), i < 4 ? a : b, false);
  850. };
  851. const auto s = g.getInternalContext().getPhysicalPixelScaleFactor();
  852. const auto scale = (int) std::round (s);
  853. if (! ui::paintCache() || scale < 1 || scale > 3 || std::abs (s - (float) scale) > 1.0e-4f || ! bg.isOpaque())
  854. {
  855. juce::Graphics::ScopedSaveState state (g);
  856. g.setOrigin (bounds.getPosition());
  857. paintInto (g, false);
  858. return;
  859. }
  860. static std::unordered_map<std::uint64_t, juce::Image> cache;
  861. static const bool registered = ui::registerImageCache ([] { cache.clear(); }); (void) registered;
  862. std::uint64_t key = 0xcbf29ce484222325ull;
  863. for (const std::uint64_t v : { (std::uint64_t) a.getARGB(), (std::uint64_t) b.getARGB(), (std::uint64_t) bg.getARGB(), (std::uint64_t) pitch,
  864. (std::uint64_t) firstCell.getWidth(), (std::uint64_t) firstCell.getHeight(), (std::uint64_t) scale,
  865. (std::uint64_t) theme::colour::bg0.getARGB(), (std::uint64_t) theme::colour::borderStrong.getARGB() })
  866. key = (key ^ v) * 0x100000001b3ull;
  867. auto it = cache.find (key);
  868. if (it == cache.end())
  869. {
  870. if (cache.size() > 200)
  871. cache.clear();
  872. juce::Image image (juce::Image::RGB, bounds.getWidth() * scale, bounds.getHeight() * scale, false,
  873. #if JUCE_WINDOWS
  874. juce::NativeImageType()
  875. #else
  876. juce::SoftwareImageType()
  877. #endif
  878. );
  879. {
  880. juce::Graphics ig (image);
  881. ig.addTransform (juce::AffineTransform::scale ((float) scale));
  882. paintInto (ig, true);
  883. }
  884. it = cache.emplace (key, std::move (image)).first;
  885. }
  886. g.setColour (juce::Colours::black); // (an image is drawn with the current colour's alpha as its opacity)
  887. if (scale == 1)
  888. g.drawImageAt (it->second, bounds.getX(), bounds.getY());
  889. else
  890. g.drawImage (it->second, bounds.toFloat(), juce::RectanglePlacement::stretchToFit);
  891. }
  892. void ChannelRackComponent::paintStepHit (juce::Graphics& g, juce::Rectangle<float> r)
  893. {
  894. theme::fillRoundRect (g, r, keycapRadius, juce::Colours::white.withAlpha (0.55f));
  895. g.setColour (theme::colour::playhead);
  896. g.drawRoundedRectangle (r.reduced (0.75f), keycapRadius, 1.5f);
  897. }
  898. // FL's raised step keycap (round 190io, design190ia FM-11; FL: a light top edge, a dark bottom edge and the dark LED slot
  899. // a quarter down): the body in the step's colour, a 1 px highlight on top, a 1 px shade at the bottom and the 1 px slot
  900. // line, which turns light on a lit step. Plain rectangles, so a playhead frame costs what the flat tiles did.
  901. void ChannelRackComponent::paintKeycap (juce::Graphics& g, juce::Rectangle<int> r, juce::Colour fill, bool lit)
  902. {
  903. theme::drawKeycap (g, r, fill, lit);
  904. }
  905. void ChannelRackComponent::paint (juce::Graphics& g)
  906. {
  907. const uitrace::Scope trace ("ChannelRackComponent paint", true, g.getClipBounds());
  908. using namespace theme::colour;
  909. g.fillAll (bg2);
  910. const auto& song = engine.getSong();
  911. if (song.patterns.empty())
  912. return;
  913. const auto& pattern = song.activePattern();
  914. const auto steps = shownSteps(), realSteps = numSteps();
  915. const auto clip = g.getClipBounds();
  916. const auto primary = engine.getSelectedChannel();
  917. const auto& selection = engine.getSelectedChannels();
  918. const auto palette = theme::currentPalette();
  919. const auto column = playheadColumn.load (std::memory_order_relaxed) && playheadStep >= 0 && playheadStep < steps;
  920. g.setFont (theme::font (theme::type::body));
  921. // round 190gr: which (channel, step) cells hold a Piano Roll note, from one pass over the pattern's notes (cellHasNote
  922. // walks every note of the pattern for each cell: rows x steps x notes per frame)
  923. const auto channelCount = (int) song.channels.size();
  924. const auto noteCells = ui::paintCache();
  925. if (noteCells)
  926. {
  927. noteCellBits.assign ((std::size_t) channelCount * (std::size_t) juce::jmax (0, steps), 0);
  928. for (const auto& n : pattern.notes)
  929. if (! n.muted && n.channel >= 0 && n.channel < channelCount && n.start >= 0)
  930. if (const auto step = n.start / ticksPerStep; step < steps)
  931. noteCellBits[(std::size_t) n.channel * (std::size_t) steps + (std::size_t) step] = 1;
  932. }
  933. const auto hasNote = [&] (int channel, int step) {
  934. if (! noteCells)
  935. return cellHasNote (channel, step);
  936. return step >= 0 && step < steps && noteCellBits[(std::size_t) channel * (std::size_t) steps + (std::size_t) step] != 0;
  937. };
  938. for (int row = 0; row < (int) song.channels.size(); ++row)
  939. {
  940. if (isZipped (row)) // (round 190mk: drawn as a bar in its strip, paintZipStrips)
  941. continue;
  942. const auto y = rowY (row);
  943. if (! isShown (row) || y > clip.getBottom() || y + rowHeight < clip.getY())
  944. continue;
  945. const auto& channel = song.channels[(std::size_t) row];
  946. const auto selected = std::binary_search (selection.begin(), selection.end(), row);
  947. const auto litOn = stepsChannelColour.load (std::memory_order_relaxed) && channel.colour != 0
  948. ? juce::Colour (themes::litStepColour (channel.colour, palette))
  949. : theme::colour::stepOn;
  950. if (selected) // selected rows are subtly lifted
  951. {
  952. g.setColour (bg3);
  953. g.fillRect (0, y + 1, getWidth(), rowHeight - 2);
  954. }
  955. if (column) // LYNX: the playing column, soft, under the step buttons (their colours and groups stay readable)
  956. {
  957. g.setColour (playhead.withAlpha (0.2f));
  958. g.fillRect (gridLeft() + playheadStep * stepWidth - 1, y, stepWidth, rowHeight);
  959. }
  960. // (round 190io, FM-12: an audio clip channel shows its steps as every channel does; FL's manual: "If desired,
  961. // Audio Clips can be triggered from the Step Sequencer or the Piano roll")
  962. const auto loopSteps = loopStepsOf (row); // the row loops: the repeats after its loop point are ghost steps
  963. const auto stepOn = litOn; // (the channel's lit-step colour, or the theme's)
  964. if (showsNotePreview (pattern, row, pianoRollView)) // piano-roll notes: a miniature instead of the step buttons
  965. {
  966. paintNotePreview (g, row, pattern, channel.colour);
  967. const auto span = previewSpan();
  968. const auto stepX = [&] (int step) { return gridLeft() + span.x + (int) ((std::int64_t) span.width * step / realSteps); };
  969. if (loopSteps > 0)
  970. {
  971. g.setColour (accent.withAlpha (0.85f));
  972. g.fillRect (stepX (loopSteps) - 2, y + 3, 2, rowHeight - 6);
  973. }
  974. if (span.fitted && playheadStep >= 0 && playheadStep < realSteps) // (the playing column is elsewhere: a line here)
  975. {
  976. g.setColour (playhead.withAlpha (0.8f));
  977. g.fillRect (stepX (playheadStep), y + 4, juce::jmax (2, span.width / realSteps), rowHeight - 8);
  978. }
  979. continue;
  980. }
  981. // round 190io: the idle keycaps (and the faint steps past the pattern's end) come eight at a time from one tile;
  982. // the steps that differ (lit, hovered, playing, loop repeats) are drawn over them one by one below
  983. const auto idleLimit = loopSteps > 0 ? juce::jmin (loopSteps, realSteps) : realSteps;
  984. const auto stripKind = [&] (int step) { // 1: a keycap strip covers it, 2: a ghost strip, 0: drawn alone
  985. const auto start = step / 8 * 8;
  986. return start + 8 <= idleLimit ? 1 : start >= realSteps && start + 8 <= steps ? 2 : 0;
  987. };
  988. // (the strip under the playing column is drawn cell by cell: the column's soft tint shows between its steps)
  989. const auto columnStrip = column ? playheadStep / 8 * 8 : -1;
  990. for (int start = 0; start + 8 <= steps; start += 8)
  991. if (const auto kind = stripKind (start); kind != 0 && start != columnStrip)
  992. {
  993. const auto first = stepBounds (row, start);
  994. if (first.getX() + 8 * stepWidth < clip.getX() || first.getX() > clip.getRight())
  995. continue;
  996. if (kind == 1)
  997. drawIdleStepStrip (g, first, stepWidth, theme::stepOff (start), theme::stepOff (start + 4), selected ? bg3 : bg2);
  998. else
  999. theme::drawStepStripOver (g, first, stepWidth, theme::stepOff (start), theme::stepOff (start + 4), true, selected ? bg3 : bg2);
  1000. }
  1001. for (int step = 0; step < steps; ++step)
  1002. {
  1003. const auto r = stepBounds (row, step);
  1004. if (r.getRight() < clip.getX() || r.getX() > clip.getRight())
  1005. continue;
  1006. auto onGrid = pattern.hasStep (row, step);
  1007. auto any = onGrid || hasNote (row, step);
  1008. const auto hoverStepHere = hoverArea == Area::grid && hoverRow == row && hoverStep == step && paintMode == PaintMode::none;
  1009. if (! any && ! hoverStepHere && stripKind (step) != 0 && step / 8 * 8 != columnStrip) // (drawn by its strip)
  1010. continue;
  1011. if (loopSteps > 0 && step >= loopSteps && step < realSteps)
  1012. {
  1013. // FL's loop ghosts: what the loop repeats here, faint; a step of its own past the loop point doesn't play
  1014. const auto source = step % loopSteps;
  1015. const auto repeated = pattern.hasStep (row, source) || hasNote (row, source);
  1016. auto fill = repeated ? stepOn : theme::stepOff (step);
  1017. theme::fillRoundRect (g, r.toFloat(), keycapRadius, fill.withMultipliedAlpha (hoverStepHere ? 0.75f : repeated ? 0.45f : 0.3f));
  1018. if (repeated && step == playheadStep) // the repeat plays: lit as a hit (below)
  1019. paintStepHit (g, r.toFloat());
  1020. if (any)
  1021. {
  1022. g.setColour (textDisabled);
  1023. g.drawRoundedRectangle (r.toFloat().reduced (1.5f), keycapRadius, 1.0f);
  1024. }
  1025. if (hoverStepHere)
  1026. {
  1027. g.setColour (textSecondary.withAlpha (0.7f));
  1028. g.drawRoundedRectangle (r.toFloat().reduced (0.5f), keycapRadius, 1.0f);
  1029. }
  1030. continue;
  1031. }
  1032. const auto ghost = step >= realSteps && ! any; // after the pattern's end: faint until clicked
  1033. auto fill = any ? (onGrid ? stepOn : stepNote) : theme::stepOff (step);
  1034. if (ghost) // a faint outlined cell, filled while hovered
  1035. {
  1036. theme::fillRoundRect (g, r.toFloat(), keycapRadius, fill.withMultipliedAlpha (hoverStepHere ? 0.9f : 0.35f));
  1037. g.setColour (fill.brighter (0.25f).withMultipliedAlpha (0.55f));
  1038. g.drawRoundedRectangle (r.toFloat().reduced (0.5f), keycapRadius, 1.0f);
  1039. }
  1040. else
  1041. paintKeycap (g, r, hoverStepHere ? fill.brighter (any ? 0.15f : 0.35f) : fill, any);
  1042. // FL lights the step as the playhead hits it (its LED turns red, round 170a [SHOT]): a step that plays now glows
  1043. if (any && step == playheadStep && step < realSteps)
  1044. paintStepHit (g, r.toFloat());
  1045. if (hoverStepHere)
  1046. {
  1047. g.setColour (textSecondary.withAlpha (0.7f));
  1048. g.drawRoundedRectangle (r.toFloat().reduced (0.5f), keycapRadius, 1.0f);
  1049. }
  1050. }
  1051. if (loopSteps > 0) // the loop point
  1052. {
  1053. g.setColour (accent.withAlpha (0.85f));
  1054. g.fillRect (gridLeft() + loopSteps * stepWidth - 2, y + 3, 2, rowHeight - 6);
  1055. }
  1056. }
  1057. paintLane (g);
  1058. const auto addY = rowY ((int) song.channels.size());
  1059. g.setColour (theme::colour::textSecondary); // (round 190oo PAT-14: an informative hint, 7:1; was textDisabled 2.9:1)
  1060. g.setFont (theme::font (theme::type::small));
  1061. textcache::drawText (g, tr ("or drop an audio file here"), gridLeft() + 4, addY + positionStripHeight, 400, rowHeight - positionStripHeight,
  1062. juce::Justification::centredLeft);
  1063. const auto stepsPerBar = (int) std::max<std::int64_t> (1, song.barTicks() / ticksPerStep);
  1064. // FL's "Playing step" strip (PosLEDPanel, REA round 170a §2): a slim LED row under the steps, beat groups
  1065. // alternating, the playing step lit — in Pattern mode and whenever the shown pattern plays in Song mode
  1066. for (int step = 0; step < realSteps; ++step)
  1067. {
  1068. const auto cell = juce::Rectangle<float> ((float) (gridLeft() + step * stepWidth) + 1.0f, (float) addY + 2.0f, (float) stepWidth - 4.0f,
  1069. (float) positionStripHeight - 3.0f);
  1070. if (cell.getRight() < (float) clip.getX() || cell.getX() > (float) clip.getRight())
  1071. continue;
  1072. theme::fillRoundRect (g, cell, 1.5f, step == playheadStep ? playhead : theme::stepOff (step).brighter (0.12f));
  1073. // LYNX: each bar's first step marked (finding the bars on 32 / 64-step patterns)
  1074. if (playheadColumn.load (std::memory_order_relaxed) && step % stepsPerBar == 0 && step != playheadStep)
  1075. {
  1076. g.setColour (accent.withAlpha (0.7f));
  1077. g.fillRect (cell.withHeight (2.0f));
  1078. }
  1079. }
  1080. // the controls column stays put while the steps scroll sideways (FL's rack keeps the channel buttons in
  1081. // view): the viewport moves the whole rack, so the column is painted again at the visible left edge
  1082. {
  1083. juce::Graphics::ScopedSaveState state (g);
  1084. g.reduceClipRegion (stickyX, 0, gridLeft(), getHeight());
  1085. g.setOrigin (stickyX, 0);
  1086. g.setColour (bg2);
  1087. g.fillRect (0, 0, gridLeft(), getHeight());
  1088. const auto column = g.getClipBounds();
  1089. for (int row = 0; row < (int) song.channels.size(); ++row)
  1090. {
  1091. if (isZipped (row)) // (round 190mk)
  1092. continue;
  1093. const auto y = rowY (row);
  1094. if (! isShown (row) || y > column.getBottom() || y + rowHeight < column.getY())
  1095. continue;
  1096. const auto& channel = song.channels[(std::size_t) row];
  1097. const auto selected = std::binary_search (selection.begin(), selection.end(), row);
  1098. const auto nameArea = juce::Rectangle<float> ((float) nameLeft(), (float) y + 3, (float) nameWidth, (float) rowHeight - 6);
  1099. if (selected) // selected rows are subtly lifted
  1100. {
  1101. g.setColour (bg3);
  1102. g.fillRect (0, y + 1, getWidth(), rowHeight - 2);
  1103. }
  1104. const auto hovered = [this, row] (Area a) { return hoverRow == row && hoverArea == a; };
  1105. // round 190ge: a playhead step repaints the selector column only (the selectors flash with the hits): the mute
  1106. // LED, knobs, route box and name, all left of it, are drawn only when the repainted area reaches them
  1107. if (column.getX() < selectLeft() || ! ui::paintSavings())
  1108. {
  1109. const auto led = juce::Rectangle<float> (9.0f, (float) y + rowHeight / 2.0f - 4.5f, 9.0f, 9.0f);
  1110. g.setColour (theme::muteLed (channel.muted));
  1111. g.fillEllipse (led);
  1112. if (hovered (Area::mute)) // hover ring
  1113. {
  1114. g.setColour (textSecondary.withAlpha (0.8f));
  1115. g.drawEllipse (led.expanded (2.5f), 1.0f);
  1116. }
  1117. const auto touched = knobDrag != Area::none && knobRow == row;
  1118. // (while the song plays an automated knob shows its automation's value, issue #12)
  1119. const auto& automated = engine.getAutomationDisplay();
  1120. theme::drawKnob (g, controlBounds (Area::pan, row).toFloat().reduced (1.0f, 2.0f),
  1121. automated.shown ({ AutomationTargetKind::channelPan, row }, (channel.pan + 1.0f) / 2.0f), true,
  1122. touched && knobDrag == Area::pan ? accent : info, hovered (Area::pan) || (touched && knobDrag == Area::pan));
  1123. theme::drawKnob (g, controlBounds (Area::volume, row).toFloat().reduced (1.0f, 2.0f),
  1124. automated.shown ({ AutomationTargetKind::channelVolume, row }, channel.volume), false,
  1125. touched && knobDrag == Area::volume ? accent : knobArc, hovered (Area::volume) || (touched && knobDrag == Area::volume));
  1126. // small box with the mixer insert ("-" = straight to Master)
  1127. const auto routeArea = controlBounds (Area::route, row).toFloat();
  1128. g.setColour (theme::drawDigitDisplay (g, routeArea, hovered (Area::route) || (touched && knobDrag == Area::route),
  1129. channel.mixerTrack <= 0));
  1130. g.setFont (theme::font (theme::type::caption, true));
  1131. textcache::drawText (g, channel.mixerTrack > 0 ? juce::String (channel.mixerTrack) : juce::String ("-"), routeArea.toNearestInt(),
  1132. juce::Justification::centred);
  1133. g.setFont (theme::font (theme::type::body));
  1134. // name button: filled with the channel colour as FL's (round 190io, design190ia FM-10: FL's Synth Bass
  1135. // #11479C, Grv Kick #892019), its brightness held where white text reads (0.42-0.58), a slight top-down
  1136. // gradient (+6 % / -6 %); FL's grey #485156 for a channel without a colour; muted = greyed and darker
  1137. {
  1138. auto fill = theme::colourFill (channel.colour != 0 ? juce::Colour (channel.colour) : juce::Colour (0xff485156), 0.42f, 0.58f);
  1139. if (channel.muted)
  1140. fill = fill.withMultipliedSaturation (0.45f).withMultipliedBrightness (0.7f);
  1141. if (hovered (Area::name))
  1142. fill = fill.brighter (0.12f);
  1143. theme::fillPlate (g, nameArea, theme::radius::sm, fill.withMultipliedBrightness (1.06f), fill.withMultipliedBrightness (0.94f));
  1144. }
  1145. if (row == primary) // the focused channel: a light outline (the selector beside it shows the selection)
  1146. {
  1147. g.setColour (textPrimary);
  1148. g.drawRoundedRectangle (nameArea.reduced (0.75f), theme::radius::sm, 1.5f);
  1149. }
  1150. const auto locked = ! channel.receiveFrom.empty();
  1151. if (locked) // FL's lock icon: the channel receives the notes of one MIDI input ("Receive notes from")
  1152. {
  1153. const auto lockBox = juce::Rectangle<float> (nameArea.getRight() - 15.0f, nameArea.getCentreY() - 6.0f, 9.0f, 11.0f);
  1154. g.setColour (warning);
  1155. juce::Path shackle;
  1156. shackle.addCentredArc (lockBox.getCentreX(), lockBox.getY() + 4.0f, 2.8f, 3.2f, 0.0f, -juce::MathConstants<float>::halfPi,
  1157. juce::MathConstants<float>::halfPi, true);
  1158. g.strokePath (shackle, juce::PathStrokeType (1.4f));
  1159. g.fillRoundedRectangle (lockBox.withTrimmedTop (4.5f), 1.5f);
  1160. }
  1161. // Online Sessions (round 168a): the plugin is not installed here, its stand-in render plays (an amber tag)
  1162. auto badgeWidth = 0.0f;
  1163. if (engine.playsStandIn (row))
  1164. {
  1165. badgeWidth = 16.0f;
  1166. juce::Graphics::ScopedSaveState keepFont (g);
  1167. const auto tag = juce::Rectangle<float> (nameArea.getRight() - (locked ? 34.0f : 18.0f), nameArea.getCentreY() - 6.5f, 14.0f, 13.0f);
  1168. theme::fillRoundRect (g, tag, 3.0f, warning.withAlpha (0.85f));
  1169. g.setColour (bg1);
  1170. g.setFont (theme::font (10.0f, true));
  1171. g.drawText ("R", tag, juce::Justification::centred, false);
  1172. }
  1173. // round 190ap: opened without third-party plugins, this channel's plugin is still off (a grey power tag)
  1174. else if (engine.channelPluginOff (row))
  1175. {
  1176. badgeWidth = 16.0f;
  1177. juce::Graphics::ScopedSaveState keepFont (g);
  1178. ui::drawPluginOffTag (g, juce::Rectangle<float> (nameArea.getRight() - (locked ? 34.0f : 18.0f), nameArea.getCentreY() - 6.5f, 14.0f, 13.0f));
  1179. }
  1180. // Freeze (round 190ay): a frozen channel plays its rendered audio, its instrument is unloaded — a snowflake
  1181. // tag (amber when the audio is out of date or missing) and a dimmed name
  1182. const auto frozen = channel.freeze.on();
  1183. if (frozen)
  1184. {
  1185. const auto attention = engine.frozenAudioMissing (row) || engine.freezeOutOfDate (row);
  1186. const auto tag = juce::Rectangle<float> (nameArea.getRight() - (locked ? 34.0f : 18.0f) - badgeWidth, nameArea.getCentreY() - 6.5f, 14.0f, 13.0f);
  1187. badgeWidth += 16.0f;
  1188. theme::fillRoundRect (g, nameArea, theme::radius::md, bg1.withAlpha (0.35f)); // the plate, dimmed
  1189. theme::drawFrozenTag (g, tag, (attention ? warning : info).withAlpha (0.85f), bg1);
  1190. }
  1191. // the channel's icon (round 168h) at the button's right edge, where FL draws it (round 190io, FM-10: FL's
  1192. // audio clip glyph sits 6 px from the right), left of the lock / tags
  1193. const auto rightUsed = (locked ? 18.0f : 0.0f) + badgeWidth;
  1194. auto iconWidth = 0.0f;
  1195. if (const auto icon = trackicons::iconFor (channel.icon); icon != trackicons::Icon::none)
  1196. {
  1197. iconWidth = 18.0f;
  1198. ui::drawTrackIcon (g, icon, juce::Rectangle<float> (nameArea.getRight() - 6.0f - rightUsed - 14.0f, nameArea.getCentreY() - 7.0f, 14.0f, 14.0f),
  1199. onAccent.withAlpha (channel.muted ? 0.45f : 0.85f));
  1200. }
  1201. // white on the colour (FL), the name's middle dropped when it is too long ("Titey..m #2", FM-14), centred in
  1202. // the room left of the icon and tags as FL centres it (round 190jp: "Synth Bass" on the button's centre line,
  1203. // "Titey Tom" between the left edge and the icon)
  1204. g.setColour (onAccent.withAlpha (channel.muted ? 0.5f : frozen ? 0.7f : 0.92f));
  1205. g.setFont (theme::font (theme::type::body + 1.0f));
  1206. // FL's Channel rack menu › Options › Channel name alignment › Left (round 190kd): the name from the left edge,
  1207. // as LYNX drew it before 190jp
  1208. const auto left = nameAlignLeft;
  1209. const auto rightTrim = rightUsed + iconWidth > 0.0f ? rightUsed + iconWidth + 4.0f : left ? 5.0f : 6.0f;
  1210. const auto textArea = nameArea.withTrimmedLeft (left ? 7.0f : 6.0f).withTrimmedRight (rightTrim);
  1211. textcache::drawText (g, theme::fitMiddle (juce::String::fromUTF8 (channel.name.c_str()), g.getCurrentFont(), textArea.getWidth()), textArea,
  1212. left ? juce::Justification::centredLeft : juce::Justification::centred, false);
  1213. g.setFont (theme::font (theme::type::body));
  1214. }
  1215. // selector: FL's tall slot (round 190io, design190ia FM-13: 12 x 27 with a 2 px outline when selected), here
  1216. // 10 x 24 in the inset colour with a hairline, outlined 2 px in the accent while its channel is selected
  1217. const auto slot = juce::Rectangle<float> ((float) selectLeft() + 1.0f, (float) y + 3.0f, (float) selectWidth - 2.0f, (float) rowHeight - 6.0f);
  1218. // (two filled plates, the rim and the well, from the rounded-rectangle tiles: no stroked path per row)
  1219. theme::fillRoundRect (g, slot, 2.0f, selected ? (hovered (Area::select) ? accentHover : accent) : hovered (Area::select) ? textDisabled : border);
  1220. theme::fillRoundRect (g, slot.reduced (selected ? 2.0f : 1.0f), selected ? 1.0f : 1.5f, bg1);
  1221. const auto pill = slot.reduced (selected ? 3.0f : 2.0f);
  1222. // FL flashes the channel's selector as its note plays (round 170a [SHOT]): the "sound lighting up when it hits"
  1223. // round 190ih: and with every note the channel plays (typing keyboard, MIDI, Piano roll, off-grid notes), bright
  1224. // at note-on, dimmer while held, fading out after the release (FL capture, details190if); orange for a
  1225. // channel locked to a controller ("Receive notes from"; FL's manual: "Orange for locked Channels")
  1226. const auto lit = row < (int) activityLights.size() ? activityLights[(std::size_t) row].level : 0.0f;
  1227. const auto hit = ! channel.muted && channelHitsNow (row);
  1228. if (hit || (! channel.muted && lit > 0.0f))
  1229. {
  1230. const auto top = selected ? 0.75f : 0.9f;
  1231. const auto colour = ! channel.receiveFrom.empty() ? warning : juce::Colours::white;
  1232. theme::fillRoundRect (g, pill, 1.5f, colour.withAlpha (juce::jmax (hit ? top : 0.0f, lit * top)));
  1233. }
  1234. }
  1235. paintLane (g);
  1236. {
  1237. const auto box = racklayout::addChannelBox (nameLeft(), nameWidth, addY, rowHeight);
  1238. const auto addArea = juce::Rectangle<float> ((float) box.x, (float) box.y, (float) box.width, (float) box.height);
  1239. if (g.clipRegionIntersects (addArea.getSmallestIntegerContainer().expanded (1)) || ! ui::paintSavings()) // (round 190ge: a playhead frame repaints the steps only)
  1240. {
  1241. const float dash[] = { 3.0f, 3.0f };
  1242. juce::Path outline, dashed;
  1243. outline.addRoundedRectangle (addArea.reduced (0.5f), theme::radius::md);
  1244. juce::PathStrokeType (1.0f).createDashedStroke (dashed, outline, dash, 2);
  1245. const auto addHovered = hoverArea == Area::addRow;
  1246. g.setColour (addHovered ? textSecondary : borderStrong);
  1247. g.fillPath (dashed);
  1248. g.setColour (addHovered ? textPrimary : textSecondary);
  1249. g.setFont (theme::font (theme::type::small));
  1250. textcache::drawText (g, tr ("+ add channel"), addArea.toNearestInt(), juce::Justification::centred);
  1251. }
  1252. }
  1253. if (stickyX > 0) // steps pass under the column: a hairline marks its edge
  1254. {
  1255. g.setColour (border);
  1256. g.fillRect (gridLeft() - 1, 0, 1, getHeight());
  1257. }
  1258. }
  1259. if (! zipLayout.top.empty())
  1260. paintZipStrips (g);
  1261. if (advancedLoopShown())
  1262. paintLoopColumn (g);
  1263. }
  1264. // The miniature of a channel's piano-roll notes across its step area (FL shows one once a channel has notes
  1265. // that steps can't show); the notes keep their time and length, pitch maps onto the row's height.
  1266. void ChannelRackComponent::paintNotePreview (juce::Graphics& g, int row, const Pattern& pattern, std::uint32_t channelColour)
  1267. {
  1268. using namespace theme::colour;
  1269. const auto steps = numSteps();
  1270. // (the whole pattern in the visible width when it is longer than the rack shows: FL's complete preview)
  1271. const auto place = previewSpan();
  1272. const auto area = juce::Rectangle<float> ((float) (gridLeft() + place.x) + 1.0f, (float) rowY (row) + 4.0f, (float) place.width - 3.0f,
  1273. (float) rowHeight - 8.0f);
  1274. const auto beatWidth = (float) place.width * 4.0f / (float) steps;
  1275. const auto hoveredHere = hoverArea == Area::grid && hoverRow == row;
  1276. theme::fillRoundRect (g, area, theme::radius::sm, hoveredHere ? bg4 : bg1);
  1277. for (int beat = 4; beat < steps; beat += 4) // beat lines, bars a little stronger (bars only once beats crowd)
  1278. {
  1279. if (beat % 16 != 0 && beatWidth < 8.0f)
  1280. continue;
  1281. g.setColour (beat % 16 == 0 ? border.brighter (0.2f) : border);
  1282. g.fillRect (area.getX() + beatWidth * (float) (beat / 4) - 1.5f, area.getY() + 2.0f, 1.0f, area.getHeight() - 4.0f);
  1283. }
  1284. const auto [low, high] = channelKeyRange (pattern, row);
  1285. const auto span = juce::jmax (high - low, 6); // a single key sits mid-row, not on an edge
  1286. const auto lowKey = low - (span - (high - low)) / 2;
  1287. const auto inner = area.reduced (1.0f, 3.0f);
  1288. const auto noteHeight = juce::jlimit (3.0f, 5.0f, inner.getHeight() / (float) (span + 1));
  1289. const auto ticks = (float) (steps * ticksPerStep);
  1290. const auto tint = channelColour != 0 ? theme::tame (juce::Colour (channelColour)).brighter (0.6f) : stepNote;
  1291. // a looping row (round 165f-rack): the notes before the loop point, then their repeats faint; a note of its own past
  1292. // the loop point doesn't play (dim)
  1293. const auto loopTicks = (std::int64_t) loopStepsOf (row) * ticksPerStep;
  1294. const auto patternTicks = (std::int64_t) steps * ticksPerStep;
  1295. for (const auto& n : pattern.notes)
  1296. {
  1297. if (n.channel != row)
  1298. continue;
  1299. const auto pastLoop = loopTicks > 0 && n.start >= loopTicks;
  1300. const auto repeats = loopTicks > 0 && ! pastLoop ? (int) ((patternTicks - n.start + loopTicks - 1) / loopTicks) : 1;
  1301. for (int k = 0; k < repeats; ++k)
  1302. {
  1303. const auto start = n.start + k * loopTicks;
  1304. const auto x = inner.getX() + inner.getWidth() * (float) start / ticks;
  1305. if (x > inner.getRight())
  1306. break;
  1307. const auto w = juce::jmax (2.0f, inner.getWidth() * (float) n.length / ticks - 1.0f);
  1308. const auto y = inner.getBottom() - noteHeight - (inner.getHeight() - noteHeight) * (float) (n.key - lowKey) / (float) span;
  1309. g.setColour (n.muted || pastLoop ? textDisabled.withAlpha (pastLoop ? 0.5f : 1.0f) : k > 0 ? tint.withAlpha (0.4f) : tint);
  1310. g.fillRect (juce::Rectangle<float> (x, y, juce::jmin (w, inner.getRight() - x), noteHeight));
  1311. }
  1312. }
  1313. // round 190mk (DE-33, capture 190mk A2.3; FL's option "Show a mute button on the piano roll preview", on): a round
  1314. // mute button in the preview's middle while it is hovered; a muted channel's preview dims and keeps the crossed icon
  1315. const auto muted = row >= 0 && row < (int) engine.getSong().channels.size() && engine.getSong().channels[(std::size_t) row].muted;
  1316. if (muted)
  1317. {
  1318. g.setColour (bg1.withAlpha (0.55f));
  1319. g.fillRect (area);
  1320. }
  1321. if (previewMuteButton && (hoveredHere || muted))
  1322. {
  1323. const auto b = previewMuteBounds (row).toFloat();
  1324. const auto hot = hoveredHere && b.contains (getMouseXYRelative().toFloat());
  1325. g.setColour (hot ? bg5 : bg3);
  1326. g.fillEllipse (b);
  1327. g.setColour (border);
  1328. g.drawEllipse (b.reduced (0.5f), 1.0f);
  1329. // a speaker: box and cone, then sound waves (on) or a cross (muted)
  1330. const auto c = b.getCentre();
  1331. const auto u = b.getWidth() / 18.0f;
  1332. juce::Path speaker;
  1333. speaker.startNewSubPath (c.x - 5.0f * u, c.y - 2.0f * u);
  1334. speaker.lineTo (c.x - 2.5f * u, c.y - 2.0f * u);
  1335. speaker.lineTo (c.x + 0.5f * u, c.y - 5.0f * u);
  1336. speaker.lineTo (c.x + 0.5f * u, c.y + 5.0f * u);
  1337. speaker.lineTo (c.x - 2.5f * u, c.y + 2.0f * u);
  1338. speaker.lineTo (c.x - 5.0f * u, c.y + 2.0f * u);
  1339. speaker.closeSubPath();
  1340. g.setColour (muted ? textSecondary : textPrimary);
  1341. g.fillPath (speaker);
  1342. if (muted)
  1343. {
  1344. g.drawLine (c.x + 2.0f * u, c.y - 2.5f * u, c.x + 5.5f * u, c.y + 2.5f * u, 1.2f * u);
  1345. g.drawLine (c.x + 2.0f * u, c.y + 2.5f * u, c.x + 5.5f * u, c.y - 2.5f * u, 1.2f * u);
  1346. }
  1347. else
  1348. {
  1349. juce::Path wave;
  1350. wave.addCentredArc (c.x + 0.5f * u, c.y, 3.5f * u, 3.5f * u, 0.0f, 0.6f, 2.55f, true);
  1351. g.strokePath (wave, juce::PathStrokeType (1.1f * u));
  1352. }
  1353. }
  1354. }
  1355. juce::Rectangle<int> ChannelRackComponent::previewMuteBounds (int row) const
  1356. {
  1357. const auto place = previewSpan();
  1358. const auto centre = juce::Point<int> (gridLeft() + place.x + place.width / 2, rowY (row) + rowHeight / 2);
  1359. return juce::Rectangle<int> (18, 18).withCentre (centre);
  1360. }
  1361. // --- the control contract for the knobs and the route box ----------------------------------------
  1362. ui::ValueSpec ChannelRackComponent::specFor (Area area, int row) const
  1363. {
  1364. const auto name = channelName (engine.getSong(), row);
  1365. if (area == Area::pan)
  1366. {
  1367. auto spec = ui::panSpec (name + " - " + tr ("Pan"));
  1368. spec.target = AutomationTarget { AutomationTargetKind::channelPan, row };
  1369. return spec;
  1370. }
  1371. if (area == Area::volume)
  1372. {
  1373. auto spec = ui::percentSpec (name + " - " + tr ("Volume"), 0.78);
  1374. spec.target = AutomationTarget { AutomationTargetKind::channelVolume, row };
  1375. return spec;
  1376. }
  1377. // route box: the mixer insert, as a number
  1378. ui::ValueSpec spec;
  1379. spec.name = name + " - " + tr ("Target mixer track");
  1380. spec.maximum = maxMixerTracks - 1; // every insert, like FL (round 162: the inserts past the mixer's last one are added)
  1381. spec.interval = 1.0;
  1382. spec.toText = [] (double v) { return v < 0.5 ? tr ("Master") : tr ("Insert {0}", { juce::String (juce::roundToInt (v)) }); };
  1383. spec.fromText = [] (const juce::String& t) -> std::optional<double> {
  1384. if (t.trim().startsWithIgnoreCase ("m"))
  1385. return 0.0;
  1386. const auto digits = t.retainCharacters ("0123456789");
  1387. return digits.isEmpty() ? std::nullopt : std::optional<double> (digits.getDoubleValue());
  1388. };
  1389. return ui::digitWheel (spec); // FL's channel rack mixer-track digiwheel (round 168s)
  1390. }
  1391. double ChannelRackComponent::valueOf (Area area, int row) const
  1392. {
  1393. const auto& c = engine.getSong().channels[(std::size_t) row];
  1394. return area == Area::pan ? c.pan : area == Area::volume ? c.volume : (double) c.mixerTrack;
  1395. }
  1396. void ChannelRackComponent::setValue (Area area, int row, double value)
  1397. {
  1398. if (row < 0 || row >= (int) engine.getSong().channels.size())
  1399. return;
  1400. if (area == Area::pan)
  1401. engine.editLevels ([row, v = (float) value] (Song& s) { s.channels[(std::size_t) row].pan = v; });
  1402. else if (area == Area::volume)
  1403. engine.editLevels ([row, v = (float) value] (Song& s) { s.channels[(std::size_t) row].volume = v; });
  1404. else if (area == Area::route)
  1405. {
  1406. const auto track = juce::roundToInt (value);
  1407. if (track != engine.getSong().channels[(std::size_t) row].mixerTrack)
  1408. engine.editSong ([row, track] (Song& s) { mixerops::routeChannel (s, row, track); });
  1409. }
  1410. }
  1411. juce::Rectangle<int> ChannelRackComponent::controlBounds (Area area, int row) const
  1412. {
  1413. const auto y = rowY (row);
  1414. switch (area)
  1415. {
  1416. case Area::pan: return { muteWidth, y, knobWidth, rowHeight };
  1417. case Area::volume: return { muteWidth + knobWidth, y, knobWidth, rowHeight };
  1418. case Area::route: return showMixerSelectors ? juce::Rectangle<int> { muteWidth + 2 * knobWidth + 1, y + 7, routeWidth - 3, rowHeight - 14 } : juce::Rectangle<int>();
  1419. case Area::lane:
  1420. {
  1421. const auto lane = laneBounds();
  1422. const auto step = juce::jmax (0, hoverStep);
  1423. return { gridLeft() + step * stepWidth, lane.getY() + 6, stepWidth, lane.getHeight() - 12 };
  1424. }
  1425. default: return {};
  1426. }
  1427. }
  1428. void ChannelRackComponent::showTip (Area area, int row, bool transient)
  1429. {
  1430. if (row < 0 || row >= (int) engine.getSong().channels.size())
  1431. return;
  1432. const auto spec = specFor (area, row);
  1433. const auto text = spec.text (valueOf (area, row));
  1434. ui::postHint (ui::valueHint (spec, valueOf (area, row), ui::valueGestures()));
  1435. const auto bounds = area == Area::lane ? controlBounds (area, row) : controlBounds (area, row).translated (stickyX, 0);
  1436. if (transient)
  1437. ui::flashValueTip (*this, bounds, text);
  1438. else
  1439. ui::showValueTip (*this, bounds, text);
  1440. }
  1441. void ChannelRackComponent::setHover (Area area, int row, int step)
  1442. {
  1443. if (area == hoverArea && row == hoverRow && step == hoverStep)
  1444. return;
  1445. auto dirty = [this] (Area a, int r) {
  1446. if (a == Area::lane || a == Area::laneLevels)
  1447. repaint (laneBounds());
  1448. else if (a == Area::addRow)
  1449. repaint (rowBounds ((int) engine.getSong().channels.size()));
  1450. else if (r >= 0)
  1451. repaint (rowBounds (r));
  1452. };
  1453. dirty (hoverArea, hoverRow);
  1454. hoverArea = area;
  1455. hoverRow = row;
  1456. hoverStep = step;
  1457. dirty (hoverArea, hoverRow);
  1458. }
  1459. void ChannelRackComponent::mouseMove (const juce::MouseEvent& e)
  1460. {
  1461. int row = 0;
  1462. const auto area = areaAt (e.x, e.y, row);
  1463. const auto step = area == Area::grid || area == Area::lane ? (e.x - gridLeft()) / stepWidth : -1;
  1464. setHover (area, row, step >= 0 && step < (area == Area::grid ? shownSteps() : numSteps()) ? step : -1);
  1465. }
  1466. void ChannelRackComponent::mouseExit (const juce::MouseEvent&)
  1467. {
  1468. setHover (Area::none, -1, -1);
  1469. }
  1470. void ChannelRackComponent::setSwingMixOfSelected (int channel)
  1471. {
  1472. const auto& song = engine.getSong();
  1473. if (channel < 0 || channel >= (int) song.channels.size())
  1474. return;
  1475. auto spec = ui::percentSpec (channelName (song, channel) + " - " + tr ("Swing mix"), 1.0);
  1476. const auto rows = engine.getSelectedChannels();
  1477. ui::typeValue (*this, spec, song.channels[(std::size_t) channel].swingMix, [this, rows, channel] (double v) {
  1478. engine.editSong ([rows, channel, v] (Song& s) {
  1479. auto targets = rows;
  1480. if (std::find (targets.begin(), targets.end(), channel) == targets.end())
  1481. targets = { channel };
  1482. for (auto r : targets)
  1483. if (r >= 0 && r < (int) s.channels.size())
  1484. s.channels[(std::size_t) r].swingMix = (float) v;
  1485. });
  1486. });
  1487. }
  1488. // --- selection -----------------------------------------------------------------------------------
  1489. void ChannelRackComponent::selectRows (std::vector<int> rows, int primary)
  1490. {
  1491. engine.setChannelSelection (std::move (rows), primary);
  1492. repaint();
  1493. }
  1494. void ChannelRackComponent::clickSelector (int row, const juce::ModifierKeys& mods)
  1495. {
  1496. ChannelSelection selection;
  1497. selection.rows = engine.getSelectedChannels();
  1498. selection.anchor = selectionAnchor;
  1499. if (mods.isShiftDown())
  1500. selection.extendTo (row);
  1501. else if (flmods::ctrl (mods) || flmods::popup (mods))
  1502. selection.toggle (row);
  1503. else
  1504. selection.selectOnly (row);
  1505. selectionAnchor = selection.anchor;
  1506. const auto primary = selection.contains (row) ? row : selection.rows.front();
  1507. selectRows (selection.rows, primary);
  1508. }
  1509. void ChannelRackComponent::selectRelative (int delta, bool extend)
  1510. {
  1511. const auto count = (int) engine.getSong().channels.size();
  1512. if (count == 0)
  1513. return;
  1514. const auto row = juce::jlimit (0, count - 1, engine.getSelectedChannel() + delta);
  1515. if (extend)
  1516. {
  1517. ChannelSelection selection;
  1518. selection.anchor = selectionAnchor;
  1519. selection.extendTo (row);
  1520. selectRows (selection.rows, row);
  1521. }
  1522. else
  1523. {
  1524. selectionAnchor = row;
  1525. selectRows ({ row }, row);
  1526. }
  1527. }
  1528. void ChannelRackComponent::selectAll()
  1529. {
  1530. ChannelSelection selection;
  1531. selection.selectAll ((int) engine.getSong().channels.size());
  1532. selectRows (selection.rows, engine.getSelectedChannel());
  1533. }
  1534. bool ChannelRackComponent::moveSelected (int delta)
  1535. {
  1536. auto rows = engine.getSelectedChannels();
  1537. const auto primary = engine.getSelectedChannel();
  1538. const auto primaryIndex = (int) (std::find (rows.begin(), rows.end(), primary) - rows.begin());
  1539. bool moved = false;
  1540. std::vector<int> after;
  1541. engine.editSongStructural ([&] (Song& s) {
  1542. after = rows;
  1543. moved = moveChannels (s, after, delta);
  1544. });
  1545. if (! moved)
  1546. return false;
  1547. const auto newPrimary = primaryIndex < (int) after.size() ? after[(std::size_t) primaryIndex] : after.front();
  1548. openEditorRow = -1;
  1549. selectionAnchor = juce::jlimit (0, (int) engine.getSong().channels.size() - 1, selectionAnchor + delta);
  1550. selectRows (after, newPrimary);
  1551. return true;
  1552. }
  1553. void ChannelRackComponent::cloneSelected()
  1554. {
  1555. auto rows = engine.getSelectedChannels();
  1556. std::vector<int> clones;
  1557. engine.editSong ([&] (Song& s) {
  1558. // from the bottom up, so earlier clones don't shift the rows still to clone
  1559. for (auto it = rows.rbegin(); it != rows.rend(); ++it)
  1560. cloneChannel (s, *it);
  1561. });
  1562. // each clone sits right below its original: original i (sorted) ends at rows[i] + i, its clone one below
  1563. for (std::size_t i = 0; i < rows.size(); ++i)
  1564. clones.push_back (rows[i] + (int) i + 1);
  1565. if (! clones.empty())
  1566. selectRows (clones, clones.front());
  1567. }
  1568. void ChannelRackComponent::deleteSelected()
  1569. {
  1570. const auto rows = engine.getSelectedChannels();
  1571. const auto count = (int) engine.getSong().channels.size();
  1572. if (rows.empty() || (int) rows.size() >= count) // at least one channel stays
  1573. return;
  1574. for (auto r : rows)
  1575. closeEditor (r);
  1576. engine.editSong ([rows] (Song& s) { removeChannels (s, rows); });
  1577. openEditorRow = -1;
  1578. const auto row = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().channels.size() - 1), rows.front());
  1579. selectRows ({ row }, row);
  1580. }
  1581. void ChannelRackComponent::sendSelectedToInserts()
  1582. {
  1583. const auto rows = engine.getSelectedChannels();
  1584. engine.editSong ([rows] (Song& s) { sendChannelsToFreeInserts (s, rows); });
  1585. }
  1586. void ChannelRackComponent::toggleMute (int channel)
  1587. {
  1588. if (channel >= 0 && channel < (int) engine.getSong().channels.size())
  1589. engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].muted = ! s.channels[(std::size_t) channel].muted; });
  1590. }
  1591. void ChannelRackComponent::toggleSoloLock (int channel)
  1592. {
  1593. if (channel >= 0 && channel < (int) engine.getSong().channels.size())
  1594. engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].soloLock = ! s.channels[(std::size_t) channel].soloLock; });
  1595. }
  1596. void ChannelRackComponent::solo (int row)
  1597. {
  1598. if (row < 0 || row >= (int) engine.getSong().channels.size())
  1599. return;
  1600. // only this channel plays (the channels with "Lock active/mute state" keep theirs); repeating on the soloed channel
  1601. // unmutes them (FL's channel solo, REA_rodada167l §5)
  1602. // (round 190bx: FL's General › "Restore previous state after solo" — the un-solo then puts back the mute switches
  1603. // from before the solo; core/FlSettings.h)
  1604. engine.editSong ([row] (Song& s) {
  1605. noteedit::soloChannel (s, row, &flsettings::channelSoloMemory(), flsettings::options().restoreStateAfterSolo.load());
  1606. });
  1607. }
  1608. // --- channel editors ---------------------------------------------------------------------------
  1609. bool ChannelRackComponent::isEditorOpen (int channel)
  1610. {
  1611. if (channel < 0 || channel >= (int) engine.getSong().channels.size())
  1612. return false;
  1613. if (engine.getSong().channels[(std::size_t) channel].instrument == InstrumentKind::plugin)
  1614. {
  1615. if (auto* plugin = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get()))
  1616. return PluginWindows::isOpen (plugin->getInstance());
  1617. return false;
  1618. }
  1619. return isChannelSettingsOpen (channel);
  1620. }
  1621. void ChannelRackComponent::closeEditor (int channel)
  1622. {
  1623. if (channel < 0 || channel >= (int) engine.getSong().channels.size())
  1624. return;
  1625. if (engine.getSong().channels[(std::size_t) channel].instrument == InstrumentKind::plugin)
  1626. {
  1627. if (auto* plugin = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get()))
  1628. PluginWindows::close (plugin->getInstance());
  1629. }
  1630. else
  1631. closeChannelSettings (channel);
  1632. }
  1633. // --- Freeze (round 190ay) -----------------------------------------------------------------------------------------------
  1634. ui::Hint ChannelRackComponent::getChannelHintForSnapshot (int channel)
  1635. {
  1636. return hintAt ({ stickyX + nameLeft() + nameWidth / 2, rowY (channel) + rowHeight / 2 });
  1637. }
  1638. juce::String ChannelRackComponent::frozenHint (int channel)
  1639. {
  1640. if (engine.frozenAudioMissing (channel))
  1641. return tr ("Frozen, but its audio files are missing: the instrument plays instead. [RMB] Re-freeze or Unfreeze");
  1642. if (engine.freezeOutOfDate (channel))
  1643. return tr ("Frozen, out of date (changed since the freeze): the old audio plays. [RMB] Re-freeze"); // (round 190bj: shorter, the action fits the bar)
  1644. return tr ("Frozen: its rendered audio plays and its instrument is unloaded (no CPU, no RAM). [RMB] Unfreeze to edit");
  1645. }
  1646. bool ChannelRackComponent::refuseFrozenEdit (int channel)
  1647. {
  1648. const auto& song = engine.getSong();
  1649. if (channel < 0 || channel >= (int) song.channels.size() || ! song.channels[(std::size_t) channel].freeze.on())
  1650. return false;
  1651. ui::postNotice ({ tr ("{0} is frozen: Unfreeze to edit ([RMB] on the channel, Unfreeze)", { channelName (song, channel) }), {} }, 3000);
  1652. return true;
  1653. }
  1654. void ChannelRackComponent::freezeChannels (std::vector<std::uint64_t> uids)
  1655. {
  1656. // round 190bj (QA: "Freeze selected channels" stopped at the first channel that can't be frozen with "Can't freeze
  1657. // this channel", not saying which): with several, the ones that can't are named with their reason and the others
  1658. // are frozen, as the selection's Unfreeze treats each channel on its own
  1659. if (uids.size() > 1)
  1660. {
  1661. const auto& song = engine.getSong();
  1662. juce::StringArray refused;
  1663. std::vector<std::uint64_t> go;
  1664. for (const auto uid : uids)
  1665. {
  1666. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [uid] (const ChannelDef& c) { return c.uid == uid && uid != 0; });
  1667. if (it == song.channels.end())
  1668. continue;
  1669. if (const auto why = freezeRefusal (engine, (int) (it - song.channels.begin())); why.isNotEmpty())
  1670. refused.add (juce::String::fromUTF8 (it->name.c_str()) + ": " + why);
  1671. else
  1672. go.push_back (uid);
  1673. }
  1674. if (! refused.isEmpty())
  1675. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Some selected channels can't be frozen"),
  1676. refused.joinIntoString ("\n\n")
  1677. + (go.empty() ? juce::String() : "\n\n" + tr ("The other selected channels are frozen.")));
  1678. uids = std::move (go);
  1679. }
  1680. while (! uids.empty())
  1681. {
  1682. const auto uid = uids.front();
  1683. uids.erase (uids.begin());
  1684. const auto& channels = engine.getSong().channels;
  1685. const auto it = std::find_if (channels.begin(), channels.end(), [uid] (const ChannelDef& c) { return c.uid == uid && uid != 0; });
  1686. if (it == channels.end())
  1687. continue;
  1688. const auto channel = (int) (it - channels.begin());
  1689. if (openEditorRow == channel) // (freezeChannel closes the channel's windows)
  1690. openEditorRow = -1;
  1691. freezeChannel (engine, channel, [safe = juce::Component::SafePointer<ChannelRackComponent> (this), uids] (bool ok) {
  1692. if (safe == nullptr)
  1693. return;
  1694. safe->repaint();
  1695. if (ok && ! uids.empty()) // (a refusal or Cancel stops the rest)
  1696. safe->freezeChannels (uids);
  1697. });
  1698. return;
  1699. }
  1700. }
  1701. void ChannelRackComponent::toggleEditor (int channel, bool keepOthers)
  1702. {
  1703. if (channel < 0 || channel >= (int) engine.getSong().channels.size())
  1704. return;
  1705. if (refuseFrozenEdit (channel)) // (its instrument is not loaded: there is nothing to show or change)
  1706. return;
  1707. const auto kind = engine.getSong().channels[(std::size_t) channel].instrument;
  1708. if (kind == InstrumentKind::patcher) // its node graph editor (REA_rodada141)
  1709. {
  1710. if (PatcherWindows::isChannelOpen (channel))
  1711. PatcherWindows::closeChannel (channel);
  1712. else
  1713. PatcherWindows::openChannel (engine, channel);
  1714. return;
  1715. }
  1716. // built-in drums and audio clips have no instrument page: as in FL, the click opens their channel window anyway
  1717. // (header + Misc tab — levels, polyphony, cut groups, arpeggiator), as the hint says; it only selected (round 165q)
  1718. if (isEditorOpen (channel))
  1719. {
  1720. closeEditor (channel);
  1721. if (openEditorRow == channel)
  1722. openEditorRow = -1;
  1723. return;
  1724. }
  1725. if (! keepOthers && ! keepEditorsOpen && openEditorRow != channel)
  1726. closeEditor (openEditorRow);
  1727. if (kind == InstrumentKind::plugin)
  1728. openPluginWindow (channel);
  1729. else
  1730. openChannelSettings (engine, channel);
  1731. openEditorRow = channel;
  1732. }
  1733. // --- step graph lanes (docs/WORKFLOW_FL.md P2, C4) --------------------------------------------------
  1734. juce::Rectangle<int> ChannelRackComponent::levelButtonBounds (int tab) const
  1735. {
  1736. const auto lane = laneBounds();
  1737. const auto width = (gridLeft() - 24) / 3;
  1738. return { 12 + (tab % 3) * width, lane.getY() + 6 + (tab / 3) * 22, width - 4, 19 };
  1739. }
  1740. void ChannelRackComponent::paintLane (juce::Graphics& g)
  1741. {
  1742. using namespace theme::colour;
  1743. const auto lane = laneBounds();
  1744. if (lane.isEmpty() || ! g.getClipBounds().intersects (lane))
  1745. return;
  1746. const auto& song = engine.getSong();
  1747. const auto& pattern = song.activePattern();
  1748. const auto channel = graphRow();
  1749. const auto level = (notetools::Level) graphLevel;
  1750. g.setColour (bg0);
  1751. g.fillRect (lane);
  1752. g.setColour (border);
  1753. g.drawHorizontalLine (lane.getY(), 0.0f, (float) getWidth());
  1754. g.drawHorizontalLine (lane.getBottom() - 1, 0.0f, (float) getWidth());
  1755. // FL's nine lanes in FL's order (issue #208); like FL, a narrow tab takes the shorter name
  1756. for (int tab = 0; tab < notetools::stepLevelCount; ++tab)
  1757. {
  1758. const auto l = (int) notetools::stepLevel (tab);
  1759. const auto r = levelButtonBounds (tab).toFloat();
  1760. theme::fillRoundRect (g, r, theme::radius::sm, l == graphLevel ? accent : bg4);
  1761. g.setColour (l == graphLevel ? onAccent : textSecondary);
  1762. const auto font = theme::font (theme::type::caption, l == graphLevel);
  1763. g.setFont (font);
  1764. auto name = tr (notetools::stepLevelName (tab, 2));
  1765. for (int size = 1; size >= 0 && juce::GlyphArrangement::getStringWidth (font, name) > r.getWidth() - 6.0f; --size)
  1766. name = tr (notetools::stepLevelName (tab, size));
  1767. textcache::drawFittedText (g, name, r.toNearestInt(), juce::Justification::centred, 1, 0.8f);
  1768. }
  1769. g.setColour (textDisabled);
  1770. g.setFont (theme::font (theme::type::caption));
  1771. textcache::drawFittedText (g, tr ("Drag: set · Ctrl+drag: scale all · Right-drag: ramp · Alt+click: reset"),
  1772. juce::Rectangle<int> (12, lane.getBottom() - 20, gridLeft() - 24, 16), juce::Justification::centredLeft, 1, 0.8f);
  1773. const auto top = (float) lane.getY() + 6.0f, bottom = (float) lane.getBottom() - 6.0f;
  1774. const auto bipolar = notetools::graphIsBipolar (graphLevel);
  1775. const auto base = bipolar ? (top + bottom) * 0.5f : bottom;
  1776. if (level == notetools::Level::notePitch) // FL's piano-roll rows: the black keys darker, the C rows named
  1777. {
  1778. const auto [lo, hi] = pitchLaneKeys();
  1779. const auto rowH = (bottom - top) / (float) (hi - lo + 1);
  1780. for (int key = lo; key <= hi; ++key)
  1781. {
  1782. const auto y = bottom - (float) (key - lo + 1) * rowH;
  1783. if (juce::MidiMessage::isMidiNoteBlack (key))
  1784. {
  1785. g.setColour (bg1);
  1786. g.fillRect (juce::Rectangle<float> ((float) gridLeft(), y, (float) (numSteps() * stepWidth), rowH));
  1787. }
  1788. if (key % 12 == 0 && rowH * 3.0f >= 9.0f)
  1789. {
  1790. g.setColour (textDisabled);
  1791. g.setFont (theme::font (theme::type::caption));
  1792. textcache::drawText (g, juce::String (notetools::levelText (level, (float) key / 127.0f)),
  1793. juce::Rectangle<int> (gridLeft() + 2, (int) (y + rowH - 10.0f), 28, 10), juce::Justification::bottomLeft);
  1794. }
  1795. }
  1796. }
  1797. for (int step = 0; step < numSteps(); ++step)
  1798. {
  1799. const auto x = (float) (gridLeft() + step * stepWidth + 1);
  1800. const auto w = (float) stepWidth - 3.0f;
  1801. if (step % 4 == 0 && step > 0)
  1802. {
  1803. g.setColour (step % 16 == 0 ? gridBar : gridBeat);
  1804. g.drawVerticalLine ((int) x - 2, top, bottom);
  1805. }
  1806. if (hoverArea == Area::lane && hoverStep == step)
  1807. {
  1808. g.setColour (bg3);
  1809. g.fillRect (juce::Rectangle<float> (x, top, w, bottom - top));
  1810. }
  1811. const auto notes = notesInStep (pattern, channel, step);
  1812. if (notes.empty())
  1813. {
  1814. g.setColour (theme::stepOff (step));
  1815. g.fillRect (x + w * 0.5f - 1.0f, base - 1.0f, 2.0f, 2.0f);
  1816. continue;
  1817. }
  1818. const auto& first = pattern.notes[(std::size_t) notes.front()];
  1819. if (level == notetools::Level::notePitch) // FL: a block at the step's key, named
  1820. {
  1821. const auto [lo, hi] = pitchLaneKeys();
  1822. const auto rowH = (bottom - top) / (float) (hi - lo + 1);
  1823. const auto block = juce::Rectangle<float> (x + 1.0f, bottom - (float) (first.key - lo + 1) * rowH, w - 2.0f, juce::jmax (3.0f, rowH));
  1824. g.setColour (info.withAlpha (0.85f));
  1825. g.fillRoundedRectangle (block, 1.5f);
  1826. if (block.getHeight() >= 9.0f && block.getWidth() >= 14.0f)
  1827. {
  1828. g.setColour (bg0);
  1829. g.setFont (theme::font (theme::type::caption));
  1830. textcache::drawFittedText (g, juce::String (notetools::levelText (level, (float) first.key / 127.0f)), block.toNearestInt(),
  1831. juce::Justification::centred, 1, 0.6f);
  1832. }
  1833. continue;
  1834. }
  1835. if (level == notetools::Level::repeat) // FL: a block at the rate's height, a line for each extra strike in a step
  1836. {
  1837. if (first.repeat <= 7)
  1838. continue; // (no second strike inside a step: FL draws nothing)
  1839. const auto rowH = (bottom - top) / 8.0f;
  1840. const auto block = juce::Rectangle<float> (x + 1.0f, bottom - (float) (first.repeat - 6) * rowH, w - 2.0f, rowH);
  1841. g.setColour (info.withAlpha (0.85f));
  1842. g.fillRoundedRectangle (block, 1.5f);
  1843. const auto strikes = noterepeat::strikeCount (first, ticksPerStep);
  1844. g.setColour (bg0.withAlpha (0.55f));
  1845. for (int k = 1; k < strikes; ++k)
  1846. g.fillRect (block.getX() + 3.0f, block.getY() + block.getHeight() * (float) k / (float) strikes - 0.5f, block.getWidth() - 6.0f, 1.0f);
  1847. continue;
  1848. }
  1849. const auto v = notetools::levelValue (first, level);
  1850. const auto y = bottom - v * (bottom - top);
  1851. g.setColour ((graphLevel == (int) notetools::Level::velocity ? accent : info).withAlpha (0.85f));
  1852. g.fillRect (juce::Rectangle<float> (x + 2.0f, juce::jmin (y, base), w - 4.0f, juce::jmax (2.0f, std::abs (base - y))));
  1853. g.setColour (textPrimary);
  1854. g.fillRect (x + 1.0f, y - 1.0f, w - 2.0f, 2.0f);
  1855. }
  1856. if (bipolar)
  1857. {
  1858. g.setColour (gridBar);
  1859. g.drawHorizontalLine ((int) base, (float) gridLeft(), (float) getWidth());
  1860. }
  1861. }
  1862. // FL's Note pitch lane is a small piano roll (issue #208, FL 2026 shot fl-note): the keys from a little below the
  1863. // channel's lowest step to a little above its highest (two octaves at least), each step a block at its key
  1864. std::pair<int, int> ChannelRackComponent::pitchLaneKeys() const
  1865. {
  1866. const auto& pattern = engine.getSong().activePattern();
  1867. const auto channel = graphRow();
  1868. int lo = 127, hi = 0;
  1869. for (const auto& n : pattern.notes)
  1870. if (n.channel == channel)
  1871. lo = juce::jmin (lo, n.key), hi = juce::jmax (hi, n.key);
  1872. if (lo > hi)
  1873. lo = hi = 60;
  1874. const auto middle = (lo + hi) / 2;
  1875. lo = juce::jmin (lo - 2, middle - 12);
  1876. hi = juce::jmax (hi + 2, middle + 12);
  1877. if (lo < 0)
  1878. hi -= lo, lo = 0;
  1879. if (hi > 127)
  1880. lo = juce::jmax (0, lo - (hi - 127)), hi = 127;
  1881. return { lo, hi };
  1882. }
  1883. float ChannelRackComponent::laneValueAt (int y) const
  1884. {
  1885. const auto lane = laneBounds();
  1886. const auto top = (float) lane.getY() + 6.0f, bottom = (float) lane.getBottom() - 6.0f;
  1887. const auto t = juce::jlimit (0.0f, 1.0f, (bottom - (float) y) / juce::jmax (1.0f, bottom - top));
  1888. const auto level = (notetools::Level) graphLevel;
  1889. if (level == notetools::Level::notePitch) // (the key row under the mouse)
  1890. {
  1891. const auto [lo, hi] = pitchLaneKeys();
  1892. return (float) juce::jlimit (0, 127, lo + (int) std::floor (t * (float) (hi - lo + 1))) / 127.0f;
  1893. }
  1894. if (level == notetools::Level::repeat) // FL's lane: 1/16 (one strike a step) at the bottom .. 1/64 triplet at the top
  1895. {
  1896. const auto index = juce::jlimit (7, noterepeat::rateCount, 7 + (int) std::floor (t * 8.0f));
  1897. return index <= 7 ? 0.0f : (float) index / (float) noterepeat::rateCount;
  1898. }
  1899. return t;
  1900. }
  1901. juce::Point<int> ChannelRackComponent::laneSpotForSnapshot (int step, float value) const
  1902. {
  1903. const auto lane = laneBounds();
  1904. const auto top = (float) lane.getY() + 6.0f, bottom = (float) lane.getBottom() - 6.0f;
  1905. return { gridLeft() + step * stepWidth + stepWidth / 2, juce::roundToInt (bottom - value * (bottom - top)) };
  1906. }
  1907. void ChannelRackComponent::setLaneValue (int step, float value)
  1908. {
  1909. const auto channel = graphRow();
  1910. const auto level = (notetools::Level) graphLevel;
  1911. engine.editSong ([channel, step, value, level] (Song& s) {
  1912. auto& p = s.activePattern();
  1913. for (auto i : notesInStep (p, channel, step))
  1914. notetools::setLevelValue (p.notes[(std::size_t) i], level, value);
  1915. if (level == notetools::Level::shift) // (Shift moves the notes inside their step)
  1916. p.sortNotes();
  1917. });
  1918. }
  1919. void ChannelRackComponent::applyLane (const juce::MouseEvent& e)
  1920. {
  1921. const auto steps = numSteps();
  1922. const auto step = juce::jlimit (0, steps - 1, (e.x - gridLeft()) / stepWidth);
  1923. const auto value = laneValueAt (e.y);
  1924. if (laneScaleBase >= 0.0f) // round 190lx (DE-31): Ctrl scales every column (bipolar lanes about their centre)
  1925. {
  1926. const auto level = graphLevel;
  1927. const auto centre = notetools::graphIsBipolar (level) ? 0.5f : 0.0f;
  1928. const auto from = laneScaleBase - centre, to = value - centre;
  1929. const auto k = std::abs (from) > 1.0e-4f ? to / from : 0.0f;
  1930. const auto offset = std::abs (from) > 1.0e-4f ? 0.0f : to - from; // (a column at the centre: they move with it)
  1931. engine.editSong ([=, items = laneScaleFrom] (Song& s) {
  1932. auto& p = s.activePattern();
  1933. for (const auto& [i, v0] : items)
  1934. if (i < p.notes.size())
  1935. notetools::setGraphValue (p.notes[i], level, centre + (v0 - centre) * k + offset, ticksPerStep);
  1936. });
  1937. return;
  1938. }
  1939. if (e.mods.isRightButtonDown()) // a straight ramp from the press to here
  1940. {
  1941. const auto from = juce::jmin (laneDownStep, step), to = juce::jmax (laneDownStep, step);
  1942. const auto channel = graphRow();
  1943. const auto level = (notetools::Level) graphLevel;
  1944. const auto v0 = laneDownStep <= step ? laneDownValue : value, v1 = laneDownStep <= step ? value : laneDownValue;
  1945. engine.editSong ([=] (Song& s) {
  1946. auto& p = s.activePattern();
  1947. for (int st = from; st <= to; ++st)
  1948. {
  1949. const auto t = to > from ? (float) (st - from) / (float) (to - from) : 0.0f;
  1950. for (auto i : notesInStep (p, channel, st))
  1951. notetools::setLevelValue (p.notes[(std::size_t) i], level, v0 + (v1 - v0) * t);
  1952. }
  1953. if (level == notetools::Level::shift)
  1954. p.sortNotes();
  1955. });
  1956. return;
  1957. }
  1958. setLaneValue (step, value);
  1959. }
  1960. void ChannelRackComponent::setPianoRollView (bool on)
  1961. {
  1962. if (pianoRollView == on)
  1963. return;
  1964. pianoRollView = on;
  1965. repaint();
  1966. if (onFilterChanged) // the title's switches follow
  1967. onFilterChanged();
  1968. }
  1969. void ChannelRackComponent::toggleGraphLanes()
  1970. {
  1971. graphLanes = ! graphLanes;
  1972. updateSize();
  1973. repaint();
  1974. if (onFilterChanged) // the title's graph switch follows
  1975. onFilterChanged();
  1976. }
  1977. // --- fills, rotation and organisation ------------------------------------------------------------
  1978. void ChannelRackComponent::rotateSelected (int steps)
  1979. {
  1980. const auto rows = engine.getSelectedChannels();
  1981. engine.editSong ([rows, steps] (Song& s) {
  1982. auto& p = s.activePattern();
  1983. const auto length = patternSteps (s, p);
  1984. for (auto r : rows)
  1985. rotateSteps (p, r, steps, length);
  1986. });
  1987. }
  1988. void ChannelRackComponent::copySelected()
  1989. {
  1990. const auto& song = engine.getSong();
  1991. stepClipboard = copyChannelSteps (song.activePattern(), engine.getSelectedChannels(), (int) song.channels.size());
  1992. }
  1993. void ChannelRackComponent::cutSelected()
  1994. {
  1995. copySelected();
  1996. const auto rows = engine.getSelectedChannels();
  1997. const auto& song = engine.getSong();
  1998. auto copy = song.activePattern();
  1999. if (clearChannelSteps (copy, rows)) // (nothing to cut: no undo step)
  2000. engine.editSong ([rows] (Song& s) { clearChannelSteps (s.activePattern(), rows); });
  2001. }
  2002. void ChannelRackComponent::pasteSelected()
  2003. {
  2004. if (stepClipboard.empty() || engine.getSelectedChannels().empty())
  2005. return;
  2006. engine.editSong ([rows = engine.getSelectedChannels(), clip = stepClipboard] (Song& s) {
  2007. pasteChannelSteps (s.activePattern(), clip, rows, (int) s.channels.size());
  2008. });
  2009. }
  2010. void ChannelRackComponent::fillSelected (int every)
  2011. {
  2012. const auto rows = engine.getSelectedChannels();
  2013. engine.editSong ([rows, every] (Song& s) {
  2014. auto& p = s.activePattern();
  2015. const auto length = patternSteps (s, p);
  2016. for (auto r : rows)
  2017. if (r < (int) s.channels.size()) // (audio clip channels too: they show their steps, round 190io)
  2018. fillEverySteps (p, r, every, length);
  2019. });
  2020. }
  2021. void ChannelRackComponent::sortChannels (ChannelSort by)
  2022. {
  2023. engine.editSong ([by] (Song& s) { permuteChannels (s, channelSortOrder (s, by)); });
  2024. openEditorRow = -1;
  2025. selectRows ({ 0 }, 0);
  2026. }
  2027. void ChannelRackComponent::selectUnused()
  2028. {
  2029. const auto unused = unusedChannels (engine.getSong());
  2030. if (unused.empty())
  2031. {
  2032. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Select unused channels"),
  2033. tr ("Every channel is used by a pattern or a Playlist clip."));
  2034. return;
  2035. }
  2036. selectRows (unused, unused.front());
  2037. }
  2038. int ChannelRackComponent::addChannel (ChannelDef c, bool openEditor, int insert)
  2039. {
  2040. int index = 0;
  2041. if (filterGroup >= 0 && c.group < 0) // a new channel joins the group shown (FL)
  2042. c.group = filterGroup;
  2043. engine.editSong ([&] (Song& s) { index = addChannelRouted (s, c, insert); });
  2044. selectionAnchor = index;
  2045. selectRows ({ index }, index);
  2046. if (openEditor && c.instrument == InstrumentKind::plugin)
  2047. {
  2048. openPluginWindow (index);
  2049. openEditorRow = index;
  2050. }
  2051. else if (openEditor && natives::instrumentEngine (c.instrument) != nullptr) // an FL engine: its editor (round 142)
  2052. openChannelSettings (engine, index);
  2053. return index;
  2054. }
  2055. bool ChannelRackComponent::loadSampleIntoSelected (const juce::File& file)
  2056. {
  2057. const auto row = engine.getSelectedChannel();
  2058. const auto& song = engine.getSong();
  2059. if (row < 0 || row >= (int) song.channels.size() || ! SampleLibrary::isSampleFile (file))
  2060. return false;
  2061. const auto kind = song.channels[(std::size_t) row].instrument;
  2062. // only channels that play a sample (or the built-in drums, which become samplers) take a file
  2063. if (kind != InstrumentKind::sampler && kind != InstrumentKind::kick && kind != InstrumentKind::snare && kind != InstrumentKind::hat
  2064. && kind != InstrumentKind::clap && kind != InstrumentKind::granulizer && kind != InstrumentKind::wavetraveller)
  2065. return false;
  2066. assignSample (row, file);
  2067. return true;
  2068. }
  2069. namespace
  2070. {
  2071. // Euclidean fill: pulses spread evenly over a length, rotated; previewed live on the pattern.
  2072. class EuclideanPanel final : public juce::Component
  2073. {
  2074. public:
  2075. EuclideanPanel (AudioEngine& e, int c) : engine (e), channel (c)
  2076. {
  2077. const auto& song = engine.getSong();
  2078. patternIndex = song.currentPattern;
  2079. originals = song.patterns[(std::size_t) patternIndex].notes;
  2080. steps = patternSteps (song, song.patterns[(std::size_t) patternIndex]);
  2081. auto setup = [this] (juce::Slider& s, juce::Label& l, const juce::String& name, double lo, double hi, double v) {
  2082. l.setText (name, juce::dontSendNotification);
  2083. addAndMakeVisible (l);
  2084. s.setSliderStyle (juce::Slider::LinearHorizontal);
  2085. s.setTextBoxStyle (juce::Slider::TextBoxRight, false, 50, 20);
  2086. s.setRange (lo, hi, 1.0);
  2087. s.setValue (v, juce::dontSendNotification);
  2088. s.onValueChange = [this] { preview(); };
  2089. addAndMakeVisible (s);
  2090. };
  2091. setup (pulses, pulsesLabel, tr ("Pulses"), 0, 32, 5);
  2092. setup (length, lengthLabel, tr ("Length (steps)"), 1, 32, 16);
  2093. setup (rotation, rotationLabel, tr ("Rotation"), -16, 16, 0);
  2094. title.setText (tr ("Euclidean fill: {0}", { juce::String::fromUTF8 (song.channels[(std::size_t) channel].name.c_str()) }), juce::dontSendNotification);
  2095. title.setFont (theme::font (theme::type::title, true));
  2096. addAndMakeVisible (title);
  2097. for (auto* b : { &accept, &cancel })
  2098. addAndMakeVisible (b);
  2099. accept.setColour (juce::TextButton::buttonColourId, theme::colour::accent);
  2100. accept.onClick = [this] {
  2101. restore();
  2102. const auto rhythm = current();
  2103. engine.editSong ([this, rhythm] (Song& s) { setStepRhythm (s.patterns[(std::size_t) patternIndex], channel, rhythm, steps); });
  2104. done = true;
  2105. closeToolWindowOf (*this);
  2106. };
  2107. cancel.onClick = [this] { closeToolWindowOf (*this); };
  2108. setSize (380, 330);
  2109. preview();
  2110. }
  2111. ~EuclideanPanel() override
  2112. {
  2113. if (! done)
  2114. restore();
  2115. }
  2116. void paint (juce::Graphics& g) override
  2117. {
  2118. g.fillAll (theme::colour::bg2);
  2119. // the rhythm as a ring of steps, the first step at the top
  2120. const auto rhythm = current();
  2121. const auto ring = juce::Rectangle<float> (0, 150, (float) getWidth(), 120).withSizeKeepingCentre (110, 110);
  2122. const auto n = (int) rhythm.size();
  2123. for (int i = 0; i < n; ++i)
  2124. {
  2125. const auto angle = juce::MathConstants<float>::twoPi * (float) i / (float) n;
  2126. const auto centre = ring.getCentre() + juce::Point<float> (std::sin (angle), -std::cos (angle)) * 50.0f;
  2127. const auto dot = juce::Rectangle<float> (12, 12).withCentre (centre);
  2128. g.setColour (rhythm[(std::size_t) i] ? theme::colour::stepOn : theme::colour::stepOff);
  2129. g.fillEllipse (dot);
  2130. if (i == 0)
  2131. {
  2132. g.setColour (theme::colour::textPrimary);
  2133. g.drawEllipse (dot.expanded (2.0f), 1.0f);
  2134. }
  2135. }
  2136. }
  2137. void resized() override
  2138. {
  2139. auto area = getLocalBounds().reduced (12, 10);
  2140. title.setBounds (area.removeFromTop (24));
  2141. area.removeFromTop (6);
  2142. for (auto [s, l] : { std::pair { &pulses, &pulsesLabel }, std::pair { &length, &lengthLabel }, std::pair { &rotation, &rotationLabel } })
  2143. {
  2144. auto row = area.removeFromTop (30);
  2145. l->setBounds (row.removeFromLeft (120));
  2146. s->setBounds (row);
  2147. }
  2148. auto buttons = area.removeFromBottom (28);
  2149. accept.setBounds (buttons.removeFromRight (100));
  2150. buttons.removeFromRight (8);
  2151. cancel.setBounds (buttons.removeFromRight (90));
  2152. }
  2153. private:
  2154. std::vector<bool> current() const
  2155. {
  2156. return euclideanRhythm ((int) pulses.getValue(), (int) length.getValue(), (int) rotation.getValue());
  2157. }
  2158. void restore()
  2159. {
  2160. if (patternIndex < (int) engine.getSong().patterns.size())
  2161. engine.editSong ([this] (Song& s) { s.patterns[(std::size_t) patternIndex].notes = originals; }, false);
  2162. }
  2163. void preview()
  2164. {
  2165. const auto rhythm = current();
  2166. if (patternIndex < (int) engine.getSong().patterns.size())
  2167. engine.editSong ([this, rhythm] (Song& s) {
  2168. auto& p = s.patterns[(std::size_t) patternIndex];
  2169. p.notes = originals;
  2170. setStepRhythm (p, channel, rhythm, steps);
  2171. }, false);
  2172. repaint();
  2173. }
  2174. AudioEngine& engine;
  2175. int channel, patternIndex = 0, steps = 16;
  2176. std::vector<Note> originals;
  2177. bool done = false;
  2178. juce::Label title, pulsesLabel, lengthLabel, rotationLabel;
  2179. juce::Slider pulses, length, rotation;
  2180. juce::TextButton accept { tr ("Accept") }, cancel { tr ("Cancel") };
  2181. };
  2182. } // namespace
  2183. std::unique_ptr<juce::Component> ChannelRackComponent::makeEuclideanPanel (int channel)
  2184. {
  2185. return std::make_unique<EuclideanPanel> (engine, juce::jlimit (0, juce::jmax (0, (int) engine.getSong().channels.size() - 1), channel));
  2186. }
  2187. void ChannelRackComponent::showEuclideanFill (int channel)
  2188. {
  2189. if (channel >= 0 && channel < (int) engine.getSong().channels.size())
  2190. openToolWindow (makeEuclideanPanel (channel), tr ("Euclidean fill"), "euclidean");
  2191. }
  2192. // --- mouse -------------------------------------------------------------------------------------
  2193. void ChannelRackComponent::mouseDown (const juce::MouseEvent& e)
  2194. {
  2195. const AudioEngine::UndoGesture undoGesture (engine, AudioEngine::UndoGesture::begin); // (round 190jv: one undo step per gesture)
  2196. int row = 0;
  2197. const auto area = areaAt (e.x, e.y, row);
  2198. const auto& song = engine.getSong();
  2199. switch (area)
  2200. {
  2201. case Area::none:
  2202. return;
  2203. case Area::zipped: // round 190mk (FL's manual: "Right-Click the button of a zipped Channel to unzip it")
  2204. if (flmods::popup (e.mods))
  2205. unzip (row);
  2206. else
  2207. {
  2208. selectionAnchor = row;
  2209. selectRows ({ row }, row);
  2210. }
  2211. return;
  2212. case Area::addRow:
  2213. showAddChannelMenu();
  2214. return;
  2215. case Area::loop: // the advanced looping control: drag sets the loop point in steps, right-click its menu
  2216. {
  2217. if (flmods::popup (e.mods))
  2218. {
  2219. showLoopPointMenu (row);
  2220. return;
  2221. }
  2222. const auto& p = song.activePattern();
  2223. const auto setting = channelloops::effectiveSetting (p, row);
  2224. const auto steps = loopStepsOf (row);
  2225. loopDragRow = row;
  2226. loopDragStart = setting > 0 ? setting : steps > 0 ? steps : numSteps();
  2227. loopDragY = e.position.y;
  2228. repaint();
  2229. return;
  2230. }
  2231. case Area::laneLevels:
  2232. for (int tab = 0; tab < notetools::stepLevelCount; ++tab)
  2233. if (levelButtonBounds (tab).contains (e.getPosition().translated (-stickyX, 0)))
  2234. {
  2235. graphLevel = (int) notetools::stepLevel (tab);
  2236. repaint();
  2237. }
  2238. return;
  2239. case Area::lane:
  2240. {
  2241. const auto step = juce::jlimit (0, numSteps() - 1, (e.x - gridLeft()) / stepWidth);
  2242. if (e.mods.isAltDown() || e.mods.isMiddleButtonDown())
  2243. {
  2244. setLaneValue (step, notetools::graphDefault (graphLevel));
  2245. return;
  2246. }
  2247. laneDownStep = step;
  2248. laneDownValue = laneValueAt (e.y);
  2249. laneScaleFrom.clear();
  2250. laneScaleBase = -1.0f;
  2251. if (e.mods.isCommandDown() && e.mods.isLeftButtonDown()) // round 190lx (DE-31)
  2252. {
  2253. const auto& p = engine.getSong().activePattern();
  2254. const auto level = graphLevel;
  2255. for (int st = 0; st < numSteps(); ++st)
  2256. for (auto i : notesInStep (p, graphRow(), st))
  2257. {
  2258. const auto v = notetools::graphValue (p.notes[(std::size_t) i], level, ticksPerStep);
  2259. if (st == step && laneScaleBase < 0.0f)
  2260. laneScaleBase = v;
  2261. laneScaleFrom.push_back ({ (std::size_t) i, v });
  2262. }
  2263. if (laneScaleBase < 0.0f) // (an empty column: nothing to scale from)
  2264. laneScaleFrom.clear();
  2265. else
  2266. return; // (the drag scales)
  2267. }
  2268. applyLane (e);
  2269. return;
  2270. }
  2271. case Area::mute:
  2272. if (flmods::popup (e.mods) || flmods::ctrl (e.mods))
  2273. solo (row);
  2274. else if (e.mods.isShiftDown()) // round 190ih (DE-11), FL's manual: "Lock state - (Shift+Click) a Mute icon"
  2275. toggleSoloLock (row);
  2276. else if (e.mods.isAltDown()) // round 190mv (DE-12): FL's Alt+click = the solo within the channel's filter group
  2277. engine.editSong ([row] (Song& s) { noteedit::soloChannelInGroup (s, row); });
  2278. else
  2279. toggleMute (row);
  2280. return;
  2281. case Area::pan:
  2282. case Area::volume:
  2283. case Area::route:
  2284. {
  2285. const auto spec = specFor (area, row);
  2286. if (flmods::popup (e.mods))
  2287. {
  2288. juce::PopupMenu menu;
  2289. menu.addSectionHeader (spec.name + ": " + spec.text (valueOf (area, row)));
  2290. ui::addValueItems (menu, *this, &engine, spec, valueOf (area, row), [this, area, row] (double v) { setValue (area, row, v); });
  2291. if (area == Area::route)
  2292. {
  2293. menu.addSeparator();
  2294. menu.addItem (tr ("Next free insert"), [this, row] {
  2295. engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.claimFreeInsert(); });
  2296. });
  2297. menu.addItem (tr ("Send the selected channels to free inserts") + "\tCtrl+L", [this] { sendSelectedToInserts(); });
  2298. if (onOpenMixerInsert)
  2299. menu.addItem (tr ("Show this insert in the Mixer"), [this, row] {
  2300. if (row < (int) engine.getSong().channels.size())
  2301. onOpenMixerInsert (engine.getSong().channels[(std::size_t) row].mixerTrack);
  2302. });
  2303. }
  2304. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  2305. return;
  2306. }
  2307. if (ui::isResetClick (e))
  2308. {
  2309. setValue (area, row, spec.defaultValue);
  2310. showTip (area, row, true);
  2311. return;
  2312. }
  2313. knobDrag = area;
  2314. knobRow = row;
  2315. drag.begin (e, valueOf (area, row));
  2316. showTip (area, row, false);
  2317. repaint();
  2318. return;
  2319. }
  2320. case Area::name:
  2321. if (flmods::popup (e.mods))
  2322. {
  2323. if (! std::binary_search (engine.getSelectedChannels().begin(), engine.getSelectedChannels().end(), row))
  2324. clickSelector (row, {});
  2325. else
  2326. selectRows (engine.getSelectedChannels(), row);
  2327. showChannelMenu (row);
  2328. return;
  2329. }
  2330. if (e.mods.isShiftDown())
  2331. {
  2332. clickSelector (row, e.mods);
  2333. return;
  2334. }
  2335. if (! std::binary_search (engine.getSelectedChannels().begin(), engine.getSelectedChannels().end(), row)
  2336. || engine.getSelectedChannels().size() > 1)
  2337. {
  2338. selectionAnchor = row;
  2339. selectRows ({ row }, row);
  2340. }
  2341. if (flmods::ctrl (e.mods)) // audition without opening anything
  2342. {
  2343. previews.press (row, 60, 0.8f); // stops on release
  2344. return;
  2345. }
  2346. // the editor opens on release, unless the button is dragged (that reorders the channels)
  2347. nameDownRow = row;
  2348. nameDragged = false;
  2349. nameDownAlt = e.mods.isAltDown();
  2350. nameDragY = e.position.y;
  2351. return;
  2352. case Area::select:
  2353. clickSelector (row, e.mods);
  2354. if (flmods::popup (e.mods)) // right-drag adds the rows it crosses
  2355. {
  2356. selectDownRow = row;
  2357. selectDragAdds = true;
  2358. selectDragBase = engine.getSelectedChannels();
  2359. }
  2360. else if (! e.mods.isShiftDown() && ! flmods::ctrl (e.mods)) // left-drag selects a range
  2361. {
  2362. selectDownRow = row;
  2363. selectDragAdds = false;
  2364. }
  2365. return;
  2366. case Area::grid:
  2367. {
  2368. auto step = (e.x - gridLeft()) / stepWidth;
  2369. if (const auto loop = loopStepsOf (row); loop > 0 && step >= loop && step < numSteps())
  2370. step %= loop; // a loop repeat edits the step it repeats
  2371. const auto& pattern = song.activePattern();
  2372. // Freeze (round 190ay): a frozen channel's steps cannot be edited; the miniature still opens the Piano Roll (read-only)
  2373. if (! showsNotePreview (pattern, row, pianoRollView) && refuseFrozenEdit (row))
  2374. return;
  2375. if (showsNotePreview (pattern, row, pianoRollView)) // the miniature opens the Piano Roll (steps would break the melody)
  2376. {
  2377. // round 190mk (DE-33, FL's manual "Channel Rack › Piano roll preview"): "Left-click the preview to show/hide
  2378. // the Piano roll window"; "Right-Click to open the Piano roll centered on the notes clicked"
  2379. if (previewMuteButton && ! flmods::popup (e.mods) && previewMuteBounds (row).contains (e.getPosition()))
  2380. {
  2381. toggleMute (row); // (FL: the preview's mute button; the preview stays)
  2382. repaint (rowBounds (row));
  2383. return;
  2384. }
  2385. const auto wasPrimary = engine.getSelectedChannel() == row;
  2386. selectionAnchor = row;
  2387. selectRows ({ row }, row);
  2388. if (flmods::popup (e.mods))
  2389. {
  2390. const auto span = previewSpan();
  2391. const auto at = juce::jlimit (0.0, 1.0, (double) (e.x - gridLeft() - span.x) / (double) juce::jmax (1, span.width));
  2392. if (onPreviewRightClick)
  2393. onPreviewRightClick (row, at * (double) numSteps() * (double) ticksPerStep);
  2394. }
  2395. else if (onPreviewClick)
  2396. onPreviewClick (wasPrimary ? row : -1); // (-1: another channel's preview: show its notes, never hide)
  2397. else if (onOpenPianoRoll)
  2398. onOpenPianoRoll();
  2399. return;
  2400. }
  2401. // FL's General › Input › "Use both mouse buttons in step sequencer" (round 190mr, DE-16 / owner E2; off as in FL,
  2402. // capture 190mr A1.1): on, the left button only switches steps on, the right one off; off, the left toggles
  2403. if (e.mods.isRightButtonDown())
  2404. paintMode = PaintMode::remove;
  2405. else
  2406. paintMode = pattern.hasStep (row, step) && ! flsettings::options().bothButtonsSteps.load() ? PaintMode::remove : PaintMode::add;
  2407. lastPaintedRow = lastPaintedStep = -1;
  2408. applyPaint (e);
  2409. return;
  2410. }
  2411. }
  2412. }
  2413. void ChannelRackComponent::mouseDrag (const juce::MouseEvent& e)
  2414. {
  2415. if (loopDragRow >= 0)
  2416. {
  2417. const auto steps = juce::jlimit (1, numSteps(), loopDragStart + juce::roundToInt ((loopDragY - e.position.y) / 6.0f));
  2418. const auto& p = engine.getSong().activePattern();
  2419. if (channelloops::storedSetting (p, loopDragRow) != steps)
  2420. setLoopPoint (loopDragRow, steps);
  2421. return;
  2422. }
  2423. if (knobDrag != Area::none && drag.isActive())
  2424. {
  2425. setValue (knobDrag, knobRow, drag.update (e, specFor (knobDrag, knobRow)));
  2426. showTip (knobDrag, knobRow, false);
  2427. return;
  2428. }
  2429. if (laneDownStep >= 0)
  2430. {
  2431. applyLane (e);
  2432. hoverStep = juce::jlimit (0, numSteps() - 1, (e.x - gridLeft()) / stepWidth);
  2433. hoverArea = Area::lane;
  2434. const auto v = laneValueAt (e.y);
  2435. const auto bipolar = notetools::graphIsBipolar (graphLevel);
  2436. const auto shown = juce::roundToInt ((bipolar ? v * 2.0f - 1.0f : v) * 100.0f);
  2437. // Note pitch, Shift and Repeat show FL's own values (a key, the part of a step, the repeat rate: issue #208)
  2438. const auto own = notetools::levelText ((notetools::Level) graphLevel, v);
  2439. auto ownText = tr (juce::String::fromUTF8 (own.c_str())); // ("No repeat"; FL's rate names keep their notation)
  2440. for (const char* word : { TR_NOOP ("dotted"), TR_NOOP ("triplet"), TR_NOOP ("Beat"), TR_NOOP ("Half step"), TR_NOOP ("Quarter step") })
  2441. ownText = ownText.replace (word, tr (word));
  2442. ownText = ownText.replace ("(Step)", "(" + tr ("Step") + ")");
  2443. const auto valueText = ! own.empty() ? ownText : (bipolar && shown > 0 ? "+" : "") + juce::String (shown) + "%";
  2444. const auto levelText = tr (notetools::levelName ((notetools::Level) graphLevel));
  2445. ui::showValueTip (*this, controlBounds (Area::lane, graphRow()), levelText + ": " + valueText);
  2446. ui::Hint live;
  2447. live.text = channelName (engine.getSong(), graphRow()) + " - " + levelText;
  2448. live.value = valueText;
  2449. ui::postHint (live);
  2450. repaint (laneBounds());
  2451. return;
  2452. }
  2453. if (nameDownRow >= 0)
  2454. {
  2455. if (! nameDragged && std::abs (e.position.y - nameDragY) < 4.0f)
  2456. return;
  2457. nameDragged = true;
  2458. setMouseCursor (juce::MouseCursor::UpDownResizeCursor);
  2459. const auto count = (int) engine.getSong().channels.size();
  2460. const auto target = juce::jlimit (0, count - 1, rowAtY (e.y));
  2461. // move the selection one row at a time until the grabbed channel is under the mouse
  2462. for (int guard = 0; guard < count && engine.getSelectedChannel() != target; ++guard)
  2463. if (! moveSelected (target < engine.getSelectedChannel() ? -1 : 1))
  2464. break;
  2465. return;
  2466. }
  2467. if (selectDownRow >= 0)
  2468. {
  2469. const auto count = (int) engine.getSong().channels.size();
  2470. const auto row = juce::jlimit (0, juce::jmax (0, count - 1), rowAtY (e.y));
  2471. std::vector<int> rows;
  2472. for (int r = juce::jmin (selectDownRow, row); r <= juce::jmax (selectDownRow, row); ++r)
  2473. rows.push_back (r);
  2474. if (selectDragAdds)
  2475. {
  2476. rows.insert (rows.end(), selectDragBase.begin(), selectDragBase.end());
  2477. std::sort (rows.begin(), rows.end());
  2478. rows.erase (std::unique (rows.begin(), rows.end()), rows.end());
  2479. }
  2480. else
  2481. selectionAnchor = selectDownRow;
  2482. if (rows != engine.getSelectedChannels())
  2483. selectRows (rows, row);
  2484. return;
  2485. }
  2486. if (paintMode != PaintMode::none)
  2487. applyPaint (e);
  2488. }
  2489. void ChannelRackComponent::mouseUp (const juce::MouseEvent& e)
  2490. {
  2491. const AudioEngine::UndoGesture undoGesture (engine, AudioEngine::UndoGesture::end); // (closes when mouseUp returns)
  2492. previews.releaseAll(); // the audition (Ctrl+click) and the steps' previews stop with the button
  2493. if (nameDownRow >= 0 && ! nameDragged && e.mouseWasClicked())
  2494. toggleEditor (nameDownRow, nameDownAlt);
  2495. if (nameDragged)
  2496. setMouseCursor (juce::MouseCursor::NormalCursor);
  2497. nameDownRow = -1;
  2498. nameDragged = false;
  2499. if (loopDragRow >= 0)
  2500. {
  2501. loopDragRow = -1;
  2502. repaint();
  2503. }
  2504. selectDownRow = -1;
  2505. selectDragBase.clear();
  2506. ui::hideValueTip();
  2507. ui::clearPostedHint();
  2508. paintMode = PaintMode::none;
  2509. laneDownStep = -1;
  2510. if (laneScaleBase >= 0.0f && graphLevel == notetools::graphShift) // (rollup step 11: a Ctrl-scaled Shift moved notes in their
  2511. engine.editSong ([] (Song& s) { s.activePattern().sortNotes(); }); // steps; sorted once the drag ends, as round 190ma's lane does)
  2512. laneScaleBase = -1.0f; // (round 190lx)
  2513. laneScaleFrom.clear();
  2514. if (knobDrag != Area::none)
  2515. repaint();
  2516. knobDrag = Area::none;
  2517. drag.end();
  2518. }
  2519. void ChannelRackComponent::mouseDoubleClick (const juce::MouseEvent& e)
  2520. {
  2521. int row = 0;
  2522. const auto area = areaAt (e.x, e.y, row);
  2523. if (area == Area::pan || area == Area::volume)
  2524. {
  2525. setValue (area, row, specFor (area, row).defaultValue);
  2526. showTip (area, row, true);
  2527. }
  2528. else if (area == Area::route)
  2529. {
  2530. if (onOpenMixerInsert)
  2531. onOpenMixerInsert (engine.getSong().channels[(std::size_t) row].mixerTrack);
  2532. else
  2533. engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.claimFreeInsert(); });
  2534. }
  2535. else if (area == Area::loop && ! flmods::popup (e.mods))
  2536. setLoopPoint (row, ChannelLoop::nextBar);
  2537. else if (area == Area::select && ! flmods::popup (e.mods))
  2538. selectAll();
  2539. // a double-click on a channel's name is two clicks, as in FL: its window opens and closes again (FL 2026, checked
  2540. // round 165q F7). It no longer opens the Piano Roll — the hint promised that, but FL's channel button does not.
  2541. }
  2542. void ChannelRackComponent::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  2543. {
  2544. int row = 0;
  2545. const auto area = areaAt (e.x, e.y, row);
  2546. if (area == Area::pan || area == Area::volume || area == Area::route)
  2547. {
  2548. if (const auto v = ui::wheelValue (valueOf (area, row), e, wheel, specFor (area, row)))
  2549. {
  2550. setValue (area, row, *v);
  2551. showTip (area, row, true);
  2552. }
  2553. return;
  2554. }
  2555. if (area == Area::loop) // the loop point, a step at a time
  2556. {
  2557. const auto& song = engine.getSong();
  2558. const auto& p = song.activePattern();
  2559. const auto setting = channelloops::effectiveSetting (p, row);
  2560. const auto steps = setting > 0 ? setting : loopStepsOf (row) > 0 ? loopStepsOf (row) : numSteps();
  2561. const auto delta = (wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? 1 : -1;
  2562. setLoopPoint (row, juce::jlimit (1, numSteps(), steps + delta));
  2563. return;
  2564. }
  2565. if ((area == Area::name || area == Area::select) && e.mods.isShiftDown()) // Shift+wheel moves the channel
  2566. {
  2567. const auto delta = (wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1;
  2568. selectRows ({ row }, row);
  2569. moveSelected (delta);
  2570. return;
  2571. }
  2572. juce::Component::mouseWheelMove (e, wheel); // scroll the viewport
  2573. }
  2574. ui::Hint ChannelRackComponent::hintAt (juce::Point<int> p)
  2575. {
  2576. int row = 0;
  2577. const auto area = areaAt (p.x, p.y, row);
  2578. const auto& song = engine.getSong();
  2579. ui::Hint h;
  2580. if (row >= 0 && row < (int) song.channels.size() && (area == Area::pan || area == Area::volume || area == Area::route))
  2581. {
  2582. const auto active = knobDrag != Area::none && drag.isActive();
  2583. const auto shownArea = active ? knobDrag : area;
  2584. const auto shownRow = active ? knobRow : row;
  2585. h = ui::valueHint (specFor (shownArea, shownRow), valueOf (shownArea, shownRow),
  2586. shownArea == Area::route ? tr ("[LMB] drag or [Wheel] routes [2xLMB] show in the Mixer [RMB] next free insert")
  2587. : ui::valueGestures());
  2588. return h;
  2589. }
  2590. const auto name = channelName (song, row);
  2591. switch (area)
  2592. {
  2593. case Area::mute:
  2594. h.text = name + " - " + tr ("Click: mute. Ctrl+click or right-click: solo. Alt+click: solo within its filter group. Shift+click: lock the mute state");
  2595. break;
  2596. case Area::name:
  2597. h.text = name + " - " + tr ("[LMB] open/close the editor [Alt]+[LMB] keep others open [Ctrl]+[LMB] audition [LMB] drag up/down reorders [RMB] channel menu");
  2598. // sample channels: where the pitch is (round 163, "getting to the sample pitching")
  2599. if (row >= 0 && row < (int) song.channels.size()
  2600. && (song.channels[(std::size_t) row].instrument == InstrumentKind::sampler || song.channels[(std::size_t) row].instrument == InstrumentKind::audioClip))
  2601. h.text << " " << tr ("(pitch: [RMB] › Pitch...)");
  2602. if (row >= 0 && row < (int) song.channels.size() && ! song.channels[(std::size_t) row].receiveFrom.empty())
  2603. h.text << " " << tr ("(locked: receives the notes of {0})", { juce::String::fromUTF8 (song.channels[(std::size_t) row].receiveFrom.c_str()) });
  2604. if (engine.playsStandIn (row))
  2605. h.text = name + " - " + tr ("Stand-in: its plugin is not installed on this PC, a render of this part plays (the plugin's settings are kept)");
  2606. else if (engine.channelPluginOff (row)) // round 190ap
  2607. h.text = name + " - " + tr ("Plugin switched off: the project was opened without third-party plugins (its settings are kept; File › Switch plugins back on...)");
  2608. // Freeze (round 190ay): what a frozen channel is, and what to do when its audio needs attention
  2609. if (row >= 0 && row < (int) song.channels.size() && song.channels[(std::size_t) row].freeze.on())
  2610. h.text = name + " - " + frozenHint (row);
  2611. h.hasMenu = true;
  2612. // FL's hint bar names what the channel is ("Audio Clip", the plugin's name) on the right (REA round 143b)
  2613. if (row >= 0 && row < (int) song.channels.size())
  2614. {
  2615. const auto& c = song.channels[(std::size_t) row];
  2616. h.value = c.instrument == InstrumentKind::plugin && ! c.plugin.name.empty() ? juce::String::fromUTF8 (c.plugin.name.c_str())
  2617. : instrumentLabel (c.instrument);
  2618. }
  2619. break;
  2620. case Area::select:
  2621. 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");
  2622. break;
  2623. case Area::grid:
  2624. if (! song.patterns.empty() && showsNotePreview (song.activePattern(), row, pianoRollView))
  2625. h.text = name + " - " + (previewMuteButton && previewMuteBounds (row).contains (p) ? tr ("Mute / unmute the channel")
  2626. : tr ("Piano roll preview: [LMB] show / hide the Piano Roll [RMB] open it centred on the notes clicked"));
  2627. else
  2628. h.text = name + " - " + (hoverStep >= numSteps() ? tr ("After the pattern's end: [LMB] adds a step and makes the pattern longer")
  2629. : loopStepsOf (row) > 0 && hoverStep >= loopStepsOf (row)
  2630. ? tr ("Loop repeat (after the loop point): [LMB] edits the step it repeats")
  2631. : tr ("Steps: [LMB] drag draws (starting on a lit step erases) [RMB] drag erases"));
  2632. // and the step's position as bar:step:tick, like FL's hint bar
  2633. if (hoverStep >= 0)
  2634. h.value = juce::String (stepPositionText ((std::int64_t) hoverStep * ticksPerStep, song.meter()));
  2635. break;
  2636. case Area::addRow:
  2637. h.text = tr ("Add a channel (or drop audio files on the rack)");
  2638. break;
  2639. case Area::zipped: // round 190mk: FL's hint is the channel's name, its type on the right
  2640. h.text = name + " - " + tr ("Zipped channel: [RMB] unzip (Alt+U unzips all)");
  2641. if (row >= 0 && row < (int) song.channels.size())
  2642. {
  2643. const auto& c = song.channels[(std::size_t) row];
  2644. h.value = c.instrument == InstrumentKind::plugin && ! c.plugin.name.empty() ? juce::String::fromUTF8 (c.plugin.name.c_str())
  2645. : instrumentLabel (c.instrument);
  2646. }
  2647. break;
  2648. case Area::loop:
  2649. h.text = name + " - " + tr ("Loop point: [LMB] drag or [Wheel] sets it in steps [2xLMB] next bar [RMB] Don't loop / Step / Beat / Bar, Burn to pattern");
  2650. h.hasMenu = true;
  2651. if (row >= 0 && row < (int) song.channels.size() && ! song.patterns.empty())
  2652. {
  2653. const auto setting = channelloops::effectiveSetting (song.activePattern(), row);
  2654. h.value = setting == ChannelLoop::dontLoop ? tr ("Don't loop") : setting > 0 ? tr ("Every {0} steps", { juce::String (setting) })
  2655. : tr (channelloops::settingText (setting).c_str());
  2656. }
  2657. break;
  2658. case Area::laneLevels:
  2659. h.text = tr ("Graph lane: choose what the bars edit (Ctrl+K hides the lane)");
  2660. break;
  2661. case Area::lane:
  2662. h.text = name + " - " + tr ("Graph lane: drag sets the steps, Ctrl+drag scales them all, right-drag draws a ramp, Alt+click resets");
  2663. break;
  2664. default:
  2665. break;
  2666. }
  2667. return h;
  2668. }
  2669. void ChannelRackComponent::applyPaint (const juce::MouseEvent& e)
  2670. {
  2671. const auto& song = engine.getSong();
  2672. const auto row = rowAtY (e.y);
  2673. auto step = (e.x - gridLeft()) / stepWidth;
  2674. if (const auto loop = loopStepsOf (row); loop > 0 && step >= loop && step < numSteps())
  2675. step %= loop; // a loop repeat edits the step it repeats
  2676. if (laneBounds().contains (e.getPosition()) || (advancedLoopShown() && e.x >= loopBoxBounds (0).getX() - 3))
  2677. return;
  2678. if (row < 0 || row >= (int) song.channels.size() || step < 0 || step >= shownSteps() || e.x < stickyX + gridLeft()
  2679. || showsNotePreview (song.activePattern(), row, pianoRollView)) // drag-paint skips note rows
  2680. return;
  2681. if (row == lastPaintedRow && step == lastPaintedStep)
  2682. return;
  2683. lastPaintedRow = row;
  2684. lastPaintedStep = step;
  2685. const auto on = paintMode == PaintMode::add;
  2686. // FL: setting a step is silent, stopped or playing (issue #11; Ctrl+click on the channel button auditions it)
  2687. engine.editSong ([row, step, on, mute = muteRemovedSteps] (Song& s) {
  2688. auto& p = s.activePattern();
  2689. setStep (p, row, step, on, mute);
  2690. // a ghost step of a pattern with a set length: the length grows to the bar holding it (automatic lengths
  2691. // follow the notes by themselves)
  2692. if (on)
  2693. p.lengthTicks = lengthWithStep (p.lengthTicks, step, ticksPerStep, s.barTicks());
  2694. });
  2695. }
  2696. // --- menus -------------------------------------------------------------------------------------
  2697. // Round 190in (details190if R8): the channel's items of FL's wrapper ▸ menu in the channel and plugin windows, done the
  2698. // way this rack's own channel menu does them
  2699. void ChannelRackComponent::fillWindowMenu (int channel, wrappermenu::Spec& spec)
  2700. {
  2701. if (channel < 0 || channel >= (int) engine.getSong().channels.size())
  2702. return;
  2703. const juce::Component::SafePointer<ChannelRackComponent> safe (this);
  2704. spec.pianoRoll = [safe, channel] {
  2705. if (safe == nullptr || channel >= (int) safe->engine.getSong().channels.size())
  2706. return;
  2707. safe->selectionAnchor = channel;
  2708. safe->selectRows ({ channel }, channel);
  2709. if (safe->onOpenPianoRoll)
  2710. safe->onOpenPianoRoll();
  2711. };
  2712. spec.replace = [safe, channel] (juce::PopupMenu& m) {
  2713. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size())
  2714. m = safe->replaceMenu (channel);
  2715. };
  2716. if (engine.getSong().channels.size() > 1)
  2717. spec.remove = [safe, channel] {
  2718. if (safe == nullptr || channel >= (int) safe->engine.getSong().channels.size() || safe->engine.getSong().channels.size() < 2)
  2719. return;
  2720. safe->closeEditor (channel);
  2721. safe->engine.editSong ([channel] (Song& s) { s.removeChannel (channel); });
  2722. safe->openEditorRow = -1;
  2723. };
  2724. spec.rename = [safe, channel] {
  2725. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size())
  2726. safe->rename (channel);
  2727. };
  2728. // FL's Channel column (round 190ld, DF-118): "Save channel state as..." writes the channel as the project stores it
  2729. // (ProjectFile::channelStateBytes, LYNX's .lynxchannel); "Browse channel states" opens one into this channel (its
  2730. // mixer track stays). FL's own .fst states are read by the Browser.
  2731. spec.saveChannelState = [safe, channel] {
  2732. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size())
  2733. safe->saveChannelState (channel);
  2734. };
  2735. spec.browseChannelStates = [safe, channel] {
  2736. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size())
  2737. safe->openChannelState (channel);
  2738. };
  2739. spec.changeColour = [safe, channel] (juce::PopupMenu& m) {
  2740. if (safe == nullptr || channel >= (int) safe->engine.getSong().channels.size())
  2741. return;
  2742. m = ui::colourMenu (safe->engine.getSong().channels[(std::size_t) channel].colour, [safe, channel] (std::uint32_t colour) {
  2743. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size()) // (issue #201: every selected channel)
  2744. safe->engine.editSong ([targets = selrename::targets (channel, safe->engine.getSelectedChannels()), colour] (Song& s) {
  2745. selrename::colourChannels (s, targets, colour);
  2746. });
  2747. }, juce::Rectangle<int> (juce::Desktop::getMousePosition().x, juce::Desktop::getMousePosition().y, 1, 1));
  2748. };
  2749. spec.randomColour = [safe, channel] {
  2750. if (safe == nullptr || channel >= (int) safe->engine.getSong().channels.size())
  2751. return;
  2752. const auto& palette = lynxPalette();
  2753. const auto colour = palette[(std::size_t) juce::Random::getSystemRandom().nextInt ((int) palette.size())];
  2754. safe->engine.editSong ([targets = selrename::targets (channel, safe->engine.getSelectedChannels()), colour] (Song& s) {
  2755. selrename::colourChannels (s, targets, colour); // (issue #201)
  2756. });
  2757. };
  2758. spec.changeIcon = [safe, channel] {
  2759. if (safe == nullptr || channel >= (int) safe->engine.getSong().channels.size())
  2760. return;
  2761. ui::showTrackIconPicker (juce::Rectangle<int> (juce::Desktop::getMousePosition().x, juce::Desktop::getMousePosition().y, 1, 1),
  2762. safe->engine.getSong().channels[(std::size_t) channel].icon, [safe, channel] (int icon) {
  2763. if (safe != nullptr && channel < (int) safe->engine.getSong().channels.size())
  2764. safe->engine.editSong ([channel, icon] (Song& s) { s.channels[(std::size_t) channel].icon = icon; });
  2765. });
  2766. };
  2767. }
  2768. // Replace › lists exactly what Insert › and "+" list (issue #18: one source, quickpick::addGeneratorMenu); the channel
  2769. // keeps its place, notes, insert, levels and colour (FL; core replaceGenerator). (Round 190in: also the wrapper menu's.)
  2770. juce::PopupMenu ChannelRackComponent::replaceMenu (int channel)
  2771. {
  2772. const auto& c = engine.getSong().channels[(std::size_t) channel];
  2773. juce::PopupMenu instruments;
  2774. quickpick::addGeneratorMenu (instruments, engine, [this, channel] (ChannelDef generator) {
  2775. if (channel >= (int) engine.getSong().channels.size())
  2776. return;
  2777. closeEditor (channel);
  2778. const auto kind = generator.instrument;
  2779. engine.editSong ([channel, &generator] (Song& s) { replaceGenerator (s, channel, std::move (generator)); });
  2780. selectionAnchor = channel;
  2781. selectRows ({ channel }, channel);
  2782. openNewGenerator (channel, kind);
  2783. }, [this, channel] { chooseSample (channel); }, &c, this);
  2784. return instruments;
  2785. }
  2786. juce::PopupMenu ChannelRackComponent::buildChannelMenu (int channel)
  2787. {
  2788. const auto& c = engine.getSong().channels[(std::size_t) channel];
  2789. auto instruments = replaceMenu (channel);
  2790. // FL's layout (REA round 104): two columns, the channel on the left (editors, name and colour, sample,
  2791. // insert / replace / clone / delete) and its steps on the right (clipboard, fills, rotation); LYNX's own
  2792. // items (settings windows, arpeggiator, organisation) follow in their place
  2793. juce::PopupMenu menu;
  2794. ui::addItemHeader (menu, tr ("Channel"), juce::String::fromUTF8 (c.name.c_str()), c.colour != 0 ? juce::Colour (c.colour) : juce::Colour());
  2795. const auto selected = (int) engine.getSelectedChannels().size();
  2796. const auto count = (int) engine.getSong().channels.size();
  2797. menu.addItem (tr ("Piano Roll") + "\tF7", [this] {
  2798. if (onOpenPianoRoll)
  2799. onOpenPianoRoll();
  2800. });
  2801. menu.addItem (tr ("Graph editor") + "\tCtrl+K", true, graphLanes, [this] { toggleGraphLanes(); });
  2802. menu.addSeparator();
  2803. menu.addItem (theme::mnemonic (tr ("Rename, color and icon..."), 'R') + "\tF2", [this, channel] { rename (channel); }); // (FL's text, round 190ng)
  2804. menu.addSubMenu (tr ("Change color"), ui::colourMenu (c.colour, [this, channel] (std::uint32_t colour) {
  2805. if (channel < (int) engine.getSong().channels.size())
  2806. engine.editSong ([channel, colour] (Song& s) { s.channels[(std::size_t) channel].colour = colour; });
  2807. }, juce::Rectangle<int> (juce::Desktop::getMousePosition().x, juce::Desktop::getMousePosition().y, 1, 1)));
  2808. menu.addItem (tr ("Random color"), [this, channel] {
  2809. const auto& palette = lynxPalette();
  2810. const auto colour = palette[(std::size_t) juce::Random::getSystemRandom().nextInt ((int) palette.size())];
  2811. engine.editSong ([channel, colour] (Song& s) { s.channels[(std::size_t) channel].colour = colour; });
  2812. });
  2813. // FL's "Change icon" (SetPlugIconMenuClick, REA round 168h); LYNX's own icon set
  2814. menu.addItem (tr ("Change icon..."), [this, channel] {
  2815. if (channel >= (int) engine.getSong().channels.size())
  2816. return;
  2817. ui::showTrackIconPicker (juce::Rectangle<int> (juce::Desktop::getMousePosition().x, juce::Desktop::getMousePosition().y, 1, 1), engine.getSong().channels[(std::size_t) channel].icon, [this, channel] (int icon) {
  2818. if (channel < (int) engine.getSong().channels.size())
  2819. engine.editSong ([channel, icon] (Song& s) { s.channels[(std::size_t) channel].icon = icon; });
  2820. });
  2821. });
  2822. // FL's "Lock active/mute state" (the mute switch's menu, round 167l): soloing another channel leaves this one alone
  2823. menu.addItem (tr ("Lock active/mute state") + "\t" + tr ("Shift+click"), true, c.soloLock, [this, channel] { toggleSoloLock (channel); });
  2824. menu.addSeparator();
  2825. menu.addItem (theme::mnemonic (tr ("Load sample..."), 'L'), c.instrument != InstrumentKind::audioClip, false, [this, channel] { chooseSample (channel); });
  2826. {
  2827. // "Cut itself": a new note cuts the channel's own sounding note (its cut group and "cut by" are its own)
  2828. // (issue #68: the same state as the sample panel's checkbox and the Misc page's "Cut self")
  2829. const auto cutsItself = channelCutsItself (c);
  2830. menu.addItem (theme::mnemonic (tr ("Cut itself"), 't'), true, cutsItself, [this, channel, cutsItself] {
  2831. engine.editSong ([channel, cutsItself] (Song& s) {
  2832. if (channel < (int) s.channels.size())
  2833. setChannelCutsItself (s.channels[(std::size_t) channel], channel, ! cutsItself);
  2834. });
  2835. });
  2836. }
  2837. menu.addSeparator();
  2838. {
  2839. juce::PopupMenu insert;
  2840. addInstrumentItems (insert, channel);
  2841. menu.addSubMenu (theme::mnemonic (tr ("Insert"), 'I'), insert);
  2842. }
  2843. menu.addSubMenu (tr ("Replace"), instruments);
  2844. // FL's Patcherize (round 165m): the channel's instrument into a new Patcher, same sound
  2845. menu.addItem (tr ("Patcherize"), selected <= 1 && patcherize::canPatcherizeChannel (engine.getSong(), channel), false, [this, channel] {
  2846. engine.capturePluginStates(); // (the plugin's current settings go with it)
  2847. closeEditor (channel);
  2848. engine.editSong ([channel] (Song& s) { patcherize::patcherizeChannel (s, channel); });
  2849. });
  2850. // Freeze (round 190ay; LYNX's own, FL has none): the channel's sound rendered to audio that plays in its place while
  2851. // its instrument / plugin is unloaded; the clicked channel, or every selected one when it is among them
  2852. {
  2853. const auto& song = engine.getSong();
  2854. std::vector<std::uint64_t> targets;
  2855. const auto& rows = engine.getSelectedChannels();
  2856. if (selected > 1 && std::binary_search (rows.begin(), rows.end(), channel))
  2857. {
  2858. for (const auto r : rows)
  2859. if (r >= 0 && r < count && song.channels[(std::size_t) r].freeze.on() == c.freeze.on())
  2860. targets.push_back (song.channels[(std::size_t) r].uid);
  2861. }
  2862. else
  2863. targets.push_back (c.uid);
  2864. const auto several = targets.size() > 1;
  2865. if (! c.freeze.on())
  2866. menu.addItem (several ? tr ("Freeze selected channels") : trc ("@channel: Freeze"), [this, targets] { freezeChannels (targets); });
  2867. else
  2868. {
  2869. menu.addItem (several ? tr ("Unfreeze selected channels") : tr ("Unfreeze"), [this, targets] {
  2870. for (const auto uid : targets)
  2871. {
  2872. const auto& channels = engine.getSong().channels;
  2873. const auto it = std::find_if (channels.begin(), channels.end(), [uid] (const ChannelDef& d) { return d.uid == uid && uid != 0; });
  2874. if (it != channels.end())
  2875. unfreezeChannel (engine, (int) (it - channels.begin()));
  2876. }
  2877. repaint();
  2878. });
  2879. menu.addItem (several ? tr ("Re-freeze selected channels") : tr ("Re-freeze"), [this, targets] { freezeChannels (targets); });
  2880. }
  2881. }
  2882. // FL's Loop Starter channel menu (round 168b): Replace sample, Generate steps, Locked, One-shot category
  2883. loopstarterui::addChannelMenu (menu, engine, channel);
  2884. menu.addItem (selected > 1 ? theme::mnemonic (tr ("Clone {0} channels", { juce::String (selected) }), 'C') : theme::mnemonic (tr ("Clone"), 'C') + "\tAlt+C", [this] { cloneSelected(); });
  2885. if (selected > 1)
  2886. menu.addItem (theme::mnemonic (tr ("Delete {0} channels", { juce::String (selected) }), 'D') + "\tAlt+Del", selected < count, false, [this] { deleteSelected(); });
  2887. else
  2888. menu.addItem (theme::mnemonic (tr ("Delete..."), 'D') + "\tAlt+Del", count > 1, false, [this, channel] {
  2889. closeEditor (channel);
  2890. engine.editSong ([channel] (Song& s) { s.removeChannel (channel); });
  2891. openEditorRow = -1;
  2892. });
  2893. // FL's "Assign to new instrument track" after a line, the last item of the left column (LineAssignInstrTrackMenu,
  2894. // round 190dx; ChannelMenuPopup FUN_011f4450: "Unassign from instrument track" while the channel has one; enabled
  2895. // only for a generator that can have one)
  2896. menu.addSeparator();
  2897. if (const auto& song = engine.getSong(); trackmodes::instrumentTracksOf (song, channel).empty())
  2898. menu.addItem (theme::mnemonic (tr ("Assign to new instrument track"), 'A'), trackmodes::canBeInstrumentTrack (song, channel), false, [this, channel] {
  2899. int track = -1;
  2900. engine.editSong ([channel, &track] (Song& s) { track = trackmodes::assignToNewInstrumentTrack (s, channel); });
  2901. if (track < 0)
  2902. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Can't create new instrument track"),
  2903. tr ("There are no available mixer tracks."));
  2904. });
  2905. else
  2906. menu.addItem (theme::mnemonic (tr ("Unassign from instrument track"), 'U'), [this, channel] {
  2907. engine.editSong ([channel] (Song& s) { trackmodes::unassignFromInstrumentTracks (s, channel); });
  2908. });
  2909. // LYNX: the channel's settings windows and its arpeggiator
  2910. menu.addSeparator();
  2911. if (c.instrument == InstrumentKind::sampler)
  2912. {
  2913. menu.addItem (tr ("Sampler settings (pitch, envelope, reverse)..."),
  2914. [this, channel] { openChannelSettings (engine, channel); });
  2915. menu.addItem (tr ("Pitch... (semitones, cents, keep length)"), ! c.samplePath.empty() || ! c.zones.empty(), false,
  2916. [this, channel] { showSamplePitchDialog (engine, channel); });
  2917. menu.addItem (tr ("Edit the sample in the audio editor..."), ! c.samplePath.empty(), false, [this, channel] { openAudioEditorForChannel (engine, channel); });
  2918. if (c.sampler.slices.empty())
  2919. menu.addItem (tr ("Slice sample (slicer): one slice per key and a pattern with the loop"), [this, channel] { sliceChannel (channel); });
  2920. else
  2921. menu.addItem (tr ("Undo slices ({0})", { juce::String ((int) c.sampler.slices.size()) }), [this, channel] {
  2922. engine.editSong ([channel] (Song& s) { s.channels[(std::size_t) channel].sampler.slices.clear(); });
  2923. });
  2924. // Live's Simpler › Slice to Drum Rack (round 190ec): the channel becomes a Drum Rack, one pad per slice from C1
  2925. menu.addItem (tr ("Slice to Drum Rack"), ! c.samplePath.empty() && c.zones.empty(), false, [this, channel] { sliceChannelToDrumRack (engine, channel); });
  2926. }
  2927. else if (c.instrument == InstrumentKind::audioClip)
  2928. menu.addItem (tr ("Pitch... (semitones, cents, keep length)"), ! c.samplePath.empty(), false,
  2929. [this, channel] { showSamplePitchDialog (engine, channel); });
  2930. else if (c.instrument == InstrumentKind::synth)
  2931. menu.addItem (tr ("Synth settings (oscillators, envelope, filter)..."), [this, channel] { openChannelSettings (engine, channel); });
  2932. else if (hasChannelSettings (c.instrument))
  2933. menu.addItem (tr ("Channel settings..."), [this, channel] { openChannelSettings (engine, channel); });
  2934. // FL's wrench tab of the channel window, for every channel (REA round 143b)
  2935. menu.addItem (tr ("Miscellaneous functions (polyphony, cut groups, arpeggiator)..."), [this, channel] { openChannelMisc (engine, channel); });
  2936. if (c.instrument == InstrumentKind::patcher) // its published parameters (round 171b)
  2937. addPatcherParameterItems (menu, engine, c.patcher, { AutomationTargetKind::channelParam, channel, 0, 0 });
  2938. if (c.instrument == InstrumentKind::plugin)
  2939. {
  2940. menu.addItem (tr ("Open plugin editor"), [this, channel] { openPluginWindow (channel); });
  2941. // round 190ao: only while the plugin is down (it crashed, or its separate process died)
  2942. if (engine.hostedPluginDown ({ channel, -1, -1 }))
  2943. menu.addItem (tr ("Reload crashed plugin"), [this, channel] { PluginWindows::reload (engine, { { channel, -1, -1 } }); });
  2944. auto* live = dynamic_cast<PluginInstrument*> (engine.getChannelInstrument (channel).get());
  2945. menu.addSubMenu (tr ("Processing"), pluginProcessingMenu (engine, c.plugin, live != nullptr ? &live->getInstance() : nullptr,
  2946. [channel] (Song& song) -> PluginRef* {
  2947. if (channel >= (int) song.channels.size())
  2948. return nullptr;
  2949. return &song.channels[(std::size_t) channel].plugin;
  2950. }));
  2951. if (live != nullptr)
  2952. addPluginParameterItems (menu, engine, live->getInstance(), { AutomationTargetKind::channelParam, channel, 0, 0 });
  2953. }
  2954. {
  2955. // channel arpeggiator
  2956. const auto arp = c.arp;
  2957. auto set = [this, channel] (std::function<void (ArpSettings&)> change) {
  2958. engine.editSong ([channel, change] (Song& s) { change (s.channels[(std::size_t) channel].arp); });
  2959. };
  2960. juce::PopupMenu arpMenu;
  2961. const char* directions[] = { TR_NOOP ("Off"), TR_NOOP ("Up"), TR_NOOP ("Down"), TR_NOOP ("Up and down"), TR_NOOP ("Random") };
  2962. for (int d = 0; d < 5; ++d)
  2963. arpMenu.addItem (tr (directions[d]), true, arp.direction == d, [set, d] { set ([d] (ArpSettings& a) { a.direction = d; }); });
  2964. arpMenu.addSeparator();
  2965. juce::PopupMenu range, speed, gate, repeat;
  2966. for (int o = 1; o <= 4; ++o)
  2967. 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; }); });
  2968. const std::pair<const char*, float> speeds[] = { { "1/4", 1.0f }, { "1/8", 0.5f }, { TR_NOOP ("1/8 triplet"), 1.0f / 3.0f },
  2969. { "1/16", 0.25f }, { TR_NOOP ("1/16 triplet"), 1.0f / 6.0f }, { "1/32", 0.125f } };
  2970. for (const auto& [name, beats] : speeds)
  2971. speed.addItem (tr (name), true, std::abs (arp.stepBeats - beats) < 1e-4f, [set, b = beats] { set ([b] (ArpSettings& a) { a.stepBeats = b; }); });
  2972. for (int g : { 25, 50, 75, 100 })
  2973. 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; }); });
  2974. for (int n : { 1, 2, 3, 4 })
  2975. repeat.addItem (juce::String (n) + "x", true, arp.repeat == n, [set, n] { set ([n] (ArpSettings& a) { a.repeat = n; }); });
  2976. arpMenu.addSubMenu (tr ("Range"), range);
  2977. arpMenu.addSubMenu (tr ("Speed"), speed);
  2978. arpMenu.addSubMenu (tr ("Gate"), gate);
  2979. arpMenu.addSubMenu (tr ("Repeat"), repeat);
  2980. menu.addSubMenu (arp.enabled() ? tr ("Arpeggiator (on)") : tr ("Arpeggiator"), arpMenu);
  2981. }
  2982. // --- second column: the channel's steps in the current pattern ---
  2983. menu.addColumnBreak();
  2984. // FL's Cut / Copy / Paste (REA round 170a §1): the selected channels' steps and notes, as Ctrl+X / C / V
  2985. {
  2986. const auto& song = engine.getSong();
  2987. const auto& selection = engine.getSelectedChannels();
  2988. const auto& notes = song.activePattern().notes;
  2989. const auto hasNotes = std::any_of (notes.begin(), notes.end(), [&selection] (const Note& n) {
  2990. return std::binary_search (selection.begin(), selection.end(), n.channel);
  2991. });
  2992. menu.addItem (theme::mnemonic (tr ("Cut"), 't') + "\tCtrl+X", hasNotes, false, [this] { cutSelected(); });
  2993. menu.addItem (theme::mnemonic (tr ("Copy"), 'o') + "\tCtrl+C", hasNotes, false, [this] { copySelected(); });
  2994. menu.addItem (theme::mnemonic (tr ("Paste"), 'p') + "\tCtrl+V", ! stepClipboard.empty(), false, [this] { pasteSelected(); });
  2995. }
  2996. menu.addSeparator();
  2997. menu.addItem (theme::mnemonic (tr ("Fill each 2 steps"), '2'), [this] { fillSelected (2); });
  2998. menu.addItem (theme::mnemonic (tr ("Fill each 4 steps"), '4'), [this] { fillSelected (4); });
  2999. menu.addItem (theme::mnemonic (tr ("Fill each 8 steps"), '8'), [this] { fillSelected (8); });
  3000. menu.addItem (theme::mnemonic (tr ("Advanced fill..."), 'f'), [this, channel] { showEuclideanFill (channel); });
  3001. menu.addSeparator();
  3002. menu.addItem (tr ("Rotate left") + "\tShift+Ctrl+Left", [this] { rotateSelected (-1); });
  3003. menu.addItem (tr ("Rotate right") + "\tShift+Ctrl+Right", [this] { rotateSelected (1); });
  3004. menu.addSeparator();
  3005. {
  3006. // FL's "Receive notes from" (round 165f-rack): a MIDI input device (any or one MIDI channel) plays only this channel
  3007. juce::PopupMenu receive;
  3008. receive.addItem (theme::mnemonic (tr ("Unlock"), 'u'), ! c.receiveFrom.empty(), false, [this, channel] {
  3009. engine.editSong ([channel] (Song& s) {
  3010. s.channels[(std::size_t) channel].receiveFrom.clear();
  3011. s.channels[(std::size_t) channel].receiveMidiChannel = 0;
  3012. });
  3013. });
  3014. receive.addSeparator();
  3015. auto lockTo = [this, channel] (std::string device, int midiChannel) {
  3016. engine.editSong ([channel, device, midiChannel] (Song& s) {
  3017. // one device + MIDI channel plays one channel: another channel that had it lets go
  3018. for (auto& other : s.channels)
  3019. if (other.receiveFrom == device && other.receiveMidiChannel == midiChannel)
  3020. other.receiveFrom.clear();
  3021. s.channels[(std::size_t) channel].receiveFrom = device;
  3022. s.channels[(std::size_t) channel].receiveMidiChannel = midiChannel;
  3023. });
  3024. };
  3025. const auto devices = juce::MidiInput::getAvailableDevices();
  3026. for (const auto& d : devices)
  3027. {
  3028. const auto name = d.name.toStdString();
  3029. juce::PopupMenu channels;
  3030. channels.addItem (tr ("Any MIDI channel"), true, c.receiveFrom == name && c.receiveMidiChannel == 0, [lockTo, name] { lockTo (name, 0); });
  3031. for (int m = 1; m <= 16; ++m)
  3032. channels.addItem (tr ("MIDI channel {0}", { juce::String (m) }), true, c.receiveFrom == name && c.receiveMidiChannel == m,
  3033. [lockTo, name, m] { lockTo (name, m); });
  3034. receive.addSubMenu (d.name, channels, true, nullptr, c.receiveFrom == name);
  3035. }
  3036. // a device of the project that is not connected now stays listed
  3037. if (! c.receiveFrom.empty() && std::none_of (devices.begin(), devices.end(), [&c] (const juce::MidiDeviceInfo& d) { return d.name.toStdString() == c.receiveFrom; }))
  3038. receive.addItem (juce::String::fromUTF8 (c.receiveFrom.c_str()) + " (" + tr ("not connected") + ")", false, true, [] {});
  3039. if (devices.isEmpty() && c.receiveFrom.empty())
  3040. receive.addItem (tr ("No MIDI input device"), false, false, [] {});
  3041. menu.addSubMenu (theme::mnemonic (c.receiveFrom.empty() ? tr ("Receive notes from") : tr ("Receive notes from") + " (" + tr ("locked") + ")", 'n'), receive);
  3042. }
  3043. {
  3044. // FL's "Burn MIDI to" (round 165f-rack): what the channel plays (loops, swing, time, arpeggiator, echo) as notes
  3045. juce::PopupMenu burn;
  3046. burn.addItem (theme::mnemonic (tr ("Current pattern"), 'c'), [this, channel] { burnMidi (channel, false); });
  3047. burn.addItem (theme::mnemonic (tr ("New pattern"), 'n'), [this, channel] { burnMidi (channel, true); });
  3048. menu.addSubMenu (tr ("Burn MIDI to"), burn);
  3049. // its loop point in this pattern (FL's advanced looping control menu)
  3050. const auto& song = engine.getSong();
  3051. const auto& p = song.activePattern();
  3052. const auto setting = channelloops::effectiveSetting (p, channel);
  3053. juce::PopupMenu loop;
  3054. loop.addItem (tr ("Don't loop"), true, setting == ChannelLoop::dontLoop && p.loopOn, [this, channel] { setLoopPoint (channel, ChannelLoop::dontLoop); });
  3055. loop.addItem (tr ("Step"), true, p.loopOn && setting == ChannelLoop::nextStep, [this, channel] { setLoopPoint (channel, ChannelLoop::nextStep); });
  3056. loop.addItem (tr ("Beat"), true, p.loopOn && setting == ChannelLoop::nextBeat, [this, channel] { setLoopPoint (channel, ChannelLoop::nextBeat); });
  3057. loop.addItem (tr ("Bar"), true, p.loopOn && setting == ChannelLoop::nextBar, [this, channel] { setLoopPoint (channel, ChannelLoop::nextBar); });
  3058. loop.addSeparator();
  3059. loop.addItem (tr ("Set to next beat"), [this, channel] { setLoopPoint (channel, 1000 + ChannelLoop::nextBeat); });
  3060. loop.addItem (tr ("Set to next bar"), [this, channel] { setLoopPoint (channel, 1000 + ChannelLoop::nextBar); });
  3061. loop.addSeparator();
  3062. loop.addItem (tr ("Burn to pattern"), loopStepsOf (channel) > 0, false, [this, channel] {
  3063. engine.editSong ([channel] (Song& s) { channelloops::burnChannel (s.activePattern(), channel, s.meter()); });
  3064. repaint();
  3065. });
  3066. menu.addSubMenu (setting > 0 && p.loopOn ? tr ("Loop point ({0} steps)", { juce::String (setting) }) : tr ("Loop point"), loop);
  3067. }
  3068. // FL's "Create DirectWave instrument..." (round 165f-rack): the instrument sampled key by key into a Sampler channel
  3069. menu.addItem (tr ("Create sampled instrument..."), c.instrument != InstrumentKind::audioClip && c.instrument != InstrumentKind::controller,
  3070. false, [this, channel] { showAutoSamplerDialog (engine, channel); });
  3071. menu.addSeparator();
  3072. menu.addItem (tr ("Swing mix ({0})...", { juce::String (juce::roundToInt (c.swingMix * 100.0f)) + "%" }),
  3073. [this, channel] { setSwingMixOfSelected (channel); });
  3074. menu.addItem (tr ("Clear notes in this pattern"), [this, channel] {
  3075. engine.editSong ([channel] (Song& s) {
  3076. auto& notes = s.activePattern().notes;
  3077. notes.erase (std::remove_if (notes.begin(), notes.end(), [channel] (const Note& n) { return n.channel == channel; }),
  3078. notes.end());
  3079. });
  3080. });
  3081. // organisation (acts on the selection; FL keeps these in the rack's options menu)
  3082. menu.addSeparator();
  3083. menu.addItem (tr ("Move up") + "\tAlt+Up", engine.getSelectedChannels().front() > 0, false, [this] { moveSelected (-1); });
  3084. menu.addItem (tr ("Move down") + "\tAlt+Down", engine.getSelectedChannels().back() < count - 1, false, [this] { moveSelected (1); });
  3085. menu.addItem (tr ("Send to free mixer inserts") + "\tCtrl+L", [this] { sendSelectedToInserts(); });
  3086. {
  3087. juce::PopupMenu sort;
  3088. sort.addItem (tr ("By name"), [this] { sortChannels (ChannelSort::name); });
  3089. sort.addItem (tr ("By color"), [this] { sortChannels (ChannelSort::colour); });
  3090. sort.addItem (tr ("By mixer insert"), [this] { sortChannels (ChannelSort::insert); });
  3091. sort.addItem (tr ("By type"), [this] { sortChannels (ChannelSort::type); });
  3092. menu.addSubMenu (tr ("Sort channels"), sort);
  3093. }
  3094. menu.addItem (tr ("Select unused channels"), [this] { selectUnused(); });
  3095. menu.addItem (theme::mnemonic (tr ("Group selected..."), 'G') + "\tAlt+G", [this] { groupSelected(); });
  3096. return menu;
  3097. }
  3098. void ChannelRackComponent::showChannelMenu (int channel)
  3099. {
  3100. buildChannelMenu (channel).showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  3101. }
  3102. juce::String flEngineName (const natives::InstrumentEngine& e)
  3103. {
  3104. const auto name = juce::String::fromUTF8 (e.name);
  3105. if (! natives::available (e, flStudioAccess()))
  3106. return tr ("{0} (requires FL Studio)", { name });
  3107. return e.beta ? tr ("{0} (beta)", { name }) : name;
  3108. }
  3109. void ChannelRackComponent::openNewGenerator (int row, InstrumentKind kind)
  3110. {
  3111. switch (kind)
  3112. {
  3113. case InstrumentKind::kick: case InstrumentKind::snare: case InstrumentKind::hat: case InstrumentKind::clap:
  3114. case InstrumentKind::synth: case InstrumentKind::sampler: case InstrumentKind::audioClip:
  3115. return; // (LYNX's drums and synth: no window, as before)
  3116. case InstrumentKind::plugin:
  3117. openPluginWindow (row); // the plugin opens when added
  3118. openEditorRow = row;
  3119. return;
  3120. case InstrumentKind::patcher:
  3121. PatcherWindows::openChannel (engine, row);
  3122. return;
  3123. default:
  3124. openChannelSettings (engine, row);
  3125. return;
  3126. }
  3127. }
  3128. void ChannelRackComponent::addInstrumentItems (juce::PopupMenu& menu, int insertAt)
  3129. {
  3130. // a new channel at the end (insertAt < 0) or at a row; the list is the one Replace › shows (issue #18)
  3131. quickpick::addGeneratorMenu (menu, engine, [this, insertAt] (ChannelDef c) {
  3132. int index = 0;
  3133. if (filterGroup >= 0 && c.group < 0)
  3134. c.group = filterGroup;
  3135. const auto kind = c.instrument;
  3136. engine.editSong ([&] (Song& s) {
  3137. c.mixerTrack = s.claimFreeInsert();
  3138. index = insertAt < 0 ? (s.channels.push_back (c), (int) s.channels.size() - 1) : insertChannel (s, insertAt, c);
  3139. });
  3140. selectionAnchor = index;
  3141. selectRows ({ index }, index);
  3142. openNewGenerator (index, kind);
  3143. }, [this, insertAt] { chooseSample (-1, insertAt); }, nullptr, this);
  3144. }
  3145. void ChannelRackComponent::showAddChannelMenu (int insertAt)
  3146. {
  3147. juce::PopupMenu menu;
  3148. // FL's renamed plugins first (issue #11): "1_Synth" renamed in the Browser's Plugin database is added by typing 1
  3149. std::vector<quickpick::Pick> picks;
  3150. if (insertAt < 0)
  3151. picks = quickpick::renamedGenerators (engine, [safe = juce::Component::SafePointer<ChannelRackComponent> (this)] (ChannelDef c) {
  3152. if (safe != nullptr)
  3153. safe->addChannel (std::move (c), true);
  3154. });
  3155. quickpick::addPicks (menu, picks);
  3156. // the generators under FL's Plugin database categories and the user's folders (rounds 166f, 170b: addGeneratorMenu)
  3157. addInstrumentItems (menu, insertAt);
  3158. quickpick::addFolderViewItems (menu, engine); // FL: Categories / Simple / Tree at the bottom
  3159. quickpick::showMenu (menu, juce::PopupMenu::Options().withTargetComponent (this).withMousePosition(), std::move (picks));
  3160. }
  3161. juce::File ChannelRackComponent::channelStateFolder()
  3162. {
  3163. return platform::documentsFolder().getChildFile ("LYNX/Channel states");
  3164. }
  3165. void ChannelRackComponent::saveChannelState (int channel)
  3166. {
  3167. const auto& c = engine.getSong().channels[(std::size_t) channel];
  3168. const auto folder = channelStateFolder();
  3169. folder.createDirectory();
  3170. const auto name = juce::File::createLegalFileName (juce::String::fromUTF8 (c.name.c_str())).trim();
  3171. fileChooser = lynx::ui::makeFileChooser (tr ("Save channel state as..."), folder.getChildFile ((name.isEmpty() ? juce::String ("Channel") : name) + ".lynxchannel"),
  3172. "*.lynxchannel");
  3173. lynx::ui::launchChooser (fileChooser, juce::FileBrowserComponent::saveMode | juce::FileBrowserComponent::canSelectFiles
  3174. | juce::FileBrowserComponent::warnAboutOverwriting,
  3175. [this, channel] (const juce::FileChooser& chooser) {
  3176. auto file = chooser.getResult();
  3177. if (file == juce::File() || channel >= (int) engine.getSong().channels.size())
  3178. return;
  3179. file = file.withFileExtension ("lynxchannel");
  3180. const auto bytes = ProjectFile::channelStateBytes (engine.getSong().channels[(std::size_t) channel]);
  3181. if (! file.replaceWithData (bytes.data(), bytes.size()))
  3182. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Save channel state as..."),
  3183. tr ("Could not write {0}", { file.getFullPathName() }));
  3184. });
  3185. }
  3186. void ChannelRackComponent::openChannelState (int channel)
  3187. {
  3188. fileChooser = lynx::ui::makeFileChooser (tr ("Browse channel states"), channelStateFolder(), "*.lynxchannel");
  3189. lynx::ui::launchChooser (fileChooser, juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
  3190. [this, channel] (const juce::FileChooser& chooser) {
  3191. juce::MemoryBlock data;
  3192. if (! chooser.getResult().loadFileAsData (data) || channel >= (int) engine.getSong().channels.size())
  3193. return;
  3194. const auto* p = static_cast<const std::uint8_t*> (data.getData());
  3195. auto loaded = ProjectFile::channelFromStateBytes (std::vector<std::uint8_t> (p, p + data.getSize()));
  3196. if (! loaded)
  3197. {
  3198. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Browse channel states"),
  3199. tr ("This file is not a channel state LYNX SHARE can read."));
  3200. return;
  3201. }
  3202. closeEditor (channel);
  3203. engine.editSongStructural ([channel, def = *loaded] (Song& s) mutable {
  3204. if (channel >= (int) s.channels.size())
  3205. return;
  3206. auto& old = s.channels[(std::size_t) channel];
  3207. def.mixerTrack = old.mixerTrack; // (the channel keeps its place in the mix and its filter group)
  3208. def.group = old.group;
  3209. old = std::move (def);
  3210. });
  3211. });
  3212. }
  3213. void ChannelRackComponent::chooseSample (int channel, int insertAt)
  3214. {
  3215. fileChooser = lynx::ui::makeFileChooser (tr ("Choose sample"), juce::File(), engine.getSampleLibrary().wildcard());
  3216. lynx::ui::launchChooser (fileChooser, juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
  3217. [this, channel, insertAt] (const juce::FileChooser& chooser) {
  3218. if (! chooser.getResult().existsAsFile())
  3219. return;
  3220. if (channel >= 0 || insertAt < 0)
  3221. {
  3222. assignSample (channel, chooser.getResult());
  3223. return;
  3224. }
  3225. // Insert › Sampler (choose file)...: a new sampler channel at the row
  3226. if (engine.getSampleLibrary().load (chooser.getResult().getFullPathName()) == nullptr)
  3227. return assignSample (channel, chooser.getResult()); // (its "could not read" message)
  3228. ChannelDef c;
  3229. c.instrument = InstrumentKind::sampler;
  3230. c.samplePath = chooser.getResult().getFullPathName().toStdString();
  3231. c.name = chooser.getResult().getFileNameWithoutExtension().toStdString();
  3232. if (filterGroup >= 0)
  3233. c.group = filterGroup;
  3234. int index = 0;
  3235. engine.editSong ([&] (Song& s) {
  3236. c.mixerTrack = s.claimFreeInsert();
  3237. index = insertChannel (s, std::min (insertAt, (int) s.channels.size()), c);
  3238. });
  3239. selectionAnchor = index;
  3240. selectRows ({ index }, index);
  3241. });
  3242. }
  3243. void ChannelRackComponent::assignSample (int channel, const juce::File& file)
  3244. {
  3245. // FL's .speech files open the Speech properties first, like FL's channel load does (round 168b); the file it
  3246. // returns (the edited copy, or this one) goes into the channel
  3247. if (file.hasFileExtension ("speech"))
  3248. {
  3249. speechui::editThenLoad (engine, file, [safe = juce::Component::SafePointer<ChannelRackComponent> (this), channel] (const juce::File& f) {
  3250. if (safe != nullptr)
  3251. safe->assignSampleNow (channel, f);
  3252. });
  3253. return;
  3254. }
  3255. assignSampleNow (channel, file);
  3256. }
  3257. void ChannelRackComponent::assignSampleNow (int channel, const juce::File& file)
  3258. {
  3259. if (engine.getSampleLibrary().load (file.getFullPathName()) == nullptr)
  3260. {
  3261. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Sample"),
  3262. tr ("Could not read {0}", { file.getFileName() }));
  3263. return;
  3264. }
  3265. const auto path = file.getFullPathName().toStdString();
  3266. const auto name = file.getFileNameWithoutExtension().toStdString();
  3267. engine.editSong ([channel, path, name] (Song& s) {
  3268. const auto isNew = channel < 0 || channel >= (int) s.channels.size();
  3269. const auto freeInsert = s.claimFreeInsert();
  3270. auto& c = isNew ? s.channels.emplace_back() : s.channels[(std::size_t) channel];
  3271. if (isNew)
  3272. c.mixerTrack = freeInsert;
  3273. // (an audio clip channel stays one: as a Sampler its Playlist clips went silent, round 190ax)
  3274. audioclips::loadSampleInto (c, path, name, flsettings::options().autoNameChannels.load());
  3275. });
  3276. if (channel < 0)
  3277. engine.setSelectedChannel ((int) engine.getSong().channels.size() - 1);
  3278. }
  3279. void ChannelRackComponent::openPluginWindow (int channel)
  3280. {
  3281. // Online Sessions P5-A1: a plugin the session's host plays for this channel opens its parameter editor
  3282. if (channel >= 0 && channel < (int) engine.getSong().channels.size()
  3283. && liveui::showHostedEditor ({ { live::Kind::channel, engine.getSong().channels[(std::size_t) channel].uid, 0 }, -1 }))
  3284. return;
  3285. auto instrument = engine.getChannelInstrument (channel);
  3286. if (auto* plugin = dynamic_cast<PluginInstrument*> (instrument.get()))
  3287. {
  3288. // round 190ao: the same channel's window again, after a reload (the channel is found again by its instance tag:
  3289. // channels may have moved since)
  3290. auto reopen = [safe = juce::Component::SafePointer<ChannelRackComponent> (this), tag = engine.getSong().channels[(std::size_t) channel].instance.id] {
  3291. if (safe == nullptr)
  3292. return;
  3293. const auto& channels = safe->engine.getSong().channels;
  3294. for (std::size_t i = 0; i < channels.size(); ++i)
  3295. if (channels[i].instance.id == tag && channels[i].instrument == InstrumentKind::plugin)
  3296. {
  3297. if (safe->engine.hostedPluginDown ({ (int) i, -1, -1 }))
  3298. PluginWindows::reload (safe->engine, { { (int) i, -1, -1 } });
  3299. if (! safe->engine.hostedPluginDown ({ (int) i, -1, -1 }))
  3300. safe->openPluginWindow ((int) i);
  3301. return;
  3302. }
  3303. };
  3304. // crashed inside a call (not called again: its window would call into it), or its separate process died:
  3305. // "Reload" starts it again without reopening the project
  3306. if (engine.hostedPluginDown ({ channel, -1, -1 }))
  3307. {
  3308. PluginWindows::promptReload (engine, { channel, -1, -1 }, [safe = juce::Component::SafePointer<ChannelRackComponent> (this), channel] {
  3309. if (safe != nullptr)
  3310. safe->openPluginWindow (channel);
  3311. });
  3312. return;
  3313. }
  3314. PluginWindows::WrapperContext context { &engine, [channel] (Song& song) -> PluginRef* {
  3315. return channel < (int) song.channels.size() ? &song.channels[(std::size_t) channel].plugin : nullptr;
  3316. }, true, -1, -1, {}, {} };
  3317. context.reload = reopen;
  3318. context.down = [weak = std::weak_ptr<Instrument> (instrument)] {
  3319. const auto held = weak.lock();
  3320. auto* p = dynamic_cast<PluginInstrument*> (held.get());
  3321. return p != nullptr && p->hasCrashed();
  3322. };
  3323. PluginWindows::open (instrument, plugin->getInstance(),
  3324. juce::String::fromUTF8 (engine.getSong().channels[(std::size_t) channel].name.c_str()), std::move (context));
  3325. return;
  3326. }
  3327. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Plugin"),
  3328. tr ("This plugin could not be loaded (it is not installed or has not been scanned)."));
  3329. }
  3330. void ChannelRackComponent::rename (int channel)
  3331. {
  3332. const auto& c = engine.getSong().channels[(std::size_t) channel];
  3333. // issue #201 (round 190ll, core/SelectionRename.h): a new colour goes to every selected channel (the name to this one)
  3334. const auto targets = selrename::targets (channel, engine.getSelectedChannels());
  3335. // issue #220 (round 190ng): FL's "Rename, color and icon" sets the channel's icon in the same window
  3336. showNameColourIconDialog (tr ("Rename channel"), juce::String::fromUTF8 (c.name.c_str()), c.colour, c.icon,
  3337. [this, channel, targets, oldName = c.name, oldColour = c.colour, oldIcon = c.icon] (const juce::String& name, std::uint32_t colour, int icon) {
  3338. if (channel < (int) engine.getSong().channels.size())
  3339. engine.editSong ([channel, targets, icon, oldIcon, r = selrename::Result { oldName, name.toStdString(), oldColour, colour }] (Song& s) {
  3340. selrename::applyToChannels (s, channel, targets, r);
  3341. if (icon >= 0 && icon != oldIcon && channel < (int) s.channels.size())
  3342. s.channels[(std::size_t) channel].icon = icon;
  3343. });
  3344. });
  3345. }
  3346. namespace
  3347. {
  3348. // Live's "Slice to Drum Rack" (round 190ec): Simpler in Slicing mode → a Drum Rack on the same track, one pad per slice
  3349. // (Live's Default slicing facts: gated, choke group 1), no clip. LYNX: the Sampler channel's own slices if it has them,
  3350. // else its transients (a .speech sample: its words), inside its played region; the channel keeps its name, insert,
  3351. // levels and colour.
  3352. void sliceChannelToDrumRack (AudioEngine& engine, int channel)
  3353. {
  3354. const auto& song = engine.getSong();
  3355. if (channel < 0 || channel >= (int) song.channels.size())
  3356. return;
  3357. const auto& c = song.channels[(std::size_t) channel];
  3358. const auto sample = engine.getSampleLibrary().load (juce::String::fromUTF8 (c.samplePath.c_str()));
  3359. if (sample == nullptr || sample->frames() == 0)
  3360. return;
  3361. std::vector<float> starts = c.sampler.slices;
  3362. if (starts.empty())
  3363. {
  3364. if (juce::File (juce::String::fromUTF8 (c.samplePath.c_str())).hasFileExtension ("speech") && ! sample->markerStarts.empty())
  3365. for (const auto start : sample->markerStarts)
  3366. starts.push_back ((float) ((double) start / (double) sample->frames()));
  3367. else
  3368. starts = detectSlices (*sample, 0.5f, rack::maxSlicePads);
  3369. }
  3370. const auto from = std::clamp (c.sampler.regionStart, 0.0f, 1.0f), to = std::clamp (c.sampler.regionEnd, from, 1.0f);
  3371. const auto slices = rack::slicesInRegion (starts, from, to);
  3372. const auto stem = juce::File (juce::String::fromUTF8 (c.samplePath.c_str())).getFileNameWithoutExtension().toStdString();
  3373. auto drum = rack::sliceToDrumRack (c.samplePath, slices, stem.empty() ? c.name : stem, rack::SliceTemplate::gated, to);
  3374. engine.editSong ([channel, patcher = std::move (drum.patcher)] (Song& s) {
  3375. if (channel >= (int) s.channels.size())
  3376. return;
  3377. auto& ch = s.channels[(std::size_t) channel];
  3378. ch.instrument = InstrumentKind::patcher;
  3379. ch.patcher = patcher;
  3380. ch.sampler.slices.clear();
  3381. });
  3382. }
  3383. } // namespace
  3384. void ChannelRackComponent::sliceChannel (int channel)
  3385. {
  3386. const auto& song = engine.getSong();
  3387. const auto& c = song.channels[(std::size_t) channel];
  3388. const auto sample = engine.getSampleLibrary().load (juce::String::fromUTF8 (c.samplePath.c_str()));
  3389. if (sample == nullptr || sample->frames() == 0)
  3390. return;
  3391. // a spoken .speech sample is cut at its words (FL hands its words to the slicer as a Zero-X grid; round 168b)
  3392. std::vector<float> slices;
  3393. if (juce::File (juce::String::fromUTF8 (c.samplePath.c_str())).hasFileExtension ("speech") && ! sample->markerStarts.empty())
  3394. for (const auto start : sample->markerStarts)
  3395. slices.push_back ((float) ((double) start / (double) sample->frames()));
  3396. else
  3397. slices = detectSlices (*sample, 0.5f);
  3398. const auto seconds = (double) sample->frames() / sample->sampleRate;
  3399. const auto ticksPerSecond = song.bpm * ppq / 60.0;
  3400. const auto name = c.name + " (slices)";
  3401. engine.editSong ([&] (Song& s) {
  3402. s.channels[(std::size_t) channel].sampler.slices = slices;
  3403. // pattern that plays the slices in their original order and timing (at the project BPM)
  3404. auto& p = s.patterns.emplace_back();
  3405. p.name = name;
  3406. for (std::size_t i = 0; i < slices.size(); ++i)
  3407. {
  3408. const auto start = slices[i] * seconds;
  3409. const auto end = i + 1 < slices.size() ? slices[i + 1] * seconds : seconds;
  3410. Note n;
  3411. n.start = (std::int64_t) std::llround (start * ticksPerSecond);
  3412. n.length = std::max<std::int64_t> (1, (std::int64_t) std::llround ((end - start) * ticksPerSecond));
  3413. n.key = 60 + (int) i;
  3414. n.channel = channel;
  3415. p.notes.push_back (n);
  3416. }
  3417. p.sortNotes();
  3418. s.currentPattern = (int) s.patterns.size() - 1;
  3419. });
  3420. }
  3421. bool ChannelRackComponent::isInterestedInFileDrag (const juce::StringArray& files)
  3422. {
  3423. for (const auto& f : files)
  3424. if (SampleLibrary::isSampleFile (juce::File (f)) || juce::File (f).hasFileExtension ("sf2;sf3;sfz")
  3425. || isSamplerInstrumentFile (juce::File (f))) // (.dwp / .dwb / .gig / .nki: round 190ii)
  3426. return true;
  3427. return false;
  3428. }
  3429. void ChannelRackComponent::filesDropped (const juce::StringArray& files, int x, int y)
  3430. {
  3431. int row = 0;
  3432. const auto area = areaAt (x, y, row);
  3433. auto target = (area == Area::none || area == Area::addRow) ? -1 : row;
  3434. for (const auto& f : files)
  3435. {
  3436. const juce::File file (f);
  3437. if (file.hasFileExtension ("sf2;sf3;sfz"))
  3438. {
  3439. // a SoundFont: into the LYNX Soundfont channel it was dropped on, else a new channel
  3440. openSoundFontFile (engine, file, target);
  3441. target = -1;
  3442. continue;
  3443. }
  3444. if (isSamplerInstrumentFile (file))
  3445. {
  3446. // another sampler's instrument (round 190ii, issue #149): into the DirectWave channel it was dropped on, else
  3447. // a new DirectWave channel
  3448. openSamplerInstrumentFile (engine, file, target);
  3449. target = -1;
  3450. continue;
  3451. }
  3452. if (! SampleLibrary::isSampleFile (file)) // (FL's WavPack pack samples too: round 165q)
  3453. continue;
  3454. assignSample (target, file);
  3455. target = -1; // the remaining files become new channels
  3456. }
  3457. }
  3458. void ChannelRackComponent::changeListenerCallback (juce::ChangeBroadcaster* source)
  3459. {
  3460. // a level drag (AudioEngine::editLevels, round 171c) changed only levels: repaint just the knobs that moved. Round
  3461. // 173f (discussion #10): every mouse move of a Mixer fader resized and repainted the whole rack, which moved nothing
  3462. const auto& channels = engine.getSong().channels;
  3463. if (source == &engine.getLevelsBroadcaster() && shownLevels.size() == channels.size())
  3464. {
  3465. for (std::size_t i = 0; i < channels.size(); ++i)
  3466. if (const std::array<float, 2> now { channels[i].volume, channels[i].pan }; now != shownLevels[i])
  3467. {
  3468. shownLevels[i] = now;
  3469. repaint (controlBounds (Area::pan, (int) i).getUnion (controlBounds (Area::volume, (int) i)).expanded (2));
  3470. }
  3471. return;
  3472. }
  3473. shownLevels.resize (channels.size());
  3474. for (std::size_t i = 0; i < channels.size(); ++i)
  3475. shownLevels[i] = { channels[i].volume, channels[i].pan };
  3476. updateSize();
  3477. repaint();
  3478. }
  3479. juce::Rectangle<int> ChannelRackComponent::selectorBounds (int row) const
  3480. {
  3481. // the whole selector slot (round 190io's tall slot, y + 3 … rowHeight - 6, whose lit pill reaches y + 5: rollup 0.1.30,
  3482. // round 190iy; 190ih's bounds were the slim pill's, so a fading light left its top and bottom rows lit), 1 px around
  3483. return { stickyX + selectLeft(), rowY (row) + 2, selectWidth, rowHeight - 4 };
  3484. }
  3485. // round 190ih (PLAN R4, DC-8 / DE-6): one step of every channel's activity light; only the selectors whose shade
  3486. // changed repaint, so a rack where nothing sounds costs one counter read per channel and no paint
  3487. void ChannelRackComponent::updateActivityLights()
  3488. {
  3489. const auto& song = engine.getSong();
  3490. const auto count = (int) song.channels.size();
  3491. if ((int) activityLights.size() != count)
  3492. activityLights.resize ((std::size_t) count);
  3493. const auto fade = ui::motion::enabled();
  3494. for (int row = 0; row < count; ++row)
  3495. {
  3496. auto& light = activityLights[(std::size_t) row];
  3497. const auto before = activity::shade (light.level);
  3498. const auto keys = engine.soundingKeys (row);
  3499. activity::step (light, engine.noteStrikes (row), (keys[0] | keys[1]) != 0, fade);
  3500. if (activity::shade (light.level) != before && isShown (row))
  3501. repaint (selectorBounds (row));
  3502. }
  3503. }
  3504. void ChannelRackComponent::timerCallback()
  3505. {
  3506. if (isShowing())
  3507. updateActivityLights();
  3508. // the channels' volume / pan knobs follow their automation (issue #12): only the automated knobs repaint
  3509. if (const auto& shown = engine.getAutomationDisplay(); shown.getGeneration() != shownAutomation)
  3510. {
  3511. shownAutomation = shown.getGeneration();
  3512. std::vector<int> rows;
  3513. for (const auto& t : shown.automatedTargets())
  3514. if (t.kind == AutomationTargetKind::channelVolume || t.kind == AutomationTargetKind::channelPan)
  3515. rows.push_back (t.index);
  3516. if (isShowing())
  3517. for (const auto list : { &rows, &automatedRows })
  3518. for (const auto r : *list)
  3519. if (r >= 0 && r < (int) engine.getSong().channels.size())
  3520. repaint (controlBounds (Area::pan, r).getUnion (controlBounds (Area::volume, r)).expanded (2));
  3521. automatedRows = std::move (rows);
  3522. }
  3523. }
  3524. bool ChannelRackComponent::playheadFrame (double)
  3525. {
  3526. auto& transport = engine.getTransport();
  3527. auto step = -1;
  3528. const auto playing = transport.playing.load();
  3529. // the playhead shows whenever the shown pattern plays: Pattern mode, or a clip of it in Song mode (Busy Works
  3530. // Beats' video, round 170a: in Song mode the rack showed none), as the Piano Roll's (transportedit::patternPlayhead)
  3531. if (playing && isShowing())
  3532. {
  3533. const auto& song = engine.getSong();
  3534. // (between two audio blocks it moves on at the song's tempo, round 174e: the step lights when it is heard)
  3535. const auto tick = transportedit::patternPlayhead (song, song.currentPattern, transport.positionTicks.load() + engine.playheadAdvanceTicks());
  3536. if (tick >= 0.0)
  3537. step = (int) (tick / ticksPerStep);
  3538. }
  3539. if (step != playheadStep)
  3540. {
  3541. // (fitted miniatures draw their own playhead line, at the pattern's scale)
  3542. const auto span = previewSpan();
  3543. const auto total = numSteps();
  3544. auto dirty = [this, span, total] (int s) {
  3545. if (s < 0)
  3546. return;
  3547. repaint (gridLeft() + s * stepWidth - 1, 0, stepWidth + 1, getHeight());
  3548. if (span.fitted && s < total)
  3549. repaint (gridLeft() + span.x + (int) ((std::int64_t) span.width * s / total) - 1, 0, juce::jmax (2, span.width / total) + 3, getHeight());
  3550. };
  3551. dirty (playheadStep);
  3552. playheadStep = step;
  3553. dirty (playheadStep);
  3554. repaint (stickyX + selectLeft(), 0, selectWidth, getHeight()); // the selectors flash with the hits
  3555. followPlayhead();
  3556. return true;
  3557. }
  3558. return playing;
  3559. }
  3560. void ChannelRackComponent::followPlayhead()
  3561. {
  3562. // Follow (issue #12): a pattern longer than the view pages along with the playing step, as the Playlist does
  3563. if (playheadStep < 0 || ! EditorGlobals::get().isFollowingView (EditorGlobals::FollowView::channelRack))
  3564. return;
  3565. auto* viewport = findParentComponentOfClass<juce::Viewport>();
  3566. if (viewport == nullptr)
  3567. return;
  3568. const auto view = viewport->getViewArea();
  3569. const auto stepsLeft = view.getX() + gridLeft(); // (the controls column stays on the left of the view)
  3570. const auto x = gridLeft() + playheadStep * stepWidth;
  3571. if (x >= stepsLeft && x + stepWidth <= view.getRight())
  3572. return;
  3573. const auto maxX = juce::jmax (0, getWidth() - view.getWidth());
  3574. viewport->setViewPosition (juce::jlimit (0, maxX, x - gridLeft()), view.getY());
  3575. }
  3576. bool ChannelRackComponent::isInterestedInDragSource (const SourceDetails& details)
  3577. {
  3578. std::string path;
  3579. int bank = 0, program = 0;
  3580. return BrowserComponent::isBrowserDrag (details) || BrowserComponent::isGeneratorDrag (details)
  3581. || parseSoundFontDrag (details.description.toString(), path, bank, program);
  3582. }
  3583. void ChannelRackComponent::itemDropped (const SourceDetails& details)
  3584. {
  3585. // a generator dragged from the Browser's Plugin database (round 162): below the channels a new channel with it, onto a
  3586. // channel it replaces that channel's instrument (FL, audit round 165z-a: insert, notes and levels stay); window open
  3587. if (BrowserComponent::isGeneratorDrag (details))
  3588. {
  3589. if (auto c = BrowserComponent::draggedGenerator (details))
  3590. {
  3591. int row = 0;
  3592. const auto area = areaAt (details.localPosition.x, details.localPosition.y, row);
  3593. if (area != Area::none && area != Area::addRow && row >= 0 && row < (int) engine.getSong().channels.size())
  3594. {
  3595. closeEditor (row);
  3596. const auto kind = c->instrument;
  3597. engine.editSong ([row, &c] (Song& s) { replaceGenerator (s, row, std::move (*c)); });
  3598. selectionAnchor = row;
  3599. selectRows ({ row }, row);
  3600. if (kind == InstrumentKind::plugin)
  3601. {
  3602. openPluginWindow (row);
  3603. openEditorRow = row;
  3604. }
  3605. else if (natives::instrumentEngine (kind) != nullptr)
  3606. openChannelSettings (engine, row);
  3607. }
  3608. else
  3609. addChannel (std::move (*c), true);
  3610. }
  3611. return;
  3612. }
  3613. // a preset dragged from a LYNX Soundfont list: onto a LYNX Soundfont channel it loads there, else a new channel
  3614. std::string path;
  3615. int bank = 0, program = 0;
  3616. if (parseSoundFontDrag (details.description.toString(), path, bank, program))
  3617. {
  3618. int row = 0;
  3619. const auto area = areaAt (details.localPosition.x, details.localPosition.y, row);
  3620. const auto& song = engine.getSong();
  3621. if (area != Area::none && area != Area::addRow && row >= 0 && row < (int) song.channels.size()
  3622. && song.channels[(std::size_t) row].instrument == InstrumentKind::soundfont)
  3623. loadSoundFontInto (engine, row, path, bank, program);
  3624. else
  3625. addSoundFontChannel (engine, path, bank, program);
  3626. return;
  3627. }
  3628. if (const auto file = BrowserComponent::draggedFile (details); file.existsAsFile())
  3629. filesDropped ({ file.getFullPathName() }, details.localPosition.x, details.localPosition.y);
  3630. }
  3631. } // namespace lynx