MixerComponent.cpp 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  1. #include "ui/MixerComponent.h"
  2. #include "core/FlEffects.h"
  3. #include "core/FruityVocoder.h"
  4. #include "core/LiveInput.h"
  5. #include "core/MixerOps.h"
  6. #include "ui/AutomationUi.h"
  7. #include "ui/BrowserComponent.h"
  8. #include "ui/ExportDialog.h"
  9. #include "core/Effects.h"
  10. #include "engine/Text.h"
  11. #include "ui/FacetEqEditor.h"
  12. #include "ui/ParametricEqEditor.h"
  13. #include "ui/PluginEditors.h"
  14. #include "ui/PluginWindow.h"
  15. #include "ui/HintBar.h"
  16. #include "ui/I18n.h"
  17. #include "core/LynxTune.h"
  18. #include "core/VocalPitch.h"
  19. #include "ui/Icons.h"
  20. #include "ui/ValueControl.h"
  21. #include "ui/Theme.h"
  22. #include "ui/AudioEditor.h"
  23. namespace lynx
  24. {
  25. namespace
  26. {
  27. // strip layout, top to bottom: header (arm, number, name), knobs (pan, stereo), mute/solo, meters +
  28. // fader, the dB read-out, then the routing lane (send switch, send knob, wires)
  29. constexpr int headerHeight = 36;
  30. constexpr int knobsHeight = 32;
  31. constexpr int buttonsHeight = 20;
  32. constexpr int readoutHeight = 16;
  33. constexpr int switchHeight = 16;
  34. constexpr int sendKnobHeight = 26;
  35. constexpr int wiresHeight = 14;
  36. constexpr int laneHeight = switchHeight + sendKnobHeight + wiresHeight + 4;
  37. constexpr int meterWidth = 6;
  38. // Fader 0..1 -> dB, with 0.8 = 0 dB (as in FL).
  39. juce::String faderToDb (float volume)
  40. {
  41. if (volume <= 0.0001f)
  42. return juce::String::fromUTF8 ("-\xe2\x88\x9e");
  43. const auto db = juce::Decibels::gainToDecibels (faderGain (volume));
  44. return (db > 0 ? "+" : "") + juce::String (db, 1);
  45. }
  46. // Meter level 0..1 on a dB scale (-60..+6).
  47. float meterPosition (float gain)
  48. {
  49. const auto db = juce::Decibels::gainToDecibels (gain, -60.0f);
  50. return juce::jlimit (0.0f, 1.0f, (db + 60.0f) / 66.0f);
  51. }
  52. // Stereo separation: -1 = widest, 0 = normal, +1 = mono (MixerTrack::stereo).
  53. ui::ValueSpec stereoSpec (const juce::String& name)
  54. {
  55. ui::ValueSpec spec;
  56. spec.name = name;
  57. spec.minimum = -1.0;
  58. spec.maximum = 1.0;
  59. spec.defaultValue = 0.0;
  60. spec.toText = [] (double v) {
  61. const auto percent = juce::roundToInt (std::abs (v) * 100.0);
  62. if (percent == 0)
  63. return tr ("Normal width");
  64. if (v > 0.0 && percent >= 100)
  65. return tr ("Mono");
  66. return v < 0.0 ? tr ("{0}% wider", { juce::String (percent) }) : tr ("{0}% narrower", { juce::String (percent) });
  67. };
  68. spec.fromText = [] (const juce::String& t) -> std::optional<double> {
  69. const auto lower = t.toLowerCase().trim();
  70. if (lower.startsWith ("mono"))
  71. return 1.0;
  72. if (lower.startsWith ("n") && ! lower.containsAnyOf ("0123456789"))
  73. return 0.0;
  74. const auto digits = lower.initialSectionContainingOnly ("+-0123456789.,").replaceCharacter (',', '.');
  75. if (digits.isEmpty() || digits.containsOnly ("+-."))
  76. return std::nullopt;
  77. auto v = digits.getDoubleValue() / 100.0;
  78. if (lower.contains ("wid") || lower.contains ("larg")) // "50% wider" / "50% mais largo"
  79. v = -std::abs (v);
  80. return v;
  81. };
  82. return spec;
  83. }
  84. ui::ValueSpec sendSpec (const juce::String& name)
  85. {
  86. auto spec = ui::percentSpec (name, 1.0);
  87. spec.pixelsForRange = 120.0;
  88. return spec;
  89. }
  90. } // namespace
  91. MixerStrips::MixerStrips (AudioEngine& e)
  92. : engine (e)
  93. {
  94. setWantsKeyboardFocus (false);
  95. setMouseClickGrabsKeyboardFocus (false);
  96. engine.addChangeListener (this);
  97. updateSize();
  98. startTimerHz (30);
  99. }
  100. MixerStrips::~MixerStrips()
  101. {
  102. engine.removeChangeListener (this);
  103. }
  104. void MixerStrips::setVisibleHeight (int height)
  105. {
  106. visibleHeight = juce::jmax (300, height);
  107. updateSize();
  108. }
  109. void MixerStrips::updateSize()
  110. {
  111. const auto tracks = (int) engine.getSong().mixer.size();
  112. setSize (stripX (tracks) + stripWidth + 8, visibleHeight);
  113. levels.resize ((std::size_t) juce::jmax (1, tracks) * 2);
  114. }
  115. int MixerStrips::stripX (int track) const noexcept
  116. {
  117. return track == 0 ? 4 : 4 + masterWidth + 8 + (track - 1) * stripWidth;
  118. }
  119. int MixerStrips::trackAt (int x) const noexcept
  120. {
  121. const auto tracks = (int) engine.getSong().mixer.size();
  122. for (int t = 0; t <= tracks; ++t) // t == tracks = "+" button
  123. if (x >= stripX (t) && x < stripX (t) + stripWidthOf (t))
  124. return t;
  125. return -1;
  126. }
  127. int MixerStrips::laneTop() const noexcept
  128. {
  129. return getHeight() - laneHeight;
  130. }
  131. juce::Rectangle<int> MixerStrips::headerArea (int t) const
  132. {
  133. return { stripX (t), 0, stripWidthOf (t) - 4, headerHeight };
  134. }
  135. juce::Rectangle<int> MixerStrips::armArea (int t) const
  136. {
  137. return { stripX (t) + 4, 6, 13, 13 };
  138. }
  139. juce::Rectangle<int> MixerStrips::inputArea (int t) const
  140. {
  141. // a thin level bar under the header (inserts with an input source)
  142. return { stripX (t) + 6, headerHeight - 4, stripWidthOf (t) - 16, 3 };
  143. }
  144. juce::Rectangle<int> MixerStrips::panArea (int t) const
  145. {
  146. const auto w = stripWidthOf (t) - 4;
  147. return { stripX (t), headerHeight, w / 2, knobsHeight };
  148. }
  149. juce::Rectangle<int> MixerStrips::stereoArea (int t) const
  150. {
  151. const auto w = stripWidthOf (t) - 4;
  152. return { stripX (t) + w / 2, headerHeight, w - w / 2, knobsHeight };
  153. }
  154. juce::Rectangle<int> MixerStrips::muteArea (int t) const
  155. {
  156. return { stripX (t) + 4, headerHeight + knobsHeight, 24, buttonsHeight };
  157. }
  158. juce::Rectangle<int> MixerStrips::soloArea (int t) const
  159. {
  160. return { stripX (t) + stripWidthOf (t) - 32, headerHeight + knobsHeight, 24, buttonsHeight };
  161. }
  162. juce::Rectangle<int> MixerStrips::faderArea (int t) const
  163. {
  164. const auto top = headerHeight + knobsHeight + buttonsHeight + 8;
  165. return { stripX (t) + 5, top, stripWidthOf (t) - 14, juce::jmax (40, laneTop() - readoutHeight - 4 - top) };
  166. }
  167. juce::Rectangle<int> MixerStrips::meterArea (int t) const
  168. {
  169. return faderArea (t).withWidth (meterWidth * 2 + 2);
  170. }
  171. juce::Rectangle<int> MixerStrips::faderRail (int t) const
  172. {
  173. return faderArea (t).withTrimmedLeft (meterWidth * 2 + 6);
  174. }
  175. juce::Rectangle<int> MixerStrips::routeArea (int t) const
  176. {
  177. return { stripX (t) + 5, laneTop() + 2, stripWidthOf (t) - 14, switchHeight };
  178. }
  179. juce::Rectangle<int> MixerStrips::sendArea (int t) const
  180. {
  181. return { stripX (t), laneTop() + 2 + switchHeight, stripWidthOf (t) - 4, sendKnobHeight };
  182. }
  183. MixerStrips::Part MixerStrips::partAt (int t, juce::Point<int> p) const
  184. {
  185. const auto& song = engine.getSong();
  186. if (t == (int) song.mixer.size())
  187. return Part::add;
  188. if (t < 0 || t > (int) song.mixer.size())
  189. return Part::none;
  190. const auto sending = selected > 0 && t != selected && selected < (int) song.mixer.size();
  191. if (t > 0 && armArea (t).expanded (1).contains (p))
  192. return Part::arm;
  193. if (t > 0 && t < (int) engine.getSong().mixer.size() && engine.getSong().mixer[(std::size_t) t].inputChannel >= 0
  194. && inputArea (t).expanded (0, 2).contains (p))
  195. return Part::input;
  196. if (headerArea (t).contains (p))
  197. return Part::header;
  198. if (panArea (t).contains (p))
  199. return Part::pan;
  200. if (stereoArea (t).contains (p))
  201. return Part::stereo;
  202. if (muteArea (t).contains (p))
  203. return Part::mute;
  204. if (t > 0 && soloArea (t).contains (p))
  205. return Part::solo;
  206. if (sending && routeArea (t).contains (p) && (song.routes (selected, t) || song.canRoute (selected, t)))
  207. return Part::route;
  208. if (sending && t > 0 && sendArea (t).contains (p) && song.routes (selected, t))
  209. return Part::send;
  210. if (meterArea (t).contains (p))
  211. return Part::meter;
  212. if (faderRail (t).expanded (0, 6).contains (p))
  213. return Part::fader;
  214. return Part::body;
  215. }
  216. juce::Rectangle<int> MixerStrips::tipArea (Part part, int t) const
  217. {
  218. switch (part)
  219. {
  220. case Part::pan: return panArea (t);
  221. case Part::stereo: return stereoArea (t);
  222. case Part::send: return sendArea (t);
  223. case Part::route: return routeArea (t);
  224. case Part::fader:
  225. {
  226. const auto rail = faderRail (t);
  227. const auto& mixer = engine.getSong().mixer;
  228. const auto v = t >= 0 && t < (int) mixer.size() ? mixer[(std::size_t) t].volume : 0.0f;
  229. const auto capY = rail.getBottom() - (int) (v * (float) rail.getHeight());
  230. return { rail.getX(), capY - 6, rail.getWidth(), 12 };
  231. }
  232. default: return {};
  233. }
  234. }
  235. void MixerStrips::showTip (Part part, int t, bool transient)
  236. {
  237. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  238. return;
  239. const auto spec = specFor (part, t);
  240. const auto text = spec.text (valueOf (part, t));
  241. ui::postHint (ui::valueHint (spec, valueOf (part, t), ui::valueGestures()));
  242. if (transient)
  243. ui::flashValueTip (*this, tipArea (part, t), text);
  244. else
  245. ui::showValueTip (*this, tipArea (part, t), text);
  246. }
  247. void MixerStrips::setHover (Part part, int t)
  248. {
  249. if (part == hoverPart && t == hoverTrack)
  250. return;
  251. auto dirty = [this] (int track) {
  252. if (track >= 0)
  253. repaint (stripX (track) - 2, 0, stripWidthOf (track) + 4, getHeight());
  254. };
  255. dirty (hoverTrack);
  256. hoverPart = part;
  257. hoverTrack = t;
  258. dirty (hoverTrack);
  259. }
  260. void MixerStrips::mouseMove (const juce::MouseEvent& e)
  261. {
  262. const auto t = trackAt (e.x);
  263. setHover (t >= 0 ? partAt (t, e.getPosition()) : Part::none, t);
  264. }
  265. void MixerStrips::mouseExit (const juce::MouseEvent&)
  266. {
  267. setHover (Part::none, -1);
  268. }
  269. void MixerStrips::paint (juce::Graphics& g)
  270. {
  271. using namespace theme;
  272. const auto& song = engine.getSong();
  273. const auto tracks = (int) song.mixer.size();
  274. const auto clip = g.getClipBounds();
  275. g.fillAll (colour::bg1);
  276. // the routing lane runs under all strips
  277. g.setColour (colour::bg0.withAlpha (0.55f));
  278. g.fillRect (0, laneTop() + switchHeight + sendKnobHeight + 2, getWidth(), wiresHeight + 2);
  279. for (int t = 0; t < tracks; ++t)
  280. {
  281. const auto x = stripX (t);
  282. const auto w = stripWidthOf (t);
  283. if (x > clip.getRight() || x + w < clip.getX())
  284. continue;
  285. const auto& track = song.mixer[(std::size_t) t];
  286. const auto strip = juce::Rectangle<int> (x, 2, w - 4, laneTop() - 2);
  287. const auto isSelected = t == selected;
  288. const auto inSelection = std::binary_search (selection.begin(), selection.end(), t);
  289. const auto stripHovered = hoverTrack == t;
  290. const auto hovered = [this, t] (Part p) { return hoverTrack == t && hoverPart == p; };
  291. const auto dragging = [this, t] (Part p) { return dragTrack == t && dragPart == p; };
  292. // the strip is tinted with the insert's colour (FL tints whole strips, so groups read at a glance);
  293. // kept dark so meters, knobs and text keep their contrast
  294. auto plate = isSelected || inSelection ? colour::bg4 : (t == 0 ? colour::bg3 : stripHovered ? colour::bg3 : colour::bg2);
  295. if (track.colour != 0)
  296. plate = plate.interpolatedWith (tame (juce::Colour (track.colour)), isSelected ? 0.26f : 0.2f);
  297. g.setColour (plate);
  298. g.fillRoundedRectangle (strip.toFloat(), radius::md);
  299. if (track.colour != 0) // thin colour band at the top
  300. {
  301. g.setColour (tame (juce::Colour (track.colour)));
  302. g.fillRoundedRectangle (strip.toFloat().withHeight (3.0f).reduced (6.0f, 0.0f), 1.5f);
  303. }
  304. if (inSelection && ! isSelected)
  305. {
  306. g.setColour (colour::accent.withAlpha (0.3f));
  307. g.drawRoundedRectangle (strip.toFloat().reduced (0.5f), radius::md, 1.0f);
  308. }
  309. if (isSelected)
  310. {
  311. g.setColour (colour::accent.withAlpha (0.55f));
  312. g.drawRoundedRectangle (strip.toFloat().reduced (0.5f), radius::md, 1.0f);
  313. g.setColour (colour::accent);
  314. g.fillRoundedRectangle (strip.toFloat().withHeight (3.0f).reduced (6.0f, 0.0f), 1.5f);
  315. }
  316. if (headerDragged && headerDragTrack == t)
  317. {
  318. g.setColour (colour::accent);
  319. g.drawRoundedRectangle (strip.toFloat().reduced (0.5f), radius::md, 1.5f);
  320. }
  321. // record arm: the input recording goes through this insert
  322. if (t > 0)
  323. {
  324. const auto armed = engine.isInsertArmed (t);
  325. const auto r = armArea (t).toFloat().reduced (2.0f);
  326. g.setColour (armed ? colour::record : hovered (Part::arm) ? colour::bg5 : colour::bg0);
  327. g.fillEllipse (r);
  328. g.setColour (armed ? colour::record.brighter (0.3f) : hovered (Part::arm) ? colour::textSecondary : colour::borderStrong);
  329. g.drawEllipse (r, 1.0f);
  330. // the input level (inserts with an audio input source)
  331. if (track.inputChannel >= 0 && (std::size_t) t < inputLevels.size())
  332. {
  333. const auto bar = inputArea (t).toFloat();
  334. g.setColour (colour::bg0);
  335. g.fillRoundedRectangle (bar, 1.0f);
  336. const auto level = inputLevels[(std::size_t) t];
  337. const auto fill = juce::jlimit (0.0f, 1.0f, (juce::Decibels::gainToDecibels (level, -60.0f) + 60.0f) / 60.0f);
  338. g.setColour (level >= 0.99f ? colour::record : colour::meterLow);
  339. g.fillRoundedRectangle (bar.withWidth (bar.getWidth() * fill), 1.0f);
  340. }
  341. }
  342. // header: number and effect count on one line, the name (once) below
  343. g.setColour (isSelected ? colour::accent : colour::textSecondary);
  344. g.setFont (font (type::caption, isSelected));
  345. g.drawText (t == 0 ? juce::String ("M") : juce::String (t), x, 6, w - 4, 13, juce::Justification::centred);
  346. if (! track.effects.empty())
  347. {
  348. const auto on = mixerops::anyEffectEnabled (song, t);
  349. g.setColour (on ? colour::info : colour::textDisabled);
  350. g.setFont (font (9.0f, true));
  351. g.drawText ("FX" + juce::String ((int) track.effects.size()), x + w - 30, 6, 23, 13, juce::Justification::centredRight);
  352. }
  353. g.setColour (track.muted ? colour::textDisabled : (hovered (Part::header) ? colour::textPrimary : colour::textPrimary.withAlpha (0.9f)));
  354. g.setFont (font (type::small, isSelected));
  355. g.drawFittedText (juce::String::fromUTF8 (track.name.c_str()), x + 3, 20, w - 10, 14, juce::Justification::centred, 1, 0.7f);
  356. // compensated latency (PDC) on the Master
  357. if (t == 0 && engine.getLatencySamples() > 0)
  358. {
  359. const auto ms = 1000.0 * engine.getLatencySamples() / juce::jmax (1.0, engine.getTransport().sampleRate.load());
  360. g.setColour (colour::textSecondary);
  361. g.setFont (font (10.0f));
  362. g.drawFittedText ("PDC " + juce::String (ms, 1) + " ms", soloArea (t).withX (x + 30).withWidth (w - 36),
  363. juce::Justification::centredLeft, 1, 0.8f); // the Master has no solo: use its spot
  364. }
  365. // pan and stereo separation
  366. drawKnob (g, panArea (t).toFloat().withSizeKeepingCentre (26.0f, 26.0f), (track.pan + 1.0f) * 0.5f, true,
  367. dragging (Part::pan) ? colour::accent : colour::info, hovered (Part::pan) || dragging (Part::pan));
  368. drawKnob (g, stereoArea (t).toFloat().withSizeKeepingCentre (22.0f, 22.0f), (track.stereo + 1.0f) * 0.5f, true,
  369. dragging (Part::stereo) ? colour::accent : colour::knobArc, hovered (Part::stereo) || dragging (Part::stereo));
  370. // mute and solo
  371. {
  372. const auto m = muteArea (t).toFloat().withSizeKeepingCentre (12.0f, 12.0f);
  373. g.setColour (muteLed (track.muted));
  374. g.fillEllipse (m);
  375. if (! track.muted || hovered (Part::mute))
  376. {
  377. g.setColour (hovered (Part::mute) ? colour::textSecondary : colour::ledOn.withAlpha (0.25f));
  378. g.drawEllipse (m.expanded (2.0f), 1.0f);
  379. }
  380. if (t > 0)
  381. {
  382. const auto s = soloArea (t).toFloat().reduced (2.0f);
  383. g.setColour (track.solo ? colour::accent : hovered (Part::solo) ? colour::bg5 : colour::bg0);
  384. g.fillRoundedRectangle (s, radius::sm);
  385. g.setColour (track.solo ? colour::onAccent : hovered (Part::solo) ? colour::textPrimary : colour::textSecondary);
  386. g.setFont (font (type::caption, true));
  387. g.drawText ("S", s.toNearestInt(), juce::Justification::centred);
  388. }
  389. }
  390. // meters + fader
  391. {
  392. const auto area = faderArea (t);
  393. const auto meters = meterArea (t);
  394. const auto fader = faderRail (t);
  395. for (int ch = 0; ch < 2; ++ch)
  396. {
  397. const auto bar = juce::Rectangle<int> (meters.getX() + ch * (meterWidth + 2), meters.getY(), meterWidth, meters.getHeight());
  398. g.setColour (colour::bg0);
  399. g.fillRoundedRectangle (bar.toFloat(), 1.5f);
  400. const auto index = (std::size_t) t * 2 + (std::size_t) ch;
  401. const auto meter = index < levels.size() ? levels[index] : meters::PeakHold {};
  402. const auto h = (int) (meterPosition (meter.level) * (float) bar.getHeight());
  403. const auto lit = bar.withTop (bar.getBottom() - h);
  404. juce::ColourGradient gradient (colour::meterClip, (float) bar.getX(), (float) bar.getY(), colour::meterLow, (float) bar.getX(),
  405. (float) bar.getBottom(), false);
  406. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (0.0f)), colour::meterClip); // above 0 dB: clip zone
  407. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (-6.0f)), colour::meterMid);
  408. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (-18.0f)), colour::meterLow);
  409. g.setGradientFill (gradient);
  410. g.fillRect (lit);
  411. if (meter.peak > 0.001f) // peak hold line
  412. {
  413. const auto peakY = bar.getBottom() - (int) (meterPosition (meter.peak) * (float) bar.getHeight());
  414. g.setColour (meter.peak >= 1.0f ? colour::meterClip : meter.peak >= 0.5f ? colour::meterMid : colour::textPrimary.withAlpha (0.85f));
  415. g.fillRect (bar.getX(), juce::jmax (bar.getY(), peakY - 1), bar.getWidth(), 2);
  416. }
  417. if (meter.clipped) // clip latch: stays until the meter is clicked
  418. {
  419. g.setColour (colour::meterClip);
  420. g.fillRect (bar.withHeight (4));
  421. }
  422. }
  423. if (hovered (Part::meter))
  424. {
  425. g.setColour (colour::textSecondary.withAlpha (0.5f));
  426. g.drawRect (meters.expanded (1), 1);
  427. }
  428. // fader track and 0 dB mark
  429. const auto railX = (float) fader.getCentreX();
  430. const auto capY = (float) fader.getBottom() - track.volume * (float) fader.getHeight();
  431. g.setColour (colour::bg0);
  432. g.fillRoundedRectangle (railX - 2.0f, (float) fader.getY(), 4.0f, (float) fader.getHeight(), 2.0f);
  433. g.setColour (colour::bg6.brighter (0.15f).withAlpha (track.muted ? 0.4f : 1.0f));
  434. g.fillRoundedRectangle (railX - 2.0f, capY, 4.0f, juce::jmax (0.0f, (float) fader.getBottom() - capY), 2.0f);
  435. const auto unityY = (float) fader.getBottom() - unityFader * (float) fader.getHeight();
  436. g.setColour (colour::textDisabled);
  437. g.drawHorizontalLine ((int) unityY, (float) fader.getX(), railX - 5.0f);
  438. g.drawHorizontalLine ((int) unityY, railX + 5.0f, (float) fader.getRight());
  439. const auto capActive = hovered (Part::fader) || dragging (Part::fader);
  440. const auto capWidth = juce::jmin ((float) fader.getWidth() + 2.0f, 26.0f);
  441. const auto cap = juce::Rectangle<float> (capWidth, 12.0f).withCentre ({ railX, capY });
  442. g.setColour (juce::Colours::black.withAlpha (0.35f));
  443. g.fillRoundedRectangle (cap.translated (0.0f, 1.5f), radius::sm);
  444. g.setColour (capActive ? juce::Colour (0xffffffff) : juce::Colour (0xffd4d7dd)); // same cap as the LookAndFeel's linear faders
  445. g.fillRoundedRectangle (cap, radius::sm);
  446. g.setColour ((dragging (Part::fader) ? colour::accent : colour::bg0).withAlpha (0.8f));
  447. g.fillRect (cap.withSizeKeepingCentre (capWidth - 8.0f, 1.5f));
  448. g.setColour (colour::textSecondary);
  449. g.setFont (font (type::caption));
  450. g.drawText (faderToDb (track.volume) + " dB", x, area.getBottom() + 3, w - 4, readoutHeight - 2, juce::Justification::centred);
  451. }
  452. // routing lane: send switch from the selected insert to this one, and its level knob
  453. if (selected > 0 && t != selected && selected < tracks)
  454. {
  455. const auto r = routeArea (t).toFloat();
  456. const auto on = song.routes (selected, t);
  457. const auto possible = on || song.canRoute (selected, t);
  458. const auto feedsSelected = t > 0 && song.routes (t, selected);
  459. if (possible)
  460. {
  461. g.setColour (on ? colour::accent : hovered (Part::route) ? colour::bg5 : colour::bg0);
  462. g.fillRoundedRectangle (r, radius::sm);
  463. if (hovered (Part::route))
  464. {
  465. g.setColour (on ? colour::accentHover : colour::textSecondary);
  466. g.drawRoundedRectangle (r.reduced (0.5f), radius::sm, 1.0f);
  467. }
  468. g.setColour (on ? colour::onAccent : colour::textSecondary);
  469. g.setFont (font (10.0f, on));
  470. g.drawText (juce::String::fromUTF8 ("\xe2\x86\x92 ") + (t == 0 ? juce::String ("M") : juce::String (t)), r.toNearestInt(),
  471. juce::Justification::centred);
  472. if (on && t > 0)
  473. for (const auto& send : song.mixer[(std::size_t) selected].sends)
  474. if (send.target == t)
  475. drawKnob (g, sendArea (t).toFloat().withSizeKeepingCentre (22.0f, 22.0f), send.level, false,
  476. dragging (Part::send) ? colour::accent : colour::accentHover, hovered (Part::send) || dragging (Part::send));
  477. }
  478. else if (feedsSelected) // this insert sends into the selected one
  479. {
  480. g.setColour (colour::info.withAlpha (0.18f));
  481. g.fillRoundedRectangle (r, radius::sm);
  482. g.setColour (colour::info);
  483. g.setFont (font (10.0f, true));
  484. g.drawText (juce::String::fromUTF8 ("\xe2\x86\x92 ") + juce::String (selected), r.toNearestInt(), juce::Justification::centred);
  485. }
  486. }
  487. }
  488. paintRouting (g);
  489. // "+" button
  490. if (tracks < maxMixerTracks)
  491. {
  492. const auto add = juce::Rectangle<int> (stripX (tracks), 2, stripWidth - 4, 60).toFloat();
  493. const auto addHovered = hoverPart == Part::add;
  494. g.setColour (addHovered ? colour::bg4 : colour::bg2);
  495. g.fillRoundedRectangle (add, radius::md);
  496. g.setColour (addHovered ? colour::borderStrong : colour::border);
  497. g.drawRoundedRectangle (add.reduced (0.5f), radius::md, 1.0f);
  498. icons::draw (g, Icon::plus, add.withSizeKeepingCentre (22.0f, 22.0f), addHovered ? colour::textPrimary : colour::textSecondary);
  499. }
  500. }
  501. // Wires in the lane under the strips: from the selected insert to where it sends (accent), and from the
  502. // inserts that feed it (blue). The Master output is drawn dimmer.
  503. void MixerStrips::paintRouting (juce::Graphics& g)
  504. {
  505. using namespace theme;
  506. const auto& song = engine.getSong();
  507. const auto tracks = (int) song.mixer.size();
  508. if (selected <= 0 || selected >= tracks)
  509. return;
  510. const auto top = (float) (laneTop() + switchHeight + sendKnobHeight + 3);
  511. const auto bottom = top + (float) wiresHeight - 2.0f;
  512. const auto centre = [this] (int t) { return (float) stripX (t) + (float) (stripWidthOf (t) - 4) * 0.5f; };
  513. auto wire = [&] (int from, int to, juce::Colour c, float thickness) {
  514. const auto x0 = centre (from), x1 = centre (to);
  515. juce::Path p;
  516. p.startNewSubPath (x0, top);
  517. p.cubicTo (x0, bottom + 2.0f, x1, bottom + 2.0f, x1, top);
  518. g.setColour (c);
  519. g.strokePath (p, juce::PathStrokeType (thickness, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  520. g.fillEllipse (x1 - 2.5f, top - 2.5f, 5.0f, 5.0f);
  521. };
  522. const auto& from = song.mixer[(std::size_t) selected];
  523. for (int t = 1; t < tracks; ++t)
  524. if (t != selected && song.routes (t, selected))
  525. wire (t, selected, colour::info.withAlpha (0.85f), 1.5f);
  526. if (from.toMaster)
  527. wire (selected, 0, colour::accent.withAlpha (0.4f), 1.0f);
  528. for (const auto& send : from.sends)
  529. if (send.target > 0 && send.target < tracks)
  530. wire (selected, send.target, colour::accent.withAlpha (0.5f + 0.5f * juce::jlimit (0.0f, 1.0f, send.level)), 1.8f);
  531. g.setColour (colour::accent);
  532. g.fillEllipse (centre (selected) - 3.0f, top - 3.0f, 6.0f, 6.0f);
  533. }
  534. void MixerStrips::mouseDown (const juce::MouseEvent& e)
  535. {
  536. const auto t = trackAt (e.x);
  537. if (t < 0)
  538. return;
  539. const auto part = partAt (t, e.getPosition());
  540. headerDragTrack = -1;
  541. headerDragged = false;
  542. if (part == Part::add)
  543. {
  544. engine.editSong ([] (Song& s) {
  545. if (s.mixer.empty())
  546. s.mixer = Song::defaultMixer (0);
  547. auto& track = s.mixer.emplace_back();
  548. track.name = "Insert " + std::to_string (s.mixer.size() - 1);
  549. });
  550. return;
  551. }
  552. if (part == Part::arm)
  553. {
  554. if (e.mods.isPopupMenu())
  555. mixerinput::showMenu (engine, t, this);
  556. else
  557. mixerinput::toggleArm (engine, t, this);
  558. repaint();
  559. return;
  560. }
  561. if (part == Part::input)
  562. {
  563. mixerinput::showMenu (engine, t, this);
  564. return;
  565. }
  566. if (part == Part::meter && ! e.mods.isPopupMenu()) // clears the clip latch and the peak hold
  567. {
  568. for (int ch = 0; ch < 2; ++ch)
  569. if (const auto i = (std::size_t) t * 2 + (std::size_t) ch; i < levels.size())
  570. levels[i].resetClip();
  571. repaint (meterArea (t).expanded (2));
  572. return;
  573. }
  574. if (part == Part::route && e.mods.isPopupMenu())
  575. {
  576. showSendMenu (t);
  577. return;
  578. }
  579. if (part == Part::send)
  580. {
  581. const auto from = selected;
  582. if (e.mods.isPopupMenu())
  583. {
  584. juce::PopupMenu menu;
  585. const auto spec = specFor (Part::send, t);
  586. menu.addSectionHeader (spec.name + ": " + spec.text (valueOf (Part::send, t)));
  587. ui::addValueItems (menu, *this, nullptr, spec, valueOf (Part::send, t), [this, t] (double v) { setValue (Part::send, t, v); });
  588. menu.addSeparator();
  589. menu.addItem (tr ("Remove this send"), [this, from, t] { engine.editSong ([from, t] (Song& s) { s.setRoute (from, t, false); }); });
  590. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  591. return;
  592. }
  593. if (ui::isResetClick (e))
  594. {
  595. setValue (Part::send, t, 1.0);
  596. showTip (Part::send, t, true);
  597. return;
  598. }
  599. dragPart = Part::send;
  600. dragTrack = t;
  601. drag.begin (e, valueOf (Part::send, t));
  602. showTip (Part::send, t, false);
  603. return;
  604. }
  605. if (part == Part::mute && (e.mods.isPopupMenu() || e.mods.isCtrlDown())) // solo on the same LED
  606. {
  607. soloExclusive (t);
  608. return;
  609. }
  610. if (part == Part::route)
  611. {
  612. // the selection is the send source: don't change it
  613. const auto from = selected;
  614. const auto on = engine.getSong().routes (from, t);
  615. engine.editSong ([from, t, on] (Song& s) { s.setRoute (from, t, ! on); });
  616. if (! on && t > 0) // just enabled: dragging adjusts the level
  617. {
  618. dragPart = Part::route;
  619. dragTrack = t;
  620. dragStartValue = 1.0f;
  621. }
  622. return;
  623. }
  624. if ((part == Part::body || part == Part::header) && ! e.mods.isPopupMenu() && (e.mods.isCtrlDown() || e.mods.isShiftDown()) && t > 0)
  625. {
  626. auto s = selection;
  627. if (e.mods.isShiftDown())
  628. {
  629. s.clear();
  630. for (int i = juce::jmin (selected, t); i <= juce::jmax (selected, t); ++i)
  631. s.push_back (i);
  632. }
  633. else if (auto it = std::find (s.begin(), s.end(), t); it != s.end() && s.size() > 1)
  634. s.erase (it);
  635. else
  636. s.push_back (t);
  637. setSelection (s, std::find (s.begin(), s.end(), t) != s.end() ? t : s.front());
  638. return;
  639. }
  640. // a plain click on a track outside the selection selects only it; inside, the group stays
  641. if (! std::binary_search (selection.begin(), selection.end(), t))
  642. setSelection ({ t }, t);
  643. else if (selected != t)
  644. setSelection (selection, t);
  645. if (part == Part::pan || part == Part::fader || part == Part::stereo)
  646. {
  647. if (e.mods.isPopupMenu())
  648. {
  649. ui::showValueMenu (*this, part == Part::stereo ? nullptr : &engine, specFor (part, t), valueOf (part, t),
  650. [this, part, t] (double v) { setValue (part, t, v); });
  651. return;
  652. }
  653. if (ui::isResetClick (e))
  654. {
  655. setValue (part, t, specFor (part, t).defaultValue);
  656. showTip (part, t, true);
  657. return;
  658. }
  659. }
  660. if (e.mods.isPopupMenu())
  661. {
  662. showMenu (t);
  663. return;
  664. }
  665. const auto& track = engine.getSong().mixer[(std::size_t) t];
  666. switch (part)
  667. {
  668. case Part::mute:
  669. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].muted = ! s.mixer[(std::size_t) t].muted; });
  670. return;
  671. case Part::solo:
  672. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].solo = ! s.mixer[(std::size_t) t].solo; });
  673. return;
  674. case Part::header:
  675. if (t > 0)
  676. headerDragTrack = t; // dragging sideways reorders
  677. return;
  678. case Part::stereo:
  679. dragPart = part;
  680. dragTrack = t;
  681. dragStartValue = track.stereo;
  682. drag.begin (e, dragStartValue);
  683. groupStart.clear();
  684. showTip (part, t, false);
  685. return;
  686. case Part::pan:
  687. case Part::fader:
  688. dragPart = part;
  689. dragTrack = t;
  690. dragStartValue = part == Part::pan ? track.pan : track.volume;
  691. if (part == Part::fader && ! e.mods.isCtrlDown()) // clicking the track jumps the fader there
  692. {
  693. const auto area = faderRail (t);
  694. const auto capY = area.getBottom() - track.volume * (float) area.getHeight();
  695. if (std::abs ((float) e.y - capY) > 9.0f)
  696. {
  697. dragStartValue = juce::jlimit (0.0f, 1.0f, (float) (area.getBottom() - e.y) / (float) area.getHeight());
  698. engine.editSong ([t, v = dragStartValue] (Song& s) { s.mixer[(std::size_t) t].volume = v; });
  699. }
  700. }
  701. drag.begin (e, dragStartValue);
  702. groupStart.clear();
  703. groupGrab = dragStartValue;
  704. if (selection.size() > 1) // the selected strips follow, keeping their offsets
  705. for (auto s : selection)
  706. if (s != t && s < (int) engine.getSong().mixer.size())
  707. groupStart.push_back ({ s, part == Part::pan ? engine.getSong().mixer[(std::size_t) s].pan
  708. : engine.getSong().mixer[(std::size_t) s].volume });
  709. showTip (part, t, false);
  710. return;
  711. case Part::arm:
  712. case Part::route:
  713. case Part::send:
  714. case Part::meter:
  715. case Part::body:
  716. case Part::none:
  717. case Part::add:
  718. return;
  719. }
  720. }
  721. void MixerStrips::mouseDrag (const juce::MouseEvent& e)
  722. {
  723. if (headerDragTrack > 0)
  724. {
  725. if (! headerDragged && std::abs (e.getDistanceFromDragStartX()) < 6)
  726. return;
  727. headerDragged = true;
  728. setMouseCursor (juce::MouseCursor::LeftRightResizeCursor);
  729. const auto count = (int) engine.getSong().mixer.size();
  730. const auto target = juce::jlimit (1, juce::jmax (1, count - 1), trackAt (juce::jlimit (stripX (1), stripX (count) - 1, e.x)));
  731. for (int guard = 0; guard < count && selected != target; ++guard)
  732. if (! moveSelected (target < selected ? -1 : 1))
  733. break;
  734. headerDragTrack = selected;
  735. repaint();
  736. return;
  737. }
  738. if (dragPart == Part::none)
  739. return;
  740. const auto t = dragTrack;
  741. if (dragPart == Part::route)
  742. {
  743. const auto from = selected;
  744. const auto v = juce::jlimit (0.0f, 1.0f, dragStartValue - (float) e.getDistanceFromDragStartY() / 120.0f);
  745. engine.editSong ([from, t, v] (Song& s) { s.setRoute (from, t, true, v); });
  746. ui::showValueTip (*this, routeArea (t), juce::String (juce::roundToInt (v * 100.0f)) + "%");
  747. return;
  748. }
  749. setValue (dragPart, t, drag.update (e, specFor (dragPart, t)));
  750. showTip (dragPart, t, false);
  751. }
  752. ui::ValueSpec MixerStrips::specFor (Part part, int t) const
  753. {
  754. const auto& song = engine.getSong();
  755. const auto nameOf = [&song] (int track) {
  756. return track >= 0 && track < (int) song.mixer.size() ? juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str()) : juce::String();
  757. };
  758. const auto name = nameOf (t);
  759. if (part == Part::pan)
  760. {
  761. auto spec = ui::panSpec (name + " - " + tr ("Pan"));
  762. spec.pixelsForRange = 150.0;
  763. spec.target = AutomationTarget { AutomationTargetKind::mixerPan, t };
  764. return spec;
  765. }
  766. if (part == Part::stereo)
  767. return stereoSpec (name + " - " + tr ("Stereo separation"));
  768. if (part == Part::send)
  769. return sendSpec (nameOf (selected) + juce::String::fromUTF8 (" \xe2\x86\x92 ") + name + " - " + tr ("Send level"));
  770. auto spec = ui::faderSpec (name + " - " + tr ("Volume"), unityFader);
  771. spec.pixelsForRange = juce::jmax (60, faderRail (juce::jmax (0, t)).getHeight());
  772. spec.target = AutomationTarget { AutomationTargetKind::mixerVolume, t };
  773. return spec;
  774. }
  775. double MixerStrips::valueOf (Part part, int t) const
  776. {
  777. const auto& song = engine.getSong();
  778. const auto& track = song.mixer[(std::size_t) t];
  779. if (part == Part::send)
  780. {
  781. if (selected > 0 && selected < (int) song.mixer.size())
  782. for (const auto& send : song.mixer[(std::size_t) selected].sends)
  783. if (send.target == t)
  784. return send.level;
  785. return 0.0;
  786. }
  787. return part == Part::pan ? track.pan : part == Part::stereo ? track.stereo : track.volume;
  788. }
  789. void MixerStrips::setValue (Part part, int t, double value)
  790. {
  791. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  792. return;
  793. if (part == Part::send)
  794. {
  795. const auto from = selected;
  796. engine.editSong ([from, t, v = (float) value] (Song& s) { s.setRoute (from, t, true, v); });
  797. return;
  798. }
  799. if (part == Part::stereo)
  800. {
  801. engine.editSong ([t, v = (float) value] (Song& s) { s.mixer[(std::size_t) t].stereo = v; });
  802. return;
  803. }
  804. // during a drag of a selected strip, the other selected strips move by the same amount
  805. const auto delta = (float) value - groupGrab;
  806. const auto group = dragTrack == t && dragPart == part ? groupStart : std::vector<std::pair<int, float>> {};
  807. if (part == Part::pan)
  808. engine.editSong ([t, v = (float) value, group, delta] (Song& s) {
  809. s.mixer[(std::size_t) t].pan = v;
  810. for (auto [other, start] : group)
  811. if (other < (int) s.mixer.size())
  812. s.mixer[(std::size_t) other].pan = juce::jlimit (-1.0f, 1.0f, start + delta);
  813. });
  814. else if (part == Part::fader)
  815. engine.editSong ([t, v = (float) value, group, delta] (Song& s) {
  816. s.mixer[(std::size_t) t].volume = v;
  817. for (auto [other, start] : group)
  818. if (other < (int) s.mixer.size())
  819. s.mixer[(std::size_t) other].volume = juce::jlimit (0.0f, 1.0f, start + delta);
  820. });
  821. }
  822. void MixerStrips::setSelection (std::vector<int> tracks, int primary)
  823. {
  824. const auto count = (int) engine.getSong().mixer.size();
  825. tracks.erase (std::remove_if (tracks.begin(), tracks.end(), [count] (int t) { return t < 0 || t >= count; }), tracks.end());
  826. primary = juce::jlimit (0, juce::jmax (0, count - 1), primary);
  827. tracks.push_back (primary);
  828. std::sort (tracks.begin(), tracks.end());
  829. tracks.erase (std::unique (tracks.begin(), tracks.end()), tracks.end());
  830. selection = tracks;
  831. const auto changed = primary != selected;
  832. selected = primary;
  833. repaint();
  834. if (changed && onSelectionChanged)
  835. onSelectionChanged (selected);
  836. }
  837. bool MixerStrips::moveSelected (int delta)
  838. {
  839. auto moved = selection;
  840. bool ok = false;
  841. engine.editSong ([&] (Song& s) { ok = mixerops::moveTracks (s, moved, delta); });
  842. if (ok)
  843. setSelection (moved, juce::jlimit (1, (int) engine.getSong().mixer.size() - 1, selected + (delta < 0 ? -1 : 1)));
  844. return ok;
  845. }
  846. void MixerStrips::linkSelectedChannels (bool startingHere)
  847. {
  848. const auto channels = engine.getSelectedChannels();
  849. engine.editSong ([channels, t = selected, startingHere] (Song& s) { mixerops::linkChannels (s, channels, t, startingHere); });
  850. }
  851. void MixerStrips::selectLinkedChannels()
  852. {
  853. const auto channels = mixerops::channelsOn (engine.getSong(), selected);
  854. if (! channels.empty())
  855. engine.setChannelSelection (channels, channels.front());
  856. }
  857. void MixerStrips::soloExclusive (int t)
  858. {
  859. if (t <= 0)
  860. return;
  861. // solo only this track; again on the only soloed track clears the solo
  862. engine.editSong ([t] (Song& s) {
  863. const auto alone = s.mixer[(std::size_t) t].solo
  864. && std::none_of (s.mixer.begin() + 1, s.mixer.end(), [&] (const MixerTrack& m) { return &m != &s.mixer[(std::size_t) t] && m.solo; });
  865. for (std::size_t i = 1; i < s.mixer.size(); ++i)
  866. s.mixer[i].solo = ! alone && (int) i == t;
  867. });
  868. }
  869. void MixerStrips::showSendMenu (int t)
  870. {
  871. const auto& song = engine.getSong();
  872. const auto from = selected;
  873. if (from <= 0 || from == t)
  874. return;
  875. const auto fromName = juce::String::fromUTF8 (song.mixer[(std::size_t) from].name.c_str());
  876. juce::PopupMenu menu;
  877. menu.addSectionHeader (fromName + juce::String::fromUTF8 (" \xe2\x86\x92 ") + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()));
  878. const auto on = song.routes (from, t);
  879. menu.addItem (on ? tr ("Remove this send") : tr ("Send here"), on || song.canRoute (from, t), false, [this, from, t, on] {
  880. engine.editSong ([from, t, on] (Song& s) { s.setRoute (from, t, ! on); });
  881. });
  882. menu.addItem (tr ("Send only here"), on || song.canRoute (from, t), false, [this, from, t] {
  883. engine.editSong ([from, t] (Song& s) { mixerops::routeTo (s, { from }, t, true); });
  884. });
  885. menu.addItem (tr ("Sidechain only (no audio): the compressors here listen to it"), t > 0 && (on || song.canRoute (from, t)), false, [this, from, t] {
  886. int linked = 0;
  887. engine.editSong ([from, t, &linked] (Song& s) { linked = mixerops::sidechainTo (s, from, t); });
  888. if (linked == 0)
  889. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Sidechain"),
  890. tr ("The link was made; add a compressor to this insert to use it."));
  891. });
  892. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  893. }
  894. void MixerStrips::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  895. {
  896. const auto t = trackAt (e.x);
  897. if (t > 0 && t < (int) engine.getSong().mixer.size() && e.mods.isShiftDown())
  898. if (const auto part = partAt (t, e.getPosition()); part == Part::body || part == Part::header)
  899. {
  900. if (! std::binary_search (selection.begin(), selection.end(), t))
  901. setSelection ({ t }, t);
  902. moveSelected ((wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1);
  903. return;
  904. }
  905. if (t >= 0 && t < (int) engine.getSong().mixer.size())
  906. if (const auto part = partAt (t, e.getPosition()); part == Part::pan || part == Part::fader || part == Part::stereo || part == Part::send)
  907. {
  908. if (const auto v = ui::wheelValue (valueOf (part, t), e, wheel, specFor (part, t)))
  909. {
  910. setValue (part, t, *v);
  911. showTip (part, t, true);
  912. }
  913. return;
  914. }
  915. juce::Component::mouseWheelMove (e, wheel);
  916. }
  917. ui::Hint MixerStrips::hintAt (juce::Point<int> p)
  918. {
  919. const auto active = dragPart == Part::pan || dragPart == Part::fader || dragPart == Part::stereo || dragPart == Part::send;
  920. const auto t = active ? dragTrack : trackAt (p.x);
  921. const auto& song = engine.getSong();
  922. ui::Hint h;
  923. if (t < 0 || t >= (int) song.mixer.size())
  924. {
  925. if (t == (int) song.mixer.size())
  926. h.text = tr ("Add a mixer insert");
  927. return h;
  928. }
  929. const auto part = active ? dragPart : partAt (t, p);
  930. const auto name = juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str());
  931. const auto gesture = ui::valueGestures();
  932. // the channels that play into this insert (FL shows them by the insert; LYNX in the hint)
  933. auto feeds = [&song, t, tracks = (int) song.mixer.size()] {
  934. juce::StringArray names;
  935. for (const auto& c : song.channels)
  936. if (juce::jlimit (0, tracks - 1, c.mixerTrack) == t)
  937. names.add (juce::String::fromUTF8 (c.name.c_str()));
  938. if (names.isEmpty())
  939. return juce::String();
  940. const auto shown = names.size() > 6 ? names[0] + ", " + names[1] + ", " + names[2] + " +" + juce::String (names.size() - 3)
  941. : names.joinIntoString (", ");
  942. return " " + tr ("Channels: {0}", { shown });
  943. };
  944. switch (part)
  945. {
  946. case Part::pan:
  947. case Part::fader:
  948. case Part::stereo:
  949. case Part::send:
  950. return ui::valueHint (specFor (part, t), valueOf (part, t), gesture);
  951. case Part::mute: h.text = name + " - " + tr ("Click: mute. Right-click or Ctrl+click: solo"); break;
  952. case Part::arm: h.text = name + " - " + tr ("Record arm: Rec + Play records this insert's audio input into a Playlist clip [RMB] input and monitoring"); h.hasMenu = true; break;
  953. case Part::input: h.text = name + " - " + tr ("Input level of this insert's audio input [LMB] choose the input"); h.hasMenu = true; break;
  954. case Part::solo: h.text = name + " - " + tr ("Solo"); break;
  955. case Part::meter: h.text = name + " - " + tr ("Peak meter (left/right) with peak hold [LMB] clears the clip light"); break;
  956. case Part::route: h.text = name + " - " + tr ("Send from the selected insert to this one (drag after enabling: level; right-click: sidechain, only here)"); h.hasMenu = true; break;
  957. case Part::add: h.text = tr ("Add a mixer insert"); break;
  958. case Part::header:
  959. h.text = name + " - " + tr ("[LMB] selects [Ctrl]+[LMB] adds [Shift]+[LMB] range [LMB] drag sideways reorders [RMB] insert menu") + feeds();
  960. h.hasMenu = true;
  961. break;
  962. case Part::body:
  963. case Part::none:
  964. h.text = name + " - " + tr ("Click selects the insert (Ctrl: add, Shift: range), right-click opens its menu; Alt+Left/Right moves the selection") + feeds();
  965. h.hasMenu = true;
  966. break;
  967. }
  968. return h;
  969. }
  970. void MixerStrips::mouseUp (const juce::MouseEvent&)
  971. {
  972. if (headerDragged)
  973. setMouseCursor (juce::MouseCursor::NormalCursor);
  974. if (dragPart != Part::none || headerDragged)
  975. repaint();
  976. headerDragTrack = -1;
  977. headerDragged = false;
  978. dragPart = Part::none;
  979. dragTrack = -1;
  980. drag.end();
  981. ui::hideValueTip();
  982. ui::clearPostedHint();
  983. }
  984. void MixerStrips::mouseDoubleClick (const juce::MouseEvent& e)
  985. {
  986. const auto t = trackAt (e.x);
  987. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  988. return;
  989. const auto part = partAt (t, e.getPosition());
  990. if (part == Part::fader || part == Part::pan || part == Part::stereo || part == Part::send)
  991. {
  992. setValue (part, t, specFor (part, t).defaultValue);
  993. showTip (part, t, true);
  994. }
  995. }
  996. void MixerStrips::selectTrack (int track)
  997. {
  998. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), track);
  999. selection = { selected };
  1000. repaint();
  1001. if (onSelectionChanged)
  1002. onSelectionChanged (selected);
  1003. }
  1004. namespace
  1005. {
  1006. // The 3 knobs of each band of the track's built-in EQ (gain, frequency, width).
  1007. class TrackEqEditor final : public juce::Component
  1008. {
  1009. public:
  1010. TrackEqEditor (AudioEngine& e, int t)
  1011. : engine (e),
  1012. track (t)
  1013. {
  1014. const juce::String bands[] = { tr ("Low"), tr ("Mid"), tr ("High") };
  1015. const auto& eq = engine.getSong().mixer[(std::size_t) track].eq;
  1016. for (int b = 0; b < 3; ++b)
  1017. {
  1018. titles[(std::size_t) b].setText (bands[b], juce::dontSendNotification);
  1019. titles[(std::size_t) b].setJustificationType (juce::Justification::centred);
  1020. addAndMakeVisible (titles[(std::size_t) b]);
  1021. auto& gain = knobs[(std::size_t) b][0];
  1022. setup (gain, -18.0, 18.0, 0.1, " dB", eq.gainDb[(std::size_t) b]);
  1023. gain.setDoubleClickReturnValue (true, 0.0);
  1024. auto& hz = knobs[(std::size_t) b][1];
  1025. setup (hz, 10.0, 16000.0, 1.0, " Hz", eq.hz[(std::size_t) b]);
  1026. hz.setSkewFactorFromMidPoint (1000.0);
  1027. auto& width = knobs[(std::size_t) b][2];
  1028. setup (width, 0.0, 1.0, 0.01, "", eq.width[(std::size_t) b]);
  1029. width.setTooltip (b == 1 ? tr ("Peak width (Q)") : tr ("Shelf slope"));
  1030. }
  1031. setSize (3 * 90, 3 * 74 + 24);
  1032. }
  1033. void resized() override
  1034. {
  1035. auto area = getLocalBounds();
  1036. auto header = area.removeFromTop (20);
  1037. for (int b = 0; b < 3; ++b)
  1038. {
  1039. titles[(std::size_t) b].setBounds (header.removeFromLeft (90));
  1040. auto column = area.removeFromLeft (90);
  1041. for (auto& k : knobs[(std::size_t) b])
  1042. k.setBounds (column.removeFromTop (74));
  1043. }
  1044. }
  1045. private:
  1046. void setup (juce::Slider& s, double lo, double hi, double step, const char* suffix, float value)
  1047. {
  1048. s.setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
  1049. s.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 72, 16);
  1050. s.setRange (lo, hi, step);
  1051. s.setTextValueSuffix (suffix);
  1052. s.setValue (value, juce::dontSendNotification);
  1053. s.onValueChange = [this] { apply(); };
  1054. addAndMakeVisible (s);
  1055. }
  1056. void apply()
  1057. {
  1058. TrackEq eq;
  1059. for (std::size_t b = 0; b < 3; ++b)
  1060. {
  1061. eq.gainDb[b] = (float) knobs[b][0].getValue();
  1062. eq.hz[b] = (float) knobs[b][1].getValue();
  1063. eq.width[b] = (float) knobs[b][2].getValue();
  1064. }
  1065. engine.editSong ([t = track, eq] (Song& s) {
  1066. if (t < (int) s.mixer.size())
  1067. s.mixer[(std::size_t) t].eq = eq;
  1068. });
  1069. }
  1070. AudioEngine& engine;
  1071. int track;
  1072. std::array<juce::Label, 3> titles;
  1073. std::array<std::array<juce::Slider, 3>, 3> knobs; // [band][gain, frequency, width]
  1074. };
  1075. } // namespace
  1076. void MixerStrips::showTrackEq (int t)
  1077. {
  1078. auto editor = std::make_unique<TrackEqEditor> (engine, t);
  1079. juce::CallOutBox::launchAsynchronously (std::move (editor), getScreenBounds().withSize (10, 10).translated (getMouseXYRelative().x, getMouseXYRelative().y), nullptr);
  1080. }
  1081. void MixerStrips::showMenu (int t)
  1082. {
  1083. const auto& song = engine.getSong();
  1084. const auto channel = engine.getSelectedChannel();
  1085. juce::PopupMenu menu;
  1086. menu.addSectionHeader (juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()));
  1087. if (t > 0)
  1088. {
  1089. const auto& mt = song.mixer[(std::size_t) t];
  1090. const auto current = mt.inputChannel >= 0
  1091. ? juce::String::fromUTF8 (inputSourceName (mt.inputChannel, mt.inputStereo, engine.getInputChannelNames()).c_str())
  1092. : tr ("none");
  1093. menu.addItem (tr ("Audio input: {0}...", { current }), [this, t] { mixerinput::showMenu (engine, t, this); });
  1094. menu.addItem (engine.isInsertArmed (t) ? tr ("Disarm recording") : tr ("Arm recording"), [this, t] { mixerinput::toggleArm (engine, t, this); });
  1095. menu.addSeparator();
  1096. }
  1097. if (channel < (int) song.channels.size())
  1098. menu.addItem (tr ("Route channel \"{0}\" here", { juce::String::fromUTF8 (song.channels[(std::size_t) channel].name.c_str()) }),
  1099. [this, channel, t] { engine.editSong ([channel, t] (Song& s) { s.channels[(std::size_t) channel].mixerTrack = t; }); });
  1100. menu.addItem (tr ("Link the selected channels here") + "\tCtrl+L", [this, t] {
  1101. setSelection (selection, t);
  1102. linkSelectedChannels (false);
  1103. });
  1104. menu.addItem (tr ("Link the selected channels starting here") + "\tCtrl+Shift+L", [this, t] {
  1105. setSelection (selection, t);
  1106. linkSelectedChannels (true);
  1107. });
  1108. menu.addItem (tr ("Select the channels linked here") + "\tAlt+L", ! mixerops::channelsOn (song, t).empty(), false, [this, t] {
  1109. setSelection (selection, t);
  1110. selectLinkedChannels();
  1111. });
  1112. {
  1113. // route the other selected inserts into this one
  1114. std::vector<int> sources;
  1115. for (auto s : selection)
  1116. if (s > 0 && s != t)
  1117. sources.push_back (s);
  1118. if (! sources.empty())
  1119. {
  1120. menu.addItem (tr ("Route the selected inserts here"), [this, sources, t] {
  1121. engine.editSong ([sources, t] (Song& s) { mixerops::routeTo (s, sources, t, false); });
  1122. });
  1123. menu.addItem (tr ("Route the selected inserts only here"), [this, sources, t] {
  1124. engine.editSong ([sources, t] (Song& s) { mixerops::routeTo (s, sources, t, true); });
  1125. });
  1126. }
  1127. }
  1128. if (t > 0)
  1129. {
  1130. menu.addItem (tr ("Move left") + "\tAlt+Left", t > 1, false, [this, t] {
  1131. setSelection ({ t }, t);
  1132. moveSelected (-1);
  1133. });
  1134. menu.addItem (tr ("Move right") + "\tAlt+Right", t + 1 < (int) song.mixer.size(), false, [this, t] {
  1135. setSelection ({ t }, t);
  1136. moveSelected (1);
  1137. });
  1138. }
  1139. menu.addSeparator();
  1140. if (selected > 0 && selected != t && (t == 0 || song.canRoute (selected, t) || song.routes (selected, t)))
  1141. menu.addItem (tr ("Send \"{0}\" only to this track", { juce::String::fromUTF8 (song.mixer[(std::size_t) selected].name.c_str()) }),
  1142. [this, from = selected, t] {
  1143. engine.editSong ([from, t] (Song& s) {
  1144. s.mixer[(std::size_t) from].sends.clear();
  1145. s.mixer[(std::size_t) from].toMaster = false;
  1146. s.setRoute (from, t, true);
  1147. });
  1148. });
  1149. if (t > 0 && (! song.mixer[(std::size_t) t].sends.empty() || ! song.mixer[(std::size_t) t].toMaster))
  1150. menu.addItem (tr ("Send only to the Master again"), [this, t] {
  1151. engine.editSong ([t] (Song& s) {
  1152. s.mixer[(std::size_t) t].sends.clear();
  1153. s.mixer[(std::size_t) t].toMaster = true;
  1154. });
  1155. });
  1156. {
  1157. const auto& track = song.mixer[(std::size_t) t];
  1158. juce::PopupMenu stereo;
  1159. const std::pair<const char*, float> widths[] = { { TR_NOOP ("Mono"), 1.0f }, { TR_NOOP ("Narrower"), 0.5f }, { TR_NOOP ("Normal"), 0.0f },
  1160. { TR_NOOP ("Wider"), -0.5f }, { TR_NOOP ("Very wide"), -1.0f } };
  1161. for (const auto& [label, value] : widths)
  1162. stereo.addItem (tr (label), true, std::abs (track.stereo - value) < 0.01f,
  1163. [this, t, v = value] { engine.editSong ([t, v] (Song& s) { s.mixer[(std::size_t) t].stereo = v; }); });
  1164. menu.addSubMenu (tr ("Stereo separation"), stereo);
  1165. menu.addItem (tr ("Track EQ..."), [this, t] { showTrackEq (t); });
  1166. menu.addItem (tr ("Invert phase"), true, track.invertPhase, [this, t] {
  1167. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].invertPhase = ! s.mixer[(std::size_t) t].invertPhase; });
  1168. });
  1169. menu.addItem (tr ("Swap left/right"), true, track.swapChannels, [this, t] {
  1170. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].swapChannels = ! s.mixer[(std::size_t) t].swapChannels; });
  1171. });
  1172. menu.addSeparator();
  1173. }
  1174. if (t > 0)
  1175. menu.addItem (tr ("Render to audio (freeze)..."), [this, t] { bounceInsertToAudio (engine, t); });
  1176. menu.addItem (tr ("Rename..."), [this, t] {
  1177. auto* window = new juce::AlertWindow (tr ("Rename track"), {}, juce::MessageBoxIconType::NoIcon);
  1178. window->addTextEditor ("name", juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) t].name.c_str()));
  1179. window->addButton (tr ("OK"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  1180. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  1181. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, t, window] (int result) {
  1182. const auto name = window->getTextEditorContents ("name").trim().toStdString();
  1183. if (result == 1 && ! name.empty() && t < (int) engine.getSong().mixer.size())
  1184. engine.editSong ([t, name] (Song& s) { s.mixer[(std::size_t) t].name = name; });
  1185. }),
  1186. true);
  1187. });
  1188. menu.addItem (tr ("Reset (0 dB, center, no mute/solo)"), [this, t] {
  1189. engine.editSong ([t] (Song& s) {
  1190. auto& track = s.mixer[(std::size_t) t];
  1191. track.volume = unityFader;
  1192. track.pan = 0.0f;
  1193. track.muted = track.solo = false;
  1194. });
  1195. });
  1196. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  1197. }
  1198. void MixerStrips::changeListenerCallback (juce::ChangeBroadcaster*)
  1199. {
  1200. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), selected);
  1201. const auto count = (int) engine.getSong().mixer.size();
  1202. selection.erase (std::remove_if (selection.begin(), selection.end(), [count] (int t) { return t >= count; }), selection.end());
  1203. if (! std::binary_search (selection.begin(), selection.end(), selected))
  1204. selection = { selected };
  1205. updateSize();
  1206. repaint();
  1207. }
  1208. void MixerStrips::timerCallback()
  1209. {
  1210. auto& meters = engine.getMeters();
  1211. const auto showing = isShowing();
  1212. const auto& mixer = engine.getSong().mixer;
  1213. inputLevels.resize (mixer.size(), 0.0f);
  1214. for (std::size_t t = 1; t < mixer.size(); ++t)
  1215. {
  1216. const auto peak = mixer[t].inputChannel >= 0 ? juce::jmax (engine.takeInputPeak ((int) t, 0), engine.takeInputPeak ((int) t, 1)) : 0.0f;
  1217. auto level = juce::jmax (peak, inputLevels[t] * 0.8f);
  1218. level = level < 1e-4f ? 0.0f : level;
  1219. if (std::abs (level - inputLevels[t]) > 1e-4f)
  1220. {
  1221. inputLevels[t] = level;
  1222. if (showing)
  1223. repaint (inputArea ((int) t).expanded (1));
  1224. }
  1225. }
  1226. for (std::size_t i = 0; i < levels.size(); ++i)
  1227. {
  1228. auto& m = levels[i];
  1229. const auto before = m;
  1230. m.tick (meters.takePeak ((int) (i / 2), (int) (i % 2))); // ~20 dB/s falloff at 30 Hz, 1.5 s peak hold
  1231. // only the meter that changed is repainted
  1232. if (showing && (std::abs (m.level - before.level) > 1e-4f || std::abs (m.peak - before.peak) > 1e-4f || m.clipped != before.clipped))
  1233. repaint (meterArea ((int) (i / 2)).expanded (1));
  1234. }
  1235. }
  1236. // ---------------------------------------------------------------------------
  1237. namespace
  1238. {
  1239. constexpr int panelHeader = 30;
  1240. constexpr int inputRowHeight = 30; // the input row (arm, audio input source) above the slots
  1241. constexpr int slotsTop = panelHeader + inputRowHeight;
  1242. constexpr int slotHeight = 24;
  1243. const EffectKind allEffects[] = { EffectKind::eq, EffectKind::parametricEq, EffectKind::facetEq, EffectKind::compressor, EffectKind::limiter, EffectKind::apexLimiter,
  1244. EffectKind::delay, EffectKind::reverb, EffectKind::chorus, EffectKind::filter,
  1245. EffectKind::distortion, EffectKind::utility, EffectKind::volumeGate,
  1246. EffectKind::pitchShift, EffectKind::freqShift, EffectKind::lofi, EffectKind::effector, EffectKind::crossover, EffectKind::frequencySplitter, EffectKind::autoPan, EffectKind::vocoder, EffectKind::pitchCorrect,
  1247. EffectKind::ridgeStrip, EffectKind::glowLeveler, EffectKind::hushDeEsser, EffectKind::aether, EffectKind::ember, // LYNX's mixing suite
  1248. EffectKind::lynxTune, // LYNX's own pitch corrector
  1249. EffectKind::epochVerb, EffectKind::atrium, EffectKind::foilPlate, EffectKind::nebula, EffectKind::halo, EffectKind::horizon, // LYNX's reverbs
  1250. EffectKind::fruityLimiter, EffectKind::fruityCompressor,
  1251. EffectKind::fruityWaveShaper, EffectKind::fruitySoftClipper,
  1252. EffectKind::reeverb2, EffectKind::reeverb1,
  1253. EffectKind::fruityDelay2, EffectKind::fruityDelay3,
  1254. EffectKind::stereoShaper, EffectKind::stereoEnhancer, EffectKind::transientProcessor,
  1255. EffectKind::spreader, EffectKind::fruityChorus, EffectKind::hyperChorus, EffectKind::vintageChorus,
  1256. EffectKind::fruityPanOMatic, EffectKind::fruityBalance, EffectKind::fruityBloodOverdrive,
  1257. EffectKind::fruityDelayBank, EffectKind::fruityMultiband,
  1258. EffectKind::fruityFastDist, EffectKind::fruityFlangus, EffectKind::fruityFlanger, EffectKind::fruityPhaser, EffectKind::luxeVerb,
  1259. EffectKind::fruityFastLP, EffectKind::fruityFreeFilter, EffectKind::fruityFilter,
  1260. EffectKind::hardcore,
  1261. EffectKind::multibandDelay };
  1262. } // namespace
  1263. const std::vector<EffectKind>& addableEffects()
  1264. {
  1265. static const std::vector<EffectKind> list (std::begin (allEffects), std::end (allEffects));
  1266. return list;
  1267. }
  1268. EffectsPanel::EffectsPanel (AudioEngine& e)
  1269. : engine (e)
  1270. {
  1271. setWantsKeyboardFocus (false);
  1272. setMouseClickGrabsKeyboardFocus (false);
  1273. engine.addChangeListener (this);
  1274. startTimerHz (30);
  1275. }
  1276. EffectsPanel::~EffectsPanel()
  1277. {
  1278. engine.removeChangeListener (this);
  1279. }
  1280. juce::Rectangle<int> EffectsPanel::armBounds() const
  1281. {
  1282. return { 8, panelHeader + 2, 24, inputRowHeight - 8 };
  1283. }
  1284. juce::Rectangle<int> EffectsPanel::inputBoxBounds() const
  1285. {
  1286. return { 38, panelHeader + 2, getWidth() - 46, inputRowHeight - 8 };
  1287. }
  1288. void EffectsPanel::timerCallback()
  1289. {
  1290. // the input level of the shown insert (the strips read their own); decays like a meter
  1291. const auto& mixer = engine.getSong().mixer;
  1292. const auto hasInput = track > 0 && track < (int) mixer.size() && mixer[(std::size_t) track].inputChannel >= 0;
  1293. auto changed = false;
  1294. for (int side = 0; side < 2; ++side)
  1295. {
  1296. const auto peak = hasInput ? engine.peekInputPeak (track, side) : 0.0f;
  1297. const auto level = juce::jmax (peak, inputLevel[(std::size_t) side] * 0.8f);
  1298. changed = changed || std::abs (level - inputLevel[(std::size_t) side]) > 1e-4f;
  1299. inputLevel[(std::size_t) side] = level < 1e-4f ? 0.0f : level;
  1300. }
  1301. if (changed && isShowing())
  1302. repaint (inputBoxBounds());
  1303. }
  1304. void EffectsPanel::setTrack (int t)
  1305. {
  1306. track = t;
  1307. const auto* s = slots();
  1308. selectedSlot = s != nullptr && ! s->empty() ? 0 : -1;
  1309. repaint();
  1310. }
  1311. const std::vector<EffectSlot>* EffectsPanel::slots() const
  1312. {
  1313. const auto& mixer = engine.getSong().mixer;
  1314. return track >= 0 && track < (int) mixer.size() ? &mixer[(std::size_t) track].effects : nullptr;
  1315. }
  1316. juce::Rectangle<int> EffectsPanel::mixBounds (int slot) const
  1317. {
  1318. return slotBounds (slot).removeFromRight (26);
  1319. }
  1320. juce::Rectangle<int> EffectsPanel::slotBounds (int slot) const
  1321. {
  1322. return { 8, slotsTop + slot * slotHeight, getWidth() - 16, slotHeight - 3 };
  1323. }
  1324. namespace
  1325. {
  1326. // parameters shown as knobs: in the rhythmic envelope, only those of the points in use
  1327. int shownParams (const EffectSlot& slot)
  1328. {
  1329. const auto all = (int) effectInfo (slot.kind).params.size();
  1330. if (slot.kind == EffectKind::rhythmGain)
  1331. return std::min (all, 2 + 3 * (int) std::lround (slot.params[1] * 12.0f));
  1332. if (slot.kind == EffectKind::lynxTune) // the note switches are the keyboard strip above the knobs
  1333. return std::min (all, (int) lynxtune::noteC);
  1334. return all;
  1335. }
  1336. } // namespace
  1337. juce::Rectangle<int> EffectsPanel::knobBounds (int param) const
  1338. {
  1339. auto top = slotsTop + maxEffectSlots * slotHeight + 40;
  1340. // many parameters (Effector: 12): 4 columns and shorter rows to fit the height
  1341. int count = 0;
  1342. if (const auto* s = slots(); ! gateSelected() && s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size())
  1343. count = shownParams ((*s)[(std::size_t) selectedSlot]);
  1344. const auto columns = count > 9 ? 4 : 3;
  1345. const auto w = (getWidth() - 16) / columns;
  1346. const auto rows = juce::jmax (1, (count + columns - 1) / columns);
  1347. const auto rowHeight = count > 9 ? juce::jlimit (64, 86, (getHeight() - top - 4) / rows) : 86;
  1348. // volume gate: the 32 steps are bars (gateStepsBounds); the knobs are mix/attack/release
  1349. if (gateSelected())
  1350. {
  1351. if (param < 32)
  1352. return {};
  1353. param -= 32;
  1354. top = gateStepsBounds().getBottom() + 22;
  1355. }
  1356. if (vocoderSelected()) // the band levels above the knobs
  1357. top = bandLevelsBounds().getBottom() + 22;
  1358. if (tuneSelected()) // LYNX Tune's note keyboard above the knobs
  1359. top = tuneKeysBounds().getBottom() + 22;
  1360. return { 8 + (param % columns) * w, top + (param / columns) * rowHeight, w, rowHeight - 4 };
  1361. }
  1362. bool EffectsPanel::gateSelected() const
  1363. {
  1364. const auto* s = slots();
  1365. return s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::volumeGate;
  1366. }
  1367. juce::Rectangle<int> EffectsPanel::gateStepsBounds() const
  1368. {
  1369. return { 10, slotsTop + maxEffectSlots * slotHeight + 40, getWidth() - 20, 90 };
  1370. }
  1371. void EffectsPanel::setGateStep (juce::Point<int> p)
  1372. {
  1373. const auto area = gateStepsBounds();
  1374. const auto step = juce::jlimit (0, 31, (int) ((p.x - area.getX()) * 32 / juce::jmax (1, area.getWidth())));
  1375. const auto value = juce::jlimit (0.0f, 1.0f, (float) (area.getBottom() - p.y) / (float) area.getHeight());
  1376. engine.editSong ([t = track, slot = selectedSlot, step, value] (Song& song) {
  1377. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) step] = value;
  1378. });
  1379. }
  1380. bool EffectsPanel::vocoderSelected() const
  1381. {
  1382. const auto* s = slots();
  1383. return s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::vocoder;
  1384. }
  1385. bool EffectsPanel::tuneSelected() const
  1386. {
  1387. const auto* s = slots();
  1388. return s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::lynxTune;
  1389. }
  1390. juce::Rectangle<int> EffectsPanel::tuneKeysBounds() const
  1391. {
  1392. return { 10, slotsTop + maxEffectSlots * slotHeight + 40, getWidth() - 20, 54 };
  1393. }
  1394. namespace
  1395. {
  1396. // LYNX Tune's keyboard strip: one octave, white keys in a row, black keys on top (C … B → key rectangles)
  1397. juce::Rectangle<float> tuneKeyRect (juce::Rectangle<float> area, int pc)
  1398. {
  1399. static constexpr int whiteIndex[12] { 0, -1, 1, -1, 2, 3, -1, 4, -1, 5, -1, 6 };
  1400. static constexpr float blackAt[12] { 0, 1.0f, 0, 2.0f, 0, 0, 4.0f, 0, 5.0f, 0, 6.0f, 0 };
  1401. const auto w = area.getWidth() / 7.0f;
  1402. if (whiteIndex[pc] >= 0)
  1403. return { area.getX() + whiteIndex[pc] * w, area.getY(), w, area.getHeight() };
  1404. return { area.getX() + blackAt[pc] * w - w * 0.3f, area.getY(), w * 0.6f, area.getHeight() * 0.58f };
  1405. }
  1406. int tuneKeyAt (juce::Rectangle<float> area, juce::Point<float> p)
  1407. {
  1408. for (int pc = 0; pc < 12; ++pc) // black keys first: they lie on top
  1409. if ((pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10) && tuneKeyRect (area, pc).contains (p))
  1410. return pc;
  1411. for (int pc = 0; pc < 12; ++pc)
  1412. if (tuneKeyRect (area, pc).contains (p))
  1413. return pc;
  1414. return -1;
  1415. }
  1416. } // namespace
  1417. void EffectsPanel::toggleTuneNote (juce::Point<int> p)
  1418. {
  1419. const auto pc = tuneKeyAt (tuneKeysBounds().toFloat(), p.toFloat());
  1420. if (pc < 0)
  1421. return;
  1422. engine.editSong ([t = track, slot = selectedSlot, pc] (Song& song) {
  1423. auto& v = song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) (lynxtune::noteC + pc)];
  1424. v = v >= 0.5f ? 0.0f : 1.0f;
  1425. });
  1426. }
  1427. juce::Rectangle<int> EffectsPanel::bandLevelsBounds() const
  1428. {
  1429. return { 10, slotsTop + maxEffectSlots * slotHeight + 40, getWidth() - 20, 70 };
  1430. }
  1431. namespace
  1432. {
  1433. // the Fruity Vocoder's state for the slot's current band count (levels kept while the count matches, as FL does)
  1434. fvocoder::State vocoderState (const EffectSlot& slot)
  1435. {
  1436. auto s = fvocoder::parse (slot.state).value_or (fvocoder::State());
  1437. const auto bands = 4 << std::clamp ((int) std::lround (slot.params[0] * 5.0f), 0, 5);
  1438. if (s.bands != bands)
  1439. {
  1440. s.bands = bands;
  1441. s.levels.fill (1.0f);
  1442. }
  1443. s.order = 1 + std::clamp ((int) std::lround (slot.params[1] * 2.0f), 0, 2);
  1444. s.listen = std::clamp ((int) std::lround (slot.params[2] * 2.0f), 0, 2);
  1445. s.swap = slot.params[3] > 0.5f;
  1446. for (int c = 0; c < fvocoder::controls; ++c)
  1447. s.c[(std::size_t) c] = fvocoder::rawFromKnob (slot.params[(std::size_t) (4 + c)], c, s.c[(std::size_t) c]);
  1448. return s;
  1449. }
  1450. constexpr float maxBandLevel = 2.0f; // the strip spans 0…2 (1 = unchanged)
  1451. } // namespace
  1452. void EffectsPanel::setBandLevel (juce::Point<int> p)
  1453. {
  1454. const auto* s = slots();
  1455. if (s == nullptr || ! vocoderSelected())
  1456. return;
  1457. const auto st = vocoderState ((*s)[(std::size_t) selectedSlot]);
  1458. const auto area = bandLevelsBounds();
  1459. const auto band = juce::jlimit (0, st.bands - 1, (int) ((p.x - area.getX()) * st.bands / juce::jmax (1, area.getWidth())));
  1460. auto level = juce::jlimit (0.0f, maxBandLevel, maxBandLevel * (float) (area.getBottom() - p.y) / (float) area.getHeight());
  1461. if (std::abs (level - 1.0f) < 0.04f)
  1462. level = 1.0f; // snaps to the unchanged level
  1463. engine.editSong ([t = track, slot = selectedSlot, band, level] (Song& song) {
  1464. auto& fx = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  1465. auto state = vocoderState (fx);
  1466. state.levels[(std::size_t) band] = level;
  1467. fx.state = fvocoder::serialise (state);
  1468. });
  1469. }
  1470. void EffectsPanel::showBandLevelMenu()
  1471. {
  1472. juce::PopupMenu menu;
  1473. auto apply = [this] (float (*level) (int, int)) {
  1474. engine.editSong ([t = track, slot = selectedSlot, level] (Song& song) {
  1475. auto& fx = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  1476. auto state = vocoderState (fx);
  1477. for (int k = 0; k < state.bands; ++k)
  1478. state.levels[(std::size_t) k] = level (k, state.bands);
  1479. fx.state = fvocoder::serialise (state);
  1480. });
  1481. };
  1482. menu.addItem (tr ("Reset band levels"), [apply] { apply ([] (int, int) { return 1.0f; }); });
  1483. menu.addItem (tr ("Brighter (tilt up)"), [apply] { apply ([] (int k, int n) { return 0.5f + (float) k / (float) std::max (1, n - 1); }); });
  1484. menu.addItem (tr ("Darker (tilt down)"), [apply] { apply ([] (int k, int n) { return 1.5f - (float) k / (float) std::max (1, n - 1); }); });
  1485. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  1486. }
  1487. void EffectsPanel::showGatePatterns()
  1488. {
  1489. // ready-made patterns (like Gross Beat's slots): value of step i of 32 (1/8 beat each)
  1490. struct Pattern
  1491. {
  1492. const char* name;
  1493. float (*level) (int);
  1494. };
  1495. static const Pattern patterns[] = {
  1496. { TR_NOOP ("All open"), [] (int) { return 1.0f; } },
  1497. { TR_NOOP ("1/2 beat gate"), [] (int i) { return (i % 4) < 2 ? 1.0f : 0.0f; } },
  1498. { TR_NOOP ("1 beat gate"), [] (int i) { return (i % 8) < 4 ? 1.0f : 0.0f; } },
  1499. { TR_NOOP ("Offbeat"), [] (int i) { return (i % 8) >= 4 ? 1.0f : 0.0f; } },
  1500. { TR_NOOP ("Sidechain (ramp per beat)"), [] (int i) { return std::min (1.0f, 0.15f + (float) (i % 8) / 5.0f); } },
  1501. { TR_NOOP ("Trance gate"), [] (int i) { return (i % 4) == 3 ? 0.0f : (i % 16) >= 12 ? 0.4f : 1.0f; } },
  1502. { TR_NOOP ("Fade-in over the bar"), [] (int i) { return (float) (i + 1) / 32.0f; } },
  1503. { TR_NOOP ("Fade-out over the bar"), [] (int i) { return 1.0f - (float) i / 32.0f; } },
  1504. };
  1505. juce::PopupMenu menu;
  1506. for (const auto& p : patterns)
  1507. menu.addItem (tr (p.name), [this, level = p.level] {
  1508. engine.editSong ([t = track, slot = selectedSlot, level] (Song& song) {
  1509. auto& params = song.mixer[(std::size_t) t].effects[(std::size_t) slot].params;
  1510. for (int i = 0; i < 32; ++i)
  1511. params[(std::size_t) i] = level (i);
  1512. });
  1513. });
  1514. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  1515. }
  1516. int EffectsPanel::knobAt (juce::Point<int> p) const
  1517. {
  1518. const auto* s = slots();
  1519. if (s == nullptr || selectedSlot < 0 || selectedSlot >= (int) s->size())
  1520. return -1;
  1521. if ((*s)[(std::size_t) selectedSlot].kind == EffectKind::parametricEq || (*s)[(std::size_t) selectedSlot].kind == EffectKind::facetEq)
  1522. return -1; // edited in its own window
  1523. const auto count = shownParams ((*s)[(std::size_t) selectedSlot]);
  1524. for (int i = gateSelected() ? 32 : 0; i < count; ++i)
  1525. if (knobBounds (i).contains (p))
  1526. return i;
  1527. return -1;
  1528. }
  1529. void EffectsPanel::paint (juce::Graphics& g)
  1530. {
  1531. using namespace theme;
  1532. g.fillAll (colour::bg2);
  1533. g.setColour (colour::border);
  1534. g.drawVerticalLine (0, 0.0f, (float) getHeight());
  1535. const auto* s = slots();
  1536. if (s == nullptr)
  1537. return;
  1538. const auto& song = engine.getSong();
  1539. g.setColour (colour::textPrimary);
  1540. g.setFont (font (type::title, true));
  1541. g.drawText (tr ("Effects: {0}", { juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str()) }), 10, 4,
  1542. getWidth() - 70, panelHeader - 8, juce::Justification::centredLeft, true);
  1543. // input row (inserts): arm switch and the audio input source with its level
  1544. if (track > 0)
  1545. {
  1546. const auto& mt = song.mixer[(std::size_t) track];
  1547. const auto armed = engine.isInsertArmed (track);
  1548. const auto a = armBounds().toFloat();
  1549. g.setColour (armed ? colour::record.withAlpha (0.25f) : hoverInput == 1 ? colour::bg5 : colour::bg3);
  1550. g.fillRoundedRectangle (a, radius::sm);
  1551. const auto dot = a.withSizeKeepingCentre (11.0f, 11.0f);
  1552. g.setColour (armed ? colour::record : colour::bg0);
  1553. g.fillEllipse (dot);
  1554. g.setColour (armed ? colour::record.brighter (0.3f) : hoverInput == 1 ? colour::textSecondary : colour::borderStrong);
  1555. g.drawEllipse (dot, 1.0f);
  1556. auto box = inputBoxBounds().toFloat();
  1557. g.setColour (hoverInput == 2 ? colour::bg4 : colour::bg3);
  1558. g.fillRoundedRectangle (box, radius::sm);
  1559. g.setColour (hoverInput == 2 ? colour::borderStrong : colour::border);
  1560. g.drawRoundedRectangle (box.reduced (0.5f), radius::sm, 1.0f);
  1561. // the level, as a thin bar along the bottom of the box (left over right)
  1562. if (mt.inputChannel >= 0)
  1563. {
  1564. const auto bar = box.reduced (4.0f, 0.0f).withTop (box.getBottom() - 5.0f).withHeight (3.0f);
  1565. g.setColour (colour::bg0);
  1566. g.fillRect (bar);
  1567. for (int side = 0; side < 2; ++side)
  1568. {
  1569. const auto db = juce::Decibels::gainToDecibels (inputLevel[(std::size_t) side], -60.0f);
  1570. const auto fill = juce::jlimit (0.0f, 1.0f, (db + 60.0f) / 60.0f);
  1571. g.setColour (inputLevel[(std::size_t) side] >= 0.99f ? colour::record : colour::meterLow);
  1572. g.fillRect (bar.withHeight (1.5f).translated (0.0f, side * 1.5f).withWidth (bar.getWidth() * fill));
  1573. }
  1574. }
  1575. icons::draw (g, Icon::mic, box.withWidth (18.0f).reduced (2.0f).translated (3.0f, -1.0f), mt.inputChannel >= 0 ? colour::textPrimary : colour::textDisabled);
  1576. const auto names = engine.getInputChannelNames();
  1577. const auto label = mt.inputChannel >= 0 ? juce::String::fromUTF8 (inputSourceName (mt.inputChannel, mt.inputStereo, names).c_str())
  1578. : tr ("No audio input");
  1579. g.setColour (mt.inputChannel >= 0 ? colour::textPrimary : colour::textSecondary);
  1580. g.setFont (font (type::body));
  1581. g.drawText (label, box.withTrimmedLeft (24.0f).withTrimmedRight (18.0f).translated (0.0f, -1.0f).toNearestInt(), juce::Justification::centredLeft, true);
  1582. icons::draw (g, Icon::chevronDown, box.removeFromRight (18.0f).reduced (4.0f).translated (-2.0f, -1.0f), colour::textSecondary);
  1583. }
  1584. // the insert's effects switch
  1585. if (! s->empty())
  1586. {
  1587. const auto on = mixerops::anyEffectEnabled (song, track);
  1588. const auto r = fxSwitchBounds().toFloat();
  1589. g.setColour (on ? colour::bg5 : colour::bg0);
  1590. g.fillRoundedRectangle (r, radius::sm);
  1591. g.setColour (muteLed (! on));
  1592. g.fillEllipse (r.withWidth (r.getHeight()).reduced (6.0f).translated (3.0f, 0.0f));
  1593. g.setColour (on ? colour::textPrimary : colour::textDisabled);
  1594. g.setFont (font (type::caption, true));
  1595. g.drawText ("FX", r.withTrimmedLeft (r.getHeight()).toNearestInt(), juce::Justification::centredLeft);
  1596. }
  1597. g.setFont (font (type::body));
  1598. for (int i = 0; i < maxEffectSlots; ++i)
  1599. {
  1600. const auto r = slotBounds (i);
  1601. const auto filled = i < (int) s->size();
  1602. const auto isSelected = i == selectedSlot && filled;
  1603. const auto slotHovered = i == hoverSlot && dragSlot < 0;
  1604. g.setColour (isSelected ? colour::bg5 : filled ? (slotHovered ? colour::bg4 : colour::bg3) : (slotHovered ? colour::bg3 : colour::bg2.brighter (0.03f)));
  1605. g.fillRoundedRectangle (r.toFloat(), radius::sm);
  1606. if (! filled)
  1607. {
  1608. g.setColour (slotHovered ? colour::borderStrong : colour::border);
  1609. g.drawRoundedRectangle (r.toFloat().reduced (0.5f), radius::sm, 1.0f);
  1610. }
  1611. if (isSelected)
  1612. {
  1613. g.setColour (colour::accent);
  1614. g.fillRoundedRectangle (r.toFloat().withWidth (3.0f).reduced (0.0f, 3.0f), 1.5f);
  1615. }
  1616. if (filled)
  1617. {
  1618. const auto& slot = (*s)[(std::size_t) i];
  1619. g.setColour (muteLed (! slot.enabled));
  1620. g.fillEllipse ((float) r.getX() + 8, (float) r.getCentreY() - 4, 8, 8);
  1621. if (slotHovered && hoverLed)
  1622. {
  1623. g.setColour (colour::textSecondary);
  1624. g.drawEllipse ((float) r.getX() + 6, (float) r.getCentreY() - 6, 12, 12, 1.0f);
  1625. }
  1626. g.setColour (slot.enabled ? colour::textPrimary : colour::textDisabled);
  1627. // a LYNX substitute shows the plugin it stands in for, with a "LYNX" badge
  1628. const auto substitute = slot.isSubstitute();
  1629. auto name = slot.kind == EffectKind::plugin ? juce::String::fromUTF8 (slot.plugin.name.c_str())
  1630. : substitute ? juce::String::fromUTF8 (substituteOriginalName (slot).c_str())
  1631. : juce::String::fromUTF8 (effectInfo (slot.kind).name);
  1632. if (slot.sidechain > 0 && slot.sidechain < (int) engine.getSong().mixer.size())
  1633. name << juce::String::fromUTF8 (" \xe2\x86\x90 ") << juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) slot.sidechain].name.c_str());
  1634. auto textArea = r.withTrimmedLeft (24).withTrimmedRight (28);
  1635. if (substitute)
  1636. {
  1637. const auto badge = textArea.removeFromRight (34).withSizeKeepingCentre (32, 14).toFloat();
  1638. g.setColour (colour::info.withAlpha (slot.enabled ? 0.25f : 0.12f));
  1639. g.fillRoundedRectangle (badge, 3.0f);
  1640. g.setColour (slot.enabled ? colour::info : colour::textDisabled);
  1641. const auto keep = g.getCurrentFont();
  1642. g.setFont (font (type::caption, true));
  1643. g.drawText ("LYNX", badge, juce::Justification::centred);
  1644. g.setFont (keep);
  1645. g.setColour (slot.enabled ? colour::textPrimary : colour::textDisabled);
  1646. }
  1647. g.drawText (juce::String (i + 1) + ". " + name, textArea, juce::Justification::centredLeft);
  1648. // slot's dry/wet knob
  1649. const auto mixActive = (hoverParam == mixParam && hoverSlot == i) || (dragParam == mixParam && selectedSlot == i);
  1650. drawKnob (g, mixBounds (i).toFloat().withSizeKeepingCentre (20.0f, 20.0f), slot.mix, false,
  1651. slot.enabled ? (dragParam == mixParam && selectedSlot == i ? colour::accent : colour::info) : colour::textDisabled, mixActive);
  1652. }
  1653. else if (i == (int) s->size() || slotHovered)
  1654. {
  1655. g.setColour (slotHovered ? colour::textSecondary : colour::textDisabled);
  1656. g.drawText (juce::String (i + 1) + ". " + tr ("(empty: click to add)"), r.withTrimmedLeft (24), juce::Justification::centredLeft);
  1657. }
  1658. }
  1659. if (dragSlot >= 0 && dropSlot >= 0 && dropSlot != dragSlot) // where the dragged slot lands
  1660. {
  1661. const auto r = slotBounds (dropSlot);
  1662. const auto y = dropSlot > dragSlot ? r.getBottom() + 1 : r.getY() - 2;
  1663. g.setColour (colour::accent);
  1664. g.fillRect (r.getX(), y, r.getWidth(), 2);
  1665. }
  1666. if (selectedSlot < 0 || selectedSlot >= (int) s->size())
  1667. return;
  1668. const auto& slot = (*s)[(std::size_t) selectedSlot];
  1669. auto drawButton = [&g] (juce::Rectangle<int> button, const juce::String& text, Icon icon) {
  1670. g.setColour (colour::bg4);
  1671. g.fillRoundedRectangle (button.toFloat(), radius::md);
  1672. const auto f = juce::Font (font (type::body));
  1673. const auto textWidth = juce::GlyphArrangement::getStringWidth (f, text);
  1674. auto content = button.toFloat().withSizeKeepingCentre (juce::jmin ((float) button.getWidth() - 12.0f, textWidth + 22.0f), (float) button.getHeight());
  1675. icons::draw (g, icon, content.removeFromLeft (16.0f).withSizeKeepingCentre (16.0f, 16.0f), colour::textSecondary);
  1676. content.removeFromLeft (6.0f);
  1677. g.setColour (colour::textPrimary);
  1678. g.setFont (f);
  1679. g.drawFittedText (text, content.toNearestInt(), juce::Justification::centredLeft, 1, 0.85f);
  1680. };
  1681. if (slot.kind == EffectKind::plugin)
  1682. {
  1683. const auto button = pluginButtonBounds();
  1684. drawButton (button, tr ("Open plugin interface"), Icon::plugin);
  1685. g.setColour (colour::textSecondary);
  1686. g.setFont (font (type::caption));
  1687. g.drawText (juce::String::fromUTF8 (slot.plugin.name.c_str()), button.translated (0, 34), juce::Justification::centred);
  1688. return;
  1689. }
  1690. if (slot.kind == EffectKind::parametricEq)
  1691. {
  1692. const auto button = pluginButtonBounds();
  1693. drawButton (button, tr ("Open parametric EQ"), Icon::settings);
  1694. const auto curve = juce::Rectangle<int> (button.getX(), button.getBottom() + 10, button.getWidth(), 120).toFloat();
  1695. drawParametricEqCurve (g, curve, slot.params, juce::jmax (8000.0, engine.getTransport().sampleRate.load()), slot.enabled);
  1696. return;
  1697. }
  1698. if (slot.kind == EffectKind::facetEq)
  1699. {
  1700. const auto button = pluginButtonBounds();
  1701. drawButton (button, tr ("Open Facet EQ"), Icon::settings);
  1702. const auto curve = juce::Rectangle<int> (button.getX(), button.getBottom() + 10, button.getWidth(), 120).toFloat();
  1703. drawFacetEqCurve (g, curve, slot.params, juce::jmax (8000.0, engine.getTransport().sampleRate.load()), slot.enabled);
  1704. return;
  1705. }
  1706. if (slot.kind == EffectKind::parallel) // container: list the branches
  1707. {
  1708. auto y = knobBounds (0).getY() - 28;
  1709. g.setColour (colour::textPrimary);
  1710. g.setFont (font (type::title, true));
  1711. g.drawText (juce::String::fromUTF8 (effectInfo (slot.kind).name), 10, y, getWidth() - 20, 20, juce::Justification::centredLeft);
  1712. g.setFont (font (type::small));
  1713. for (std::size_t b = 0; b < slot.branches.size(); ++b)
  1714. {
  1715. y += 22;
  1716. juce::String chain;
  1717. if (slot.branches[b].chain.empty())
  1718. chain = tr ("dry");
  1719. for (std::size_t c = 0; c < slot.branches[b].chain.size(); ++c)
  1720. chain += (c > 0 ? " -> " : "") + juce::String::fromUTF8 (effectInfo (slot.branches[b].chain[c].kind).name);
  1721. const auto line = tr ("Branch {0} (x{1}): {2}", { juce::String ((int) b + 1), juce::String (slot.branches[b].gain, 2), chain });
  1722. g.setColour (colour::textSecondary);
  1723. g.drawText (line, 14, y, getWidth() - 28, 20, juce::Justification::centredLeft, true);
  1724. }
  1725. return;
  1726. }
  1727. const auto& info = effectInfo (slot.kind);
  1728. const auto gate = slot.kind == EffectKind::volumeGate;
  1729. const auto vocoder = slot.kind == EffectKind::vocoder;
  1730. g.setColour (colour::textPrimary);
  1731. g.setFont (font (type::title, true));
  1732. const auto tune = slot.kind == EffectKind::lynxTune;
  1733. const auto editorButton = ! gate && ! vocoder && NativeEffectWindows::canOpen (slot.kind);
  1734. const auto titleY = (gate ? gateStepsBounds().getY() : vocoder ? bandLevelsBounds().getY() : tune ? tuneKeysBounds().getY() : knobBounds (0).getY()) - 28;
  1735. const auto titleRight = getWidth() - 10 - (editorButton ? editorButtonBounds().getWidth() + 6 : 0);
  1736. g.drawText (juce::String::fromUTF8 (info.name), 10, titleY, titleRight - 10, 20, juce::Justification::centredLeft, true);
  1737. if (slot.isSubstitute()) // which missing plugin this stands in for (the project keeps it)
  1738. {
  1739. const auto titleWidth = juce::GlyphArrangement::getStringWidthInt (g.getCurrentFont(), juce::String::fromUTF8 (info.name));
  1740. g.setColour (colour::info);
  1741. g.setFont (font (type::caption));
  1742. g.drawText (tr ("LYNX substitute for {0}", { juce::String::fromUTF8 (substituteOriginalName (slot).c_str()) }), 10 + titleWidth + 10, titleY,
  1743. juce::jmax (0, titleRight - 20 - titleWidth), 20, juce::Justification::centredLeft, true);
  1744. }
  1745. if (editorButton) // the effect's own window (FL's plugin window)
  1746. {
  1747. const auto b = editorButtonBounds().toFloat();
  1748. const auto hover = getMouseXYRelative().toFloat();
  1749. g.setColour (b.contains (hover) ? colour::bg5 : colour::bg4);
  1750. g.fillRoundedRectangle (b, radius::md);
  1751. icons::draw (g, Icon::plugin, b.withWidth (22.0f).withSizeKeepingCentre (13.0f, 13.0f).translated (4.0f, 0.0f), colour::textSecondary);
  1752. g.setColour (colour::textPrimary);
  1753. g.setFont (font (type::small));
  1754. g.drawFittedText (tr ("Editor"), b.withTrimmedLeft (24.0f).toNearestInt(), juce::Justification::centredLeft, 1, 0.8f);
  1755. }
  1756. if (tune) // the scale's notes on one octave; a note switched off is never a target
  1757. {
  1758. const auto area = tuneKeysBounds().toFloat();
  1759. const auto scaleMask = vocal::scaleMask ((vocal::Scale) lynxtune::scaleIndex (slot.params[lynxtune::scale]), lynxtune::keyIndex (slot.params[lynxtune::key]), 0xfff);
  1760. const auto custom = lynxtune::scaleIndex (slot.params[lynxtune::scale]) == (int) vocal::Scale::custom;
  1761. for (const auto black : { false, true })
  1762. for (int pc = 0; pc < 12; ++pc)
  1763. {
  1764. const auto isBlack = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
  1765. if (isBlack != black)
  1766. continue;
  1767. const auto r = tuneKeyRect (area, pc).reduced (1.0f);
  1768. const auto on = slot.params[(std::size_t) (lynxtune::noteC + pc)] >= 0.5f;
  1769. const auto inScale = custom || (scaleMask & (1u << pc)) != 0;
  1770. auto fill = isBlack ? colour::bg0 : colour::bg2;
  1771. if (inScale && on)
  1772. fill = (slot.enabled ? colour::accent : colour::textDisabled).withAlpha (isBlack ? 0.85f : 0.65f);
  1773. g.setColour (fill);
  1774. g.fillRoundedRectangle (r, 2.0f);
  1775. g.setColour (colour::gridBar);
  1776. g.drawRoundedRectangle (r, 2.0f, 1.0f);
  1777. if (inScale && ! on) // in the scale but switched off
  1778. {
  1779. g.setColour (colour::textSecondary);
  1780. g.drawLine (r.getX() + 3.0f, r.getBottom() - 3.0f, r.getRight() - 3.0f, r.getBottom() - 14.0f, 1.2f);
  1781. }
  1782. if (! isBlack)
  1783. {
  1784. static const char* names[12] { "C", "", "D", "", "E", "F", "", "G", "", "A", "", "B" };
  1785. g.setColour (inScale && on ? colour::textPrimary : colour::textSecondary);
  1786. g.setFont (font (type::caption));
  1787. g.drawText (names[pc], r.withTrimmedTop (r.getHeight() - 16.0f), juce::Justification::centred);
  1788. }
  1789. }
  1790. g.setColour (colour::textSecondary);
  1791. g.setFont (font (type::caption));
  1792. g.drawFittedText (tr ("Target notes: click a key to switch it off or on"), tuneKeysBounds().withY (tuneKeysBounds().getBottom() + 2).withHeight (14),
  1793. juce::Justification::centredLeft, 1, 0.8f);
  1794. }
  1795. if (vocoder) // the analysis bands' levels, low to high (FL's band level strip)
  1796. {
  1797. const auto st = vocoderState (slot);
  1798. const auto area = bandLevelsBounds().toFloat();
  1799. g.setColour (colour::bg0);
  1800. g.fillRoundedRectangle (area, radius::sm);
  1801. const auto w = area.getWidth() / (float) st.bands;
  1802. const auto unity = area.getBottom() - area.getHeight() / maxBandLevel;
  1803. for (int k = 0; k < st.bands; ++k)
  1804. {
  1805. const auto h = area.getHeight() * juce::jlimit (0.0f, 1.0f, st.levels[(std::size_t) k] / maxBandLevel);
  1806. g.setColour ((slot.enabled ? colour::accent : colour::textDisabled).withAlpha (k % 2 == 0 ? 1.0f : 0.8f));
  1807. g.fillRect (area.getX() + (float) k * w + (w > 4.0f ? 1.0f : 0.0f), area.getBottom() - h, juce::jmax (1.0f, w - (w > 4.0f ? 2.0f : 0.0f)), h);
  1808. }
  1809. g.setColour (colour::gridBar);
  1810. g.drawHorizontalLine ((int) unity, area.getX(), area.getRight());
  1811. g.setColour (colour::textSecondary);
  1812. g.setFont (font (type::caption));
  1813. g.drawFittedText (tr ("Band levels: draw them; right-click = presets"), area.withY (area.getBottom() + 2).withHeight (14).toNearestInt(),
  1814. juce::Justification::centredLeft, 1, 0.8f);
  1815. }
  1816. if (gate) // 32 steps = 1 bar of 4 beats
  1817. {
  1818. const auto area = gateStepsBounds().toFloat();
  1819. g.setColour (colour::bg0);
  1820. g.fillRoundedRectangle (area, radius::sm);
  1821. const auto w = area.getWidth() / 32.0f;
  1822. for (int i = 0; i < 32; ++i)
  1823. {
  1824. const auto h = area.getHeight() * juce::jlimit (0.0f, 1.0f, slot.params[(std::size_t) i]);
  1825. g.setColour ((slot.enabled ? colour::accent : colour::textDisabled).withAlpha ((i / 8) % 2 == 0 ? 1.0f : 0.7f));
  1826. g.fillRect (area.getX() + i * w + 1.0f, area.getBottom() - h, w - 2.0f, h);
  1827. }
  1828. g.setColour (colour::gridBar);
  1829. for (int beat = 1; beat < 4; ++beat)
  1830. g.drawVerticalLine ((int) (area.getX() + beat * 8 * w), area.getY(), area.getBottom());
  1831. g.setColour (colour::textSecondary);
  1832. g.setFont (font (type::caption));
  1833. g.drawFittedText (tr ("1 bar: draw the steps; right-click = patterns"), area.withY (area.getBottom() + 2).withHeight (14).toNearestInt(),
  1834. juce::Justification::centredLeft, 1, 0.8f);
  1835. }
  1836. for (int p = gate ? 32 : 0; p < shownParams (slot); ++p)
  1837. {
  1838. const auto r = knobBounds (p);
  1839. const auto value = slot.params[(std::size_t) p];
  1840. // 38 px knob; in tight grids (Effector) it shrinks to fit name and value below
  1841. const auto knobHeight = juce::jlimit (30, 44, r.getHeight() - 30);
  1842. drawKnob (g, r.withHeight (knobHeight).toFloat(), value, false, slot.enabled ? colour::knobArc : colour::textDisabled,
  1843. dragParam == p || (hoverParam == p && hoverSlot < 0));
  1844. g.setColour (colour::textSecondary);
  1845. g.setFont (font (type::caption));
  1846. g.drawFittedText (juce::String::fromUTF8 (info.params[(std::size_t) p].name), r.getX(), r.getY() + knobHeight + 2, r.getWidth(), 14,
  1847. juce::Justification::centred, 1, 0.8f);
  1848. g.setColour (colour::textPrimary);
  1849. g.drawText (juce::String::fromUTF8 (formatEffectParam (slot.kind, p, value, song.bpm, slot.params).c_str()), r.getX(), r.getY() + knobHeight + 16,
  1850. r.getWidth(), 14, juce::Justification::centred);
  1851. }
  1852. }
  1853. void EffectsPanel::mouseDown (const juce::MouseEvent& e)
  1854. {
  1855. const auto* s = slots();
  1856. if (s == nullptr)
  1857. return;
  1858. if (track > 0 && armBounds().contains (e.getPosition()))
  1859. {
  1860. if (e.mods.isPopupMenu())
  1861. mixerinput::showMenu (engine, track, this);
  1862. else
  1863. mixerinput::toggleArm (engine, track, this);
  1864. repaint();
  1865. return;
  1866. }
  1867. if (track > 0 && inputBoxBounds().contains (e.getPosition()))
  1868. {
  1869. mixerinput::showMenu (engine, track, this);
  1870. return;
  1871. }
  1872. if (! s->empty() && fxSwitchBounds().contains (e.getPosition()))
  1873. {
  1874. if (e.mods.isPopupMenu())
  1875. toggleAllWindows();
  1876. else
  1877. {
  1878. const auto on = ! mixerops::anyEffectEnabled (engine.getSong(), track);
  1879. engine.editSong ([t = track, on] (Song& song) { mixerops::setAllEffectsEnabled (song, t, on); });
  1880. }
  1881. return;
  1882. }
  1883. for (int i = 0; i < maxEffectSlots; ++i)
  1884. {
  1885. const auto r = slotBounds (i);
  1886. if (! r.contains (e.getPosition()))
  1887. continue;
  1888. if (i >= (int) s->size())
  1889. {
  1890. showAddMenu (i);
  1891. return;
  1892. }
  1893. selectedSlot = i;
  1894. repaint();
  1895. if (mixBounds (i).contains (e.getPosition()))
  1896. {
  1897. if (e.mods.isPopupMenu())
  1898. ui::showValueMenu (*this, &engine, specFor (mixParam, i), valueOf (mixParam, i), [this, i] (double v) { setValue (mixParam, i, v); });
  1899. else if (ui::isResetClick (e))
  1900. {
  1901. setValue (mixParam, i, 1.0);
  1902. showTip (mixParam, i, true);
  1903. }
  1904. else
  1905. {
  1906. dragParam = mixParam;
  1907. dragStart = (*s)[(std::size_t) i].mix;
  1908. drag.begin (e, dragStart);
  1909. showTip (mixParam, i, false);
  1910. }
  1911. return;
  1912. }
  1913. if (e.mods.isPopupMenu())
  1914. showSlotMenu (i);
  1915. else if (e.x < r.getX() + 22) // LED: bypass
  1916. engine.editSong ([t = track, i] (Song& song) {
  1917. auto& slot = song.mixer[(std::size_t) t].effects[(std::size_t) i];
  1918. slot.enabled = ! slot.enabled;
  1919. });
  1920. else
  1921. dragSlot = i; // dragging the name reorders the slot
  1922. return;
  1923. }
  1924. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::plugin
  1925. && pluginButtonBounds().contains (e.getPosition()))
  1926. {
  1927. openPluginWindow (selectedSlot);
  1928. return;
  1929. }
  1930. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::parametricEq
  1931. && pluginButtonBounds().withHeight (170).contains (e.getPosition()))
  1932. {
  1933. ParametricEqWindows::open (engine, track, selectedSlot);
  1934. return;
  1935. }
  1936. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::facetEq
  1937. && pluginButtonBounds().withHeight (170).contains (e.getPosition()))
  1938. {
  1939. FacetEqWindows::open (engine, track, selectedSlot);
  1940. return;
  1941. }
  1942. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind != EffectKind::volumeGate
  1943. && ! vocoderSelected() && NativeEffectWindows::canOpen ((*s)[(std::size_t) selectedSlot].kind)
  1944. && editorButtonBounds().contains (e.getPosition()))
  1945. {
  1946. openEditor (selectedSlot);
  1947. return;
  1948. }
  1949. if (vocoderSelected() && bandLevelsBounds().contains (e.getPosition()))
  1950. {
  1951. if (e.mods.isPopupMenu())
  1952. showBandLevelMenu();
  1953. else
  1954. {
  1955. dragParam = bandLevelParam;
  1956. setBandLevel (e.getPosition());
  1957. }
  1958. return;
  1959. }
  1960. if (tuneSelected() && tuneKeysBounds().contains (e.getPosition()))
  1961. {
  1962. toggleTuneNote (e.getPosition());
  1963. return;
  1964. }
  1965. if (gateSelected() && gateStepsBounds().contains (e.getPosition()))
  1966. {
  1967. if (e.mods.isPopupMenu())
  1968. showGatePatterns();
  1969. else
  1970. {
  1971. dragParam = gateStepParam;
  1972. setGateStep (e.getPosition());
  1973. }
  1974. return;
  1975. }
  1976. dragParam = knobAt (e.getPosition());
  1977. if (dragParam >= 0 && e.mods.isPopupMenu())
  1978. {
  1979. const auto p = dragParam;
  1980. ui::showValueMenu (*this, &engine, specFor (p, selectedSlot), valueOf (p, selectedSlot),
  1981. [this, p, slot = selectedSlot] (double v) { setValue (p, slot, v); });
  1982. dragParam = -1;
  1983. return;
  1984. }
  1985. if (dragParam >= 0 && ui::isResetClick (e))
  1986. {
  1987. setValue (dragParam, selectedSlot, specFor (dragParam, selectedSlot).defaultValue);
  1988. showTip (dragParam, selectedSlot, true);
  1989. dragParam = -1;
  1990. return;
  1991. }
  1992. if (dragParam >= 0)
  1993. {
  1994. dragStart = (*s)[(std::size_t) selectedSlot].params[(std::size_t) dragParam];
  1995. drag.begin (e, dragStart);
  1996. showTip (dragParam, selectedSlot, false);
  1997. }
  1998. }
  1999. ui::ValueSpec EffectsPanel::specFor (int param, int slot) const
  2000. {
  2001. const auto& song = engine.getSong();
  2002. ui::ValueSpec spec;
  2003. spec.pixelsForRange = 150.0;
  2004. if (const auto* s = slots(); s == nullptr || slot < 0 || slot >= (int) s->size())
  2005. return spec;
  2006. const auto& fx = (*slots())[(std::size_t) slot];
  2007. const auto& info = effectInfo (fx.kind);
  2008. const auto trackName = juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str());
  2009. if (param == mixParam)
  2010. {
  2011. spec = ui::percentSpec (trackName + " - " + utf8 (info.name) + " - " + tr ("Mix"), 1.0);
  2012. spec.target = AutomationTarget { AutomationTargetKind::effectMix, track, slot };
  2013. return spec;
  2014. }
  2015. if (param < 0 || param >= (int) info.params.size())
  2016. return spec;
  2017. spec.name = trackName + " - " + utf8 (info.name) + " - " + utf8 (info.params[(std::size_t) param].name);
  2018. spec.defaultValue = info.params[(std::size_t) param].defaultValue;
  2019. spec.toText = [kind = fx.kind, param, bpm = song.bpm, context = fx.params] (double v) { return utf8 (formatEffectParam (kind, param, (float) v, bpm, context).c_str()); };
  2020. spec.fromText = ui::inverseOf (spec.toText, 0.0, 1.0);
  2021. spec.target = AutomationTarget { AutomationTargetKind::effectParam, track, slot, param };
  2022. return spec;
  2023. }
  2024. double EffectsPanel::valueOf (int param, int slot) const
  2025. {
  2026. const auto* s = slots();
  2027. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  2028. return 0.0;
  2029. const auto& fx = (*s)[(std::size_t) slot];
  2030. return param == mixParam ? fx.mix : (param >= 0 && param < (int) fx.params.size() ? fx.params[(std::size_t) param] : 0.0f);
  2031. }
  2032. void EffectsPanel::setValue (int param, int slot, double value)
  2033. {
  2034. const auto* s = slots();
  2035. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  2036. return;
  2037. if (param == mixParam)
  2038. engine.editSong ([t = track, slot, v = (float) value] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) slot].mix = v; });
  2039. else if (param >= 0)
  2040. engine.editSong ([t = track, slot, param, v = (float) value] (Song& song) {
  2041. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) param] = v;
  2042. });
  2043. }
  2044. int EffectsPanel::mixKnobAt (juce::Point<int> p) const
  2045. {
  2046. for (int i = 0; slots() != nullptr && i < (int) slots()->size(); ++i)
  2047. if (mixBounds (i).contains (p))
  2048. return i;
  2049. return -1;
  2050. }
  2051. void EffectsPanel::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  2052. {
  2053. if (const auto slot = mixKnobAt (e.getPosition()); slot >= 0)
  2054. {
  2055. if (const auto v = ui::wheelValue (valueOf (mixParam, slot), e, wheel, specFor (mixParam, slot)))
  2056. {
  2057. setValue (mixParam, slot, *v);
  2058. showTip (mixParam, slot, true);
  2059. }
  2060. return;
  2061. }
  2062. if (const auto p = knobAt (e.getPosition()); p >= 0)
  2063. {
  2064. if (const auto v = ui::wheelValue (valueOf (p, selectedSlot), e, wheel, specFor (p, selectedSlot)))
  2065. {
  2066. setValue (p, selectedSlot, *v);
  2067. showTip (p, selectedSlot, true);
  2068. }
  2069. return;
  2070. }
  2071. // the wheel over a slot's name moves it up or down
  2072. if (const auto slot = slotAt (e.getPosition()); slot >= 0)
  2073. {
  2074. const auto to = slot + ((wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1);
  2075. bool moved = false;
  2076. engine.editSong ([t = track, slot, to, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, slot, to); });
  2077. if (moved)
  2078. selectedSlot = to;
  2079. repaint();
  2080. return;
  2081. }
  2082. juce::Component::mouseWheelMove (e, wheel);
  2083. }
  2084. ui::Hint EffectsPanel::hintAt (juce::Point<int> p)
  2085. {
  2086. const auto gesture = ui::valueGestures();
  2087. if (dragParam == mixParam || (dragParam >= 0 && drag.isActive()))
  2088. return ui::valueHint (specFor (dragParam, dragParam == mixParam ? juce::jmax (0, selectedSlot) : selectedSlot),
  2089. valueOf (dragParam, dragParam == mixParam ? juce::jmax (0, selectedSlot) : selectedSlot), gesture);
  2090. if (const auto slot = mixKnobAt (p); slot >= 0)
  2091. return ui::valueHint (specFor (mixParam, slot), valueOf (mixParam, slot), gesture);
  2092. if (const auto param = knobAt (p); param >= 0)
  2093. return ui::valueHint (specFor (param, selectedSlot), valueOf (param, selectedSlot), gesture);
  2094. ui::Hint h;
  2095. if (const auto* s = slots(); s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size()
  2096. && NativeEffectWindows::canOpen ((*s)[(std::size_t) selectedSlot].kind) && editorButtonBounds().contains (p))
  2097. {
  2098. h.text = tr ("Open the effect's window (its full controls, as in FL); double-clicking the slot does the same");
  2099. return h;
  2100. }
  2101. if (track > 0 && armBounds().contains (p))
  2102. {
  2103. h.text = tr ("Record arm: Rec + Play records this insert's audio input into a Playlist clip [RMB] input and monitoring");
  2104. h.hasMenu = true;
  2105. return h;
  2106. }
  2107. if (track > 0 && inputBoxBounds().contains (p))
  2108. {
  2109. h.text = tr ("Audio input source: the microphone or interface input this insert plays and records [LMB] choose");
  2110. h.hasMenu = true;
  2111. const auto& mt = engine.getSong().mixer[(std::size_t) track];
  2112. if (mt.inputChannel >= 0)
  2113. h.value = juce::String::fromUTF8 (inputSourceName (mt.inputChannel, mt.inputStereo, engine.getInputChannelNames()).c_str());
  2114. return h;
  2115. }
  2116. if (fxSwitchBounds().contains (p) && slots() != nullptr && ! slots()->empty())
  2117. {
  2118. h.text = tr ("All effects of this insert on/off; right-click opens or closes their plugin windows");
  2119. h.hasMenu = true;
  2120. return h;
  2121. }
  2122. for (int i = 0; i < maxEffectSlots; ++i)
  2123. if (slotBounds (i).contains (p))
  2124. {
  2125. const auto used = slots() != nullptr && i < (int) slots()->size();
  2126. h.text = used ? tr ("Effect slot: click edits, the LED bypasses, drag or wheel reorders, right-click: replace, move, delete")
  2127. : tr ("Empty slot: click to add an effect");
  2128. h.hasMenu = used;
  2129. }
  2130. return h;
  2131. }
  2132. void EffectsPanel::mouseDrag (const juce::MouseEvent& e)
  2133. {
  2134. if (dragSlot >= 0)
  2135. {
  2136. if (e.getDistanceFromDragStart() > 4)
  2137. {
  2138. const auto count = slots() != nullptr ? (int) slots()->size() : 0;
  2139. const auto target = juce::jlimit (0, juce::jmax (0, count - 1), (e.y - slotsTop) / slotHeight);
  2140. if (target != dropSlot)
  2141. {
  2142. dropSlot = target;
  2143. setMouseCursor (juce::MouseCursor::DraggingHandCursor);
  2144. repaint();
  2145. }
  2146. }
  2147. return;
  2148. }
  2149. if (dragParam == gateStepParam)
  2150. {
  2151. setGateStep (e.getPosition());
  2152. return;
  2153. }
  2154. if (dragParam == bandLevelParam)
  2155. {
  2156. setBandLevel (e.getPosition());
  2157. return;
  2158. }
  2159. if (dragParam == mixParam)
  2160. {
  2161. setValue (mixParam, selectedSlot, drag.update (e, specFor (mixParam, selectedSlot)));
  2162. showTip (mixParam, selectedSlot, false);
  2163. return;
  2164. }
  2165. if (dragParam < 0)
  2166. return;
  2167. setValue (dragParam, selectedSlot, drag.update (e, specFor (dragParam, selectedSlot)));
  2168. showTip (dragParam, selectedSlot, false);
  2169. }
  2170. void EffectsPanel::showTip (int param, int slot, bool transient)
  2171. {
  2172. const auto* s = slots();
  2173. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  2174. return;
  2175. const auto area = param == mixParam ? mixBounds (slot) : knobBounds (param).withHeight (juce::jlimit (30, 44, knobBounds (param).getHeight() - 30));
  2176. const auto text = specFor (param, slot).text (valueOf (param, slot));
  2177. ui::postHint (ui::valueHint (specFor (param, slot), valueOf (param, slot), ui::valueGestures()));
  2178. if (transient)
  2179. ui::flashValueTip (*this, area, text);
  2180. else
  2181. ui::showValueTip (*this, area, text);
  2182. }
  2183. void EffectsPanel::mouseMove (const juce::MouseEvent& e)
  2184. {
  2185. const auto p = e.getPosition();
  2186. auto slot = slotAt (p);
  2187. auto param = -1;
  2188. auto led = false;
  2189. for (int i = 0; i < maxEffectSlots; ++i)
  2190. if (slotBounds (i).contains (p))
  2191. slot = i;
  2192. if (const auto m = mixKnobAt (p); m >= 0)
  2193. {
  2194. slot = m;
  2195. param = mixParam;
  2196. }
  2197. else if (slot < 0)
  2198. param = knobAt (p);
  2199. else
  2200. led = p.x < slotBounds (slot).getX() + 22;
  2201. const auto input = track > 0 && armBounds().contains (p) ? 1 : track > 0 && inputBoxBounds().contains (p) ? 2 : 0;
  2202. if (slot != hoverSlot || param != hoverParam || led != hoverLed || input != hoverInput)
  2203. {
  2204. hoverInput = input;
  2205. hoverSlot = slot;
  2206. hoverParam = param;
  2207. hoverLed = led;
  2208. repaint();
  2209. }
  2210. }
  2211. void EffectsPanel::mouseExit (const juce::MouseEvent&)
  2212. {
  2213. if (hoverSlot >= 0 || hoverParam != -1 || hoverInput != 0)
  2214. {
  2215. hoverInput = 0;
  2216. hoverSlot = hoverParam = -1;
  2217. hoverLed = false;
  2218. repaint();
  2219. }
  2220. }
  2221. void EffectsPanel::mouseUp (const juce::MouseEvent&)
  2222. {
  2223. if (dragSlot >= 0 && dropSlot >= 0 && dropSlot != dragSlot)
  2224. {
  2225. bool moved = false;
  2226. engine.editSong ([t = track, from = dragSlot, to = dropSlot, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, from, to); });
  2227. if (moved)
  2228. selectedSlot = dropSlot;
  2229. }
  2230. if (dragSlot >= 0)
  2231. setMouseCursor (juce::MouseCursor::NormalCursor);
  2232. dragSlot = dropSlot = -1;
  2233. repaint();
  2234. dragParam = -1;
  2235. drag.end();
  2236. ui::hideValueTip();
  2237. ui::clearPostedHint();
  2238. }
  2239. void EffectsPanel::mouseDoubleClick (const juce::MouseEvent& e)
  2240. {
  2241. for (int i = 0; slots() != nullptr && i < (int) slots()->size(); ++i)
  2242. if (mixBounds (i).contains (e.getPosition()))
  2243. {
  2244. engine.editSong ([t = track, i] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) i].mix = 1.0f; });
  2245. return;
  2246. }
  2247. else if (slotBounds (i).contains (e.getPosition()) && e.x >= slotBounds (i).getX() + 22)
  2248. {
  2249. openEditor (i); // double-click on the name: the effect's window (as clicking a slot in FL)
  2250. return;
  2251. }
  2252. const auto p = knobAt (e.getPosition());
  2253. if (p < 0)
  2254. return;
  2255. const auto kind = (*slots())[(std::size_t) selectedSlot].kind;
  2256. const auto value = effectInfo (kind).params[(std::size_t) p].defaultValue;
  2257. engine.editSong ([t = track, slot = selectedSlot, p, value] (Song& song) {
  2258. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) p] = value;
  2259. });
  2260. }
  2261. bool EffectsPanel::isInterestedInDragSource (const SourceDetails& details)
  2262. {
  2263. return BrowserComponent::isBrowserDrag (details) && BrowserComponent::draggedFile (details).hasFileExtension ("fst");
  2264. }
  2265. void EffectsPanel::itemDropped (const SourceDetails& details)
  2266. {
  2267. dropPreset (BrowserComponent::draggedFile (details), details.localPosition);
  2268. }
  2269. bool EffectsPanel::isInterestedInFileDrag (const juce::StringArray& files)
  2270. {
  2271. return files.size() == 1 && juce::File (files[0]).hasFileExtension ("fst");
  2272. }
  2273. void EffectsPanel::filesDropped (const juce::StringArray& files, int x, int y)
  2274. {
  2275. if (! files.isEmpty())
  2276. dropPreset (juce::File (files[0]), { x, y });
  2277. }
  2278. bool EffectsPanel::dropPreset (const juce::File& file, juce::Point<int> position)
  2279. {
  2280. juce::MemoryBlock data;
  2281. if (slots() == nullptr || ! file.loadFileAsData (data))
  2282. return false;
  2283. const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
  2284. const auto fx = effectFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), engine.getSong().bpm);
  2285. if (! fx)
  2286. {
  2287. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Preset"),
  2288. tr ("LYNX can't build an effect from this preset."));
  2289. return false;
  2290. }
  2291. auto target = -1;
  2292. for (int i = 0; i < (int) slots()->size(); ++i)
  2293. if (slotBounds (i).contains (position))
  2294. target = i;
  2295. if (target < 0 && (int) slots()->size() >= maxEffectSlots)
  2296. return false;
  2297. engine.editSong ([t = track, target, fx = *fx] (Song& song) {
  2298. auto& effects = song.mixer[(std::size_t) t].effects;
  2299. if (target >= 0 && target < (int) effects.size())
  2300. effects[(std::size_t) target] = fx;
  2301. else
  2302. effects.push_back (fx);
  2303. });
  2304. selectedSlot = target >= 0 ? target : (int) slots()->size() - 1;
  2305. repaint();
  2306. return true;
  2307. }
  2308. juce::Rectangle<int> EffectsPanel::fxSwitchBounds() const
  2309. {
  2310. return { getWidth() - 58, 6, 48, panelHeader - 12 };
  2311. }
  2312. int EffectsPanel::slotAt (juce::Point<int> p) const
  2313. {
  2314. const auto* s = slots();
  2315. for (int i = 0; s != nullptr && i < (int) s->size(); ++i)
  2316. if (slotBounds (i).contains (p) && ! mixBounds (i).contains (p) && p.x >= slotBounds (i).getX() + 22)
  2317. return i;
  2318. return -1;
  2319. }
  2320. void EffectsPanel::toggleAllWindows()
  2321. {
  2322. // opens every plugin effect window of the insert, or closes them all when one is open
  2323. const auto* s = slots();
  2324. if (s == nullptr)
  2325. return;
  2326. bool anyOpen = false;
  2327. for (int i = 0; i < (int) s->size(); ++i)
  2328. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, i).get()))
  2329. anyOpen = anyOpen || PluginWindows::isOpen (p->getInstance());
  2330. for (int i = 0; i < (int) s->size(); ++i)
  2331. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, i).get()))
  2332. {
  2333. if (anyOpen)
  2334. PluginWindows::close (p->getInstance());
  2335. else
  2336. openPluginWindow (i);
  2337. }
  2338. }
  2339. juce::Rectangle<int> EffectsPanel::pluginButtonBounds() const
  2340. {
  2341. return { 20, slotsTop + maxEffectSlots * slotHeight + 30, getWidth() - 40, 30 };
  2342. }
  2343. void EffectsPanel::openEditor (int slot)
  2344. {
  2345. const auto* s = slots();
  2346. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  2347. return;
  2348. const auto kind = (*s)[(std::size_t) slot].kind;
  2349. if (kind == EffectKind::plugin)
  2350. openPluginWindow (slot);
  2351. else if (kind == EffectKind::parametricEq)
  2352. ParametricEqWindows::open (engine, track, slot);
  2353. else if (NativeEffectWindows::canOpen (kind))
  2354. NativeEffectWindows::open (engine, track, slot);
  2355. }
  2356. juce::Rectangle<int> EffectsPanel::editorButtonBounds() const
  2357. {
  2358. // on the title row of the selected effect's knobs
  2359. return { getWidth() - 96, (tuneSelected() ? tuneKeysBounds().getY() : knobBounds (0).getY()) - 29, 86, 20 };
  2360. }
  2361. void EffectsPanel::openPluginWindow (int slot)
  2362. {
  2363. auto effect = engine.getEffect (track, slot);
  2364. if (auto* plugin = dynamic_cast<PluginEffect*> (effect.get()))
  2365. {
  2366. const auto& s = engine.getSong().mixer[(std::size_t) track].effects[(std::size_t) slot];
  2367. PluginWindows::open (effect, plugin->getInstance(), juce::String::fromUTF8 (s.plugin.name.c_str()));
  2368. return;
  2369. }
  2370. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Plugin"),
  2371. tr ("This plugin could not be loaded (it is not installed or has not been scanned)."));
  2372. }
  2373. void EffectsPanel::showAddMenu (int)
  2374. {
  2375. juce::PopupMenu menu;
  2376. for (auto kind : allEffects)
  2377. menu.addItem (juce::String::fromUTF8 (effectInfo (kind).name), [this, kind] {
  2378. engine.editSong ([t = track, kind] (Song& song) {
  2379. auto& effects = song.mixer[(std::size_t) t].effects;
  2380. if ((int) effects.size() < maxEffectSlots)
  2381. effects.push_back (makeEffectSlot (kind));
  2382. });
  2383. selectedSlot = (int) slots()->size() - 1;
  2384. repaint();
  2385. });
  2386. // LYNX equivalents of well-known third-party effects, with the same default preset a substitute gets
  2387. juce::PopupMenu equivalents;
  2388. for (const auto& entry : pluginSubstitutes())
  2389. if (! entry.instrument)
  2390. equivalents.addItem (juce::String::fromUTF8 (entry.product.c_str()) + juce::String::fromUTF8 (" \xe2\x86\x92 ") + juce::String::fromUTF8 (effectInfo (entry.effect).name),
  2391. [this, entry] {
  2392. engine.editSong ([t = track, entry] (Song& song) {
  2393. auto& effects = song.mixer[(std::size_t) t].effects;
  2394. if ((int) effects.size() < maxEffectSlots)
  2395. effects.push_back (makeSubstituteSlot (entry));
  2396. });
  2397. selectedSlot = (int) slots()->size() - 1;
  2398. repaint();
  2399. });
  2400. if (equivalents.getNumItems() > 0)
  2401. menu.addSubMenu (tr ("LYNX equivalents"), equivalents);
  2402. juce::PopupMenu plugins;
  2403. addPluginItems (plugins, engine, false, [this] (PluginRef ref) {
  2404. engine.editSong ([t = track, ref] (Song& song) {
  2405. auto& effects = song.mixer[(std::size_t) t].effects;
  2406. if ((int) effects.size() >= maxEffectSlots)
  2407. return;
  2408. EffectSlot slot;
  2409. slot.kind = EffectKind::plugin;
  2410. slot.plugin = ref;
  2411. effects.push_back (slot);
  2412. });
  2413. selectedSlot = (int) slots()->size() - 1;
  2414. openPluginWindow (selectedSlot);
  2415. repaint();
  2416. });
  2417. menu.addSeparator();
  2418. menu.addSubMenu (tr ("Plugin (VST/VST3/CLAP)"), plugins);
  2419. menu.addSeparator();
  2420. menu.addItem (tr ("Audio editor (record this insert)..."), [this] { openAudioEditorForInsert (engine, track); });
  2421. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  2422. }
  2423. void EffectsPanel::showSlotMenu (int slot)
  2424. {
  2425. juce::PopupMenu replace;
  2426. for (auto kind : allEffects)
  2427. replace.addItem (juce::String::fromUTF8 (effectInfo (kind).name), [this, slot, kind] {
  2428. engine.editSong ([t = track, slot, kind] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) slot] = makeEffectSlot (kind); });
  2429. });
  2430. const auto count = (int) slots()->size();
  2431. juce::PopupMenu menu;
  2432. menu.addSubMenu (tr ("Replace with"), replace);
  2433. menu.addItem (tr ("Audio editor (record this insert)..."), [this] { openAudioEditorForInsert (engine, track); });
  2434. // FL presets (Plugin presets\Effects\<plugin>) that map to this effect
  2435. {
  2436. juce::PopupMenu presets;
  2437. const auto roots = findFlStudioRoots();
  2438. const auto base = juce::File (juce::String::fromUTF8 (roots.data.c_str())).getChildFile ("Patches/Plugin presets/Effects");
  2439. for (const auto& plugin : flPluginsForEffect ((*slots())[(std::size_t) slot].kind))
  2440. {
  2441. juce::PopupMenu sub;
  2442. auto files = base.getChildFile (juce::String::fromUTF8 (plugin.c_str())).findChildFiles (juce::File::findFiles, true, "*.fst");
  2443. std::sort (files.begin(), files.end(), [] (const juce::File& a, const juce::File& b) { return a.getFileName().compareNatural (b.getFileName()) < 0; });
  2444. for (const auto& f : files)
  2445. sub.addItem (f.getFileNameWithoutExtension(), [this, slot, f] {
  2446. juce::MemoryBlock data;
  2447. if (! f.loadFileAsData (data))
  2448. return;
  2449. const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
  2450. if (auto fx = effectFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), engine.getSong().bpm))
  2451. engine.editSong ([t = track, slot, fx = *fx] (Song& song) {
  2452. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  2453. const auto enabled = target.enabled;
  2454. const auto mix = target.mix;
  2455. target = fx;
  2456. target.enabled = enabled;
  2457. target.mix = mix;
  2458. });
  2459. });
  2460. if (sub.getNumItems() > 0)
  2461. presets.addSubMenu (juce::String::fromUTF8 (plugin.c_str()), sub);
  2462. }
  2463. if (presets.getNumItems() > 0)
  2464. menu.addSubMenu (tr ("FL presets"), presets);
  2465. }
  2466. // side inputs: another track's output feeds the compressor's detector, or the vocoder's modulator/carrier
  2467. const auto kind = (*slots())[(std::size_t) slot].kind;
  2468. auto sideMenu = [&] (const juce::String& title, bool second) {
  2469. juce::PopupMenu side;
  2470. const auto& fx = (*slots())[(std::size_t) slot];
  2471. const auto current = second ? fx.sidechain2 : fx.sidechain;
  2472. auto assign = [this, slot, second] (int source) {
  2473. engine.editSong ([tk = track, slot, second, source] (Song& s) {
  2474. auto& target = s.mixer[(std::size_t) tk].effects[(std::size_t) slot];
  2475. (second ? target.sidechain2 : target.sidechain) = source;
  2476. if (target.kind == EffectKind::vocoder && source >= 0)
  2477. target.params[16] = 1.0f; // separate inputs (otherwise L = modulator, R = carrier)
  2478. });
  2479. };
  2480. side.addItem (tr ("None"), true, current < 0, [assign] { assign (-1); });
  2481. const auto& song = engine.getSong();
  2482. for (int t = 1; t < (int) song.mixer.size(); ++t)
  2483. if (t != track)
  2484. side.addItem (juce::String (t) + " " + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()), true, current == t,
  2485. [assign, t] { assign (t); });
  2486. menu.addSubMenu (title, side);
  2487. };
  2488. if (kind == EffectKind::compressor || kind == EffectKind::fruityLimiter || kind == EffectKind::fruityCompressor || kind == EffectKind::facetEq)
  2489. sideMenu (tr ("Sidechain from"), false);
  2490. if (kind == EffectKind::vocoder || kind == EffectKind::vocodex)
  2491. {
  2492. sideMenu (tr ("Modulator from"), false);
  2493. sideMenu (tr ("Carrier from"), true);
  2494. }
  2495. if (kind == EffectKind::newtone || kind == EffectKind::newtime)
  2496. {
  2497. // the sample the plugin plays in slave mode (REA_rodada73): FL's edits are kept in the state but not reproduced
  2498. const auto& fx = (*slots())[(std::size_t) slot];
  2499. const auto current = juce::File (juce::String::fromUTF8 (fx.samplePath.c_str()));
  2500. juce::PopupMenu source;
  2501. source.addItem (fx.samplePath.empty() ? tr ("No sample") : current.getFileName(), false, false, [] {});
  2502. source.addItem (tr ("Choose a sample..."), [this, slot, current] {
  2503. sampleChooser = std::make_unique<juce::FileChooser> (tr ("Source sample"), current.existsAsFile() ? current : juce::File(),
  2504. "*.wav;*.aif;*.aiff;*.flac;*.ogg;*.mp3");
  2505. sampleChooser->launchAsync (juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
  2506. [this, slot] (const juce::FileChooser& fc) {
  2507. const auto file = fc.getResult();
  2508. if (file == juce::File())
  2509. return;
  2510. engine.editSong ([t = track, slot, path = file.getFullPathName().toStdString()] (Song& s) {
  2511. if (t < (int) s.mixer.size() && slot < (int) s.mixer[(std::size_t) t].effects.size())
  2512. s.mixer[(std::size_t) t].effects[(std::size_t) slot].samplePath = path;
  2513. });
  2514. });
  2515. });
  2516. source.addItem (tr ("Remove the sample"), ! fx.samplePath.empty(), false, [this, slot] {
  2517. engine.editSong ([t = track, slot] (Song& s) {
  2518. if (t < (int) s.mixer.size() && slot < (int) s.mixer[(std::size_t) t].effects.size())
  2519. s.mixer[(std::size_t) t].effects[(std::size_t) slot].samplePath.clear();
  2520. });
  2521. });
  2522. menu.addSubMenu (tr ("Source sample"), source);
  2523. }
  2524. {
  2525. // A/B compare: keep a copy of the settings, then swap between it and the current ones
  2526. const auto key = std::make_pair (track, slot);
  2527. const auto hasSpare = spare.count (key) > 0 && spare[key].kind == (*slots())[(std::size_t) slot].kind;
  2528. juce::PopupMenu ab;
  2529. ab.addItem (tr ("Store the current settings as B"), [this, key, slot] {
  2530. spare[key] = (*slots())[(std::size_t) slot];
  2531. });
  2532. ab.addItem (tr ("Swap with B"), hasSpare, false, [this, key, slot] {
  2533. const auto stored = spare[key];
  2534. spare[key] = (*slots())[(std::size_t) slot];
  2535. engine.editSong ([t = track, slot, stored] (Song& song) {
  2536. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  2537. const auto enabled = target.enabled;
  2538. target = stored;
  2539. target.enabled = enabled;
  2540. });
  2541. });
  2542. ab.addItem (tr ("Forget B"), hasSpare, false, [this, key] { spare.erase (key); });
  2543. menu.addSubMenu (hasSpare ? tr ("A/B compare (B stored)") : tr ("A/B compare"), ab);
  2544. }
  2545. menu.addItem (tr ("Move up"), slot > 0, false, [this, slot] {
  2546. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot - 1); });
  2547. selectedSlot = slot - 1;
  2548. });
  2549. menu.addItem (tr ("Move down"), slot < count - 1, false, [this, slot] {
  2550. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot + 1); });
  2551. selectedSlot = slot + 1;
  2552. });
  2553. if ((*slots())[(std::size_t) slot].kind == EffectKind::plugin)
  2554. {
  2555. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, slot).get()))
  2556. addPluginParameterItems (menu, engine, p->getInstance(), { AutomationTargetKind::effectParam, track, slot, 0 });
  2557. }
  2558. menu.addSeparator();
  2559. menu.addItem (tr ("Delete"), [this, slot] {
  2560. engine.editSong ([t = track, slot] (Song& song) { song.removeEffectSlot (t, slot); });
  2561. selectedSlot = juce::jmin (slot, (int) slots()->size() - 1);
  2562. repaint();
  2563. });
  2564. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  2565. }
  2566. void EffectsPanel::changeListenerCallback (juce::ChangeBroadcaster*)
  2567. {
  2568. const auto* s = slots();
  2569. if (s == nullptr)
  2570. track = 0;
  2571. else if (selectedSlot >= (int) s->size())
  2572. selectedSlot = (int) s->size() - 1;
  2573. repaint();
  2574. }
  2575. // ---------------------------------------------------------------------------
  2576. namespace mixerinput
  2577. {
  2578. std::function<void()> openAudioSettings;
  2579. namespace
  2580. {
  2581. juce::String issueText (const AudioEngine::RecordingNotice& notice)
  2582. {
  2583. using I = AudioEngine::InputIssue;
  2584. switch (notice.issue)
  2585. {
  2586. case I::noDevice: return tr ("No audio device is open. Choose one in Settings > Audio.");
  2587. case I::noInputDevice:
  2588. return tr ("No audio input device was found. Connect a microphone or an audio interface, then choose it as the input device in Settings > Audio.");
  2589. case I::noInputOpen: return tr ("No audio input is open, so nothing can be recorded. Choose an input device in Settings > Audio.");
  2590. case I::armedWithoutInput:
  2591. return tr ("Please select an audio input first: the armed insert has none. Click the input box above the insert's effect slots in the Mixer (or right-click its arm button).");
  2592. case I::insertWithoutInput: return tr ("Insert {0} is armed but has no audio input, so it records nothing.", { juce::String (notice.track) });
  2593. case I::failed: return tr ("The audio input could not be used: {0}", { notice.detail });
  2594. case I::none: break;
  2595. }
  2596. return {};
  2597. }
  2598. bool aboutTheDevice (AudioEngine::InputIssue issue)
  2599. {
  2600. using I = AudioEngine::InputIssue;
  2601. return issue == I::noDevice || issue == I::noInputDevice || issue == I::noInputOpen || issue == I::failed;
  2602. }
  2603. } // namespace
  2604. void explain (const AudioEngine::RecordingNotice& notice)
  2605. {
  2606. if (notice.issue == AudioEngine::InputIssue::none)
  2607. return;
  2608. auto options = juce::MessageBoxOptions()
  2609. .withIconType (juce::MessageBoxIconType::WarningIcon)
  2610. .withTitle (tr ("Audio input needed"))
  2611. .withMessage (issueText (notice));
  2612. if (aboutTheDevice (notice.issue) && openAudioSettings)
  2613. {
  2614. options = options.withButton (tr ("Audio settings...")).withButton (tr ("Close"));
  2615. juce::AlertWindow::showAsync (options, [] (int result) {
  2616. if (result == 1 && openAudioSettings)
  2617. openAudioSettings();
  2618. });
  2619. }
  2620. else
  2621. juce::AlertWindow::showAsync (options.withButton (tr ("OK")), nullptr);
  2622. }
  2623. void showMenu (AudioEngine& engine, int track, juce::Component* target, std::function<void()> chosen)
  2624. {
  2625. if (track <= 0 || track >= (int) engine.getSong().mixer.size())
  2626. return;
  2627. // the device input opens on demand (only then does Windows show the microphone as in use)
  2628. if (! engine.hasAudioInput())
  2629. if (const auto result = engine.openAudioInput(); result.issue != AudioEngine::InputIssue::none)
  2630. {
  2631. explain (result);
  2632. return;
  2633. }
  2634. const auto& mt = engine.getSong().mixer[(std::size_t) track];
  2635. const auto names = engine.getInputChannelNames();
  2636. juce::PopupMenu menu;
  2637. menu.addSectionHeader (tr ("Audio input source"));
  2638. menu.addItem (tr ("(none)"), true, mt.inputChannel < 0, [&engine, track] { engine.setInsertInput (track, -1, true); });
  2639. const auto sources = inputSources (names);
  2640. if (sources.empty())
  2641. menu.addItem (tr ("The audio device has no open input channels"), false, false, nullptr);
  2642. for (const auto& source : sources)
  2643. {
  2644. if (! source.stereo && source.channel == 0 && names.size() > 1)
  2645. menu.addSeparator(); // pairs, then single channels
  2646. const auto ticked = mt.inputChannel == source.channel && mt.inputStereo == source.stereo;
  2647. menu.addItem (juce::String::fromUTF8 (source.name.c_str()) + (source.stereo ? "" : " " + tr ("(mono)")), true, ticked,
  2648. [&engine, track, source, chosen] {
  2649. engine.setInsertInput (track, source.channel, source.stereo);
  2650. if (chosen)
  2651. chosen();
  2652. });
  2653. }
  2654. menu.addSectionHeader (tr ("Monitor external input"));
  2655. const std::pair<InputMonitor, const char*> modes[] = { { InputMonitor::off, TR_NOOP ("Off") },
  2656. { InputMonitor::whenArmed, TR_NOOP ("When armed") },
  2657. { InputMonitor::automatic, TR_NOOP ("Auto (when armed, not while playing back)") },
  2658. { InputMonitor::on, TR_NOOP ("On") } };
  2659. for (const auto& [mode, label] : modes)
  2660. menu.addItem (tr (label), true, mt.monitor == mode, [&engine, track, mode = mode] { engine.setInsertMonitor (track, mode); });
  2661. menu.addSeparator();
  2662. menu.addItem (tr ("Audio settings (input device)..."), openAudioSettings != nullptr, false, [] {
  2663. if (openAudioSettings)
  2664. openAudioSettings();
  2665. });
  2666. auto options = juce::PopupMenu::Options();
  2667. if (target != nullptr)
  2668. options = options.withTargetComponent (target).withMousePosition();
  2669. menu.showMenuAsync (options);
  2670. }
  2671. void toggleArm (AudioEngine& engine, int track, juce::Component* target)
  2672. {
  2673. if (engine.isInsertArmed (track))
  2674. {
  2675. engine.setInsertArmed (track, false);
  2676. return;
  2677. }
  2678. const auto& mixer = engine.getSong().mixer;
  2679. if (track <= 0 || track >= (int) mixer.size())
  2680. return;
  2681. if (mixer[(std::size_t) track].inputChannel >= 0)
  2682. {
  2683. engine.setInsertArmed (track, true);
  2684. if (const auto notice = engine.takeRecordingNotice(); notice.issue != AudioEngine::InputIssue::none)
  2685. explain (notice);
  2686. return;
  2687. }
  2688. // FL: "Audio input needed - Please select an audio input first." Here the input menu opens right away
  2689. ui::postHint ({ tr ("Audio input needed: choose this insert's input, then it is armed"), {}, false, false }, 4000);
  2690. showMenu (engine, track, target, [&engine, track] { engine.setInsertArmed (track, true); });
  2691. }
  2692. } // namespace mixerinput
  2693. // ---------------------------------------------------------------------------
  2694. MixerComponent::MixerComponent (AudioEngine& e)
  2695. : engine (e),
  2696. strips (e),
  2697. effects (e)
  2698. {
  2699. viewport.setViewedComponent (&strips, false);
  2700. viewport.setScrollBarsShown (false, true);
  2701. addAndMakeVisible (viewport);
  2702. addAndMakeVisible (effects);
  2703. strips.onSelectionChanged = [this] (int track) { effects.setTrack (track); };
  2704. }
  2705. bool MixerComponent::runAction (const std::string& id)
  2706. {
  2707. if (id == "mixer.moveLeft")
  2708. strips.moveSelected (-1);
  2709. else if (id == "mixer.moveRight")
  2710. strips.moveSelected (1);
  2711. else if (id == "mixer.linkChannels")
  2712. strips.linkSelectedChannels (false);
  2713. else if (id == "mixer.linkChannelsFrom")
  2714. strips.linkSelectedChannels (true);
  2715. else if (id == "mixer.selectLinkedChannels")
  2716. strips.selectLinkedChannels();
  2717. else
  2718. return false;
  2719. return true;
  2720. }
  2721. bool MixerComponent::addEffectToSelected (EffectSlot slot)
  2722. {
  2723. const auto t = strips.getSelectedTrack();
  2724. bool added = false;
  2725. engine.editSong ([t, &slot, &added] (Song& s) {
  2726. if (s.mixer.empty())
  2727. s.mixer = Song::defaultMixer (0);
  2728. if (t < (int) s.mixer.size() && (int) s.mixer[(std::size_t) t].effects.size() < maxEffectSlots)
  2729. {
  2730. s.mixer[(std::size_t) t].effects.push_back (std::move (slot));
  2731. added = true;
  2732. }
  2733. });
  2734. if (added)
  2735. {
  2736. effects.setTrack (t);
  2737. effects.selectSlot ((int) engine.getSong().mixer[(std::size_t) t].effects.size() - 1);
  2738. }
  2739. return added;
  2740. }
  2741. void MixerComponent::selectFirstTrackWithEffects()
  2742. {
  2743. const auto& mixer = engine.getSong().mixer;
  2744. for (int t = 0; t < (int) mixer.size(); ++t)
  2745. if (! mixer[(std::size_t) t].effects.empty())
  2746. {
  2747. strips.selectTrack (t);
  2748. return;
  2749. }
  2750. }
  2751. void MixerComponent::selectTrack (int track)
  2752. {
  2753. strips.selectTrack (track);
  2754. // scroll the strip into view
  2755. const auto t = strips.getSelectedTrack();
  2756. const auto x = t == 0 ? 0 : 4 + MixerStrips::masterWidth + 8 + (t - 1) * MixerStrips::stripWidth;
  2757. const auto w = t == 0 ? MixerStrips::masterWidth : MixerStrips::stripWidth;
  2758. const auto view = viewport.getViewArea();
  2759. if (x < view.getX() || x + w > view.getRight())
  2760. viewport.setViewPosition (juce::jmax (0, x - view.getWidth() / 2), 0);
  2761. }
  2762. void MixerComponent::resized()
  2763. {
  2764. auto area = getLocalBounds();
  2765. effects.setBounds (area.removeFromRight (effectsWidth));
  2766. viewport.setBounds (area);
  2767. strips.setVisibleHeight (area.getHeight() - viewport.getScrollBarThickness());
  2768. }
  2769. } // namespace lynx