MixerComponent.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #include "ui/MixerComponent.h"
  2. #include "ui/AutomationUi.h"
  3. #include "core/Effects.h"
  4. #include "engine/Text.h"
  5. #include "ui/PluginWindow.h"
  6. #include "ui/Theme.h"
  7. namespace lynx
  8. {
  9. namespace
  10. {
  11. constexpr int headerHeight = 34;
  12. constexpr int panHeight = 34;
  13. constexpr int buttonsHeight = 22;
  14. constexpr int feedsHeight = 46;
  15. // Fader 0..1 -> dB, com 0,8 = 0 dB (como no FL).
  16. juce::String faderToDb (float volume)
  17. {
  18. if (volume <= 0.0001f)
  19. return "-inf";
  20. const auto db = juce::Decibels::gainToDecibels (volume / unityFader);
  21. return (db > 0 ? "+" : "") + juce::String (db, 1);
  22. }
  23. // Nível 0..1 do medidor em escala de dB (-60..+6).
  24. float meterPosition (float gain)
  25. {
  26. const auto db = juce::Decibels::gainToDecibels (gain, -60.0f);
  27. return juce::jlimit (0.0f, 1.0f, (db + 60.0f) / 66.0f);
  28. }
  29. } // namespace
  30. MixerStrips::MixerStrips (AudioEngine& e)
  31. : engine (e)
  32. {
  33. setWantsKeyboardFocus (false);
  34. setMouseClickGrabsKeyboardFocus (false);
  35. engine.addChangeListener (this);
  36. updateSize();
  37. startTimerHz (30);
  38. }
  39. MixerStrips::~MixerStrips()
  40. {
  41. engine.removeChangeListener (this);
  42. }
  43. void MixerStrips::setVisibleHeight (int height)
  44. {
  45. visibleHeight = juce::jmax (260, height);
  46. updateSize();
  47. }
  48. void MixerStrips::updateSize()
  49. {
  50. const auto tracks = (int) engine.getSong().mixer.size();
  51. setSize (stripX (tracks) + stripWidth + 8, visibleHeight);
  52. levels.resize ((std::size_t) juce::jmax (1, tracks) * 2, 0.0f);
  53. }
  54. int MixerStrips::stripX (int track) const noexcept
  55. {
  56. return track == 0 ? 4 : 4 + masterWidth + 10 + (track - 1) * stripWidth;
  57. }
  58. int MixerStrips::trackAt (int x) const noexcept
  59. {
  60. const auto tracks = (int) engine.getSong().mixer.size();
  61. for (int t = 0; t <= tracks; ++t) // t == tracks = botão "+"
  62. if (x >= stripX (t) && x < stripX (t) + stripWidthOf (t))
  63. return t;
  64. return -1;
  65. }
  66. juce::Rectangle<int> MixerStrips::panArea (int t) const
  67. {
  68. return { stripX (t), headerHeight, stripWidthOf (t), panHeight };
  69. }
  70. juce::Rectangle<int> MixerStrips::muteArea (int t) const
  71. {
  72. return { stripX (t) + 6, headerHeight + panHeight, 26, buttonsHeight };
  73. }
  74. juce::Rectangle<int> MixerStrips::soloArea (int t) const
  75. {
  76. return { stripX (t) + stripWidthOf (t) - 32, headerHeight + panHeight, 26, buttonsHeight };
  77. }
  78. juce::Rectangle<int> MixerStrips::faderArea (int t) const
  79. {
  80. const auto top = headerHeight + panHeight + buttonsHeight + 8;
  81. return { stripX (t) + 6, top, stripWidthOf (t) - 12, getHeight() - top - feedsHeight - 22 };
  82. }
  83. MixerStrips::Part MixerStrips::partAt (int t, juce::Point<int> p) const
  84. {
  85. if (t == (int) engine.getSong().mixer.size())
  86. return Part::add;
  87. if (panArea (t).contains (p))
  88. return Part::pan;
  89. if (muteArea (t).contains (p))
  90. return Part::mute;
  91. if (t > 0 && soloArea (t).contains (p))
  92. return Part::solo;
  93. if (faderArea (t).expanded (0, 6).contains (p))
  94. return Part::fader;
  95. return Part::body;
  96. }
  97. void MixerStrips::paint (juce::Graphics& g)
  98. {
  99. const auto& song = engine.getSong();
  100. const auto tracks = (int) song.mixer.size();
  101. const auto clip = g.getClipBounds();
  102. g.fillAll (theme::window);
  103. for (int t = 0; t < tracks; ++t)
  104. {
  105. const auto x = stripX (t);
  106. const auto w = stripWidthOf (t);
  107. if (x > clip.getRight() || x + w < clip.getX())
  108. continue;
  109. const auto& track = song.mixer[(std::size_t) t];
  110. const auto strip = juce::Rectangle<int> (x, 2, w - 4, getHeight() - 4);
  111. g.setColour (t == selected ? juce::Colour (0xff3c444d) : (t == 0 ? juce::Colour (0xff343a41) : theme::panel));
  112. g.fillRoundedRectangle (strip.toFloat(), 4.0f);
  113. if (t == selected)
  114. {
  115. g.setColour (theme::accentOrange);
  116. g.drawRoundedRectangle (strip.toFloat(), 4.0f, 1.5f);
  117. }
  118. // cabeçalho: número e nome
  119. g.setColour (theme::text.withAlpha (0.55f));
  120. g.setFont (juce::FontOptions (11.0f));
  121. g.drawText (t == 0 ? juce::String ("M") : juce::String (t), x, 4, w - 4, 12, juce::Justification::centred);
  122. g.setColour (track.muted ? juce::Colours::grey : theme::text);
  123. g.setFont (juce::FontOptions (12.0f));
  124. g.drawFittedText (juce::String::fromUTF8 (track.name.c_str()), x + 2, 16, w - 8, 16, juce::Justification::centred, 1, 0.7f);
  125. if (! track.effects.empty())
  126. {
  127. g.setColour (theme::accentBlue);
  128. g.setFont (juce::FontOptions (9.0f));
  129. g.drawText ("FX " + juce::String ((int) track.effects.size()), x + w - 30, 3, 24, 11, juce::Justification::centredRight);
  130. }
  131. // pan
  132. {
  133. const auto area = panArea (t).toFloat().withSizeKeepingCentre (26.0f, 26.0f);
  134. const auto centre = area.getCentre();
  135. constexpr auto start = juce::MathConstants<float>::pi * 1.25f;
  136. constexpr auto range = juce::MathConstants<float>::pi * 1.5f;
  137. const auto to = start + range * (track.pan + 1.0f) * 0.5f;
  138. const auto mid = start + range * 0.5f;
  139. g.setColour (theme::knobBackground);
  140. g.fillEllipse (area);
  141. juce::Path arc;
  142. arc.addCentredArc (centre.x, centre.y, 11.5f, 11.5f, 0.0f, juce::jmin (mid, to), juce::jmax (mid, to), true);
  143. g.setColour (theme::accentBlue);
  144. g.strokePath (arc, juce::PathStrokeType (2.5f));
  145. g.setColour (juce::Colours::white);
  146. g.drawLine (centre.x, centre.y, centre.x + std::sin (to) * 9.0f, centre.y - std::cos (to) * 9.0f, 1.5f);
  147. }
  148. // mute e solo
  149. {
  150. const auto m = muteArea (t).toFloat();
  151. g.setColour (theme::muteLed (track.muted));
  152. g.fillEllipse (m.withSizeKeepingCentre (12.0f, 12.0f));
  153. if (t > 0)
  154. {
  155. const auto s = soloArea (t).toFloat().reduced (2.0f);
  156. g.setColour (track.solo ? theme::accentOrange : theme::knobBackground);
  157. g.fillRoundedRectangle (s, 3.0f);
  158. g.setColour (track.solo ? juce::Colours::black : theme::text.withAlpha (0.6f));
  159. g.setFont (juce::FontOptions (11.0f));
  160. g.drawText ("S", s.toNearestInt(), juce::Justification::centred);
  161. }
  162. }
  163. // medidores + fader
  164. {
  165. const auto area = faderArea (t);
  166. const auto meterW = 6;
  167. const auto meters = area.withWidth (meterW * 2 + 3);
  168. const auto fader = area.withTrimmedLeft (meterW * 2 + 8);
  169. for (int ch = 0; ch < 2; ++ch)
  170. {
  171. const auto bar = juce::Rectangle<int> (meters.getX() + ch * (meterW + 3), meters.getY(), meterW, meters.getHeight());
  172. g.setColour (theme::knobBackground);
  173. g.fillRect (bar);
  174. 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;
  175. const auto h = (int) (meterPosition (level) * (float) bar.getHeight());
  176. const auto lit = bar.withTop (bar.getBottom() - h);
  177. g.setGradientFill (juce::ColourGradient (juce::Colour (0xffff4040), (float) bar.getX(), (float) bar.getY(),
  178. theme::accentGreen, (float) bar.getX(), (float) bar.getBottom(), false));
  179. g.fillRect (lit);
  180. }
  181. // trilho do fader e marca de 0 dB
  182. const auto railX = (float) fader.getCentreX();
  183. g.setColour (theme::knobBackground);
  184. g.fillRoundedRectangle (railX - 2.0f, (float) fader.getY(), 4.0f, (float) fader.getHeight(), 2.0f);
  185. const auto unityY = (float) fader.getBottom() - unityFader * (float) fader.getHeight();
  186. g.setColour (theme::text.withAlpha (0.3f));
  187. g.drawHorizontalLine ((int) unityY, (float) fader.getX(), (float) fader.getRight());
  188. const auto capY = (float) fader.getBottom() - track.volume * (float) fader.getHeight();
  189. const auto cap = juce::Rectangle<float> ((float) fader.getX() + 2.0f, capY - 7.0f, (float) fader.getWidth() - 4.0f, 14.0f);
  190. g.setColour (juce::Colour (0xffb8bec5));
  191. g.fillRoundedRectangle (cap, 3.0f);
  192. g.setColour (juce::Colours::black.withAlpha (0.6f));
  193. g.drawHorizontalLine ((int) capY, cap.getX() + 2.0f, cap.getRight() - 2.0f);
  194. g.setColour (theme::text.withAlpha (0.6f));
  195. g.setFont (juce::FontOptions (10.0f));
  196. g.drawText (faderToDb (track.volume) + " dB", x, area.getBottom() + 3, w - 4, 14, juce::Justification::centred);
  197. }
  198. // canais que chegam nesta trilha
  199. {
  200. juce::StringArray feeds;
  201. for (const auto& c : song.channels)
  202. if (juce::jlimit (0, tracks - 1, c.mixerTrack) == t)
  203. feeds.add (juce::String::fromUTF8 (c.name.c_str()));
  204. g.setColour (theme::accentOrange.withAlpha (0.8f));
  205. g.setFont (juce::FontOptions (10.0f));
  206. const auto text = feeds.size() > 3 ? feeds[0] + ", " + feeds[1] + " +" + juce::String (feeds.size() - 2) : feeds.joinIntoString (", ");
  207. g.drawFittedText (text, x + 3, getHeight() - feedsHeight, w - 10, feedsHeight - 6, juce::Justification::centredTop, 3, 0.8f);
  208. }
  209. }
  210. // botão "+"
  211. if (tracks < maxMixerTracks)
  212. {
  213. const auto add = juce::Rectangle<int> (stripX (tracks), 2, stripWidth - 4, 60);
  214. g.setColour (theme::panel);
  215. g.fillRoundedRectangle (add.toFloat(), 4.0f);
  216. g.setColour (theme::text.withAlpha (0.6f));
  217. g.setFont (juce::FontOptions (22.0f));
  218. g.drawText ("+", add, juce::Justification::centred);
  219. }
  220. }
  221. void MixerStrips::mouseDown (const juce::MouseEvent& e)
  222. {
  223. const auto t = trackAt (e.x);
  224. if (t < 0)
  225. return;
  226. const auto part = partAt (t, e.getPosition());
  227. if (part == Part::add)
  228. {
  229. engine.editSong ([] (Song& s) {
  230. if (s.mixer.empty())
  231. s.mixer = Song::defaultMixer (0);
  232. auto& track = s.mixer.emplace_back();
  233. track.name = "Insert " + std::to_string (s.mixer.size() - 1);
  234. });
  235. return;
  236. }
  237. if (selected != t)
  238. {
  239. selected = t;
  240. repaint();
  241. if (onSelectionChanged)
  242. onSelectionChanged (t);
  243. }
  244. if (e.mods.isPopupMenu() && (part == Part::pan || part == Part::fader))
  245. {
  246. juce::PopupMenu menu;
  247. addAutomationItem (menu, engine, { part == Part::pan ? AutomationTargetKind::mixerPan : AutomationTargetKind::mixerVolume, t });
  248. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  249. return;
  250. }
  251. if (e.mods.isPopupMenu())
  252. {
  253. showMenu (t);
  254. return;
  255. }
  256. const auto& track = engine.getSong().mixer[(std::size_t) t];
  257. switch (part)
  258. {
  259. case Part::mute:
  260. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].muted = ! s.mixer[(std::size_t) t].muted; });
  261. return;
  262. case Part::solo:
  263. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].solo = ! s.mixer[(std::size_t) t].solo; });
  264. return;
  265. case Part::pan:
  266. case Part::fader:
  267. dragPart = part;
  268. dragTrack = t;
  269. dragStartValue = part == Part::pan ? track.pan : track.volume;
  270. if (part == Part::fader) // clicar no trilho pula o fader para lá
  271. {
  272. const auto area = faderArea (t);
  273. const auto capY = area.getBottom() - track.volume * (float) area.getHeight();
  274. if (std::abs ((float) e.y - capY) > 9.0f)
  275. {
  276. dragStartValue = juce::jlimit (0.0f, 1.0f, (float) (area.getBottom() - e.y) / (float) area.getHeight());
  277. engine.editSong ([t, v = dragStartValue] (Song& s) { s.mixer[(std::size_t) t].volume = v; });
  278. }
  279. }
  280. return;
  281. case Part::body:
  282. case Part::none:
  283. case Part::add:
  284. return;
  285. }
  286. }
  287. void MixerStrips::mouseDrag (const juce::MouseEvent& e)
  288. {
  289. if (dragPart == Part::none)
  290. return;
  291. const auto t = dragTrack;
  292. if (dragPart == Part::fader)
  293. {
  294. const auto height = (float) faderArea (t).getHeight();
  295. const auto fine = e.mods.isShiftDown() ? 0.2f : 1.0f;
  296. const auto v = juce::jlimit (0.0f, 1.0f, dragStartValue - (float) e.getDistanceFromDragStartY() / height * fine);
  297. engine.editSong ([t, v] (Song& s) { s.mixer[(std::size_t) t].volume = v; });
  298. }
  299. else
  300. {
  301. const auto v = juce::jlimit (-1.0f, 1.0f, dragStartValue - (float) e.getDistanceFromDragStartY() / 75.0f);
  302. engine.editSong ([t, v] (Song& s) { s.mixer[(std::size_t) t].pan = v; });
  303. }
  304. }
  305. void MixerStrips::mouseUp (const juce::MouseEvent&)
  306. {
  307. dragPart = Part::none;
  308. dragTrack = -1;
  309. }
  310. void MixerStrips::mouseDoubleClick (const juce::MouseEvent& e)
  311. {
  312. const auto t = trackAt (e.x);
  313. if (t < 0 || t >= (int) engine.getSong().mixer.size())
  314. return;
  315. const auto part = partAt (t, e.getPosition());
  316. if (part == Part::fader)
  317. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].volume = unityFader; });
  318. else if (part == Part::pan)
  319. engine.editSong ([t] (Song& s) { s.mixer[(std::size_t) t].pan = 0.0f; });
  320. }
  321. void MixerStrips::selectTrack (int track)
  322. {
  323. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), track);
  324. repaint();
  325. if (onSelectionChanged)
  326. onSelectionChanged (selected);
  327. }
  328. void MixerStrips::showMenu (int t)
  329. {
  330. const auto& song = engine.getSong();
  331. const auto channel = engine.getSelectedChannel();
  332. juce::PopupMenu menu;
  333. menu.addSectionHeader (juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()));
  334. if (channel < (int) song.channels.size())
  335. menu.addItem (utf8 ("Rotear o canal \"") + juce::String::fromUTF8 (song.channels[(std::size_t) channel].name.c_str())
  336. + utf8 ("\" para cá"),
  337. [this, channel, t] { engine.editSong ([channel, t] (Song& s) { s.channels[(std::size_t) channel].mixerTrack = t; }); });
  338. menu.addItem ("Renomear...", [this, t] {
  339. auto* window = new juce::AlertWindow ("Renomear trilha", {}, juce::MessageBoxIconType::NoIcon);
  340. window->addTextEditor ("name", juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) t].name.c_str()));
  341. window->addButton ("OK", 1, juce::KeyPress (juce::KeyPress::returnKey));
  342. window->addButton ("Cancelar", 0, juce::KeyPress (juce::KeyPress::escapeKey));
  343. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, t, window] (int result) {
  344. const auto name = window->getTextEditorContents ("name").trim().toStdString();
  345. if (result == 1 && ! name.empty() && t < (int) engine.getSong().mixer.size())
  346. engine.editSong ([t, name] (Song& s) { s.mixer[(std::size_t) t].name = name; });
  347. }),
  348. true);
  349. });
  350. menu.addItem ("Resetar (0 dB, centro, sem mute/solo)", [this, t] {
  351. engine.editSong ([t] (Song& s) {
  352. auto& track = s.mixer[(std::size_t) t];
  353. track.volume = unityFader;
  354. track.pan = 0.0f;
  355. track.muted = track.solo = false;
  356. });
  357. });
  358. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  359. }
  360. void MixerStrips::changeListenerCallback (juce::ChangeBroadcaster*)
  361. {
  362. selected = juce::jlimit (0, juce::jmax (0, (int) engine.getSong().mixer.size() - 1), selected);
  363. updateSize();
  364. repaint();
  365. }
  366. void MixerStrips::timerCallback()
  367. {
  368. auto& meters = engine.getMeters();
  369. bool changed = false;
  370. for (std::size_t i = 0; i < levels.size(); ++i)
  371. {
  372. const auto peak = meters.takePeak ((int) (i / 2), (int) (i % 2));
  373. const auto next = juce::jmax (peak, levels[i] * 0.86f); // queda de ~20 dB/s a 30 Hz
  374. const auto shown = next < 0.001f ? 0.0f : next;
  375. changed = changed || std::abs (shown - levels[i]) > 1e-4f;
  376. levels[i] = shown;
  377. }
  378. if (changed && isShowing())
  379. repaint();
  380. }
  381. // ---------------------------------------------------------------------------
  382. namespace
  383. {
  384. constexpr int panelHeader = 30;
  385. constexpr int slotHeight = 24;
  386. constexpr EffectKind allEffects[] = { EffectKind::eq, EffectKind::compressor, EffectKind::delay, EffectKind::reverb };
  387. } // namespace
  388. EffectsPanel::EffectsPanel (AudioEngine& e)
  389. : engine (e)
  390. {
  391. setWantsKeyboardFocus (false);
  392. setMouseClickGrabsKeyboardFocus (false);
  393. engine.addChangeListener (this);
  394. }
  395. EffectsPanel::~EffectsPanel()
  396. {
  397. engine.removeChangeListener (this);
  398. }
  399. void EffectsPanel::setTrack (int t)
  400. {
  401. track = t;
  402. const auto* s = slots();
  403. selectedSlot = s != nullptr && ! s->empty() ? 0 : -1;
  404. repaint();
  405. }
  406. const std::vector<EffectSlot>* EffectsPanel::slots() const
  407. {
  408. const auto& mixer = engine.getSong().mixer;
  409. return track >= 0 && track < (int) mixer.size() ? &mixer[(std::size_t) track].effects : nullptr;
  410. }
  411. juce::Rectangle<int> EffectsPanel::slotBounds (int slot) const
  412. {
  413. return { 8, panelHeader + slot * slotHeight, getWidth() - 16, slotHeight - 3 };
  414. }
  415. juce::Rectangle<int> EffectsPanel::knobBounds (int param) const
  416. {
  417. const auto top = panelHeader + maxEffectSlots * slotHeight + 40;
  418. const auto w = (getWidth() - 16) / 3;
  419. return { 8 + (param % 3) * w, top + (param / 3) * 86, w, 82 };
  420. }
  421. int EffectsPanel::knobAt (juce::Point<int> p) const
  422. {
  423. const auto* s = slots();
  424. if (s == nullptr || selectedSlot < 0 || selectedSlot >= (int) s->size())
  425. return -1;
  426. const auto count = (int) effectInfo ((*s)[(std::size_t) selectedSlot].kind).params.size();
  427. for (int i = 0; i < count; ++i)
  428. if (knobBounds (i).contains (p))
  429. return i;
  430. return -1;
  431. }
  432. void EffectsPanel::paint (juce::Graphics& g)
  433. {
  434. g.fillAll (theme::panel);
  435. g.setColour (theme::window);
  436. g.drawVerticalLine (0, 0.0f, (float) getHeight());
  437. const auto* s = slots();
  438. if (s == nullptr)
  439. return;
  440. const auto& song = engine.getSong();
  441. g.setColour (theme::accentOrange);
  442. g.setFont (juce::FontOptions (14.0f));
  443. g.drawText (utf8 ("Efeitos — ") + juce::String::fromUTF8 (song.mixer[(std::size_t) track].name.c_str()), 10, 4,
  444. getWidth() - 20, panelHeader - 8, juce::Justification::centredLeft, true);
  445. g.setFont (juce::FontOptions (13.0f));
  446. for (int i = 0; i < maxEffectSlots; ++i)
  447. {
  448. const auto r = slotBounds (i);
  449. const auto filled = i < (int) s->size();
  450. g.setColour (i == selectedSlot ? theme::channelButtonSelected : theme::channelButton.darker (filled ? 0.0f : 0.4f));
  451. g.fillRoundedRectangle (r.toFloat(), 3.0f);
  452. if (filled)
  453. {
  454. const auto& slot = (*s)[(std::size_t) i];
  455. g.setColour (theme::muteLed (! slot.enabled));
  456. g.fillEllipse ((float) r.getX() + 6, (float) r.getCentreY() - 5, 10, 10);
  457. g.setColour (slot.enabled ? theme::text : juce::Colours::grey);
  458. const auto name = slot.kind == EffectKind::plugin ? juce::String::fromUTF8 (slot.plugin.name.c_str())
  459. : utf8 (effectInfo (slot.kind).name);
  460. g.drawText (juce::String (i + 1) + ". " + name, r.withTrimmedLeft (24), juce::Justification::centredLeft);
  461. }
  462. else if (i == (int) s->size())
  463. {
  464. g.setColour (theme::text.withAlpha (0.45f));
  465. g.drawText (juce::String (i + 1) + utf8 (". (vazio — clique para adicionar)"), r.withTrimmedLeft (24),
  466. juce::Justification::centredLeft);
  467. }
  468. }
  469. if (selectedSlot < 0 || selectedSlot >= (int) s->size())
  470. return;
  471. const auto& slot = (*s)[(std::size_t) selectedSlot];
  472. if (slot.kind == EffectKind::plugin)
  473. {
  474. const auto button = pluginButtonBounds();
  475. g.setColour (theme::channelButton);
  476. g.fillRoundedRectangle (button.toFloat(), 4.0f);
  477. g.setColour (theme::text);
  478. g.setFont (juce::FontOptions (13.0f));
  479. g.drawText (utf8 ("Abrir interface do plugin"), button, juce::Justification::centred);
  480. g.setColour (theme::text.withAlpha (0.5f));
  481. g.setFont (juce::FontOptions (11.0f));
  482. g.drawText (juce::String::fromUTF8 (slot.plugin.name.c_str()), button.translated (0, 34), juce::Justification::centred);
  483. return;
  484. }
  485. const auto& info = effectInfo (slot.kind);
  486. g.setColour (theme::text);
  487. g.setFont (juce::FontOptions (14.0f));
  488. g.drawText (utf8 (info.name), 10, knobBounds (0).getY() - 28, getWidth() - 20, 20, juce::Justification::centredLeft);
  489. for (int p = 0; p < (int) info.params.size(); ++p)
  490. {
  491. const auto r = knobBounds (p);
  492. const auto value = slot.params[(std::size_t) p];
  493. const auto area = r.withHeight (44).toFloat().withSizeKeepingCentre (38.0f, 38.0f);
  494. const auto centre = area.getCentre();
  495. constexpr auto start = juce::MathConstants<float>::pi * 1.25f;
  496. constexpr auto range = juce::MathConstants<float>::pi * 1.5f;
  497. const auto to = start + range * value;
  498. g.setColour (theme::knobBackground);
  499. g.fillEllipse (area);
  500. juce::Path arc;
  501. arc.addCentredArc (centre.x, centre.y, 17.0f, 17.0f, 0.0f, start, to, true);
  502. g.setColour (slot.enabled ? theme::accentOrange : juce::Colours::grey);
  503. g.strokePath (arc, juce::PathStrokeType (3.0f));
  504. g.setColour (juce::Colours::white);
  505. g.drawLine (centre.x, centre.y, centre.x + std::sin (to) * 13.0f, centre.y - std::cos (to) * 13.0f, 2.0f);
  506. g.setColour (theme::text.withAlpha (0.8f));
  507. g.setFont (juce::FontOptions (11.0f));
  508. g.drawText (utf8 (info.params[(std::size_t) p].name), r.getX(), r.getY() + 46, r.getWidth(), 14, juce::Justification::centred);
  509. g.setColour (theme::accentBlue);
  510. g.drawText (juce::String::fromUTF8 (formatEffectParam (slot.kind, p, value, song.bpm).c_str()), r.getX(), r.getY() + 60,
  511. r.getWidth(), 14, juce::Justification::centred);
  512. }
  513. }
  514. void EffectsPanel::mouseDown (const juce::MouseEvent& e)
  515. {
  516. const auto* s = slots();
  517. if (s == nullptr)
  518. return;
  519. for (int i = 0; i < maxEffectSlots; ++i)
  520. {
  521. const auto r = slotBounds (i);
  522. if (! r.contains (e.getPosition()))
  523. continue;
  524. if (i >= (int) s->size())
  525. {
  526. showAddMenu (i);
  527. return;
  528. }
  529. selectedSlot = i;
  530. repaint();
  531. if (e.mods.isPopupMenu())
  532. showSlotMenu (i);
  533. else if (e.x < r.getX() + 22) // LED: bypass
  534. engine.editSong ([t = track, i] (Song& song) {
  535. auto& slot = song.mixer[(std::size_t) t].effects[(std::size_t) i];
  536. slot.enabled = ! slot.enabled;
  537. });
  538. return;
  539. }
  540. if (selectedSlot >= 0 && selectedSlot < (int) s->size() && (*s)[(std::size_t) selectedSlot].kind == EffectKind::plugin
  541. && pluginButtonBounds().contains (e.getPosition()))
  542. {
  543. openPluginWindow (selectedSlot);
  544. return;
  545. }
  546. dragParam = knobAt (e.getPosition());
  547. if (dragParam >= 0 && e.mods.isPopupMenu())
  548. {
  549. juce::PopupMenu menu;
  550. addAutomationItem (menu, engine, { AutomationTargetKind::effectParam, track, selectedSlot, dragParam });
  551. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  552. dragParam = -1;
  553. return;
  554. }
  555. if (dragParam >= 0)
  556. dragStart = (*s)[(std::size_t) selectedSlot].params[(std::size_t) dragParam];
  557. }
  558. void EffectsPanel::mouseDrag (const juce::MouseEvent& e)
  559. {
  560. if (dragParam < 0)
  561. return;
  562. const auto fine = e.mods.isShiftDown() ? 0.2f : 1.0f;
  563. const auto v = juce::jlimit (0.0f, 1.0f, dragStart - (float) e.getDistanceFromDragStartY() / 150.0f * fine);
  564. engine.editSong ([t = track, slot = selectedSlot, p = dragParam, v] (Song& song) {
  565. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) p] = v;
  566. });
  567. }
  568. void EffectsPanel::mouseUp (const juce::MouseEvent&)
  569. {
  570. dragParam = -1;
  571. }
  572. void EffectsPanel::mouseDoubleClick (const juce::MouseEvent& e)
  573. {
  574. const auto p = knobAt (e.getPosition());
  575. if (p < 0)
  576. return;
  577. const auto kind = (*slots())[(std::size_t) selectedSlot].kind;
  578. const auto value = effectInfo (kind).params[(std::size_t) p].defaultValue;
  579. engine.editSong ([t = track, slot = selectedSlot, p, value] (Song& song) {
  580. song.mixer[(std::size_t) t].effects[(std::size_t) slot].params[(std::size_t) p] = value;
  581. });
  582. }
  583. juce::Rectangle<int> EffectsPanel::pluginButtonBounds() const
  584. {
  585. return { 20, panelHeader + maxEffectSlots * slotHeight + 30, getWidth() - 40, 30 };
  586. }
  587. void EffectsPanel::openPluginWindow (int slot)
  588. {
  589. auto effect = engine.getEffect (track, slot);
  590. if (auto* plugin = dynamic_cast<PluginEffect*> (effect.get()))
  591. {
  592. const auto& s = engine.getSong().mixer[(std::size_t) track].effects[(std::size_t) slot];
  593. PluginWindows::open (effect, plugin->getInstance(), juce::String::fromUTF8 (s.plugin.name.c_str()));
  594. return;
  595. }
  596. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, "Plugin",
  597. utf8 ("Este plugin não pôde ser carregado (não está instalado ou não foi escaneado)."));
  598. }
  599. void EffectsPanel::showAddMenu (int)
  600. {
  601. juce::PopupMenu menu;
  602. for (auto kind : allEffects)
  603. menu.addItem (utf8 (effectInfo (kind).name), [this, kind] {
  604. engine.editSong ([t = track, kind] (Song& song) {
  605. auto& effects = song.mixer[(std::size_t) t].effects;
  606. if ((int) effects.size() < maxEffectSlots)
  607. effects.push_back (makeEffectSlot (kind));
  608. });
  609. selectedSlot = (int) slots()->size() - 1;
  610. repaint();
  611. });
  612. juce::PopupMenu plugins;
  613. addPluginItems (plugins, engine, false, [this] (PluginRef ref) {
  614. engine.editSong ([t = track, ref] (Song& song) {
  615. auto& effects = song.mixer[(std::size_t) t].effects;
  616. if ((int) effects.size() >= maxEffectSlots)
  617. return;
  618. EffectSlot slot;
  619. slot.kind = EffectKind::plugin;
  620. slot.plugin = ref;
  621. effects.push_back (slot);
  622. });
  623. selectedSlot = (int) slots()->size() - 1;
  624. openPluginWindow (selectedSlot);
  625. repaint();
  626. });
  627. menu.addSeparator();
  628. menu.addSubMenu ("Plugin VST3", plugins);
  629. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  630. }
  631. void EffectsPanel::showSlotMenu (int slot)
  632. {
  633. juce::PopupMenu replace;
  634. for (auto kind : allEffects)
  635. replace.addItem (utf8 (effectInfo (kind).name), [this, slot, kind] {
  636. engine.editSong ([t = track, slot, kind] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) slot] = makeEffectSlot (kind); });
  637. });
  638. const auto count = (int) slots()->size();
  639. juce::PopupMenu menu;
  640. menu.addSubMenu ("Trocar por", replace);
  641. menu.addItem ("Mover para cima", slot > 0, false, [this, slot] {
  642. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot - 1); });
  643. selectedSlot = slot - 1;
  644. });
  645. menu.addItem ("Mover para baixo", slot < count - 1, false, [this, slot] {
  646. engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot + 1); });
  647. selectedSlot = slot + 1;
  648. });
  649. if ((*slots())[(std::size_t) slot].kind == EffectKind::plugin)
  650. {
  651. if (auto* p = dynamic_cast<PluginEffect*> (engine.getEffect (track, slot).get()))
  652. addPluginParameterItems (menu, engine, p->getInstance(), { AutomationTargetKind::effectParam, track, slot, 0 });
  653. }
  654. menu.addSeparator();
  655. menu.addItem ("Remover", [this, slot] {
  656. engine.editSong ([t = track, slot] (Song& song) { song.removeEffectSlot (t, slot); });
  657. selectedSlot = juce::jmin (slot, (int) slots()->size() - 1);
  658. repaint();
  659. });
  660. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  661. }
  662. void EffectsPanel::changeListenerCallback (juce::ChangeBroadcaster*)
  663. {
  664. const auto* s = slots();
  665. if (s == nullptr)
  666. track = 0;
  667. else if (selectedSlot >= (int) s->size())
  668. selectedSlot = (int) s->size() - 1;
  669. repaint();
  670. }
  671. // ---------------------------------------------------------------------------
  672. MixerComponent::MixerComponent (AudioEngine& e)
  673. : engine (e),
  674. strips (e),
  675. effects (e)
  676. {
  677. viewport.setViewedComponent (&strips, false);
  678. viewport.setScrollBarsShown (false, true);
  679. addAndMakeVisible (viewport);
  680. addAndMakeVisible (effects);
  681. strips.onSelectionChanged = [this] (int track) { effects.setTrack (track); };
  682. }
  683. void MixerComponent::selectFirstTrackWithEffects()
  684. {
  685. const auto& mixer = engine.getSong().mixer;
  686. for (int t = 0; t < (int) mixer.size(); ++t)
  687. if (! mixer[(std::size_t) t].effects.empty())
  688. {
  689. strips.selectTrack (t);
  690. return;
  691. }
  692. }
  693. void MixerComponent::resized()
  694. {
  695. auto area = getLocalBounds();
  696. effects.setBounds (area.removeFromRight (effectsWidth));
  697. viewport.setBounds (area);
  698. strips.setVisibleHeight (area.getHeight() - viewport.getScrollBarThickness());
  699. }
  700. } // namespace lynx