4
0

MixerComponent.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. #include "ui/MixerComponent.h"
  2. #include "core/FlEffects.h"
  3. #include "core/MixerOps.h"
  4. #include "ui/AutomationUi.h"
  5. #include "ui/BrowserComponent.h"
  6. #include "ui/ExportDialog.h"
  7. #include "core/Effects.h"
  8. #include "engine/Text.h"
  9. #include "ui/ParametricEqEditor.h"
  10. #include "ui/PluginWindow.h"
  11. #include "ui/I18n.h"
  12. #include "ui/Icons.h"
  13. #include "ui/ValueControl.h"
  14. #include "ui/Theme.h"
  15. namespace lynx
  16. {
  17. namespace
  18. {
  19. constexpr int headerHeight = 34;
  20. constexpr int panHeight = 34;
  21. constexpr int buttonsHeight = 22;
  22. constexpr int feedsHeight = 46;
  23. // Fader 0..1 -> dB, with 0.8 = 0 dB (as in FL).
  24. juce::String faderToDb (float volume)
  25. {
  26. if (volume <= 0.0001f)
  27. return juce::String::fromUTF8 ("-\xe2\x88\x9e");
  28. const auto db = juce::Decibels::gainToDecibels (faderGain (volume));
  29. return (db > 0 ? "+" : "") + juce::String (db, 1);
  30. }
  31. // Meter level 0..1 on a dB scale (-60..+6).
  32. float meterPosition (float gain)
  33. {
  34. const auto db = juce::Decibels::gainToDecibels (gain, -60.0f);
  35. return juce::jlimit (0.0f, 1.0f, (db + 60.0f) / 66.0f);
  36. }
  37. } // namespace
  38. MixerStrips::MixerStrips (AudioEngine& e)
  39. : engine (e)
  40. {
  41. setWantsKeyboardFocus (false);
  42. setMouseClickGrabsKeyboardFocus (false);
  43. engine.addChangeListener (this);
  44. updateSize();
  45. startTimerHz (30);
  46. }
  47. MixerStrips::~MixerStrips()
  48. {
  49. engine.removeChangeListener (this);
  50. }
  51. void MixerStrips::setVisibleHeight (int height)
  52. {
  53. visibleHeight = juce::jmax (260, height);
  54. updateSize();
  55. }
  56. void MixerStrips::updateSize()
  57. {
  58. const auto tracks = (int) engine.getSong().mixer.size();
  59. setSize (stripX (tracks) + stripWidth + 8, visibleHeight);
  60. levels.resize ((std::size_t) juce::jmax (1, tracks) * 2, 0.0f);
  61. }
  62. int MixerStrips::stripX (int track) const noexcept
  63. {
  64. return track == 0 ? 4 : 4 + masterWidth + 10 + (track - 1) * stripWidth;
  65. }
  66. int MixerStrips::trackAt (int x) const noexcept
  67. {
  68. const auto tracks = (int) engine.getSong().mixer.size();
  69. for (int t = 0; t <= tracks; ++t) // t == tracks = "+" button
  70. if (x >= stripX (t) && x < stripX (t) + stripWidthOf (t))
  71. return t;
  72. return -1;
  73. }
  74. juce::Rectangle<int> MixerStrips::panArea (int t) const
  75. {
  76. return { stripX (t), headerHeight, stripWidthOf (t), panHeight };
  77. }
  78. juce::Rectangle<int> MixerStrips::muteArea (int t) const
  79. {
  80. return { stripX (t) + 6, headerHeight + panHeight, 26, buttonsHeight };
  81. }
  82. juce::Rectangle<int> MixerStrips::soloArea (int t) const
  83. {
  84. return { stripX (t) + stripWidthOf (t) - 32, headerHeight + panHeight, 26, buttonsHeight };
  85. }
  86. juce::Rectangle<int> MixerStrips::routeArea (int t) const
  87. {
  88. return { stripX (t) + 6, getHeight() - feedsHeight - 2, stripWidthOf (t) - 16, 16 };
  89. }
  90. juce::Rectangle<int> MixerStrips::armArea (int t) const
  91. {
  92. return { stripX (t) + 4, 4, 14, 14 };
  93. }
  94. juce::Rectangle<int> MixerStrips::faderArea (int t) const
  95. {
  96. const auto top = headerHeight + panHeight + buttonsHeight + 8;
  97. return { stripX (t) + 6, top, stripWidthOf (t) - 12, getHeight() - top - feedsHeight - 22 };
  98. }
  99. MixerStrips::Part MixerStrips::partAt (int t, juce::Point<int> p) const
  100. {
  101. if (t == (int) engine.getSong().mixer.size())
  102. return Part::add;
  103. if (t > 0 && armArea (t).contains (p))
  104. return Part::arm;
  105. if (panArea (t).contains (p))
  106. return Part::pan;
  107. if (muteArea (t).contains (p))
  108. return Part::mute;
  109. if (t > 0 && soloArea (t).contains (p))
  110. return Part::solo;
  111. if (routeArea (t).contains (p) && selected > 0 && t != selected)
  112. return Part::route;
  113. if (faderArea (t).expanded (0, 6).contains (p))
  114. return Part::fader;
  115. return Part::body;
  116. }
  117. void MixerStrips::paint (juce::Graphics& g)
  118. {
  119. using namespace theme;
  120. const auto& song = engine.getSong();
  121. const auto tracks = (int) song.mixer.size();
  122. const auto clip = g.getClipBounds();
  123. g.fillAll (colour::bg1);
  124. for (int t = 0; t < tracks; ++t)
  125. {
  126. const auto x = stripX (t);
  127. const auto w = stripWidthOf (t);
  128. if (x > clip.getRight() || x + w < clip.getX())
  129. continue;
  130. const auto& track = song.mixer[(std::size_t) t];
  131. const auto strip = juce::Rectangle<int> (x, 2, w - 4, getHeight() - 4);
  132. const auto isSelected = t == selected;
  133. const auto inSelection = std::binary_search (selection.begin(), selection.end(), t);
  134. g.setColour (isSelected || inSelection ? colour::bg4 : (t == 0 ? colour::bg3 : colour::bg2));
  135. g.fillRoundedRectangle (strip.toFloat(), radius::md);
  136. if (track.colour != 0) // thin colour band at the top
  137. {
  138. g.setColour (tame (juce::Colour (track.colour)));
  139. g.fillRoundedRectangle (strip.toFloat().withHeight (3.0f).reduced (6.0f, 0.0f), 1.5f);
  140. }
  141. if (inSelection && ! isSelected)
  142. {
  143. g.setColour (colour::accent.withAlpha (0.3f));
  144. g.drawRoundedRectangle (strip.toFloat().reduced (0.5f), radius::md, 1.0f);
  145. }
  146. if (isSelected)
  147. {
  148. g.setColour (colour::accent.withAlpha (0.55f));
  149. g.drawRoundedRectangle (strip.toFloat().reduced (0.5f), radius::md, 1.0f);
  150. g.setColour (colour::accent);
  151. g.fillRoundedRectangle (strip.toFloat().withHeight (3.0f).reduced (6.0f, 0.0f), 1.5f);
  152. }
  153. // record arm: the input recording goes through this insert
  154. if (t > 0)
  155. {
  156. const auto armed = engine.isInsertArmed (t);
  157. const auto r = armArea (t).toFloat().reduced (2.0f);
  158. g.setColour (armed ? colour::record : colour::bg0);
  159. g.fillEllipse (r);
  160. g.setColour (armed ? colour::record.brighter (0.3f) : colour::borderStrong);
  161. g.drawEllipse (r, 1.0f);
  162. }
  163. // header: number and name
  164. g.setColour (isSelected ? colour::accent : colour::textSecondary);
  165. g.setFont (font (type::caption, isSelected));
  166. g.drawText (t == 0 ? juce::String ("M") : juce::String (t), x, 5, w - 4, 12, juce::Justification::centred);
  167. g.setColour (track.muted ? colour::textDisabled : colour::textPrimary);
  168. g.setFont (font (type::small));
  169. g.drawFittedText (juce::String::fromUTF8 (track.name.c_str()), x + 3, 17, w - 10, 15, juce::Justification::centred, 1, 0.7f);
  170. // compensated latency (PDC) on the Master
  171. if (t == 0 && engine.getLatencySamples() > 0)
  172. {
  173. const auto ms = 1000.0 * engine.getLatencySamples() / juce::jmax (1.0, engine.getTransport().sampleRate.load());
  174. g.setColour (colour::textSecondary);
  175. g.setFont (font (10.0f));
  176. g.drawFittedText ("PDC " + juce::String (ms, 1) + " ms", soloArea (t).withX (x + 34).withWidth (w - 40),
  177. juce::Justification::centredLeft, 1, 0.8f); // the Master has no solo: use its spot
  178. }
  179. if (! track.effects.empty())
  180. {
  181. g.setColour (colour::info);
  182. g.setFont (font (9.0f, true));
  183. g.drawText ("FX " + juce::String ((int) track.effects.size()), x + w - 31, 5, 24, 11, juce::Justification::centredRight);
  184. }
  185. // pan
  186. drawKnob (g, panArea (t).toFloat().withSizeKeepingCentre (28.0f, 28.0f), (track.pan + 1.0f) * 0.5f, true, colour::info);
  187. // mute and solo
  188. {
  189. const auto m = muteArea (t).toFloat().withSizeKeepingCentre (12.0f, 12.0f);
  190. g.setColour (muteLed (track.muted));
  191. g.fillEllipse (m);
  192. if (! track.muted)
  193. {
  194. g.setColour (colour::ledOn.withAlpha (0.25f));
  195. g.drawEllipse (m.expanded (2.0f), 1.0f);
  196. }
  197. if (t > 0)
  198. {
  199. const auto s = soloArea (t).toFloat().reduced (2.0f);
  200. g.setColour (track.solo ? colour::accent : colour::bg0);
  201. g.fillRoundedRectangle (s, radius::sm);
  202. g.setColour (track.solo ? colour::onAccent : colour::textSecondary);
  203. g.setFont (font (type::caption, true));
  204. g.drawText ("S", s.toNearestInt(), juce::Justification::centred);
  205. }
  206. }
  207. // meters + fader
  208. {
  209. const auto area = faderArea (t);
  210. const auto meterW = 5;
  211. const auto meters = area.withWidth (meterW * 2 + 2);
  212. const auto fader = area.withTrimmedLeft (meterW * 2 + 8);
  213. for (int ch = 0; ch < 2; ++ch)
  214. {
  215. const auto bar = juce::Rectangle<int> (meters.getX() + ch * (meterW + 2), meters.getY(), meterW, meters.getHeight());
  216. g.setColour (colour::bg0);
  217. g.fillRoundedRectangle (bar.toFloat(), 1.5f);
  218. const auto level = (std::size_t) t * 2 + (std::size_t) ch < levels.size() ? levels[(std::size_t) t * 2 + (std::size_t) ch] : 0.0f;
  219. const auto h = (int) (meterPosition (level) * (float) bar.getHeight());
  220. const auto lit = bar.withTop (bar.getBottom() - h);
  221. juce::ColourGradient gradient (colour::meterClip, (float) bar.getX(), (float) bar.getY(), colour::meterLow, (float) bar.getX(),
  222. (float) bar.getBottom(), false);
  223. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (0.0f)), colour::meterClip); // above 0 dB: clip zone
  224. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (-6.0f)), colour::meterMid);
  225. gradient.addColour (1.0 - meterPosition (juce::Decibels::decibelsToGain (-18.0f)), colour::meterLow);
  226. g.setGradientFill (gradient);
  227. g.fillRect (lit);
  228. if (level >= 1.0f) // clip indicator
  229. {
  230. g.setColour (colour::meterClip);
  231. g.fillRect (bar.withHeight (3));
  232. }
  233. }
  234. // fader track and 0 dB mark
  235. const auto railX = (float) fader.getCentreX();
  236. const auto capY = (float) fader.getBottom() - track.volume * (float) fader.getHeight();
  237. g.setColour (colour::bg0);
  238. g.fillRoundedRectangle (railX - 2.0f, (float) fader.getY(), 4.0f, (float) fader.getHeight(), 2.0f);
  239. g.setColour (colour::bg6.brighter (0.15f).withAlpha (track.muted ? 0.4f : 1.0f));
  240. g.fillRoundedRectangle (railX - 2.0f, capY, 4.0f, juce::jmax (0.0f, (float) fader.getBottom() - capY), 2.0f);
  241. const auto unityY = (float) fader.getBottom() - unityFader * (float) fader.getHeight();
  242. g.setColour (colour::textDisabled);
  243. g.drawHorizontalLine ((int) unityY, (float) fader.getX(), railX - 5.0f);
  244. g.drawHorizontalLine ((int) unityY, railX + 5.0f, (float) fader.getRight());
  245. const auto capWidth = juce::jmin ((float) fader.getWidth() - 2.0f, 26.0f);
  246. const auto cap = juce::Rectangle<float> (capWidth, 12.0f).withCentre ({ railX, capY });
  247. g.setColour (juce::Colours::black.withAlpha (0.35f));
  248. g.fillRoundedRectangle (cap.translated (0.0f, 1.5f), radius::sm);
  249. g.setColour (juce::Colour (0xffd4d7dd)); // same cap as the LookAndFeel's linear faders
  250. g.fillRoundedRectangle (cap, radius::sm);
  251. g.setColour (colour::bg0.withAlpha (0.8f));
  252. g.fillRect (cap.withSizeKeepingCentre (capWidth - 8.0f, 1.5f));
  253. g.setColour (colour::textSecondary);
  254. g.setFont (font (type::caption));
  255. g.drawText (faderToDb (track.volume) + " dB", x, area.getBottom() + 4, w - 4, 14, juce::Justification::centred);
  256. }
  257. // send from the selected track to this one
  258. if (selected > 0 && t != selected && selected < tracks)
  259. {
  260. const auto r = routeArea (t).toFloat();
  261. const auto on = song.routes (selected, t);
  262. const auto possible = on || song.canRoute (selected, t);
  263. if (possible)
  264. {
  265. g.setColour (on ? colour::accent : colour::bg0);
  266. g.fillRoundedRectangle (r, radius::sm);
  267. g.setColour (on ? colour::onAccent : colour::textSecondary);
  268. g.setFont (font (10.0f, on));
  269. juce::String label = juce::String::fromUTF8 ("\xe2\x86\x92"); // arrow
  270. if (on && t > 0)
  271. for (const auto& send : song.mixer[(std::size_t) selected].sends)
  272. if (send.target == t)
  273. label << " " << juce::roundToInt (send.level * 100.0f) << "%";
  274. g.drawText (label, r.toNearestInt(), juce::Justification::centred);
  275. }
  276. }
  277. // channels routed into this track
  278. {
  279. juce::StringArray feeds;
  280. for (const auto& c : song.channels)
  281. if (juce::jlimit (0, tracks - 1, c.mixerTrack) == t)
  282. feeds.add (juce::String::fromUTF8 (c.name.c_str()));
  283. g.setColour (colour::textSecondary);
  284. g.setFont (font (10.0f));
  285. const auto text = feeds.size() > 3 ? feeds[0] + ", " + feeds[1] + " +" + juce::String (feeds.size() - 2) : feeds.joinIntoString (", ");
  286. g.drawFittedText (text, x + 3, getHeight() - feedsHeight + 16, w - 10, feedsHeight - 20, juce::Justification::centredTop, 2, 0.8f);
  287. }
  288. }
  289. // "+" button
  290. if (tracks < maxMixerTracks)
  291. {
  292. const auto add = juce::Rectangle<int> (stripX (tracks), 2, stripWidth - 4, 60).toFloat();
  293. g.setColour (colour::bg2);
  294. g.fillRoundedRectangle (add, radius::md);
  295. g.setColour (colour::border);
  296. g.drawRoundedRectangle (add.reduced (0.5f), radius::md, 1.0f);
  297. icons::draw (g, Icon::plus, add.withSizeKeepingCentre (22.0f, 22.0f), colour::textSecondary);
  298. }
  299. }
  300. void MixerStrips::mouseDown (const juce::MouseEvent& e)
  301. {
  302. const auto t = trackAt (e.x);
  303. if (t < 0)
  304. return;
  305. const auto part = partAt (t, e.getPosition());
  306. if (part == Part::add)
  307. {
  308. engine.editSong ([] (Song& s) {
  309. if (s.mixer.empty())
  310. s.mixer = Song::defaultMixer (0);
  311. auto& track = s.mixer.emplace_back();
  312. track.name = "Insert " + std::to_string (s.mixer.size() - 1);
  313. });
  314. return;
  315. }
  316. if (part == Part::arm)
  317. {
  318. engine.setInsertArmed (t, ! engine.isInsertArmed (t));
  319. repaint();
  320. return;
  321. }
  322. if (part == Part::route && e.mods.isPopupMenu())
  323. {
  324. showSendMenu (t);
  325. return;
  326. }
  327. if (part == Part::mute && (e.mods.isPopupMenu() || e.mods.isCtrlDown())) // solo on the same LED
  328. {
  329. soloExclusive (t);
  330. return;
  331. }
  332. if (part == Part::route)
  333. {
  334. // the selection is the send source: don't change it
  335. const auto from = selected;
  336. const auto on = engine.getSong().routes (from, t);
  337. engine.editSong ([from, t, on] (Song& s) { s.setRoute (from, t, ! on); });
  338. if (! on && t > 0) // just enabled: dragging adjusts the level
  339. {
  340. dragPart = Part::route;
  341. dragTrack = t;
  342. dragStartValue = 1.0f;
  343. }
  344. return;
  345. }
  346. if (part == Part::body && ! e.mods.isPopupMenu() && (e.mods.isCtrlDown() || e.mods.isShiftDown()) && t > 0)
  347. {
  348. auto s = selection;
  349. if (e.mods.isShiftDown())
  350. {
  351. s.clear();
  352. for (int i = juce::jmin (selected, t); i <= juce::jmax (selected, t); ++i)
  353. s.push_back (i);
  354. }
  355. else if (auto it = std::find (s.begin(), s.end(), t); it != s.end() && s.size() > 1)
  356. s.erase (it);
  357. else
  358. s.push_back (t);
  359. setSelection (s, std::find (s.begin(), s.end(), t) != s.end() ? t : s.front());
  360. return;
  361. }
  362. // a plain click on a track outside the selection selects only it; inside, the group stays
  363. if (! std::binary_search (selection.begin(), selection.end(), t))
  364. setSelection ({ t }, t);
  365. else if (selected != t)
  366. setSelection (selection, t);
  367. if (part == Part::pan || part == Part::fader)
  368. {
  369. if (e.mods.isPopupMenu())
  370. {
  371. ui::showValueMenu (*this, &engine, specFor (part, t), valueOf (part, t), [this, part, t] (double v) { setValue (part, t, v); });
  372. return;
  373. }
  374. if (ui::isResetClick (e))
  375. {
  376. setValue (part, t, specFor (part, t).defaultValue);
  377. return;
  378. }
  379. }
  380. if (e.mods.isPopupMenu())
  381. {
  382. showMenu (t);
  383. return;
  384. }
  385. const auto& track = engine.getSong().mixer[(std::size_t) t];
  386. switch (part)
  387. {
  388. case Part::mute:
  389. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].muted = ! s.mixer[(std::size_t) t].muted; });
  390. return;
  391. case Part::solo:
  392. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].solo = ! s.mixer[(std::size_t) t].solo; });
  393. return;
  394. case Part::pan:
  395. case Part::fader:
  396. dragPart = part;
  397. dragTrack = t;
  398. dragStartValue = part == Part::pan ? track.pan : track.volume;
  399. if (part == Part::fader && ! e.mods.isCtrlDown()) // clicking the track jumps the fader there
  400. {
  401. const auto area = faderArea (t);
  402. const auto capY = area.getBottom() - track.volume * (float) area.getHeight();
  403. if (std::abs ((float) e.y - capY) > 9.0f)
  404. {
  405. dragStartValue = juce::jlimit (0.0f, 1.0f, (float) (area.getBottom() - e.y) / (float) area.getHeight());
  406. engine.editSong ([t, v = dragStartValue] (Song& s) { s.mixer[(std::size_t) t].volume = v; });
  407. }
  408. }
  409. drag.begin (e, dragStartValue);
  410. groupStart.clear();
  411. groupGrab = part == Part::pan ? track.pan : track.volume;
  412. if (selection.size() > 1) // the selected strips follow, keeping their offsets
  413. for (auto s : selection)
  414. if (s != t && s < (int) engine.getSong().mixer.size())
  415. groupStart.push_back ({ s, part == Part::pan ? engine.getSong().mixer[(std::size_t) s].pan
  416. : engine.getSong().mixer[(std::size_t) s].volume });
  417. return;
  418. case Part::arm:
  419. case Part::route:
  420. case Part::body:
  421. case Part::none:
  422. case Part::add:
  423. return;
  424. }
  425. }
  426. void MixerStrips::mouseDrag (const juce::MouseEvent& e)
  427. {
  428. if (dragPart == Part::none)
  429. return;
  430. const auto t = dragTrack;
  431. if (dragPart == Part::route)
  432. {
  433. const auto from = selected;
  434. const auto v = juce::jlimit (0.0f, 1.0f, dragStartValue - (float) e.getDistanceFromDragStartY() / 120.0f);
  435. engine.editSong ([from, t, v] (Song& s) { s.setRoute (from, t, true, v); });
  436. return;
  437. }
  438. setValue (dragPart, t, drag.update (e, specFor (dragPart, t)));
  439. }
  440. ui::ValueSpec MixerStrips::specFor (Part part, int t) const
  441. {
  442. const auto& song = engine.getSong();
  443. const auto name = t >= 0 && t < (int) song.mixer.size() ? juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()) : juce::String();
  444. if (part == Part::pan)
  445. {
  446. auto spec = ui::panSpec (name + " - " + tr ("Pan"));
  447. spec.pixelsForRange = 150.0;
  448. spec.target = AutomationTarget { AutomationTargetKind::mixerPan, t };
  449. return spec;
  450. }
  451. auto spec = ui::faderSpec (name + " - " + tr ("Volume"), unityFader);
  452. spec.pixelsForRange = juce::jmax (60, faderArea (juce::jmax (0, t)).getHeight());
  453. spec.target = AutomationTarget { AutomationTargetKind::mixerVolume, t };
  454. return spec;
  455. }
  456. double MixerStrips::valueOf (Part part, int t) const
  457. {
  458. const auto& track = engine.getSong().mixer[(std::size_t) t];
  459. return part == Part::pan ? track.pan : track.volume;
  460. }
  461. void MixerStrips::setValue (Part part, int t, double value)
  462. {
  463. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  464. return;
  465. // during a drag of a selected strip, the other selected strips move by the same amount
  466. const auto delta = (float) value - groupGrab;
  467. const auto group = dragTrack == t && dragPart == part ? groupStart : std::vector<std::pair<int, float>> {};
  468. if (part == Part::pan)
  469. engine.editSong ([t, v = (float) value, group, delta] (Song& s) {
  470. s.mixer[(std::size_t) t].pan = v;
  471. for (auto [other, start] : group)
  472. if (other < (int) s.mixer.size())
  473. s.mixer[(std::size_t) other].pan = juce::jlimit (-1.0f, 1.0f, start + delta);
  474. });
  475. else if (part == Part::fader)
  476. engine.editSong ([t, v = (float) value, group, delta] (Song& s) {
  477. s.mixer[(std::size_t) t].volume = v;
  478. for (auto [other, start] : group)
  479. if (other < (int) s.mixer.size())
  480. s.mixer[(std::size_t) other].volume = juce::jlimit (0.0f, 1.0f, start + delta);
  481. });
  482. }
  483. void MixerStrips::setSelection (std::vector<int> tracks, int primary)
  484. {
  485. const auto count = (int) engine.getSong().mixer.size();
  486. tracks.erase (std::remove_if (tracks.begin(), tracks.end(), [count] (int t) { return t < 0 || t >= count; }), tracks.end());
  487. primary = juce::jlimit (0, juce::jmax (0, count - 1), primary);
  488. tracks.push_back (primary);
  489. std::sort (tracks.begin(), tracks.end());
  490. tracks.erase (std::unique (tracks.begin(), tracks.end()), tracks.end());
  491. selection = tracks;
  492. const auto changed = primary != selected;
  493. selected = primary;
  494. repaint();
  495. if (changed && onSelectionChanged)
  496. onSelectionChanged (selected);
  497. }
  498. bool MixerStrips::moveSelected (int delta)
  499. {
  500. auto moved = selection;
  501. bool ok = false;
  502. engine.editSong ([&] (Song& s) { ok = mixerops::moveTracks (s, moved, delta); });
  503. if (ok)
  504. setSelection (moved, juce::jlimit (1, (int) engine.getSong().mixer.size() - 1, selected + (delta < 0 ? -1 : 1)));
  505. return ok;
  506. }
  507. void MixerStrips::linkSelectedChannels (bool startingHere)
  508. {
  509. const auto channels = engine.getSelectedChannels();
  510. engine.editSong ([channels, t = selected, startingHere] (Song& s) { mixerops::linkChannels (s, channels, t, startingHere); });
  511. }
  512. void MixerStrips::selectLinkedChannels()
  513. {
  514. const auto channels = mixerops::channelsOn (engine.getSong(), selected);
  515. if (! channels.empty())
  516. engine.setChannelSelection (channels, channels.front());
  517. }
  518. void MixerStrips::soloExclusive (int t)
  519. {
  520. if (t <= 0)
  521. return;
  522. // solo only this track; again on the only soloed track clears the solo
  523. engine.editSong ([t] (Song& s) {
  524. const auto alone = s.mixer[(std::size_t) t].solo
  525. && std::none_of (s.mixer.begin() + 1, s.mixer.end(), [&] (const MixerTrack& m) { return &m != &s.mixer[(std::size_t) t] && m.solo; });
  526. for (std::size_t i = 1; i < s.mixer.size(); ++i)
  527. s.mixer[i].solo = ! alone && (int) i == t;
  528. });
  529. }
  530. void MixerStrips::showSendMenu (int t)
  531. {
  532. const auto& song = engine.getSong();
  533. const auto from = selected;
  534. if (from <= 0 || from == t)
  535. return;
  536. const auto fromName = juce::String::fromUTF8 (song.mixer[(std::size_t) from].name.c_str());
  537. juce::PopupMenu menu;
  538. menu.addSectionHeader (fromName + juce::String::fromUTF8 (" \xe2\x86\x92 ") + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()));
  539. const auto on = song.routes (from, t);
  540. menu.addItem (on ? tr ("Remove this send") : tr ("Send here"), on || song.canRoute (from, t), false, [this, from, t, on] {
  541. engine.editSong ([from, t, on] (Song& s) { s.setRoute (from, t, ! on); });
  542. });
  543. menu.addItem (tr ("Send only here"), on || song.canRoute (from, t), false, [this, from, t] {
  544. engine.editSong ([from, t] (Song& s) { mixerops::routeTo (s, { from }, t, true); });
  545. });
  546. menu.addItem (tr ("Sidechain only (no audio): the compressors here listen to it"), t > 0 && (on || song.canRoute (from, t)), false, [this, from, t] {
  547. int linked = 0;
  548. engine.editSong ([from, t, &linked] (Song& s) { linked = mixerops::sidechainTo (s, from, t); });
  549. if (linked == 0)
  550. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Sidechain"),
  551. tr ("The link was made; add a compressor to this insert to use it."));
  552. });
  553. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  554. }
  555. void MixerStrips::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  556. {
  557. const auto t = trackAt (e.x);
  558. if (t > 0 && t < (int) engine.getSong().mixer.size() && e.mods.isShiftDown() && partAt (t, e.getPosition()) == Part::body)
  559. {
  560. if (! std::binary_search (selection.begin(), selection.end(), t))
  561. setSelection ({ t }, t);
  562. moveSelected ((wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1);
  563. return;
  564. }
  565. if (t >= 0 && t < (int) engine.getSong().mixer.size())
  566. if (const auto part = partAt (t, e.getPosition()); part == Part::pan || part == Part::fader)
  567. {
  568. if (const auto v = ui::wheelValue (valueOf (part, t), e, wheel, specFor (part, t)))
  569. setValue (part, t, *v);
  570. return;
  571. }
  572. juce::Component::mouseWheelMove (e, wheel);
  573. }
  574. ui::Hint MixerStrips::hintAt (juce::Point<int> p)
  575. {
  576. const auto active = dragPart == Part::pan || dragPart == Part::fader;
  577. const auto t = active ? dragTrack : trackAt (p.x);
  578. ui::Hint h;
  579. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  580. {
  581. if (t == (int) engine.getSong().mixer.size())
  582. h.text = tr ("Add a mixer insert");
  583. return h;
  584. }
  585. const auto part = active ? dragPart : partAt (t, p);
  586. const auto name = juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) t].name.c_str());
  587. switch (part)
  588. {
  589. case Part::pan:
  590. case Part::fader:
  591. return ui::valueHint (specFor (part, t), valueOf (part, t), tr ("drag (Ctrl: fine), wheel, Alt+click resets"));
  592. case Part::mute: h.text = name + " - " + tr ("Click: mute. Right-click or Ctrl+click: solo"); break;
  593. case Part::arm: h.text = name + " - " + tr ("Record arm: audio takes from the input play through this insert"); break;
  594. case Part::solo: h.text = name + " - " + tr ("Solo"); break;
  595. 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;
  596. case Part::add: h.text = tr ("Add a mixer insert"); break;
  597. case Part::body:
  598. case Part::none:
  599. h.text = name + " - " + tr ("Click selects the insert (Ctrl: add, Shift: range), right-click opens its menu; Alt+Left/Right moves the selection");
  600. h.hasMenu = true;
  601. break;
  602. }
  603. return h;
  604. }
  605. void MixerStrips::mouseUp (const juce::MouseEvent&)
  606. {
  607. dragPart = Part::none;
  608. dragTrack = -1;
  609. drag.end();
  610. }
  611. void MixerStrips::mouseDoubleClick (const juce::MouseEvent& e)
  612. {
  613. const auto t = trackAt (e.x);
  614. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  615. return;
  616. const auto part = partAt (t, e.getPosition());
  617. if (part == Part::fader)
  618. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].volume = unityFader; });
  619. else if (part == Part::pan)
  620. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].pan = 0.0f; });
  621. }
  622. void MixerStrips::selectTrack (int track)
  623. {
  624. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), track);
  625. selection = { selected };
  626. repaint();
  627. if (onSelectionChanged)
  628. onSelectionChanged (selected);
  629. }
  630. namespace
  631. {
  632. // The 3 knobs of each band of the track's built-in EQ (gain, frequency, width).
  633. class TrackEqEditor final : public juce::Component
  634. {
  635. public:
  636. TrackEqEditor (AudioEngine& e, int t)
  637. : engine (e),
  638. track (t)
  639. {
  640. const juce::String bands[] = { tr ("Low"), tr ("Mid"), tr ("High") };
  641. const auto& eq = engine.getSong().mixer[(std::size_t) track].eq;
  642. for (int b = 0; b < 3; ++b)
  643. {
  644. titles[(std::size_t) b].setText (bands[b], juce::dontSendNotification);
  645. titles[(std::size_t) b].setJustificationType (juce::Justification::centred);
  646. addAndMakeVisible (titles[(std::size_t) b]);
  647. auto& gain = knobs[(std::size_t) b][0];
  648. setup (gain, -18.0, 18.0, 0.1, " dB", eq.gainDb[(std::size_t) b]);
  649. gain.setDoubleClickReturnValue (true, 0.0);
  650. auto& hz = knobs[(std::size_t) b][1];
  651. setup (hz, 10.0, 16000.0, 1.0, " Hz", eq.hz[(std::size_t) b]);
  652. hz.setSkewFactorFromMidPoint (1000.0);
  653. auto& width = knobs[(std::size_t) b][2];
  654. setup (width, 0.0, 1.0, 0.01, "", eq.width[(std::size_t) b]);
  655. width.setTooltip (b == 1 ? tr ("Peak width (Q)") : tr ("Shelf slope"));
  656. }
  657. setSize (3 * 90, 3 * 74 + 24);
  658. }
  659. void resized() override
  660. {
  661. auto area = getLocalBounds();
  662. auto header = area.removeFromTop (20);
  663. for (int b = 0; b < 3; ++b)
  664. {
  665. titles[(std::size_t) b].setBounds (header.removeFromLeft (90));
  666. auto column = area.removeFromLeft (90);
  667. for (auto& k : knobs[(std::size_t) b])
  668. k.setBounds (column.removeFromTop (74));
  669. }
  670. }
  671. private:
  672. void setup (juce::Slider& s, double lo, double hi, double step, const char* suffix, float value)
  673. {
  674. s.setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
  675. s.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 72, 16);
  676. s.setRange (lo, hi, step);
  677. s.setTextValueSuffix (suffix);
  678. s.setValue (value, juce::dontSendNotification);
  679. s.onValueChange = [this] { apply(); };
  680. addAndMakeVisible (s);
  681. }
  682. void apply()
  683. {
  684. TrackEq eq;
  685. for (std::size_t b = 0; b < 3; ++b)
  686. {
  687. eq.gainDb[b] = (float) knobs[b][0].getValue();
  688. eq.hz[b] = (float) knobs[b][1].getValue();
  689. eq.width[b] = (float) knobs[b][2].getValue();
  690. }
  691. engine.editSong ([t = track, eq] (Song& s) {
  692. if (t < (int) s.mixer.size())
  693. s.mixer[(std::size_t) t].eq = eq;
  694. });
  695. }
  696. AudioEngine& engine;
  697. int track;
  698. std::array<juce::Label, 3> titles;
  699. std::array<std::array<juce::Slider, 3>, 3> knobs; // [band][gain, frequency, width]
  700. };
  701. } // namespace
  702. void MixerStrips::showTrackEq (int t)
  703. {
  704. auto editor = std::make_unique<TrackEqEditor> (engine, t);
  705. juce::CallOutBox::launchAsynchronously (std::move (editor), getScreenBounds().withSize (10, 10).translated (getMouseXYRelative().x, getMouseXYRelative().y), nullptr);
  706. }
  707. void MixerStrips::showMenu (int t)
  708. {
  709. const auto& song = engine.getSong();
  710. const auto channel = engine.getSelectedChannel();
  711. juce::PopupMenu menu;
  712. menu.addSectionHeader (juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()));
  713. if (channel < (int) song.channels.size())
  714. menu.addItem (tr ("Route channel \"{0}\" here", { juce::String::fromUTF8 (song.channels[(std::size_t) channel].name.c_str()) }),
  715. [this, channel, t] { engine.editSong ([channel, t] (Song& s) { s.channels[(std::size_t) channel].mixerTrack = t; }); });
  716. menu.addItem (tr ("Link the selected channels here") + "\tCtrl+L", [this, t] {
  717. setSelection (selection, t);
  718. linkSelectedChannels (false);
  719. });
  720. menu.addItem (tr ("Link the selected channels starting here") + "\tCtrl+Shift+L", [this, t] {
  721. setSelection (selection, t);
  722. linkSelectedChannels (true);
  723. });
  724. menu.addItem (tr ("Select the channels linked here") + "\tAlt+L", ! mixerops::channelsOn (song, t).empty(), false, [this, t] {
  725. setSelection (selection, t);
  726. selectLinkedChannels();
  727. });
  728. {
  729. // route the other selected inserts into this one
  730. std::vector<int> sources;
  731. for (auto s : selection)
  732. if (s > 0 && s != t)
  733. sources.push_back (s);
  734. if (! sources.empty())
  735. {
  736. menu.addItem (tr ("Route the selected inserts here"), [this, sources, t] {
  737. engine.editSong ([sources, t] (Song& s) { mixerops::routeTo (s, sources, t, false); });
  738. });
  739. menu.addItem (tr ("Route the selected inserts only here"), [this, sources, t] {
  740. engine.editSong ([sources, t] (Song& s) { mixerops::routeTo (s, sources, t, true); });
  741. });
  742. }
  743. }
  744. if (t > 0)
  745. {
  746. menu.addItem (tr ("Move left") + "\tAlt+Left", t > 1, false, [this, t] {
  747. setSelection ({ t }, t);
  748. moveSelected (-1);
  749. });
  750. menu.addItem (tr ("Move right") + "\tAlt+Right", t + 1 < (int) song.mixer.size(), false, [this, t] {
  751. setSelection ({ t }, t);
  752. moveSelected (1);
  753. });
  754. }
  755. menu.addSeparator();
  756. if (selected > 0 && selected != t && (t == 0 || song.canRoute (selected, t) || song.routes (selected, t)))
  757. menu.addItem (tr ("Send \"{0}\" only to this track", { juce::String::fromUTF8 (song.mixer[(std::size_t) selected].name.c_str()) }),
  758. [this, from = selected, t] {
  759. engine.editSong ([from, t] (Song& s) {
  760. s.mixer[(std::size_t) from].sends.clear();
  761. s.mixer[(std::size_t) from].toMaster = false;
  762. s.setRoute (from, t, true);
  763. });
  764. });
  765. if (t > 0 && (! song.mixer[(std::size_t) t].sends.empty() || ! song.mixer[(std::size_t) t].toMaster))
  766. menu.addItem (tr ("Send only to the Master again"), [this, t] {
  767. engine.editSong ([t] (Song& s) {
  768. s.mixer[(std::size_t) t].sends.clear();
  769. s.mixer[(std::size_t) t].toMaster = true;
  770. });
  771. });
  772. {
  773. const auto& track = song.mixer[(std::size_t) t];
  774. juce::PopupMenu stereo;
  775. const std::pair<const char*, float> widths[] = { { TR_NOOP ("Mono"), 1.0f }, { TR_NOOP ("Narrower"), 0.5f }, { TR_NOOP ("Normal"), 0.0f },
  776. { TR_NOOP ("Wider"), -0.5f }, { TR_NOOP ("Very wide"), -1.0f } };
  777. for (const auto& [label, value] : widths)
  778. stereo.addItem (tr (label), true, std::abs (track.stereo - value) < 0.01f,
  779. [this, t, v = value] { engine.editSong ([t, v] (Song& s) { s.mixer[(std::size_t) t].stereo = v; }); });
  780. menu.addSubMenu (tr ("Stereo separation"), stereo);
  781. menu.addItem (tr ("Track EQ..."), [this, t] { showTrackEq (t); });
  782. menu.addItem (tr ("Invert phase"), true, track.invertPhase, [this, t] {
  783. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].invertPhase = ! s.mixer[(std::size_t) t].invertPhase; });
  784. });
  785. menu.addItem (tr ("Swap left/right"), true, track.swapChannels, [this, t] {
  786. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].swapChannels = ! s.mixer[(std::size_t) t].swapChannels; });
  787. });
  788. menu.addSeparator();
  789. }
  790. if (t > 0)
  791. menu.addItem (tr ("Render to audio (freeze)..."), [this, t] { bounceInsertToAudio (engine, t); });
  792. menu.addItem (tr ("Rename..."), [this, t] {
  793. auto* window = new juce::AlertWindow (tr ("Rename track"), {}, juce::MessageBoxIconType::NoIcon);
  794. window->addTextEditor ("name", juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) t].name.c_str()));
  795. window->addButton (tr ("OK"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  796. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  797. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, t, window] (int result) {
  798. const auto name = window->getTextEditorContents ("name").trim().toStdString();
  799. if (result == 1 && ! name.empty() && t < (int) engine.getSong().mixer.size())
  800. engine.editSong ([t, name] (Song& s) { s.mixer[(std::size_t) t].name = name; });
  801. }),
  802. true);
  803. });
  804. menu.addItem (tr ("Reset (0 dB, center, no mute/solo)"), [this, t] {
  805. engine.editSong ([t] (Song& s) {
  806. auto& track = s.mixer[(std::size_t) t];
  807. track.volume = unityFader;
  808. track.pan = 0.0f;
  809. track.muted = track.solo = false;
  810. });
  811. });
  812. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  813. }
  814. void MixerStrips::changeListenerCallback (juce::ChangeBroadcaster*)
  815. {
  816. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), selected);
  817. const auto count = (int) engine.getSong().mixer.size();
  818. selection.erase (std::remove_if (selection.begin(), selection.end(), [count] (int t) { return t >= count; }), selection.end());
  819. if (! std::binary_search (selection.begin(), selection.end(), selected))
  820. selection = { selected };
  821. updateSize();
  822. repaint();
  823. }
  824. void MixerStrips::timerCallback()
  825. {
  826. auto& meters = engine.getMeters();
  827. const auto showing = isShowing();
  828. for (std::size_t i = 0; i < levels.size(); ++i)
  829. {
  830. const auto peak = meters.takePeak ((int) (i / 2), (int) (i % 2));
  831. const auto next = juce::jmax (peak, levels[i] * 0.86f); // ~20 dB/s falloff at 30 Hz
  832. const auto shown = next < 0.001f ? 0.0f : next;
  833. // only the meter that changed is repainted
  834. if (showing && std::abs (shown - levels[i]) > 1e-4f)
  835. {
  836. const auto t = (int) (i / 2);
  837. const auto area = faderArea (t);
  838. repaint (area.getX(), area.getY(), 16, area.getHeight());
  839. }
  840. levels[i] = shown;
  841. }
  842. }
  843. // ---------------------------------------------------------------------------
  844. namespace
  845. {
  846. constexpr int panelHeader = 30;
  847. constexpr int slotHeight = 24;
  848. const EffectKind allEffects[] = { EffectKind::eq, EffectKind::parametricEq, EffectKind::compressor, EffectKind::limiter,
  849. EffectKind::delay, EffectKind::reverb, EffectKind::chorus, EffectKind::filter,
  850. EffectKind::distortion, EffectKind::utility, EffectKind::volumeGate,
  851. EffectKind::pitchShift, EffectKind::freqShift, EffectKind::lofi, EffectKind::effector, EffectKind::crossover, EffectKind::autoPan, EffectKind::vocoder, EffectKind::pitchCorrect,
  852. EffectKind::fruityLimiter, EffectKind::fruityCompressor,
  853. EffectKind::fruityWaveShaper, EffectKind::fruitySoftClipper,
  854. EffectKind::reeverb2, EffectKind::reeverb1, EffectKind::fruityChorus, EffectKind::hyperChorus, EffectKind::vintageChorus };
  855. } // namespace
  856. const std::vector<EffectKind>& addableEffects()
  857. {
  858. static const std::vector<EffectKind> list (std::begin (allEffects), std::end (allEffects));
  859. return list;
  860. }
  861. EffectsPanel::EffectsPanel (AudioEngine& e)
  862. : engine (e)
  863. {
  864. setWantsKeyboardFocus (false);
  865. setMouseClickGrabsKeyboardFocus (false);
  866. engine.addChangeListener (this);
  867. }
  868. EffectsPanel::~EffectsPanel()
  869. {
  870. engine.removeChangeListener (this);
  871. }
  872. void EffectsPanel::setTrack (int t)
  873. {
  874. track = t;
  875. const auto* s = slots();
  876. selectedSlot = s != nullptr && ! s->empty() ? 0 : -1;
  877. repaint();
  878. }
  879. const std::vector<EffectSlot>* EffectsPanel::slots() const
  880. {
  881. const auto& mixer = engine.getSong().mixer;
  882. return track >= 0 && track < (int) mixer.size() ? &mixer[(std::size_t) track].effects : nullptr;
  883. }
  884. juce::Rectangle<int> EffectsPanel::mixBounds (int slot) const
  885. {
  886. return slotBounds (slot).removeFromRight (26);
  887. }
  888. juce::Rectangle<int> EffectsPanel::slotBounds (int slot) const
  889. {
  890. return { 8, panelHeader + slot * slotHeight, getWidth() - 16, slotHeight - 3 };
  891. }
  892. namespace
  893. {
  894. // parameters shown as knobs: in the rhythmic envelope, only those of the points in use
  895. int shownParams (const EffectSlot& slot)
  896. {
  897. const auto all = (int) effectInfo (slot.kind).params.size();
  898. if (slot.kind == EffectKind::rhythmGain)
  899. return std::min (all, 2 + 3 * (int) std::lround (slot.params[1] * 12.0f));
  900. return all;
  901. }
  902. } // namespace
  903. juce::Rectangle<int> EffectsPanel::knobBounds (int param) const
  904. {
  905. auto top = panelHeader + maxEffectSlots * slotHeight + 40;
  906. // many parameters (Effector: 12): 4 columns and shorter rows to fit the height
  907. int count = 0;
  908. if (const auto* s = slots(); ! gateSelected() && s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size())
  909. count = shownParams ((*s)[(std::size_t) selectedSlot]);
  910. const auto columns = count > 9 ? 4 : 3;
  911. const auto w = (getWidth() - 16) / columns;
  912. const auto rows = juce::jmax (1, (count + columns - 1) / columns);
  913. const auto rowHeight = count > 9 ? juce::jlimit (64, 86, (getHeight() - top - 4) / rows) : 86;
  914. // volume gate: the 32 steps are bars (gateStepsBounds); the knobs are mix/attack/release
  915. if (gateSelected())
  916. {
  917. if (param < 32)
  918. return {};
  919. param -= 32;
  920. top = gateStepsBounds().getBottom() + 22;
  921. }
  922. return { 8 + (param % columns) * w, top + (param / columns) * rowHeight, w, rowHeight - 4 };
  923. }
  924. bool EffectsPanel::gateSelected() const
  925. {
  926. const auto* s = slots();
  927. return s != nullptr && selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::volumeGate;
  928. }
  929. juce::Rectangle<int> EffectsPanel::gateStepsBounds() const
  930. {
  931. return { 10, panelHeader + maxEffectSlots * slotHeight + 40, getWidth() - 20, 90 };
  932. }
  933. void EffectsPanel::setGateStep (juce::Point<int> p)
  934. {
  935. const auto area = gateStepsBounds();
  936. const auto step = juce::jlimit (0, 31, (int) ((p.x - area.getX()) * 32 / juce::jmax (1, area.getWidth())));
  937. const auto value = juce::jlimit (0.0f, 1.0f, (float) (area.getBottom() - p.y) / (float) area.getHeight());
  938. engine.editSong ([t = track, slot = selectedSlot, step, value] (Song& song) {
  939. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) step] = value;
  940. });
  941. }
  942. void EffectsPanel::showGatePatterns()
  943. {
  944. // ready-made patterns (like Gross Beat's slots): value of step i of 32 (1/8 beat each)
  945. struct Pattern
  946. {
  947. const char* name;
  948. float (*level) (int);
  949. };
  950. static const Pattern patterns[] = {
  951. { TR_NOOP ("All open"), [] (int) { return 1.0f; } },
  952. { TR_NOOP ("1/2 beat gate"), [] (int i) { return (i % 4) < 2 ? 1.0f : 0.0f; } },
  953. { TR_NOOP ("1 beat gate"), [] (int i) { return (i % 8) < 4 ? 1.0f : 0.0f; } },
  954. { TR_NOOP ("Offbeat"), [] (int i) { return (i % 8) >= 4 ? 1.0f : 0.0f; } },
  955. { TR_NOOP ("Sidechain (ramp per beat)"), [] (int i) { return std::min (1.0f, 0.15f + (float) (i % 8) / 5.0f); } },
  956. { TR_NOOP ("Trance gate"), [] (int i) { return (i % 4) == 3 ? 0.0f : (i % 16) >= 12 ? 0.4f : 1.0f; } },
  957. { TR_NOOP ("Fade-in over the bar"), [] (int i) { return (float) (i + 1) / 32.0f; } },
  958. { TR_NOOP ("Fade-out over the bar"), [] (int i) { return 1.0f - (float) i / 32.0f; } },
  959. };
  960. juce::PopupMenu menu;
  961. for (const auto& p : patterns)
  962. menu.addItem (tr (p.name), [this, level = p.level] {
  963. engine.editSong ([t = track, slot = selectedSlot, level] (Song& song) {
  964. auto& params = song.mixer[(std::size_t) t].effects[(std::size_t) slot].params;
  965. for (int i = 0; i < 32; ++i)
  966. params[(std::size_t) i] = level (i);
  967. });
  968. });
  969. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  970. }
  971. int EffectsPanel::knobAt (juce::Point<int> p) const
  972. {
  973. const auto* s = slots();
  974. if (s == nullptr || selectedSlot < 0 || selectedSlot >= (int) s->size())
  975. return -1;
  976. if ((*s)[(std::size_t) selectedSlot].kind == EffectKind::parametricEq)
  977. return -1; // edited in its own window
  978. const auto count = shownParams ((*s)[(std::size_t) selectedSlot]);
  979. for (int i = gateSelected() ? 32 : 0; i < count; ++i)
  980. if (knobBounds (i).contains (p))
  981. return i;
  982. return -1;
  983. }
  984. void EffectsPanel::paint (juce::Graphics& g)
  985. {
  986. using namespace theme;
  987. g.fillAll (colour::bg2);
  988. g.setColour (colour::border);
  989. g.drawVerticalLine (0, 0.0f, (float) getHeight());
  990. const auto* s = slots();
  991. if (s == nullptr)
  992. return;
  993. const auto& song = engine.getSong();
  994. g.setColour (colour::textPrimary);
  995. g.setFont (font (type::title, true));
  996. g.drawText (tr ("Effects: {0}", { juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str()) }), 10, 4,
  997. getWidth() - 70, panelHeader - 8, juce::Justification::centredLeft, true);
  998. // the insert's effects switch
  999. if (! s->empty())
  1000. {
  1001. const auto on = mixerops::anyEffectEnabled (song, track);
  1002. const auto r = fxSwitchBounds().toFloat();
  1003. g.setColour (on ? colour::bg5 : colour::bg0);
  1004. g.fillRoundedRectangle (r, radius::sm);
  1005. g.setColour (muteLed (! on));
  1006. g.fillEllipse (r.withWidth (r.getHeight()).reduced (6.0f).translated (3.0f, 0.0f));
  1007. g.setColour (on ? colour::textPrimary : colour::textDisabled);
  1008. g.setFont (font (type::caption, true));
  1009. g.drawText ("FX", r.withTrimmedLeft (r.getHeight()).toNearestInt(), juce::Justification::centredLeft);
  1010. }
  1011. g.setFont (font (type::body));
  1012. for (int i = 0; i < maxEffectSlots; ++i)
  1013. {
  1014. const auto r = slotBounds (i);
  1015. const auto filled = i < (int) s->size();
  1016. const auto isSelected = i == selectedSlot && filled;
  1017. g.setColour (isSelected ? colour::bg5 : filled ? colour::bg3 : colour::bg2.brighter (0.03f));
  1018. g.fillRoundedRectangle (r.toFloat(), radius::sm);
  1019. if (! filled)
  1020. {
  1021. g.setColour (colour::border);
  1022. g.drawRoundedRectangle (r.toFloat().reduced (0.5f), radius::sm, 1.0f);
  1023. }
  1024. if (isSelected)
  1025. {
  1026. g.setColour (colour::accent);
  1027. g.fillRoundedRectangle (r.toFloat().withWidth (3.0f).reduced (0.0f, 3.0f), 1.5f);
  1028. }
  1029. if (filled)
  1030. {
  1031. const auto& slot = (*s)[(std::size_t) i];
  1032. g.setColour (muteLed (! slot.enabled));
  1033. g.fillEllipse ((float) r.getX() + 8, (float) r.getCentreY() - 4, 8, 8);
  1034. g.setColour (slot.enabled ? colour::textPrimary : colour::textDisabled);
  1035. auto name = slot.kind == EffectKind::plugin ? juce::String::fromUTF8 (slot.plugin.name.c_str())
  1036. : juce::String::fromUTF8 (effectInfo (slot.kind).name);
  1037. if (slot.sidechain > 0 && slot.sidechain < (int) engine.getSong().mixer.size())
  1038. name << juce::String::fromUTF8 (" \xe2\x86\x90 ") << juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) slot.sidechain].name.c_str());
  1039. g.drawText (juce::String (i + 1) + ". " + name, r.withTrimmedLeft (24).withTrimmedRight (28), juce::Justification::centredLeft);
  1040. // slot's dry/wet knob
  1041. drawKnob (g, mixBounds (i).toFloat().withSizeKeepingCentre (20.0f, 20.0f), slot.mix, false,
  1042. slot.enabled ? colour::info : colour::textDisabled);
  1043. }
  1044. else if (i == (int) s->size())
  1045. {
  1046. g.setColour (colour::textDisabled);
  1047. g.drawText (juce::String (i + 1) + ". " + tr ("(empty: click to add)"), r.withTrimmedLeft (24), juce::Justification::centredLeft);
  1048. }
  1049. }
  1050. if (dragSlot >= 0 && dropSlot >= 0 && dropSlot != dragSlot) // where the dragged slot lands
  1051. {
  1052. const auto r = slotBounds (dropSlot);
  1053. const auto y = dropSlot > dragSlot ? r.getBottom() + 1 : r.getY() - 2;
  1054. g.setColour (colour::accent);
  1055. g.fillRect (r.getX(), y, r.getWidth(), 2);
  1056. }
  1057. if (selectedSlot < 0 || selectedSlot >= (int) s->size())
  1058. return;
  1059. const auto& slot = (*s)[(std::size_t) selectedSlot];
  1060. auto drawButton = [&g] (juce::Rectangle<int> button, const juce::String& text, Icon icon) {
  1061. g.setColour (colour::bg4);
  1062. g.fillRoundedRectangle (button.toFloat(), radius::md);
  1063. const auto f = juce::Font (font (type::body));
  1064. const auto textWidth = juce::GlyphArrangement::getStringWidth (f, text);
  1065. auto content = button.toFloat().withSizeKeepingCentre (juce::jmin ((float) button.getWidth() - 12.0f, textWidth + 22.0f), (float) button.getHeight());
  1066. icons::draw (g, icon, content.removeFromLeft (16.0f).withSizeKeepingCentre (16.0f, 16.0f), colour::textSecondary);
  1067. content.removeFromLeft (6.0f);
  1068. g.setColour (colour::textPrimary);
  1069. g.setFont (f);
  1070. g.drawFittedText (text, content.toNearestInt(), juce::Justification::centredLeft, 1, 0.85f);
  1071. };
  1072. if (slot.kind == EffectKind::plugin)
  1073. {
  1074. const auto button = pluginButtonBounds();
  1075. drawButton (button, tr ("Open plugin interface"), Icon::plugin);
  1076. g.setColour (colour::textSecondary);
  1077. g.setFont (font (type::caption));
  1078. g.drawText (juce::String::fromUTF8 (slot.plugin.name.c_str()), button.translated (0, 34), juce::Justification::centred);
  1079. return;
  1080. }
  1081. if (slot.kind == EffectKind::parametricEq)
  1082. {
  1083. const auto button = pluginButtonBounds();
  1084. drawButton (button, tr ("Open parametric EQ"), Icon::settings);
  1085. const auto curve = juce::Rectangle<int> (button.getX(), button.getBottom() + 10, button.getWidth(), 120).toFloat();
  1086. drawParametricEqCurve (g, curve, slot.params, juce::jmax (8000.0, engine.getTransport().sampleRate.load()), slot.enabled);
  1087. return;
  1088. }
  1089. if (slot.kind == EffectKind::parallel) // container: list the branches
  1090. {
  1091. auto y = knobBounds (0).getY() - 28;
  1092. g.setColour (colour::textPrimary);
  1093. g.setFont (font (type::title, true));
  1094. g.drawText (juce::String::fromUTF8 (effectInfo (slot.kind).name), 10, y, getWidth() - 20, 20, juce::Justification::centredLeft);
  1095. g.setFont (font (type::small));
  1096. for (std::size_t b = 0; b < slot.branches.size(); ++b)
  1097. {
  1098. y += 22;
  1099. juce::String chain;
  1100. if (slot.branches[b].chain.empty())
  1101. chain = tr ("dry");
  1102. for (std::size_t c = 0; c < slot.branches[b].chain.size(); ++c)
  1103. chain += (c > 0 ? " -> " : "") + juce::String::fromUTF8 (effectInfo (slot.branches[b].chain[c].kind).name);
  1104. const auto line = tr ("Branch {0} (x{1}): {2}", { juce::String ((int) b + 1), juce::String (slot.branches[b].gain, 2), chain });
  1105. g.setColour (colour::textSecondary);
  1106. g.drawText (line, 14, y, getWidth() - 28, 20, juce::Justification::centredLeft, true);
  1107. }
  1108. return;
  1109. }
  1110. const auto& info = effectInfo (slot.kind);
  1111. const auto gate = slot.kind == EffectKind::volumeGate;
  1112. g.setColour (colour::textPrimary);
  1113. g.setFont (font (type::title, true));
  1114. g.drawText (juce::String::fromUTF8 (info.name), 10, (gate ? gateStepsBounds().getY() : knobBounds (0).getY()) - 28, getWidth() - 20, 20,
  1115. juce::Justification::centredLeft);
  1116. if (gate) // 32 steps = 1 bar of 4 beats
  1117. {
  1118. const auto area = gateStepsBounds().toFloat();
  1119. g.setColour (colour::bg0);
  1120. g.fillRoundedRectangle (area, radius::sm);
  1121. const auto w = area.getWidth() / 32.0f;
  1122. for (int i = 0; i < 32; ++i)
  1123. {
  1124. const auto h = area.getHeight() * juce::jlimit (0.0f, 1.0f, slot.params[(std::size_t) i]);
  1125. g.setColour ((slot.enabled ? colour::accent : colour::textDisabled).withAlpha ((i / 8) % 2 == 0 ? 1.0f : 0.7f));
  1126. g.fillRect (area.getX() + i * w + 1.0f, area.getBottom() - h, w - 2.0f, h);
  1127. }
  1128. g.setColour (colour::gridBar);
  1129. for (int beat = 1; beat < 4; ++beat)
  1130. g.drawVerticalLine ((int) (area.getX() + beat * 8 * w), area.getY(), area.getBottom());
  1131. g.setColour (colour::textSecondary);
  1132. g.setFont (font (type::caption));
  1133. g.drawFittedText (tr ("1 bar: draw the steps; right-click = patterns"), area.withY (area.getBottom() + 2).withHeight (14).toNearestInt(),
  1134. juce::Justification::centredLeft, 1, 0.8f);
  1135. }
  1136. for (int p = gate ? 32 : 0; p < shownParams (slot); ++p)
  1137. {
  1138. const auto r = knobBounds (p);
  1139. const auto value = slot.params[(std::size_t) p];
  1140. // 38 px knob; in tight grids (Effector) it shrinks to fit name and value below
  1141. const auto knobHeight = juce::jlimit (30, 44, r.getHeight() - 30);
  1142. drawKnob (g, r.withHeight (knobHeight).toFloat(), value, false, slot.enabled ? colour::knobArc : colour::textDisabled, dragParam == p);
  1143. g.setColour (colour::textSecondary);
  1144. g.setFont (font (type::caption));
  1145. g.drawFittedText (juce::String::fromUTF8 (info.params[(std::size_t) p].name), r.getX(), r.getY() + knobHeight + 2, r.getWidth(), 14,
  1146. juce::Justification::centred, 1, 0.8f);
  1147. g.setColour (colour::textPrimary);
  1148. g.drawText (juce::String::fromUTF8 (formatEffectParam (slot.kind, p, value, song.bpm).c_str()), r.getX(), r.getY() + knobHeight + 16,
  1149. r.getWidth(), 14, juce::Justification::centred);
  1150. }
  1151. }
  1152. void EffectsPanel::mouseDown (const juce::MouseEvent& e)
  1153. {
  1154. const auto* s = slots();
  1155. if (s == nullptr)
  1156. return;
  1157. if (! s->empty() && fxSwitchBounds().contains (e.getPosition()))
  1158. {
  1159. if (e.mods.isPopupMenu())
  1160. toggleAllWindows();
  1161. else
  1162. {
  1163. const auto on = ! mixerops::anyEffectEnabled (engine.getSong(), track);
  1164. engine.editSong ([t = track, on] (Song& song) { mixerops::setAllEffectsEnabled (song, t, on); });
  1165. }
  1166. return;
  1167. }
  1168. for (int i = 0; i < maxEffectSlots; ++i)
  1169. {
  1170. const auto r = slotBounds (i);
  1171. if (! r.contains (e.getPosition()))
  1172. continue;
  1173. if (i >= (int) s->size())
  1174. {
  1175. showAddMenu (i);
  1176. return;
  1177. }
  1178. selectedSlot = i;
  1179. repaint();
  1180. if (mixBounds (i).contains (e.getPosition()))
  1181. {
  1182. if (e.mods.isPopupMenu())
  1183. ui::showValueMenu (*this, &engine, specFor (mixParam, i), valueOf (mixParam, i), [this, i] (double v) { setValue (mixParam, i, v); });
  1184. else if (ui::isResetClick (e))
  1185. setValue (mixParam, i, 1.0);
  1186. else
  1187. {
  1188. dragParam = mixParam;
  1189. dragStart = (*s)[(std::size_t) i].mix;
  1190. drag.begin (e, dragStart);
  1191. }
  1192. return;
  1193. }
  1194. if (e.mods.isPopupMenu())
  1195. showSlotMenu (i);
  1196. else if (e.x < r.getX() + 22) // LED: bypass
  1197. engine.editSong ([t = track, i] (Song& song) {
  1198. auto& slot = song.mixer[(std::size_t) t].effects[(std::size_t) i];
  1199. slot.enabled = ! slot.enabled;
  1200. });
  1201. else
  1202. dragSlot = i; // dragging the name reorders the slot
  1203. return;
  1204. }
  1205. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::plugin
  1206. && pluginButtonBounds().contains (e.getPosition()))
  1207. {
  1208. openPluginWindow (selectedSlot);
  1209. return;
  1210. }
  1211. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::parametricEq
  1212. && pluginButtonBounds().withHeight (170).contains (e.getPosition()))
  1213. {
  1214. ParametricEqWindows::open (engine, track, selectedSlot);
  1215. return;
  1216. }
  1217. if (gateSelected() && gateStepsBounds().contains (e.getPosition()))
  1218. {
  1219. if (e.mods.isPopupMenu())
  1220. showGatePatterns();
  1221. else
  1222. {
  1223. dragParam = gateStepParam;
  1224. setGateStep (e.getPosition());
  1225. }
  1226. return;
  1227. }
  1228. dragParam = knobAt (e.getPosition());
  1229. if (dragParam >= 0 && e.mods.isPopupMenu())
  1230. {
  1231. const auto p = dragParam;
  1232. ui::showValueMenu (*this, &engine, specFor (p, selectedSlot), valueOf (p, selectedSlot),
  1233. [this, p, slot = selectedSlot] (double v) { setValue (p, slot, v); });
  1234. dragParam = -1;
  1235. return;
  1236. }
  1237. if (dragParam >= 0 && ui::isResetClick (e))
  1238. {
  1239. setValue (dragParam, selectedSlot, specFor (dragParam, selectedSlot).defaultValue);
  1240. dragParam = -1;
  1241. return;
  1242. }
  1243. if (dragParam >= 0)
  1244. {
  1245. dragStart = (*s)[(std::size_t) selectedSlot].params[(std::size_t) dragParam];
  1246. drag.begin (e, dragStart);
  1247. }
  1248. }
  1249. ui::ValueSpec EffectsPanel::specFor (int param, int slot) const
  1250. {
  1251. const auto& song = engine.getSong();
  1252. ui::ValueSpec spec;
  1253. spec.pixelsForRange = 150.0;
  1254. if (const auto* s = slots(); s == nullptr || slot < 0 || slot >= (int) s->size())
  1255. return spec;
  1256. const auto& fx = (*slots())[(std::size_t) slot];
  1257. const auto& info = effectInfo (fx.kind);
  1258. const auto trackName = juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str());
  1259. if (param == mixParam)
  1260. {
  1261. spec = ui::percentSpec (trackName + " - " + utf8 (info.name) + " - " + tr ("Mix"), 1.0);
  1262. spec.target = AutomationTarget { AutomationTargetKind::effectMix, track, slot };
  1263. return spec;
  1264. }
  1265. if (param < 0 || param >= (int) info.params.size())
  1266. return spec;
  1267. spec.name = trackName + " - " + utf8 (info.name) + " - " + utf8 (info.params[(std::size_t) param].name);
  1268. spec.defaultValue = info.params[(std::size_t) param].defaultValue;
  1269. spec.toText = [kind = fx.kind, param, bpm = song.bpm] (double v) { return utf8 (formatEffectParam (kind, param, (float) v, bpm).c_str()); };
  1270. spec.fromText = ui::inverseOf (spec.toText, 0.0, 1.0);
  1271. spec.target = AutomationTarget { AutomationTargetKind::effectParam, track, slot, param };
  1272. return spec;
  1273. }
  1274. double EffectsPanel::valueOf (int param, int slot) const
  1275. {
  1276. const auto* s = slots();
  1277. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  1278. return 0.0;
  1279. const auto& fx = (*s)[(std::size_t) slot];
  1280. return param == mixParam ? fx.mix : (param >= 0 && param < (int) fx.params.size() ? fx.params[(std::size_t) param] : 0.0f);
  1281. }
  1282. void EffectsPanel::setValue (int param, int slot, double value)
  1283. {
  1284. const auto* s = slots();
  1285. if (s == nullptr || slot < 0 || slot >= (int) s->size())
  1286. return;
  1287. if (param == mixParam)
  1288. engine.editSong ([t = track, slot, v = (float) value] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) slot].mix = v; });
  1289. else if (param >= 0)
  1290. engine.editSong ([t = track, slot, param, v = (float) value] (Song& song) {
  1291. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) param] = v;
  1292. });
  1293. }
  1294. int EffectsPanel::mixKnobAt (juce::Point<int> p) const
  1295. {
  1296. for (int i = 0; slots() != nullptr && i < (int) slots()->size(); ++i)
  1297. if (mixBounds (i).contains (p))
  1298. return i;
  1299. return -1;
  1300. }
  1301. void EffectsPanel::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  1302. {
  1303. if (const auto slot = mixKnobAt (e.getPosition()); slot >= 0)
  1304. {
  1305. if (const auto v = ui::wheelValue (valueOf (mixParam, slot), e, wheel, specFor (mixParam, slot)))
  1306. setValue (mixParam, slot, *v);
  1307. return;
  1308. }
  1309. if (const auto p = knobAt (e.getPosition()); p >= 0)
  1310. {
  1311. if (const auto v = ui::wheelValue (valueOf (p, selectedSlot), e, wheel, specFor (p, selectedSlot)))
  1312. setValue (p, selectedSlot, *v);
  1313. return;
  1314. }
  1315. // the wheel over a slot's name moves it up or down
  1316. if (const auto slot = slotAt (e.getPosition()); slot >= 0)
  1317. {
  1318. const auto to = slot + ((wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1);
  1319. bool moved = false;
  1320. engine.editSong ([t = track, slot, to, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, slot, to); });
  1321. if (moved)
  1322. selectedSlot = to;
  1323. repaint();
  1324. return;
  1325. }
  1326. juce::Component::mouseWheelMove (e, wheel);
  1327. }
  1328. ui::Hint EffectsPanel::hintAt (juce::Point<int> p)
  1329. {
  1330. const auto gesture = tr ("drag (Ctrl: fine), wheel, Alt+click resets");
  1331. if (dragParam == mixParam || (dragParam >= 0 && drag.isActive()))
  1332. return ui::valueHint (specFor (dragParam, dragParam == mixParam ? juce::jmax (0, selectedSlot) : selectedSlot),
  1333. valueOf (dragParam, dragParam == mixParam ? juce::jmax (0, selectedSlot) : selectedSlot), gesture);
  1334. if (const auto slot = mixKnobAt (p); slot >= 0)
  1335. return ui::valueHint (specFor (mixParam, slot), valueOf (mixParam, slot), gesture);
  1336. if (const auto param = knobAt (p); param >= 0)
  1337. return ui::valueHint (specFor (param, selectedSlot), valueOf (param, selectedSlot), gesture);
  1338. ui::Hint h;
  1339. if (fxSwitchBounds().contains (p) && slots() != nullptr && ! slots()->empty())
  1340. {
  1341. h.text = tr ("All effects of this insert on/off; right-click opens or closes their plugin windows");
  1342. h.hasMenu = true;
  1343. return h;
  1344. }
  1345. for (int i = 0; i < maxEffectSlots; ++i)
  1346. if (slotBounds (i).contains (p))
  1347. {
  1348. const auto used = slots() != nullptr && i < (int) slots()->size();
  1349. h.text = used ? tr ("Effect slot: click edits, the LED bypasses, drag or wheel reorders, right-click: replace, move, delete")
  1350. : tr ("Empty slot: click to add an effect");
  1351. h.hasMenu = used;
  1352. }
  1353. return h;
  1354. }
  1355. void EffectsPanel::mouseDrag (const juce::MouseEvent& e)
  1356. {
  1357. if (dragSlot >= 0)
  1358. {
  1359. if (e.getDistanceFromDragStart() > 4)
  1360. {
  1361. const auto count = slots() != nullptr ? (int) slots()->size() : 0;
  1362. const auto target = juce::jlimit (0, juce::jmax (0, count - 1), (e.y - panelHeader) / slotHeight);
  1363. if (target != dropSlot)
  1364. {
  1365. dropSlot = target;
  1366. setMouseCursor (juce::MouseCursor::DraggingHandCursor);
  1367. repaint();
  1368. }
  1369. }
  1370. return;
  1371. }
  1372. if (dragParam == gateStepParam)
  1373. {
  1374. setGateStep (e.getPosition());
  1375. return;
  1376. }
  1377. if (dragParam == mixParam)
  1378. {
  1379. setValue (mixParam, selectedSlot, drag.update (e, specFor (mixParam, selectedSlot)));
  1380. return;
  1381. }
  1382. if (dragParam < 0)
  1383. return;
  1384. setValue (dragParam, selectedSlot, drag.update (e, specFor (dragParam, selectedSlot)));
  1385. }
  1386. void EffectsPanel::mouseUp (const juce::MouseEvent&)
  1387. {
  1388. if (dragSlot >= 0 && dropSlot >= 0 && dropSlot != dragSlot)
  1389. {
  1390. bool moved = false;
  1391. engine.editSong ([t = track, from = dragSlot, to = dropSlot, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, from, to); });
  1392. if (moved)
  1393. selectedSlot = dropSlot;
  1394. }
  1395. if (dragSlot >= 0)
  1396. setMouseCursor (juce::MouseCursor::NormalCursor);
  1397. dragSlot = dropSlot = -1;
  1398. repaint();
  1399. dragParam = -1;
  1400. drag.end();
  1401. }
  1402. void EffectsPanel::mouseDoubleClick (const juce::MouseEvent& e)
  1403. {
  1404. for (int i = 0; slots() != nullptr && i < (int) slots()->size(); ++i)
  1405. if (mixBounds (i).contains (e.getPosition()))
  1406. {
  1407. engine.editSong ([t = track, i] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) i].mix = 1.0f; });
  1408. return;
  1409. }
  1410. const auto p = knobAt (e.getPosition());
  1411. if (p < 0)
  1412. return;
  1413. const auto kind = (*slots())[(std::size_t) selectedSlot].kind;
  1414. const auto value = effectInfo (kind).params[(std::size_t) p].defaultValue;
  1415. engine.editSong ([t = track, slot = selectedSlot, p, value] (Song& song) {
  1416. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) p] = value;
  1417. });
  1418. }
  1419. bool EffectsPanel::isInterestedInDragSource (const SourceDetails& details)
  1420. {
  1421. return BrowserComponent::isBrowserDrag (details) && BrowserComponent::draggedFile (details).hasFileExtension ("fst");
  1422. }
  1423. void EffectsPanel::itemDropped (const SourceDetails& details)
  1424. {
  1425. dropPreset (BrowserComponent::draggedFile (details), details.localPosition);
  1426. }
  1427. bool EffectsPanel::isInterestedInFileDrag (const juce::StringArray& files)
  1428. {
  1429. return files.size() == 1 && juce::File (files[0]).hasFileExtension ("fst");
  1430. }
  1431. void EffectsPanel::filesDropped (const juce::StringArray& files, int x, int y)
  1432. {
  1433. if (! files.isEmpty())
  1434. dropPreset (juce::File (files[0]), { x, y });
  1435. }
  1436. bool EffectsPanel::dropPreset (const juce::File& file, juce::Point<int> position)
  1437. {
  1438. juce::MemoryBlock data;
  1439. if (slots() == nullptr || ! file.loadFileAsData (data))
  1440. return false;
  1441. const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
  1442. const auto fx = effectFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), engine.getSong().bpm);
  1443. if (! fx)
  1444. {
  1445. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::InfoIcon, tr ("Preset"),
  1446. tr ("LYNX can't build an effect from this preset."));
  1447. return false;
  1448. }
  1449. auto target = -1;
  1450. for (int i = 0; i < (int) slots()->size(); ++i)
  1451. if (slotBounds (i).contains (position))
  1452. target = i;
  1453. if (target < 0 && (int) slots()->size() >= maxEffectSlots)
  1454. return false;
  1455. engine.editSong ([t = track, target, fx = *fx] (Song& song) {
  1456. auto& effects = song.mixer[(std::size_t) t].effects;
  1457. if (target >= 0 && target < (int) effects.size())
  1458. effects[(std::size_t) target] = fx;
  1459. else
  1460. effects.push_back (fx);
  1461. });
  1462. selectedSlot = target >= 0 ? target : (int) slots()->size() - 1;
  1463. repaint();
  1464. return true;
  1465. }
  1466. juce::Rectangle<int> EffectsPanel::fxSwitchBounds() const
  1467. {
  1468. return { getWidth() - 58, 6, 48, panelHeader - 12 };
  1469. }
  1470. int EffectsPanel::slotAt (juce::Point<int> p) const
  1471. {
  1472. const auto* s = slots();
  1473. for (int i = 0; s != nullptr && i < (int) s->size(); ++i)
  1474. if (slotBounds (i).contains (p) && ! mixBounds (i).contains (p) && p.x >= slotBounds (i).getX() + 22)
  1475. return i;
  1476. return -1;
  1477. }
  1478. void EffectsPanel::toggleAllWindows()
  1479. {
  1480. // opens every plugin effect window of the insert, or closes them all when one is open
  1481. const auto* s = slots();
  1482. if (s == nullptr)
  1483. return;
  1484. bool anyOpen = false;
  1485. for (int i = 0; i < (int) s->size(); ++i)
  1486. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, i).get()))
  1487. anyOpen = anyOpen || PluginWindows::isOpen (p->getInstance());
  1488. for (int i = 0; i < (int) s->size(); ++i)
  1489. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, i).get()))
  1490. {
  1491. if (anyOpen)
  1492. PluginWindows::close (p->getInstance());
  1493. else
  1494. openPluginWindow (i);
  1495. }
  1496. }
  1497. juce::Rectangle<int> EffectsPanel::pluginButtonBounds() const
  1498. {
  1499. return { 20, panelHeader + maxEffectSlots * slotHeight + 30, getWidth() - 40, 30 };
  1500. }
  1501. void EffectsPanel::openPluginWindow (int slot)
  1502. {
  1503. auto effect = engine.getEffect (track, slot);
  1504. if (auto* plugin = dynamic_cast<PluginEffect*> (effect.get()))
  1505. {
  1506. const auto& s = engine.getSong().mixer[(std::size_t) track].effects[(std::size_t) slot];
  1507. PluginWindows::open (effect, plugin->getInstance(), juce::String::fromUTF8 (s.plugin.name.c_str()));
  1508. return;
  1509. }
  1510. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Plugin"),
  1511. tr ("This plugin could not be loaded (it is not installed or has not been scanned)."));
  1512. }
  1513. void EffectsPanel::showAddMenu (int)
  1514. {
  1515. juce::PopupMenu menu;
  1516. for (auto kind : allEffects)
  1517. menu.addItem (juce::String::fromUTF8 (effectInfo (kind).name), [this, kind] {
  1518. engine.editSong ([t = track, kind] (Song& song) {
  1519. auto& effects = song.mixer[(std::size_t) t].effects;
  1520. if ((int) effects.size() < maxEffectSlots)
  1521. effects.push_back (makeEffectSlot (kind));
  1522. });
  1523. selectedSlot = (int) slots()->size() - 1;
  1524. repaint();
  1525. });
  1526. juce::PopupMenu plugins;
  1527. addPluginItems (plugins, engine, false, [this] (PluginRef ref) {
  1528. engine.editSong ([t = track, ref] (Song& song) {
  1529. auto& effects = song.mixer[(std::size_t) t].effects;
  1530. if ((int) effects.size() >= maxEffectSlots)
  1531. return;
  1532. EffectSlot slot;
  1533. slot.kind = EffectKind::plugin;
  1534. slot.plugin = ref;
  1535. effects.push_back (slot);
  1536. });
  1537. selectedSlot = (int) slots()->size() - 1;
  1538. openPluginWindow (selectedSlot);
  1539. repaint();
  1540. });
  1541. menu.addSeparator();
  1542. menu.addSubMenu (tr ("Plugin (VST3/CLAP)"), plugins);
  1543. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  1544. }
  1545. void EffectsPanel::showSlotMenu (int slot)
  1546. {
  1547. juce::PopupMenu replace;
  1548. for (auto kind : allEffects)
  1549. replace.addItem (juce::String::fromUTF8 (effectInfo (kind).name), [this, slot, kind] {
  1550. engine.editSong ([t = track, slot, kind] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) slot] = makeEffectSlot (kind); });
  1551. });
  1552. const auto count = (int) slots()->size();
  1553. juce::PopupMenu menu;
  1554. menu.addSubMenu (tr ("Replace with"), replace);
  1555. // FL presets (Plugin presets\Effects\<plugin>) that map to this effect
  1556. {
  1557. juce::PopupMenu presets;
  1558. const auto roots = findFlStudioRoots();
  1559. const auto base = juce::File (juce::String::fromUTF8 (roots.data.c_str())).getChildFile ("Patches/Plugin presets/Effects");
  1560. for (const auto& plugin : flPluginsForEffect ((*slots())[(std::size_t) slot].kind))
  1561. {
  1562. juce::PopupMenu sub;
  1563. auto files = base.getChildFile (juce::String::fromUTF8 (plugin.c_str())).findChildFiles (juce::File::findFiles, true, "*.fst");
  1564. std::sort (files.begin(), files.end(), [] (const juce::File& a, const juce::File& b) { return a.getFileName().compareNatural (b.getFileName()) < 0; });
  1565. for (const auto& f : files)
  1566. sub.addItem (f.getFileNameWithoutExtension(), [this, slot, f] {
  1567. juce::MemoryBlock data;
  1568. if (! f.loadFileAsData (data))
  1569. return;
  1570. const auto* bytes = static_cast<const std::uint8_t*> (data.getData());
  1571. if (auto fx = effectFromFlPreset (std::vector<std::uint8_t> (bytes, bytes + data.getSize()), engine.getSong().bpm))
  1572. engine.editSong ([t = track, slot, fx = *fx] (Song& song) {
  1573. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  1574. const auto enabled = target.enabled;
  1575. const auto mix = target.mix;
  1576. target = fx;
  1577. target.enabled = enabled;
  1578. target.mix = mix;
  1579. });
  1580. });
  1581. if (sub.getNumItems() > 0)
  1582. presets.addSubMenu (juce::String::fromUTF8 (plugin.c_str()), sub);
  1583. }
  1584. if (presets.getNumItems() > 0)
  1585. menu.addSubMenu (tr ("FL presets"), presets);
  1586. }
  1587. // side inputs: another track's output feeds the compressor's detector, or the vocoder's modulator/carrier
  1588. const auto kind = (*slots())[(std::size_t) slot].kind;
  1589. auto sideMenu = [&] (const juce::String& title, bool second) {
  1590. juce::PopupMenu side;
  1591. const auto& fx = (*slots())[(std::size_t) slot];
  1592. const auto current = second ? fx.sidechain2 : fx.sidechain;
  1593. auto assign = [this, slot, second] (int source) {
  1594. engine.editSong ([tk = track, slot, second, source] (Song& s) {
  1595. auto& target = s.mixer[(std::size_t) tk].effects[(std::size_t) slot];
  1596. (second ? target.sidechain2 : target.sidechain) = source;
  1597. if (target.kind == EffectKind::vocoder && source >= 0)
  1598. target.params[16] = 1.0f; // separate inputs (otherwise L = modulator, R = carrier)
  1599. });
  1600. };
  1601. side.addItem (tr ("None"), true, current < 0, [assign] { assign (-1); });
  1602. const auto& song = engine.getSong();
  1603. for (int t = 1; t < (int) song.mixer.size(); ++t)
  1604. if (t != track)
  1605. side.addItem (juce::String (t) + " " + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()), true, current == t,
  1606. [assign, t] { assign (t); });
  1607. menu.addSubMenu (title, side);
  1608. };
  1609. if (kind == EffectKind::compressor || kind == EffectKind::fruityLimiter || kind == EffectKind::fruityCompressor)
  1610. sideMenu (tr ("Sidechain from"), false);
  1611. if (kind == EffectKind::vocoder || kind == EffectKind::vocodex)
  1612. {
  1613. sideMenu (tr ("Modulator from"), false);
  1614. sideMenu (tr ("Carrier from"), true);
  1615. }
  1616. {
  1617. // A/B compare: keep a copy of the settings, then swap between it and the current ones
  1618. const auto key = std::make_pair (track, slot);
  1619. const auto hasSpare = spare.count (key) > 0 && spare[key].kind == (*slots())[(std::size_t) slot].kind;
  1620. juce::PopupMenu ab;
  1621. ab.addItem (tr ("Store the current settings as B"), [this, key, slot] {
  1622. spare[key] = (*slots())[(std::size_t) slot];
  1623. });
  1624. ab.addItem (tr ("Swap with B"), hasSpare, false, [this, key, slot] {
  1625. const auto stored = spare[key];
  1626. spare[key] = (*slots())[(std::size_t) slot];
  1627. engine.editSong ([t = track, slot, stored] (Song& song) {
  1628. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) slot];
  1629. const auto enabled = target.enabled;
  1630. target = stored;
  1631. target.enabled = enabled;
  1632. });
  1633. });
  1634. ab.addItem (tr ("Forget B"), hasSpare, false, [this, key] { spare.erase (key); });
  1635. menu.addSubMenu (hasSpare ? tr ("A/B compare (B stored)") : tr ("A/B compare"), ab);
  1636. }
  1637. menu.addItem (tr ("Move up"), slot > 0, false, [this, slot] {
  1638. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot - 1); });
  1639. selectedSlot = slot - 1;
  1640. });
  1641. menu.addItem (tr ("Move down"), slot < count - 1, false, [this, slot] {
  1642. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot + 1); });
  1643. selectedSlot = slot + 1;
  1644. });
  1645. if ((*slots())[(std::size_t) slot].kind == EffectKind::plugin)
  1646. {
  1647. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, slot).get()))
  1648. addPluginParameterItems (menu, engine, p->getInstance(), { AutomationTargetKind::effectParam, track, slot, 0 });
  1649. }
  1650. menu.addSeparator();
  1651. menu.addItem (tr ("Delete"), [this, slot] {
  1652. engine.editSong ([t = track, slot] (Song& song) { song.removeEffectSlot (t, slot); });
  1653. selectedSlot = juce::jmin (slot, (int) slots()->size() - 1);
  1654. repaint();
  1655. });
  1656. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  1657. }
  1658. void EffectsPanel::changeListenerCallback (juce::ChangeBroadcaster*)
  1659. {
  1660. const auto* s = slots();
  1661. if (s == nullptr)
  1662. track = 0;
  1663. else if (selectedSlot >= (int) s->size())
  1664. selectedSlot = (int) s->size() - 1;
  1665. repaint();
  1666. }
  1667. // ---------------------------------------------------------------------------
  1668. MixerComponent::MixerComponent (AudioEngine& e)
  1669. : engine (e),
  1670. strips (e),
  1671. effects (e)
  1672. {
  1673. viewport.setViewedComponent (&strips, false);
  1674. viewport.setScrollBarsShown (false, true);
  1675. addAndMakeVisible (viewport);
  1676. addAndMakeVisible (effects);
  1677. strips.onSelectionChanged = [this] (int track) { effects.setTrack (track); };
  1678. }
  1679. bool MixerComponent::runAction (const std::string& id)
  1680. {
  1681. if (id == "mixer.moveLeft")
  1682. strips.moveSelected (-1);
  1683. else if (id == "mixer.moveRight")
  1684. strips.moveSelected (1);
  1685. else if (id == "mixer.linkChannels")
  1686. strips.linkSelectedChannels (false);
  1687. else if (id == "mixer.linkChannelsFrom")
  1688. strips.linkSelectedChannels (true);
  1689. else if (id == "mixer.selectLinkedChannels")
  1690. strips.selectLinkedChannels();
  1691. else
  1692. return false;
  1693. return true;
  1694. }
  1695. bool MixerComponent::addEffectToSelected (EffectSlot slot)
  1696. {
  1697. const auto t = strips.getSelectedTrack();
  1698. bool added = false;
  1699. engine.editSong ([t, &slot, &added] (Song& s) {
  1700. if (s.mixer.empty())
  1701. s.mixer = Song::defaultMixer (0);
  1702. if (t < (int) s.mixer.size() && (int) s.mixer[(std::size_t) t].effects.size() < maxEffectSlots)
  1703. {
  1704. s.mixer[(std::size_t) t].effects.push_back (std::move (slot));
  1705. added = true;
  1706. }
  1707. });
  1708. if (added)
  1709. {
  1710. effects.setTrack (t);
  1711. effects.selectSlot ((int) engine.getSong().mixer[(std::size_t) t].effects.size() - 1);
  1712. }
  1713. return added;
  1714. }
  1715. void MixerComponent::selectFirstTrackWithEffects()
  1716. {
  1717. const auto& mixer = engine.getSong().mixer;
  1718. for (int t = 0; t < (int) mixer.size(); ++t)
  1719. if (! mixer[(std::size_t) t].effects.empty())
  1720. {
  1721. strips.selectTrack (t);
  1722. return;
  1723. }
  1724. }
  1725. void MixerComponent::resized()
  1726. {
  1727. auto area = getLocalBounds();
  1728. effects.setBounds (area.removeFromRight (effectsWidth));
  1729. viewport.setBounds (area);
  1730. strips.setVisibleHeight (area.getHeight() - viewport.getScrollBarThickness());
  1731. }
  1732. } // namespace lynx