FacetEqEditor.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. #include "ui/SlotWatch.h"
  2. #include "ui/FacetEqEditor.h"
  3. #include "core/MixerOps.h"
  4. #include "core/ShownEffectSlot.h"
  5. #include <juce_dsp/juce_dsp.h>
  6. #include <cmath>
  7. #include <map>
  8. #include "core/Effects.h"
  9. #include "core/FacetEq.h"
  10. #include "core/RackRenderer.h"
  11. #include "engine/Text.h"
  12. #include "ui/I18n.h"
  13. #include "ui/PluginHeader.h"
  14. #include "ui/PluginSkin.h"
  15. #include "ui/Theme.h"
  16. #include "ui/ValueControl.h"
  17. #include "ui/WindowOwner.h"
  18. namespace lynx
  19. {
  20. namespace
  21. {
  22. using namespace facet;
  23. constexpr double minHz = 10.0, maxHz = 30000.0; // the graph's frequency axis (the bands' whole range)
  24. constexpr double specTop = 0.0; // analyser scale top (dBFS, tilted around 1 kHz); the bottom is the Analyzer Range setting
  25. constexpr int fftOrder = 13, fftSize = 1 << fftOrder;
  26. juce::Colour bandColour (int band)
  27. {
  28. // one hue per band at the same lightness; the first is LYNX's red
  29. static const juce::Colour colours[] = { juce::Colour (0xffef5b5f), juce::Colour (0xfff2a46b), juce::Colour (0xffe8cf6a),
  30. juce::Colour (0xff86d993), juce::Colour (0xff62cfd0), juce::Colour (0xff7aa9f5),
  31. juce::Colour (0xffb59af2), juce::Colour (0xffef7fc0) };
  32. return colours[band & 7];
  33. }
  34. // display names of facet::Shape (core names, translated here)
  35. const char* const shapeNames[] = { TR_NOOP ("Off"), TR_NOOP ("Bell"), TR_NOOP ("Low shelf"), TR_NOOP ("High shelf"), TR_NOOP ("Low cut"),
  36. TR_NOOP ("High cut"), TR_NOOP ("Notch"), TR_NOOP ("Band pass"), TR_NOOP ("Tilt shelf"),
  37. TR_NOOP ("Flat tilt"), TR_NOOP ("All pass") }; // round 135: the last two
  38. const char* const phaseNames[] = { TR_NOOP ("Zero latency"), TR_NOOP ("Natural phase"), TR_NOOP ("Linear phase") };
  39. const char* const analyzerShort[] = { TR_NOOP ("Off"), TR_NOOP ("Pre"), TR_NOOP ("Post"), TR_NOOP ("Pre + Post") };
  40. const char* const analyzerNames[] = { TR_NOOP ("Analyzer off"), TR_NOOP ("Analyzer: pre"), TR_NOOP ("Analyzer: post"), TR_NOOP ("Analyzer: pre + post") };
  41. juce::String noteName (double hz)
  42. {
  43. static const char* names[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  44. const auto n = 69.0 + 12.0 * std::log2 (hz / 440.0);
  45. const auto k = (int) std::lround (n);
  46. const auto cents = (int) std::lround ((n - k) * 100.0);
  47. return juce::String (names[((k % 12) + 12) % 12]) + juce::String (k / 12 - 1) + (cents == 0 ? juce::String() : (cents > 0 ? " +" : " ") + juce::String (cents));
  48. }
  49. juce::String hzText (double hz)
  50. {
  51. return hz < 1000.0 ? juce::String (hz, hz < 100.0 ? 1 : 0) + " Hz" : juce::String (hz / 1000.0, hz < 10000.0 ? 2 : 1) + " kHz";
  52. }
  53. // a band's stereo placement for display: Mid / Side stay as they are (the usual terms in every language)
  54. juce::String placementLabel (facet::Placement p)
  55. {
  56. return p == facet::Placement::mid || p == facet::Placement::side ? juce::String (facet::placementName (p)) : tr (facet::placementName (p));
  57. }
  58. // the power symbol (an open ring with a bar), for the band power buttons
  59. void drawPowerGlyph (juce::Graphics& g, juce::Rectangle<float> r, juce::Colour c)
  60. {
  61. const auto d = juce::jmin (r.getWidth(), r.getHeight()) * 0.62f;
  62. const auto ctr = r.getCentre();
  63. juce::Path ring;
  64. ring.addCentredArc (ctr.x, ctr.y + d * 0.06f, d * 0.5f, d * 0.5f, 0.0f, 0.75f, juce::MathConstants<float>::twoPi - 0.75f, true);
  65. g.setColour (c);
  66. g.strokePath (ring, juce::PathStrokeType (1.5f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  67. g.drawLine (ctr.x, ctr.y - d * 0.62f, ctr.x, ctr.y - d * 0.05f, 1.5f);
  68. }
  69. class FacetEqEditor final : public juce::Component,
  70. public ui::HintSource,
  71. private juce::ChangeListener,
  72. private juce::Timer
  73. {
  74. public:
  75. friend void ::lynx::tickFacetEqEditor (juce::Component&); // round 166f (--eq166fshot)
  76. FacetEqEditor (AudioEngine& e, int t, int s, bool demoSpectrum)
  77. : engine (e),
  78. track (t),
  79. slot (s),
  80. demo (demoSpectrum),
  81. fft (fftOrder)
  82. {
  83. fftData.assign ((std::size_t) fftSize * 2, 0.0f);
  84. window.resize ((std::size_t) fftSize);
  85. for (int i = 0; i < fftSize; ++i)
  86. window[(std::size_t) i] = 0.5f - 0.5f * std::cos (2.0f * juce::MathConstants<float>::pi * (float) i / (float) fftSize);
  87. if (const auto* fx = current())
  88. for (int b = 0; b < bands; ++b)
  89. if (shape (fx->params[(std::size_t) param (b, 0)]) != Shape::off) { selected = b; break; }
  90. engine.addChangeListener (this);
  91. setWantsKeyboardFocus (true);
  92. setSize (960, 560);
  93. if (demo)
  94. {
  95. for (int b = bands; --b >= 0;) // show a dynamic band, if any
  96. if (current() != nullptr && isDynamic (current()->params, b)) { selected = b; break; }
  97. updateSpectrum();
  98. }
  99. claimTap();
  100. startTimerHz (30);
  101. }
  102. ~FacetEqEditor() override
  103. {
  104. stopTimer();
  105. engine.removeChangeListener (this);
  106. if (auto* times = engine.getProcessTimes(); times != nullptr && times->analyzer.key.load() == tapKey())
  107. times->analyzer.key.store (-1);
  108. // solo is a listening aid: it never stays on once the editor is gone
  109. if (const auto* fx = current(); fx != nullptr && soloBand (fx->params[(std::size_t) soloParam]) >= 0)
  110. engine.editSong ([tk = track, sl = slot] (Song& song) { song.mixer[(std::size_t) tk].effects[(std::size_t) sl].params[(std::size_t) soloParam] = 0.0f; }, false);
  111. ui::hideValueTip();
  112. }
  113. // the slot as shown: automated band handles and knobs at their automation's value while the song plays (issue #11)
  114. const EffectSlot* current() const { return shownSlot.get (engine.getAutomationDisplay(), stored(), track, slot, juce::ModifierKeys::currentModifiers.isAnyMouseButtonDown()); }
  115. const EffectSlot* stored() const
  116. {
  117. const auto& mixer = engine.getSong().mixer;
  118. if (track < 0 || track >= (int) mixer.size() || slot < 0 || slot >= (int) mixer[(std::size_t) track].effects.size())
  119. return nullptr;
  120. const auto& fx = mixer[(std::size_t) track].effects[(std::size_t) slot];
  121. return fx.kind == EffectKind::facetEq ? &fx : nullptr;
  122. }
  123. // --- geometry ------------------------------------------------------------------------------------
  124. // the whole window is the graph (curves over the analyser); a slim title bar on top, the global bar at the bottom,
  125. // and the selected band's controls float over the bottom of the graph
  126. juce::Rectangle<float> headerArea() const { return { 0.0f, 0.0f, (float) getWidth(), 30.0f }; }
  127. juce::Rectangle<float> barArea() const { return { 0.0f, (float) getHeight() - 30.0f, (float) getWidth(), 30.0f }; }
  128. juce::Rectangle<float> graphArea() const { return { 0.0f, 30.0f, (float) getWidth(), barArea().getY() - 30.0f }; }
  129. // round 166f: the comparative EQ's switch, segments 0 EQ, 1 Source, 2 Target, 3 its menu (top left of the graph)
  130. juce::Rectangle<float> listenSegment (int k) const
  131. {
  132. const auto x = 50.0f + (k < 3 ? 58.0f * (float) k : 174.0f);
  133. return { x, 40.0f, k < 3 ? 56.0f : 20.0f, 18.0f };
  134. }
  135. juce::Rectangle<float> plotArea() const { return graphArea().withTrimmedRight (64.0f).withTrimmedBottom (20.0f).withTrimmedTop (6.0f); }
  136. juce::Rectangle<float> panelArea() const
  137. {
  138. const auto w = hasGain (bandShape (selected)) ? 690.0f : 368.0f;
  139. constexpr float h = 118.0f;
  140. return { juce::jmax (4.0f, plotArea().getCentreX() - w / 2.0f), plotArea().getBottom() - h - 4.0f, w, h };
  141. }
  142. bool panelShown() const { return bandOn (selected); }
  143. // the small read-out box next to a hovered node (frequency, gain, Q, solo, remove)
  144. juce::Rectangle<float> tipArea (int b) const
  145. {
  146. const auto n = nodePosition (b);
  147. auto r = juce::Rectangle<float> (n.x - 70.0f, n.y - 74.0f, 140.0f, 58.0f);
  148. if (r.getY() < plotArea().getY()) r.setY (n.y + 16.0f);
  149. return r.constrainedWithin (plotArea());
  150. }
  151. float xForHz (double f) const
  152. {
  153. const auto a = plotArea();
  154. return a.getX() + a.getWidth() * (float) (std::log (juce::jlimit (minHz, maxHz, f) / minHz) / std::log (maxHz / minHz));
  155. }
  156. double hzForX (float x) const
  157. {
  158. const auto a = plotArea();
  159. return minHz * std::pow (maxHz / minHz, juce::jlimit (0.0, 1.0, (double) ((x - a.getX()) / a.getWidth())));
  160. }
  161. float yForDb (double db) const
  162. {
  163. const auto a = plotArea();
  164. const auto rangeDb = viewRange();
  165. return a.getCentreY() - a.getHeight() * 0.5f * (float) (juce::jlimit (-rangeDb * 1.1, rangeDb * 1.1, db) / rangeDb) * 0.92f;
  166. }
  167. double dbForY (float y) const
  168. {
  169. const auto a = plotArea();
  170. return (double) ((a.getCentreY() - y) / (a.getHeight() * 0.5f * 0.92f)) * viewRange();
  171. }
  172. float yForSpectrum (double db) const
  173. {
  174. const auto a = plotArea();
  175. const auto specBottom = specFloor();
  176. return a.getY() + a.getHeight() * (float) ((specTop - juce::jlimit (specBottom, specTop, db)) / (specTop - specBottom));
  177. }
  178. float param01 (int p) const
  179. {
  180. const auto* fx = current();
  181. return fx != nullptr && p >= 0 && p < maxEffectParams ? fx->params[(std::size_t) p] : 0.0f;
  182. }
  183. Shape bandShape (int b) const { return shape (param01 (param (b, 0))); }
  184. // display settings (saved with the slot, round 128): the graph's ± dB range, the analyser's floor, speed and tilt
  185. double viewRange() const { return displayRangeDb (param01 (displayRangeParam)); }
  186. double specFloor() const { return -analyzerRangeDb (param01 (specRangeParam)); }
  187. bool bandPowered (int b) const { return param01 (powerParam0 + b) < 0.5f; }
  188. Placement bandPlacement (int b) const { return placement (param01 (placementParam0 + b)); }
  189. bool bandOn (int b) const { return b >= 0 && b < bands && bandShape (b) != Shape::off; }
  190. // the live dynamic gain of a band (dB), from the audio thread
  191. double liveDynamic (int b) const
  192. {
  193. const auto* times = engine.getProcessTimes();
  194. const auto key = (std::size_t) (track * maxEffectSlots + slot) * ProcessTimes::meterOutputs + (std::size_t) b;
  195. if (times == nullptr || key >= times->effectMeters.size()) return 0.0;
  196. return (double) times->effectMeters[key].load (std::memory_order_relaxed) * 60.0;
  197. }
  198. juce::Point<float> nodePosition (int b) const
  199. {
  200. const auto f = hz (param01 (param (b, 1)));
  201. const auto db = hasGain (bandShape (b)) ? gainDb (param01 (param (b, 2))) * gainScale (param01 (scaleParam)) : 0.0;
  202. return { xForHz (f), yForDb (db) };
  203. }
  204. // the range handle of a dynamic band: at static gain + range
  205. juce::Point<float> rangeHandle (int b) const
  206. {
  207. const auto scale = gainScale (param01 (scaleParam));
  208. const auto db = (gainDb (param01 (param (b, 2))) + rangeDb_ (b)) * scale;
  209. return { nodePosition (b).x, yForDb (db) };
  210. }
  211. double rangeDb_ (int b) const { return facet::rangeDb (param01 (rangeParam0 + b)); }
  212. // round 166f (issue #16): one rule for both targets (core/FacetEq.h pickGraphTarget) — a press on a node always
  213. // moves the band; the range handle of a dynamic band is a small target of its own, off the node
  214. GraphHit hitAt (juce::Point<float> p) const
  215. {
  216. float nx[bands], ny[bands], rx[bands], ry[bands];
  217. bool on[bands], dyn[bands];
  218. const auto* fx = current();
  219. for (int b = 0; b < bands; ++b)
  220. {
  221. on[b] = bandOn (b);
  222. dyn[b] = on[b] && fx != nullptr && isDynamic (fx->params, b);
  223. const auto n = nodePosition (b);
  224. nx[b] = n.x; ny[b] = n.y;
  225. const auto r = dyn[b] ? rangeHandle (b) : n;
  226. rx[b] = r.x; ry[b] = r.y;
  227. }
  228. return pickGraphTarget (nx, ny, on, dyn, rx, ry, bands, selected, p.x, p.y);
  229. }
  230. int nodeAt (juce::Point<float> p) const { const auto h = hitAt (p); return h.range ? -1 : h.band; }
  231. int rangeHandleAt (juce::Point<float> p) const { const auto h = hitAt (p); return h.range ? h.band : -1; }
  232. // --- controls of the bottom strip and the header -------------------------------------------------
  233. enum class Ctl { none, shapeMenu, freq, gain, q, slopeMenu, range, threshold, solo, dynamic, remove, attack, release,
  234. phase, analyzer, detector, output, scale, scaleDb, prevBand, nextBand, close, piano, tipSolo, tipRemove,
  235. power, placementMenu, tipPower, autoGain, pan,
  236. spectral, spectralTilt, match, // round 152
  237. listen, compareMenu }; // round 166f: the comparative EQ's switch
  238. struct Control { Ctl type; juce::Rectangle<float> r; int param = -1; int index = 0; };
  239. std::vector<Control> controls() const
  240. {
  241. std::vector<Control> out;
  242. // the bottom bar: piano / frequency scale, processing mode, analyser, detector … gain scale and output on the right
  243. const auto bar = barArea();
  244. const auto cy = bar.getCentreY();
  245. out.push_back ({ Ctl::piano, { 8.0f, cy - 10.0f, 26.0f, 20.0f } });
  246. out.push_back ({ Ctl::phase, { 44.0f, cy - 11.0f, 150.0f, 22.0f }, phaseParam });
  247. out.push_back ({ Ctl::analyzer, { 202.0f, cy - 11.0f, 170.0f, 22.0f }, analyzerParam });
  248. out.push_back ({ Ctl::detector, { 380.0f, cy - 11.0f, 150.0f, 22.0f }, detectorParam });
  249. out.push_back ({ Ctl::match, { 536.0f, cy - 11.0f, 48.0f, 22.0f } }); // round 152: EQ match
  250. out.push_back ({ Ctl::output, { bar.getRight() - 100.0f, cy - 11.0f, 92.0f, 22.0f }, outputParam });
  251. out.push_back ({ Ctl::scale, { bar.getRight() - 190.0f, cy - 11.0f, 84.0f, 22.0f }, scaleParam });
  252. out.push_back ({ Ctl::pan, { bar.getRight() - 282.0f, cy - 11.0f, 86.0f, 22.0f }, panParam });
  253. out.push_back ({ Ctl::autoGain, { bar.getRight() - 372.0f, cy - 11.0f, 84.0f, 22.0f }, autoGainParam });
  254. // the graph's dB range (top right: ±3 / 6 / 12 / 30)
  255. const auto plot = plotArea();
  256. // round 166f (issue #16): the comparative EQ — listen to the EQ, the source or the target (top left of the graph)
  257. for (int k = 0; k < 3; ++k)
  258. out.push_back ({ Ctl::listen, listenSegment (k), listenParam, k });
  259. out.push_back ({ Ctl::compareMenu, listenSegment (3) });
  260. out.push_back ({ Ctl::scaleDb, { plot.getRight() + 6.0f, plot.getY() + 2.0f, 50.0f, 18.0f } });
  261. // the hovered node's read-out: solo and remove
  262. if (tipBand >= 0 && bandOn (tipBand) && dragging == Drag::none)
  263. {
  264. const auto t = tipArea (tipBand);
  265. out.push_back ({ Ctl::tipPower, { t.getRight() - 62.0f, t.getY() + 4.0f, 18.0f, 18.0f }, -1, tipBand });
  266. out.push_back ({ Ctl::tipSolo, { t.getRight() - 42.0f, t.getY() + 4.0f, 18.0f, 18.0f }, -1, tipBand });
  267. out.push_back ({ Ctl::tipRemove, { t.getRight() - 22.0f, t.getY() + 4.0f, 18.0f, 18.0f }, -1, tipBand });
  268. }
  269. // the floating band panel: shape | FREQ | GAIN | dynamics | Q | band navigation, solo, remove
  270. if (panelShown())
  271. {
  272. const auto p = panelArea();
  273. const auto b = selected;
  274. const auto sh = bandShape (b);
  275. const auto y = p.getY();
  276. auto x = p.getX();
  277. out.push_back ({ Ctl::power, { x + 10.0f, y + 4.0f, 24.0f, 24.0f }, powerParam0 + b });
  278. out.push_back ({ Ctl::shapeMenu, { x + 12.0f, y + 30.0f, 108.0f, 26.0f }, param (b, 0) });
  279. if (sh == Shape::lowCut || sh == Shape::highCut || sh == Shape::lowShelf || sh == Shape::highShelf || sh == Shape::tiltShelf)
  280. out.push_back ({ Ctl::slopeMenu, { x + 12.0f, y + 62.0f, 108.0f, 22.0f }, param (b, 4) });
  281. x += 126.0f;
  282. out.push_back ({ Ctl::freq, { x, y + 8.0f, 70.0f, 104.0f }, param (b, 1) });
  283. x += 72.0f;
  284. if (hasGain (sh))
  285. {
  286. out.push_back ({ Ctl::gain, { x, y + 2.0f, 96.0f, 112.0f }, param (b, 2) });
  287. x += 104.0f;
  288. out.push_back ({ Ctl::dynamic, { x + 4.0f, y + 8.0f, 86.0f, 20.0f }, rangeParam0 + b });
  289. // round 152: Pro-Q 4's spectral dynamics (right-click: density) and its tilt
  290. if (isDynamic (current()->params, b))
  291. {
  292. out.push_back ({ Ctl::spectral, { x + 94.0f, y + 8.0f, 70.0f, 20.0f }, spectralParam0 + b });
  293. if (isSpectral (current()->params, b))
  294. out.push_back ({ Ctl::spectralTilt, { x + 168.0f, y + 8.0f, 46.0f, 20.0f }, spectralTiltParam0 + b });
  295. }
  296. if (isDynamic (current()->params, b))
  297. {
  298. out.push_back ({ Ctl::range, { x - 2.0f, y + 34.0f, 56.0f, 80.0f }, rangeParam0 + b });
  299. out.push_back ({ Ctl::threshold, { x + 54.0f, y + 34.0f, 56.0f, 80.0f }, thresholdParam0 + b });
  300. out.push_back ({ Ctl::attack, { x + 110.0f, y + 34.0f, 52.0f, 80.0f }, attackParam });
  301. out.push_back ({ Ctl::release, { x + 162.0f, y + 34.0f, 52.0f, 80.0f }, releaseParam });
  302. }
  303. x += 218.0f;
  304. }
  305. out.push_back ({ Ctl::q, { x, y + 8.0f, 70.0f, 104.0f }, param (b, 3) });
  306. x += 78.0f;
  307. out.push_back ({ Ctl::prevBand, { x + 2.0f, y + 6.0f, 20.0f, 20.0f } });
  308. out.push_back ({ Ctl::nextBand, { x + 44.0f, y + 6.0f, 20.0f, 20.0f } });
  309. out.push_back ({ Ctl::close, { x + 66.0f, y + 6.0f, 20.0f, 20.0f } });
  310. out.push_back ({ Ctl::solo, { x + 4.0f, y + 34.0f, 80.0f, 22.0f }, soloParam });
  311. out.push_back ({ Ctl::placementMenu, { x + 4.0f, y + 60.0f, 80.0f, 22.0f }, placementParam0 + b });
  312. out.push_back ({ Ctl::remove, { x + 4.0f, y + 86.0f, 80.0f, 22.0f } });
  313. }
  314. return out;
  315. }
  316. const Control* controlAt (juce::Point<float> p, std::vector<Control>& all) const
  317. {
  318. all = controls();
  319. for (const auto& c : all)
  320. if (c.r.contains (p)) return &c;
  321. return nullptr;
  322. }
  323. juce::Rectangle<float> knobCircle (const Control& c) const
  324. {
  325. const auto d = juce::jmin (c.r.getWidth() - 12.0f, c.r.getHeight() - 44.0f);
  326. return { c.r.getCentreX() - d / 2.0f, c.r.getY() + 4.0f, d, d };
  327. }
  328. juce::String trackName() const
  329. {
  330. const auto& mixer = engine.getSong().mixer;
  331. if (track == 0) return tr ("Master");
  332. const auto name = track < (int) mixer.size() ? juce::String::fromUTF8 (mixer[(std::size_t) track].name.c_str()) : juce::String();
  333. return name.isNotEmpty() ? name : tr ("Insert {0}", { juce::String (track) });
  334. }
  335. ui::ValueSpec specFor (int prm) const
  336. {
  337. ui::ValueSpec spec;
  338. const auto& in = info();
  339. if (prm < 0 || prm >= (int) in.params.size()) return spec;
  340. spec.name = trackName() + " - Facet EQ - " + utf8 (in.params[(std::size_t) prm].name);
  341. spec.defaultValue = in.params[(std::size_t) prm].defaultValue;
  342. spec.pixelsForRange = 200.0;
  343. spec.toText = [prm] (double v) { return utf8 (paramText (prm, (float) v).c_str()); };
  344. spec.fromText = ui::inverseOf (spec.toText, 0.0, 1.0);
  345. spec.target = AutomationTarget { AutomationTargetKind::effectParam, track, slot, prm };
  346. return spec;
  347. }
  348. juce::String valueText (int prm) const { return utf8 (paramText (prm, param01 (prm)).c_str()); }
  349. // --- editing -------------------------------------------------------------------------------------
  350. void set (std::vector<std::pair<int, float>> changes, bool undoable = true)
  351. {
  352. if (current() == nullptr) return;
  353. if (undoable) bar.willEdit();
  354. engine.editSong ([tk = track, sl = slot, changes] (Song& song) {
  355. auto& params = song.mixer[(std::size_t) tk].effects[(std::size_t) sl].params;
  356. for (const auto& [p, v] : changes)
  357. if (p >= 0 && p < maxEffectParams) params[(std::size_t) p] = juce::jlimit (0.0f, 1.0f, v);
  358. }, undoable);
  359. }
  360. void addBand (juce::Point<float> at)
  361. {
  362. for (int b = 0; b < bands; ++b)
  363. if (! bandOn (b))
  364. {
  365. const auto f = hzForX (at.x);
  366. // the ends of the spectrum get cuts, as a quick start; anything else is a bell at the clicked gain
  367. const auto sh = f < 25.0 ? Shape::lowCut : f > 18000.0 ? Shape::highCut : Shape::bell;
  368. const auto scale = juce::jmax (0.01, gainScale (param01 (scaleParam)));
  369. set ({ { param (b, 0), shapeValue (sh) }, { param (b, 1), hzValue (f) }, { param (b, 2), sh == Shape::bell ? gainValue (dbForY (at.y) / scale) : 0.5f },
  370. { param (b, 3), qValue (1.0) }, { param (b, 4), slopeValue (sh == Shape::bell ? 12 : 24) }, { rangeParam0 + b, 0.5f },
  371. { powerParam0 + b, 0.0f }, { placementParam0 + b, 0.0f } });
  372. selected = b;
  373. return;
  374. }
  375. }
  376. void removeBand (int b)
  377. {
  378. std::vector<std::pair<int, float>> c { { param (b, 0), 0.0f }, { rangeParam0 + b, 0.5f } };
  379. if (soloBand (param01 (soloParam)) == b) c.push_back ({ soloParam, 0.0f });
  380. set (c);
  381. if (selected == b)
  382. {
  383. selected = -1;
  384. for (int k = 0; k < bands; ++k)
  385. if (k != b && bandOn (k)) { selected = k; break; }
  386. }
  387. }
  388. void toggleSolo (int b)
  389. {
  390. set ({ { soloParam, soloBand (param01 (soloParam)) == b ? 0.0f : soloValue (b) } }, false);
  391. }
  392. void showBandMenu (int b)
  393. {
  394. juce::PopupMenu menu;
  395. menu.addSectionHeader (tr ("Band {0}", { juce::String (b + 1) }));
  396. const auto sh = bandShape (b);
  397. for (int k = 1; k < shapeCount; ++k)
  398. menu.addItem (tr (shapeNames[k]), true, (int) sh == k, [this, b, k] { set ({ { param (b, 0), shapeValue ((Shape) k) } }); });
  399. if (sh == Shape::lowCut || sh == Shape::highCut || sh == Shape::lowShelf || sh == Shape::highShelf || sh == Shape::tiltShelf)
  400. {
  401. juce::PopupMenu slopes;
  402. for (const auto d : facet::slopes)
  403. slopes.addItem (juce::String (d) + " dB/oct", true, slopeDb (param01 (param (b, 4))) == d, [this, b, d] { set ({ { param (b, 4), slopeValue (d) } }); });
  404. menu.addSubMenu (tr ("Slope"), slopes);
  405. }
  406. if (hasGain (sh))
  407. {
  408. juce::PopupMenu dyn;
  409. for (const auto r : { 0.0, -3.0, -6.0, -12.0, 3.0, 6.0 })
  410. dyn.addItem (r == 0.0 ? tr ("Off") : juce::String (r > 0 ? "+" : "") + juce::String ((int) r) + " dB", true, std::abs (rangeDb_ (b) - r) < 0.05,
  411. [this, b, r] { set ({ { rangeParam0 + b, rangeValue (r) } }); });
  412. menu.addSubMenu (tr ("Dynamic range"), dyn);
  413. }
  414. juce::PopupMenu where;
  415. for (int k = 0; k < placementCount; ++k)
  416. where.addItem (placementLabel ((Placement) k), true, (int) bandPlacement (b) == k, [this, b, k] { set ({ { placementParam0 + b, placementValue ((Placement) k) } }); });
  417. menu.addSubMenu (tr ("Stereo placement"), where);
  418. menu.addSeparator();
  419. menu.addItem (tr ("Band on"), true, bandPowered (b), [this, b] { set ({ { powerParam0 + b, bandPowered (b) ? 1.0f : 0.0f } }); });
  420. menu.addItem (tr ("Solo band"), true, soloBand (param01 (soloParam)) == b, [this, b] { toggleSolo (b); });
  421. menu.addItem (tr ("Reset gain"), hasGain (sh), false, [this, b] { set ({ { param (b, 2), 0.5f } }); });
  422. menu.addItem (tr ("Remove band"), [this, b] { removeBand (b); });
  423. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  424. }
  425. void showDetectorMenu()
  426. {
  427. juce::PopupMenu menu;
  428. const auto* fx = current();
  429. if (fx == nullptr) return;
  430. const auto side = sidechainDetector (param01 (detectorParam));
  431. menu.addItem (tr ("Detect the EQ's input"), true, ! side, [this] { set ({ { detectorParam, 0.0f } }); });
  432. menu.addItem (tr ("Detect the sidechain"), true, side, [this] { set ({ { detectorParam, 1.0f } }); });
  433. juce::PopupMenu from;
  434. const auto assign = [this] (int source) {
  435. engine.editSong ([tk = track, sl = slot, source] (Song& s) {
  436. // round 177b: the pick makes the route (a level-0 send) as the mixer's picker does; a loop changes nothing
  437. if (mixerops::sidechainFrom (s, tk, sl, source) == mixerops::SidechainPick::cycle)
  438. return;
  439. auto& target = s.mixer[(std::size_t) tk].effects[(std::size_t) sl];
  440. if (source >= 0) target.params[(std::size_t) detectorParam] = 1.0f;
  441. });
  442. };
  443. from.addItem (tr ("None"), true, fx->sidechain < 0, [assign] { assign (-1); });
  444. const auto& song = engine.getSong();
  445. for (int t = 1; t < (int) song.mixer.size(); ++t)
  446. if (t != track)
  447. from.addItem (juce::String (t) + " " + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()), true, fx->sidechain == t, [assign, t] { assign (t); });
  448. menu.addSubMenu (tr ("Sidechain from"), from);
  449. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  450. }
  451. // round 152: the spectral band's density (right-click on Spectral)
  452. void showDensityMenu (int b)
  453. {
  454. if (b < 0) return;
  455. juce::PopupMenu menu;
  456. const auto now = param01 (densityParam0 + b);
  457. for (const int pct : { 0, 25, 50, 75, 100 })
  458. menu.addItem (tr ("Density {0} %", { juce::String (pct) }), true, std::abs (now - pct / 100.0f) < 0.01f,
  459. [this, b, pct] { set ({ { densityParam0 + b, pct / 100.0f } }); });
  460. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  461. }
  462. // round 166f (issue #16): the comparative EQ — the target is the slot's sidechain track
  463. bool hasTarget() const
  464. {
  465. const auto* fx = current();
  466. return fx != nullptr && fx->sidechain > 0 && fx->sidechain != track && fx->sidechain < (int) engine.getSong().mixer.size();
  467. }
  468. juce::String targetName() const
  469. {
  470. if (! hasTarget()) return {};
  471. const auto t = current()->sidechain;
  472. return juce::String (t) + " " + juce::String::fromUTF8 (engine.getSong().mixer[(std::size_t) t].name.c_str());
  473. }
  474. void showCompareMenu (bool thenListen = false)
  475. {
  476. const auto* fx = current();
  477. if (fx == nullptr) return;
  478. juce::PopupMenu menu;
  479. menu.addSectionHeader (tr ("Compare: listen to the EQ, to this track without it (source) or to another track (target)"));
  480. const auto now = listen (param01 (listenParam));
  481. for (const auto l : { Listen::eq, Listen::source, Listen::target })
  482. menu.addItem (tr (listenLabel (l)), l != Listen::target || hasTarget(), now == l, [this, l] { set ({ { listenParam, listenValue (l) } }, false); });
  483. menu.addSeparator();
  484. juce::PopupMenu from;
  485. const auto assign = [this, thenListen] (int source) {
  486. engine.editSong ([tk = track, sl = slot, source, thenListen] (Song& s) {
  487. // round 177b: the pick makes the route (a level-0 send) as the mixer's picker does; a loop changes nothing
  488. if (mixerops::sidechainFrom (s, tk, sl, source) == mixerops::SidechainPick::cycle)
  489. return;
  490. auto& target = s.mixer[(std::size_t) tk].effects[(std::size_t) sl]; // (the same input the dynamic bands' sidechain detector hears)
  491. if (source < 0 && listen (target.params[(std::size_t) listenParam]) == Listen::target) target.params[(std::size_t) listenParam] = listenValue (Listen::eq);
  492. if (source >= 0 && thenListen) target.params[(std::size_t) listenParam] = listenValue (Listen::target);
  493. }, false);
  494. };
  495. from.addItem (tr ("None"), true, fx->sidechain < 0, [assign] { assign (-1); });
  496. const auto& song = engine.getSong();
  497. for (int t = 1; t < (int) song.mixer.size(); ++t)
  498. if (t != track)
  499. from.addItem (juce::String (t) + " " + juce::String::fromUTF8 (song.mixer[(std::size_t) t].name.c_str()), true, fx->sidechain == t, [assign, t] { assign (t); });
  500. menu.addSubMenu (tr ("Target track (sidechain)"), from);
  501. menu.addItem (tr ("Match the target's level to this track"), true, param01 (targetLevelParam) >= 0.5f,
  502. [this] { set ({ { targetLevelParam, param01 (targetLevelParam) >= 0.5f ? 0.0f : 1.0f } }); });
  503. menu.addItem (tr ("Show the target's spectrum"), hasTarget(), showTarget, [this] { showTarget = ! showTarget; repaint(); });
  504. menu.addSeparator();
  505. menu.addItem (tr ("Capture this track and the target together (play now), then Match"), hasTarget() && analyzer (param01 (analyzerParam)) != Analyzer::off,
  506. capture == Capture::both, [this] {
  507. if (capture == Capture::both) capture = Capture::none;
  508. else { capture = Capture::both; matchRef.clear(); matchCur.clear(); refFrames = curFrames = 0; }
  509. repaint();
  510. });
  511. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  512. }
  513. static const char* listenLabel (Listen l)
  514. {
  515. return l == Listen::eq ? TR_NOOP ("EQ") : l == Listen::source ? TR_NOOP ("Source") : TR_NOOP ("Target");
  516. }
  517. bool showTarget = true;
  518. // round 152: EQ match — capture the reference (play it through this slot), capture this track, then match
  519. enum class Capture { none, reference, current, both }; // (round 166f: both = this track and the target at once)
  520. juce::String matchStateText() const
  521. {
  522. const auto secs = [] (int frames) { return juce::String (frames / 30); }; // the editor's 30 Hz frames
  523. if (capture == Capture::reference) return tr ("reference {0} s", { secs (refFrames) });
  524. if (capture == Capture::current) return tr ("this track {0} s", { secs (curFrames) });
  525. if (capture == Capture::both) return tr ("this track and the target {0} s", { secs (curFrames) });
  526. if (refFrames > 0 && curFrames > 0) return tr ("ready");
  527. if (refFrames > 0) return tr ("reference captured");
  528. return tr ("off");
  529. }
  530. void showMatchMenu()
  531. {
  532. juce::PopupMenu menu;
  533. if (analyzer (param01 (analyzerParam)) == Analyzer::off)
  534. menu.addItem (tr ("(turn the analyzer on to capture spectra)"), false, false, [] {});
  535. menu.addItem (capture == Capture::reference ? tr ("Stop capturing the reference") : tr ("Capture the reference (play it through this slot now)"), true,
  536. capture == Capture::reference, [this] {
  537. if (capture == Capture::reference) capture = Capture::none;
  538. else { capture = Capture::reference; matchRef.clear(); refFrames = 0; }
  539. repaint();
  540. });
  541. if (capture == Capture::both) // round 166f: started from the Compare menu
  542. menu.addItem (tr ("Stop capturing this track and the target"), true, true, [this] { capture = Capture::none; repaint(); });
  543. menu.addItem (capture == Capture::current ? tr ("Stop capturing this track") : tr ("Capture this track (play it now)"), true,
  544. capture == Capture::current, [this] {
  545. if (capture == Capture::current) capture = Capture::none;
  546. else { capture = Capture::current; matchCur.clear(); curFrames = 0; }
  547. repaint();
  548. });
  549. menu.addSeparator();
  550. const bool ready = refFrames > 0 && curFrames > 0 && capture == Capture::none;
  551. for (const int amount : { 100, 75, 50, 25 })
  552. menu.addItem (tr ("Match {0} %", { juce::String (amount) }), ready, false, [this, amount] { runMatch (amount / 100.0); });
  553. juce::PopupMenu bandsMenu;
  554. for (const int nb : { 2, 4, 6, 8 })
  555. bandsMenu.addItem (tr ("Up to {0} bands", { juce::String (nb) }), true, matchBands == nb, [this, nb] { matchBands = nb; });
  556. menu.addSubMenu (tr ("Bands"), bandsMenu);
  557. juce::PopupMenu smooth;
  558. for (const int den : { 12, 6, 3, 1 })
  559. smooth.addItem (tr ("Smoothing 1/{0} octave", { juce::String (den) }), true, matchSmoothing == den, [this, den] { matchSmoothing = den; });
  560. menu.addSubMenu (tr ("Smoothing"), smooth);
  561. menu.addItem (tr ("Clear the captures"), refFrames > 0 || curFrames > 0, false, [this] {
  562. capture = Capture::none; matchRef.clear(); matchCur.clear(); refFrames = curFrames = 0; repaint();
  563. });
  564. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  565. }
  566. void runMatch (double amount)
  567. {
  568. const auto* fx = current();
  569. if (fx == nullptr || matchRef.size() != (std::size_t) (fftSize / 2) || matchCur.size() != matchRef.size()) return;
  570. const auto sr = displayRate();
  571. constexpr int n = 240;
  572. std::vector<double> f (n), cur (n), ref (n);
  573. for (int i = 0; i < n; ++i)
  574. {
  575. f[(std::size_t) i] = 20.0 * std::pow (1000.0, (double) i / (n - 1));
  576. // the averaged power over the bins of this point's 1/24 octave
  577. const auto band = [&] (const std::vector<double>& acc, int frames) {
  578. const auto k0 = juce::jlimit (1, fftSize / 2 - 1, (int) std::floor (f[(std::size_t) i] * std::pow (2.0, -1.0 / 48.0) * fftSize / sr));
  579. const auto k1 = juce::jlimit (k0, fftSize / 2 - 1, (int) std::ceil (f[(std::size_t) i] * std::pow (2.0, 1.0 / 48.0) * fftSize / sr));
  580. double s = 0.0;
  581. for (int k = k0; k <= k1; ++k) s += acc[(std::size_t) k];
  582. return 10.0 * std::log10 (s / (k1 - k0 + 1) / juce::jmax (1, frames) + 1e-20);
  583. };
  584. cur[(std::size_t) i] = band (matchCur, curFrames);
  585. ref[(std::size_t) i] = band (matchRef, refFrames);
  586. }
  587. std::vector<std::pair<int, float>> changes;
  588. if (facet::matchEq (fx->params, f.data(), cur.data(), ref.data(), n, amount, matchBands, 1.0 / matchSmoothing, changes) > 0)
  589. set (changes); // one undo step
  590. repaint();
  591. }
  592. void showAnalyzerMenu()
  593. {
  594. juce::PopupMenu menu;
  595. const auto a = (int) analyzer (param01 (analyzerParam));
  596. for (int k = 0; k < 4; ++k)
  597. menu.addItem (tr (analyzerNames[k]), true, a == k, [this, k] { set ({ { analyzerParam, analyzerValue ((Analyzer) k) } }, false); });
  598. // round 128: the analyser's range, speed, tilt and freeze (Pro-Q's analyser settings)
  599. menu.addSeparator();
  600. juce::PopupMenu range, speed, tilt;
  601. for (int k = 0; k < 3; ++k)
  602. range.addItem (juce::String (60 + 30 * k) + " dB", true, (int) analyzerRangeDb (param01 (specRangeParam)) == 60 + 30 * k,
  603. [this, k] { set ({ { specRangeParam, (float) k / 2.0f } }, false); });
  604. static const char* speeds[] = { TR_NOOP ("Very slow"), TR_NOOP ("Slow"), TR_NOOP ("Medium"), TR_NOOP ("Fast"), TR_NOOP ("Very fast") };
  605. for (int k = 0; k < 5; ++k)
  606. {
  607. speed.addItem (tr (speeds[k]), true, analyzerSpeed (param01 (specSpeedParam)) == k, [this, k] { set ({ { specSpeedParam, (float) k / 4.0f } }, false); });
  608. tilt.addItem (juce::String (1.5 * k, 1) + " dB/oct", true, std::abs (analyzerTiltDb (param01 (specTiltParam)) - 1.5 * k) < 0.01,
  609. [this, k] { set ({ { specTiltParam, (float) k / 4.0f } }, false); });
  610. }
  611. menu.addSubMenu (tr ("Range"), range);
  612. menu.addSubMenu (tr ("Speed"), speed);
  613. menu.addSubMenu (tr ("Tilt"), tilt);
  614. menu.addItem (tr ("Freeze"), true, frozen, [this] { frozen = ! frozen; repaint(); });
  615. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  616. }
  617. void showShapeMenu (int b)
  618. {
  619. juce::PopupMenu menu;
  620. const auto sh = bandShape (b);
  621. for (int k = 1; k < shapeCount; ++k)
  622. menu.addItem (tr (shapeNames[k]), true, (int) sh == k, [this, b, k] { set ({ { param (b, 0), shapeValue ((Shape) k) } }); });
  623. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  624. }
  625. void showPlacementMenu (int b)
  626. {
  627. juce::PopupMenu menu;
  628. for (int k = 0; k < placementCount; ++k)
  629. menu.addItem (placementLabel ((Placement) k), true, (int) bandPlacement (b) == k, [this, b, k] { set ({ { placementParam0 + b, placementValue ((Placement) k) } }); });
  630. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  631. }
  632. // right-click on OUT: the value menu plus the output options (polarity, pan mode)
  633. void showOutputMenu()
  634. {
  635. juce::PopupMenu menu;
  636. const auto spec = specFor (outputParam);
  637. menu.addSectionHeader (spec.name + ": " + spec.text (param01 (outputParam)));
  638. ui::addValueItems (menu, *this, &engine, spec, param01 (outputParam), [this] (double v) { set ({ { outputParam, (float) v } }); });
  639. menu.addSeparator();
  640. menu.addItem (tr ("Invert polarity"), true, param01 (invertParam) >= 0.5f, [this] { set ({ { invertParam, param01 (invertParam) >= 0.5f ? 0.0f : 1.0f } }); });
  641. menu.addItem (tr ("Auto gain"), true, param01 (autoGainParam) >= 0.5f, [this] { set ({ { autoGainParam, param01 (autoGainParam) >= 0.5f ? 0.0f : 1.0f } }); });
  642. juce::PopupMenu mode;
  643. mode.addItem (tr ("Pan: left / right"), true, ! panMidSide (param01 (panModeParam)), [this] { set ({ { panModeParam, 0.0f } }); });
  644. mode.addItem (tr ("Pan: mid / side"), true, panMidSide (param01 (panModeParam)), [this] { set ({ { panModeParam, 1.0f } }); });
  645. menu.addSubMenu (tr ("Pan mode"), mode);
  646. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  647. }
  648. void showSlopeMenu (int b)
  649. {
  650. juce::PopupMenu menu;
  651. for (const auto d : facet::slopes)
  652. menu.addItem (juce::String (d) + " dB/oct", true, slopeDb (param01 (param (b, 4))) == d, [this, b, d] { set ({ { param (b, 4), slopeValue (d) } }); });
  653. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  654. }
  655. // --- mouse ---------------------------------------------------------------------------------------
  656. void showPhaseMenu()
  657. {
  658. juce::PopupMenu menu;
  659. const auto m = (int) phase (param01 (phaseParam));
  660. for (int k = 0; k < phaseCount; ++k)
  661. menu.addItem (tr (phaseNames[k]) + (k == 0 ? juce::String() : " (" + tr ("latency {0} samples", { juce::String (latencyFor ((Phase) k, displayRate(), facet::resolution (param01 (resolutionParam)))) }) + ")"),
  662. true, m == k, [this, k] { set ({ { phaseParam, phaseValue ((Phase) k) } }); });
  663. // round 144: the linear-phase resolution (the reference's Low … Maximum): a longer FIR reaches lower
  664. // frequencies, at the cost of latency and CPU
  665. menu.addSectionHeader (tr ("Linear phase resolution"));
  666. static const char* const resolutions[] { TR_NOOP ("Low resolution"), TR_NOOP ("Medium resolution"), TR_NOOP ("High resolution"), TR_NOOP ("Very high resolution"), TR_NOOP ("Maximum resolution") };
  667. const auto res = facet::resolution (param01 (resolutionParam));
  668. for (int k = 0; k < facet::resolutionCount; ++k)
  669. menu.addItem (tr (resolutions[k]) + " (" + tr ("latency {0} samples", { juce::String (latencyFor (Phase::linear, displayRate(), k)) }) + ")",
  670. true, res == k, [this, k] { set ({ { resolutionParam, facet::resolutionValue (k) } }); });
  671. // round 135: the character (the reference's Clean / Subtle / Warm)
  672. menu.addSectionHeader (tr ("Character"));
  673. static const char* const characters[] { TR_NOOP ("Clean"), TR_NOOP ("Subtle"), TR_NOOP ("Warm") };
  674. const auto ch = facet::character (param01 (characterParam));
  675. for (int k = 0; k < 3; ++k)
  676. menu.addItem (tr (characters[k]), true, ch == k, [this, k] { set ({ { characterParam, (float) k / 2.0f } }); });
  677. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  678. }
  679. // right-click on the graph away from the nodes: every band at once
  680. void showGraphMenu()
  681. {
  682. juce::PopupMenu menu;
  683. menu.addItem (tr ("Invert all gains"), [this] {
  684. std::vector<std::pair<int, float>> c;
  685. for (int b = 0; b < bands; ++b)
  686. if (bandOn (b)) c.push_back ({ param (b, 2), 1.0f - param01 (param (b, 2)) });
  687. set (c);
  688. });
  689. menu.addItem (tr ("Remove all bands"), [this] {
  690. std::vector<std::pair<int, float>> c { { soloParam, 0.0f } };
  691. for (int b = 0; b < bands; ++b) { c.push_back ({ param (b, 0), 0.0f }); c.push_back ({ rangeParam0 + b, 0.5f }); }
  692. set (c);
  693. selected = -1;
  694. });
  695. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  696. }
  697. void stepBand (int dir)
  698. {
  699. for (int k = 1; k <= bands; ++k)
  700. {
  701. const auto b = ((selected < 0 ? 0 : selected) + dir * k + bands * 2) % bands;
  702. if (bandOn (b)) { selected = b; repaint(); return; }
  703. }
  704. }
  705. void mouseDown (const juce::MouseEvent& e) override
  706. {
  707. const auto p = e.position;
  708. dragging = Drag::none;
  709. // the preset bar in the header (round 129's undo / redo, A / B, presets; round 128 wires it into Facet)
  710. if (const auto part = bar.partAt (p); part != pluginheader::Part::none)
  711. {
  712. bar.click (part, *this);
  713. return;
  714. }
  715. bar.beginGesture();
  716. if (e.mods.isPopupMenu() && headerArea().contains (p)) // round 126: the editor's style
  717. {
  718. juce::PopupMenu m;
  719. skin::addStyleMenu (m, skin::styleKey (EffectKind::facetEq), [safe = juce::Component::SafePointer<juce::Component> (this)] {
  720. if (safe != nullptr) safe->repaint();
  721. });
  722. m.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withMousePosition());
  723. return;
  724. }
  725. std::vector<Control> all;
  726. if (const auto* c = controlAt (p, all))
  727. {
  728. const auto type = c->type;
  729. const auto prm = c->param;
  730. const auto index = c->index;
  731. if (e.mods.isPopupMenu())
  732. {
  733. if (type == Ctl::analyzer) { showAnalyzerMenu(); return; }
  734. if (type == Ctl::detector) { showDetectorMenu(); return; }
  735. if (type == Ctl::phase) { showPhaseMenu(); return; }
  736. if (type == Ctl::output) { showOutputMenu(); return; }
  737. if (type == Ctl::placementMenu) { showPlacementMenu (selected); return; }
  738. if (type == Ctl::spectral) { showDensityMenu (selected); return; }
  739. if (type == Ctl::match) { showMatchMenu(); return; }
  740. if (type == Ctl::spectralTilt) return;
  741. if (type == Ctl::listen || type == Ctl::compareMenu) { showCompareMenu(); return; } // round 166f
  742. if (prm >= 0 && type != Ctl::solo && type != Ctl::dynamic && type != Ctl::shapeMenu && type != Ctl::slopeMenu && type != Ctl::power
  743. && type != Ctl::autoGain)
  744. ui::showValueMenu (*this, &engine, specFor (prm), param01 (prm), [this, prm] (double v) { set ({ { prm, (float) v } }); });
  745. return;
  746. }
  747. switch (type)
  748. {
  749. case Ctl::shapeMenu: showShapeMenu (selected); return;
  750. case Ctl::slopeMenu: showSlopeMenu (selected); return;
  751. case Ctl::remove: removeBand (selected); return;
  752. case Ctl::solo: toggleSolo (selected); return;
  753. case Ctl::tipSolo: toggleSolo (index); return;
  754. case Ctl::tipPower: case Ctl::power: { const auto b = type == Ctl::power ? selected : index; set ({ { powerParam0 + b, bandPowered (b) ? 1.0f : 0.0f } }); return; }
  755. case Ctl::autoGain: set ({ { prm, param01 (prm) >= 0.5f ? 0.0f : 1.0f } }); return;
  756. case Ctl::placementMenu: showPlacementMenu (selected); return;
  757. case Ctl::tipRemove: removeBand (index); tipBand = -1; return;
  758. case Ctl::prevBand: stepBand (-1); return;
  759. case Ctl::nextBand: stepBand (1); return;
  760. case Ctl::close: selected = -1; repaint(); return;
  761. case Ctl::piano: piano = ! piano; repaint(); return;
  762. case Ctl::dynamic: set ({ { prm, isDynamic (current()->params, selected) ? 0.5f : rangeValue (-6.0) } }); return;
  763. case Ctl::spectral: case Ctl::spectralTilt: set ({ { prm, param01 (prm) >= 0.5f ? 0.0f : 1.0f } }); return;
  764. case Ctl::match: showMatchMenu(); return;
  765. case Ctl::compareMenu: showCompareMenu(); return; // round 166f
  766. case Ctl::listen:
  767. {
  768. const auto l = (Listen) juce::jlimit (0, 2, index);
  769. if (l == Listen::target && ! hasTarget()) { showCompareMenu (true); return; } // pick the target first
  770. set ({ { listenParam, listenValue (l) } }, false);
  771. return;
  772. }
  773. case Ctl::phase: showPhaseMenu(); return;
  774. case Ctl::analyzer: showAnalyzerMenu(); return;
  775. case Ctl::detector: showDetectorMenu(); return;
  776. case Ctl::scaleDb:
  777. {
  778. const auto r = viewRange();
  779. set ({ { displayRangeParam, displayRangeValue (r >= 30.0 ? 3.0 : r >= 12.0 ? 30.0 : r >= 6.0 ? 12.0 : 6.0) } }, false);
  780. return;
  781. }
  782. default: break;
  783. }
  784. if (prm >= 0)
  785. {
  786. if (ui::isResetClick (e))
  787. {
  788. set ({ { prm, (float) specFor (prm).defaultValue } });
  789. return;
  790. }
  791. dragging = Drag::control;
  792. dragParam = prm;
  793. dragRect = c->r;
  794. valueDrag.begin (e, param01 (prm));
  795. }
  796. return;
  797. }
  798. if (panelShown() && panelArea().contains (p)) return; // the panel's background
  799. if (! plotArea().contains (p)) return;
  800. if (const auto rb = rangeHandleAt (p); rb >= 0 && ! e.mods.isPopupMenu())
  801. {
  802. dragging = Drag::range;
  803. dragBand = rb;
  804. return;
  805. }
  806. const auto b = nodeAt (p);
  807. if (b >= 0)
  808. {
  809. selected = b;
  810. if (e.mods.isPopupMenu()) { showBandMenu (b); return; }
  811. if (e.mods.isAltDown()) { removeBand (b); return; } // Alt-click deletes, as in the common EQs
  812. if (e.mods.isMiddleButtonDown()) { set ({ { param (b, 2), 0.5f } }); return; }
  813. dragging = Drag::node;
  814. dragBand = b;
  815. dragOffset = nodePosition (b) - p;
  816. dragStart = p;
  817. repaint();
  818. return;
  819. }
  820. if (e.mods.isPopupMenu()) { showGraphMenu(); return; }
  821. selected = -1; // a click on the empty graph closes the band panel
  822. repaint();
  823. }
  824. void mouseDrag (const juce::MouseEvent& e) override
  825. {
  826. if (dragging == Drag::control && dragParam >= 0)
  827. {
  828. const auto spec = specFor (dragParam);
  829. const auto v = valueDrag.update (e, spec);
  830. set ({ { dragParam, (float) v } });
  831. ui::showValueTip (*this, dragRect.toNearestInt(), spec.text (v));
  832. return;
  833. }
  834. if (dragging == Drag::node && bandOn (dragBand))
  835. {
  836. auto p = e.position + dragOffset;
  837. const auto b = dragBand;
  838. std::vector<std::pair<int, float>> c;
  839. const auto fine = e.mods.isCtrlDown() || e.mods.isCommandDown();
  840. if (fine) // fine: a quarter of the mouse movement
  841. p = nodePosition (b) + (p - nodePosition (b)) * 0.25f;
  842. const auto d = e.position - dragStart;
  843. const auto lockX = e.mods.isShiftDown() && std::abs (d.y) > std::abs (d.x); // Shift: only the main direction
  844. const auto lockY = e.mods.isShiftDown() && ! lockX;
  845. if (! lockX) c.push_back ({ param (b, 1), hzValue (hzForX (p.x)) });
  846. if (hasGain (bandShape (b)) && ! lockY)
  847. {
  848. const auto scale = juce::jmax (0.01, gainScale (param01 (scaleParam)));
  849. c.push_back ({ param (b, 2), gainValue (juce::jlimit (-30.0, 30.0, dbForY (p.y) / scale)) });
  850. }
  851. set (c);
  852. ui::showValueTip (*this, juce::Rectangle<int> (16, 16).withCentre (nodePosition (b).toInt()), bandSummary (b));
  853. return;
  854. }
  855. if (dragging == Drag::range && bandOn (dragBand))
  856. {
  857. const auto b = dragBand;
  858. const auto scale = juce::jmax (0.01, gainScale (param01 (scaleParam)));
  859. const auto r = dbForY (e.position.y) / scale - gainDb (param01 (param (b, 2)));
  860. set ({ { rangeParam0 + b, rangeValue (juce::jlimit (-30.0, 30.0, r)) } });
  861. ui::showValueTip (*this, juce::Rectangle<int> (16, 16).withCentre (rangeHandle (b).toInt()), tr ("Range") + " " + valueText (rangeParam0 + b));
  862. }
  863. }
  864. void mouseUp (const juce::MouseEvent&) override
  865. {
  866. dragging = Drag::none;
  867. valueDrag.end();
  868. ui::hideValueTip();
  869. }
  870. void mouseDoubleClick (const juce::MouseEvent& e) override
  871. {
  872. if (! plotArea().contains (e.position)) return;
  873. std::vector<Control> all;
  874. if (controlAt (e.position, all) != nullptr) return; // (round 166f: e.g. the Compare switch over the graph)
  875. if (const auto b = nodeAt (e.position); b >= 0) removeBand (b);
  876. else addBand (e.position);
  877. }
  878. void mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel) override
  879. {
  880. std::vector<Control> all;
  881. if (const auto* c = controlAt (e.position, all); c != nullptr && c->param >= 0 && c->type != Ctl::phase && c->type != Ctl::power
  882. && c->type != Ctl::autoGain && c->type != Ctl::placementMenu && c->type != Ctl::spectral && c->type != Ctl::spectralTilt && c->type != Ctl::match && c->type != Ctl::listen)
  883. {
  884. const auto prm = c->param;
  885. const auto spec = specFor (prm);
  886. if (const auto v = ui::wheelValue (param01 (prm), e, wheel, spec))
  887. {
  888. set ({ { prm, (float) *v } });
  889. ui::flashValueTip (*this, c->r.toNearestInt(), spec.text (*v));
  890. }
  891. return;
  892. }
  893. // over the graph: the Q of the band under the mouse (or the selected one)
  894. if (! plotArea().contains (e.position)) return;
  895. auto b = nodeAt (e.position);
  896. if (b < 0) b = selected;
  897. if (! bandOn (b) || wheel.deltaY == 0.0f) return;
  898. const auto step = (e.mods.isCtrlDown() ? 0.04 : 0.15) * (wheel.deltaY > 0 ? 1.0 : -1.0);
  899. const auto q0 = q (param01 (param (b, 3)));
  900. set ({ { param (b, 3), qValue (juce::jlimit (0.025, 40.0, q0 * std::exp (step * 2.0))) } });
  901. ui::flashValueTip (*this, juce::Rectangle<int> (16, 16).withCentre (nodePosition (b).toInt()), "Q " + valueText (param (b, 3)));
  902. }
  903. void mouseMove (const juce::MouseEvent& e) override
  904. {
  905. mouse = e.position;
  906. bar.hover (e.position);
  907. const auto h = nodeAt (e.position);
  908. if (h != hover) hover = h;
  909. // the read-out box stays while the mouse is on its node or inside the box (to reach solo / remove)
  910. if (h >= 0) tipBand = h;
  911. else if (tipBand >= 0 && ! (bandOn (tipBand) && tipArea (tipBand).expanded (4.0f).contains (e.position))) tipBand = -1;
  912. repaint();
  913. }
  914. void mouseExit (const juce::MouseEvent&) override
  915. {
  916. mouse = { -1.0f, -1.0f };
  917. bar.hoverExit();
  918. hover = -1;
  919. tipBand = -1;
  920. repaint();
  921. }
  922. bool keyPressed (const juce::KeyPress& key) override
  923. {
  924. if ((key == juce::KeyPress::deleteKey || key == juce::KeyPress::backspaceKey) && bandOn (selected))
  925. {
  926. removeBand (selected);
  927. return true;
  928. }
  929. if (key.getTextCharacter() == 's' && bandOn (selected))
  930. {
  931. toggleSolo (selected);
  932. return true;
  933. }
  934. return false;
  935. }
  936. juce::String bandSummary (int b) const
  937. {
  938. juce::String t = tr (shapeNames[(int) bandShape (b)]) + " " + hzText (hz (param01 (param (b, 1))));
  939. if (hasGain (bandShape (b))) t << " " << valueText (param (b, 2));
  940. t << " Q " << valueText (param (b, 3));
  941. return t;
  942. }
  943. ui::Hint hintAt (juce::Point<int> position) override
  944. {
  945. ui::Hint h;
  946. const auto p = position.toFloat();
  947. if (const auto part = bar.partAt (p); part != pluginheader::Part::none)
  948. return { bar.hintFor (part), bar.presetLabel(), false, true };
  949. std::vector<Control> all;
  950. if (const auto* c = controlAt (p, all))
  951. {
  952. switch (c->type)
  953. {
  954. case Ctl::phase: h.text = tr ("Processing: zero latency (minimum phase), natural phase (the analog curve's phase) or linear phase (no phase shift); the latency is reported to PDC"); h.hasMenu = true; return h;
  955. case Ctl::analyzer: h.text = tr ("Spectrum analyzer: off, pre, post or pre + post"); h.hasMenu = true; return h;
  956. case Ctl::piano: h.text = tr ("Frequency scale: numbers or a piano keyboard"); return h;
  957. case Ctl::prevBand: case Ctl::nextBand: h.text = tr ("Previous / next band"); return h;
  958. case Ctl::close: h.text = tr ("Close the band panel (click on the empty graph does the same)"); return h;
  959. case Ctl::tipSolo: h.text = tr ("Solo: hear only this band's region (S)"); return h;
  960. case Ctl::tipRemove: h.text = tr ("Remove the band (Delete)"); return h;
  961. case Ctl::tipPower: case Ctl::power: h.text = tr ("Band on / off: a band switched off keeps its settings"); return h;
  962. case Ctl::placementMenu: h.text = tr ("Stereo placement: the band works on both channels, the left or right one, or the mid or side signal"); h.hasMenu = true; return h;
  963. case Ctl::autoGain: h.text = tr ("Auto gain: takes back the EQ curve's overall level change so louder never fools the ear"); return h;
  964. case Ctl::output: h.text = tr ("Output level - right-click: polarity, auto gain, pan mode"); h.value = valueText (outputParam); h.hasMenu = true; return h;
  965. case Ctl::detector: h.text = tr ("What the dynamic bands listen to: the EQ's input or a sidechain track"); h.hasMenu = true; return h;
  966. case Ctl::scaleDb: h.text = tr ("Graph scale: click to switch between 3, 6, 12 and 30 dB"); return h;
  967. case Ctl::remove: h.text = tr ("Remove the band (Delete)"); return h;
  968. case Ctl::solo: h.text = tr ("Solo: hear only this band's region (S)"); return h;
  969. case Ctl::dynamic: h.text = tr ("Dynamic EQ: the band's gain moves by up to its range when its region goes over the threshold"); return h;
  970. case Ctl::spectral: h.text = tr ("Spectral dynamics: the band works frequency by frequency, only where the sound goes over the threshold (adds 5120 samples of latency) - right-click: density"); h.hasMenu = true; return h;
  971. case Ctl::spectralTilt: h.text = tr ("Tilt: the spectral threshold slopes 4.5 dB/oct so the band treats a natural (pink) spectrum evenly"); return h;
  972. case Ctl::listen:
  973. case Ctl::compareMenu:
  974. h.text = tr ("Compare: listen to the EQ, to this track without it (source) or to another track (target), level-matched; right-click or the arrow = target track and options")
  975. + (hasTarget() ? " (" + tr ("target") + " " + targetName() + ")" : juce::String());
  976. h.hasMenu = true;
  977. return h;
  978. case Ctl::match: h.text = tr ("EQ match: capture a reference spectrum and this track's, then let Facet place bands that make this track sound like the reference") + " (" + matchStateText() + ")"; h.hasMenu = true; return h;
  979. case Ctl::shapeMenu: h.text = tr ("Band shape"); h.hasMenu = true; return h;
  980. case Ctl::slopeMenu: h.text = tr ("Slope"); h.hasMenu = true; return h;
  981. default: break;
  982. }
  983. if (c->param >= 0)
  984. return ui::valueHint (specFor (c->param), param01 (c->param));
  985. return h;
  986. }
  987. if (plotArea().contains (p))
  988. {
  989. if (const auto b = nodeAt (p); b >= 0)
  990. h.text = tr ("Band {0}", { juce::String (b + 1) }) + ": " + bandSummary (b) + " - "
  991. + tr ("drag = frequency and gain (Ctrl fine, Shift one direction only); wheel = Q; Alt-click or double-click = remove; right-click = menu");
  992. else if (rangeHandleAt (p) >= 0)
  993. h.text = tr ("Dynamic range: drag up or down");
  994. else
  995. h.text = tr ("Double-click to add a band; mouse wheel = Q of the selected band");
  996. h.hasMenu = true;
  997. }
  998. return h;
  999. }
  1000. // --- painting ------------------------------------------------------------------------------------
  1001. void paint (juce::Graphics& g) override
  1002. {
  1003. using namespace theme;
  1004. const skin::StyleScope styleScope (skin::styleFor (EffectKind::facetEq), skin::Art::facet); // round 126: one style per plugin (170j: Themed)
  1005. g.fillAll (colour::bg0);
  1006. const auto* fx = current();
  1007. if (fx == nullptr)
  1008. {
  1009. g.setColour (colour::textSecondary);
  1010. g.setFont (font (type::body));
  1011. g.drawText (tr ("The effect was removed from this slot."), getLocalBounds(), juce::Justification::centred);
  1012. return;
  1013. }
  1014. auto all = controls();
  1015. paintGraph (g, *fx);
  1016. paintHeader (g, *fx);
  1017. paintBar (g, *fx, all);
  1018. if (panelShown()) paintPanel (g, *fx, all);
  1019. if (tipBand >= 0 && bandOn (tipBand) && dragging == Drag::none) paintTip (g, *fx, tipBand);
  1020. }
  1021. void paintHeader (juce::Graphics& g, const EffectSlot& fx)
  1022. {
  1023. using namespace theme;
  1024. const auto h = headerArea();
  1025. skin::headerBar (g, h, colour::accent);
  1026. skin::brandMark (g, juce::Rectangle<float> (8.0f, 0.0f, 20.0f, h.getHeight()));
  1027. skin::powerLed (g, { 36.0f, h.getCentreY() }, 7.0f, fx.enabled);
  1028. const auto w = skin::titleText (g, juce::Rectangle<float> (46.0f, 0.0f, 120.0f, h.getHeight()), "Facet EQ", colour::accent);
  1029. g.setColour (colour::textSecondary);
  1030. g.setFont (font (type::small));
  1031. g.drawText (trackName() + " - " + tr ("slot {0}", { juce::String (slot + 1) }), juce::Rectangle<float> (58.0f + w, 0.0f, 300.0f, h.getHeight()),
  1032. juce::Justification::centredLeft, true);
  1033. // the preset bar (undo / redo, A / B, presets) on the right, the graph's gestures between
  1034. const auto left = bar.place (h.withTrimmedLeft (w + 300.0f));
  1035. bar.paint (g, colour::accent);
  1036. g.setColour (colour::textDisabled);
  1037. g.setFont (font (type::caption));
  1038. g.drawText (tr ("Double-click: new band - drag: frequency / gain - wheel: Q - right-click: menu"),
  1039. left.withTrimmedRight (8.0f), juce::Justification::centredRight, true);
  1040. }
  1041. void paintBar (juce::Graphics& g, const EffectSlot& fx, const std::vector<Control>& all)
  1042. {
  1043. using namespace theme;
  1044. const auto bar = barArea();
  1045. skin::bottomBar (g, bar);
  1046. for (const auto& c : all)
  1047. {
  1048. const auto hover = c.r.contains (mouse);
  1049. if (c.type == Ctl::piano)
  1050. {
  1051. g.setColour (piano ? colour::accent : (hover ? colour::textPrimary : colour::textSecondary));
  1052. const auto k = c.r.reduced (4.0f, 4.0f);
  1053. for (int i = 0; i < 4; ++i)
  1054. g.drawRect (juce::Rectangle<float> (k.getX() + k.getWidth() * (float) i / 4.0f, k.getY(), k.getWidth() / 4.0f, k.getHeight()), 1.0f);
  1055. for (int i = 1; i < 4; ++i)
  1056. g.fillRect (juce::Rectangle<float> (k.getX() + k.getWidth() * (float) i / 4.0f - 1.5f, k.getY(), 3.0f, k.getHeight() * 0.55f));
  1057. }
  1058. else if (c.type == Ctl::match) // round 152: lit while it captures
  1059. {
  1060. const auto on = capture != Capture::none;
  1061. skin::button (g, c.r, on, hover, colour::accent);
  1062. g.setColour (on || (refFrames > 0 && curFrames > 0) ? colour::textPrimary : colour::textSecondary);
  1063. g.setFont (font (type::small, on));
  1064. g.drawText (tr ("Match"), c.r, juce::Justification::centred);
  1065. }
  1066. else if (c.type == Ctl::listen) // round 166f: EQ | Source | Target
  1067. {
  1068. const auto l = (Listen) c.index;
  1069. const auto on = listen (param01 (listenParam)) == l;
  1070. const auto usable = l != Listen::target || hasTarget();
  1071. const auto tint = l == Listen::target ? colour::info : l == Listen::source ? colour::warning : colour::accent;
  1072. g.setColour (colour::bg0.withAlpha (0.75f));
  1073. g.fillRoundedRectangle (c.r, radius::sm);
  1074. skin::button (g, c.r, on, hover, tint);
  1075. g.setColour (on ? colour::textPrimary : usable ? colour::textSecondary : colour::textDisabled);
  1076. g.setFont (font (type::small, on));
  1077. g.drawFittedText (tr (listenLabel (l)), c.r.toNearestInt(), juce::Justification::centred, 1, 0.7f);
  1078. }
  1079. else if (c.type == Ctl::compareMenu)
  1080. {
  1081. g.setColour (colour::bg0.withAlpha (0.75f));
  1082. g.fillRoundedRectangle (c.r, radius::sm);
  1083. skin::button (g, c.r, capture == Capture::both, hover, colour::info);
  1084. g.setColour (colour::textSecondary);
  1085. g.setFont (font (type::small));
  1086. g.drawText (juce::String::fromUTF8 ("\xe2\x96\xbe"), c.r, juce::Justification::centred);
  1087. }
  1088. else if (c.type == Ctl::phase || c.type == Ctl::analyzer || c.type == Ctl::detector)
  1089. {
  1090. juce::String caption, value;
  1091. if (c.type == Ctl::phase)
  1092. {
  1093. caption = tr ("Processing:");
  1094. value = tr (phaseNames[(int) phase (param01 (phaseParam))]);
  1095. static const char* const characters[] { TR_NOOP ("Clean"), TR_NOOP ("Subtle"), TR_NOOP ("Warm") };
  1096. if (const auto ch = facet::character (param01 (characterParam)); ch > 0) value << juce::String::fromUTF8 (" · ") << tr (characters[ch]);
  1097. static const char* const resShort[] { TR_NOOP ("Low resolution"), TR_NOOP ("Medium resolution"), TR_NOOP ("High resolution"), TR_NOOP ("Very high resolution"), TR_NOOP ("Maximum resolution") };
  1098. if (phase (param01 (phaseParam)) == Phase::linear) value << juce::String::fromUTF8 (" · ") << tr (resShort[facet::resolution (param01 (resolutionParam))]); // round 144
  1099. }
  1100. else if (c.type == Ctl::analyzer) { caption = tr ("Analyzer:"); value = tr (analyzerShort[(int) analyzer (param01 (analyzerParam))]); }
  1101. else
  1102. {
  1103. caption = tr ("Detector:");
  1104. const auto side = sidechainDetector (param01 (detectorParam));
  1105. value = side ? tr ("Sidechain") : tr ("Input");
  1106. if (side && fx.sidechain > 0 && fx.sidechain < (int) engine.getSong().mixer.size()) value << " " << juce::String (fx.sidechain);
  1107. }
  1108. if (hover)
  1109. {
  1110. g.setColour (juce::Colours::white.withAlpha (0.06f));
  1111. g.fillRoundedRectangle (c.r, radius::md);
  1112. }
  1113. g.setFont (font (type::small));
  1114. g.setColour (colour::textDisabled);
  1115. const auto cw = juce::GlyphArrangement::getStringWidth (g.getCurrentFont(), caption) + 6.0f;
  1116. g.drawText (caption, c.r.withWidth (cw).translated (6.0f, 0.0f), juce::Justification::centredLeft);
  1117. g.setColour (colour::textPrimary);
  1118. g.setFont (font (type::small, true));
  1119. g.drawText (value + juce::String::fromUTF8 (" \xe2\x96\xbe"), c.r.withTrimmedLeft (cw + 6.0f), juce::Justification::centredLeft, true);
  1120. }
  1121. else if (c.type == Ctl::autoGain)
  1122. {
  1123. const auto on = param01 (autoGainParam) >= 0.5f;
  1124. skin::button (g, c.r, on, hover, colour::accent);
  1125. g.setColour (on ? colour::textPrimary : colour::textSecondary);
  1126. g.setFont (font (type::small, on));
  1127. g.drawText (tr ("Auto gain"), c.r, juce::Justification::centred);
  1128. }
  1129. else if (c.type == Ctl::output || c.type == Ctl::scale || c.type == Ctl::pan)
  1130. {
  1131. skin::valuePlate (g, c.r, radius::md);
  1132. if (hover)
  1133. {
  1134. g.setColour (colour::accent.withAlpha (0.5f));
  1135. g.drawRoundedRectangle (c.r.reduced (0.5f), radius::md, 1.0f);
  1136. }
  1137. g.setColour (colour::textDisabled);
  1138. g.setFont (font (type::caption));
  1139. g.drawText (c.type == Ctl::output ? tr ("OUT") : c.type == Ctl::pan ? (panMidSide (param01 (panModeParam)) ? tr ("M/S") : tr ("PAN")) : tr ("SCALE"),
  1140. c.r.reduced (6.0f, 0.0f), juce::Justification::centredLeft);
  1141. g.setColour (colour::textPrimary);
  1142. g.setFont (font (type::small, true));
  1143. g.drawText (valueText (c.param), c.r.reduced (6.0f, 0.0f), juce::Justification::centredRight);
  1144. }
  1145. }
  1146. // latency of the processing mode (PDC compensates it)
  1147. auto lat = latencyFor (phase (param01 (phaseParam)), displayRate(), facet::resolution (param01 (resolutionParam)));
  1148. for (int b = 0; b < bands; ++b) // round 152: a spectral band adds the STFT's latency
  1149. if (isSpectral (fx.params, b)) { lat += spectralLatency (displayRate()); break; }
  1150. if (lat > 0)
  1151. {
  1152. g.setColour (colour::textDisabled);
  1153. g.setFont (font (type::caption));
  1154. g.drawFittedText (tr ("{0} smp", { juce::String (lat) }), juce::Rectangle<float> (588.0f, bar.getY(), bar.getRight() - 376.0f - 588.0f, bar.getHeight()).toNearestInt(),
  1155. juce::Justification::centred, 1, 0.7f);
  1156. }
  1157. }
  1158. // the read-out box of a hovered node
  1159. void paintTip (juce::Graphics& g, const EffectSlot& fx, int b)
  1160. {
  1161. using namespace theme;
  1162. const auto r = tipArea (b);
  1163. skin::floatingPanel (g, r, bandColour (b), radius::lg);
  1164. g.setFont (font (type::small, true));
  1165. g.setColour (colour::textPrimary);
  1166. auto t = r.reduced (8.0f, 4.0f);
  1167. g.drawText (hzText (hz (param01 (param (b, 1)))), t.removeFromTop (17.0f).withTrimmedRight (60.0f), juce::Justification::centredLeft);
  1168. g.setFont (font (type::small));
  1169. g.drawText (hasGain (bandShape (b)) ? valueText (param (b, 2)) : tr (shapeNames[(int) bandShape (b)]), t.removeFromTop (16.0f), juce::Justification::centredLeft);
  1170. g.setColour (colour::textSecondary);
  1171. const auto pl = bandPlacement (b);
  1172. g.drawText ("Q " + valueText (param (b, 3)) + (pl != Placement::stereo ? " " + placementLabel (pl) : juce::String()),
  1173. t.removeFromTop (16.0f), juce::Justification::centredLeft);
  1174. // power, solo (headphones) and remove (×)
  1175. const auto pw = juce::Rectangle<float> (r.getRight() - 62.0f, r.getY() + 4.0f, 18.0f, 18.0f);
  1176. drawPowerGlyph (g, pw, bandPowered (b) ? bandColour (b) : (pw.contains (mouse) ? colour::textPrimary : colour::textDisabled));
  1177. const auto solo = juce::Rectangle<float> (r.getRight() - 42.0f, r.getY() + 4.0f, 18.0f, 18.0f);
  1178. const auto rm = juce::Rectangle<float> (r.getRight() - 22.0f, r.getY() + 4.0f, 18.0f, 18.0f);
  1179. const auto soloOn = soloBand (fx.params[(std::size_t) soloParam]) == b;
  1180. g.setColour (soloOn ? colour::warning : (solo.contains (mouse) ? colour::textPrimary : colour::textSecondary));
  1181. juce::Path phones;
  1182. const auto pc = solo.reduced (3.0f);
  1183. phones.addCentredArc (pc.getCentreX(), pc.getCentreY() + 1.0f, pc.getWidth() * 0.45f, pc.getHeight() * 0.45f, 0.0f,
  1184. -juce::MathConstants<float>::halfPi, juce::MathConstants<float>::halfPi, true);
  1185. g.strokePath (phones, juce::PathStrokeType (1.5f));
  1186. g.fillRoundedRectangle (juce::Rectangle<float> (pc.getX() - 0.5f, pc.getCentreY(), 3.5f, 6.0f), 1.0f);
  1187. g.fillRoundedRectangle (juce::Rectangle<float> (pc.getRight() - 3.0f, pc.getCentreY(), 3.5f, 6.0f), 1.0f);
  1188. g.setColour (rm.contains (mouse) ? colour::accent : colour::textSecondary);
  1189. g.drawLine (rm.getX() + 5.0f, rm.getY() + 5.0f, rm.getRight() - 5.0f, rm.getBottom() - 5.0f, 1.5f);
  1190. g.drawLine (rm.getRight() - 5.0f, rm.getY() + 5.0f, rm.getX() + 5.0f, rm.getBottom() - 5.0f, 1.5f);
  1191. }
  1192. double displayRate() const
  1193. {
  1194. const auto* times = engine.getProcessTimes();
  1195. const auto sr = times != nullptr ? times->analyzer.sampleRate.load (std::memory_order_relaxed) : 48000.0;
  1196. return sr > 8000.0 ? sr : 48000.0;
  1197. }
  1198. void paintGraph (juce::Graphics& g, const EffectSlot& fx)
  1199. {
  1200. using namespace theme;
  1201. const auto area = graphArea();
  1202. const auto plot = plotArea();
  1203. skin::canvas (g, area, colour::accent);
  1204. // round 170j: Themed draws the analyser in ice and the sum's glow in warm gold; the other styles in the accent
  1205. const auto themed = skin::style() == skin::Style::themed;
  1206. const auto specColour = themed ? skin::artAccent2 (skin::Art::facet) : colour::accent;
  1207. const auto sumGlow = themed ? skin::artAccent (skin::Art::facet) : colour::accent;
  1208. // grid: frequencies (numbers or a piano keyboard along the bottom)
  1209. g.setFont (font (type::caption));
  1210. for (double f : { 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 2000.0,
  1211. 3000.0, 4000.0, 5000.0, 6000.0, 7000.0, 8000.0, 9000.0, 10000.0, 20000.0 })
  1212. {
  1213. const auto major = f == 20.0 || f == 50.0 || f == 100.0 || f == 200.0 || f == 500.0 || f == 1000.0 || f == 2000.0 || f == 5000.0 || f == 10000.0 || f == 20000.0;
  1214. const auto x = xForHz (f);
  1215. g.setColour (major ? colour::gridBeat.brighter (0.15f) : colour::gridStep);
  1216. g.drawVerticalLine ((int) x, plot.getY(), plot.getBottom());
  1217. if (major && ! piano)
  1218. {
  1219. g.setColour (colour::textDisabled);
  1220. g.drawText (f >= 1000.0 ? juce::String ((int) (f / 1000.0)) + "k" : juce::String ((int) f),
  1221. juce::Rectangle<float> (x - 20.0f, plot.getBottom() + 3.0f, 40.0f, 14.0f), juce::Justification::centred);
  1222. }
  1223. }
  1224. if (piano) // one key per semitone; C labelled
  1225. {
  1226. const auto strip = juce::Rectangle<float> (plot.getX(), plot.getBottom() + 2.0f, plot.getWidth(), 16.0f);
  1227. for (int n = 12; n < 136; ++n)
  1228. {
  1229. const auto f0 = 440.0 * std::pow (2.0, (n - 69.5) / 12.0), f1 = 440.0 * std::pow (2.0, (n - 68.5) / 12.0);
  1230. const auto x0 = xForHz (f0), x1 = xForHz (f1);
  1231. if (x1 < strip.getX() || x0 > strip.getRight()) continue;
  1232. const auto pc = n % 12;
  1233. const auto black = pc == 1 || pc == 3 || pc == 6 || pc == 8 || pc == 10;
  1234. g.setColour (black ? colour::keyBlack : colour::keyWhite.withAlpha (0.75f));
  1235. g.fillRect (juce::Rectangle<float> (x0, strip.getY(), juce::jmax (1.0f, x1 - x0 - 0.5f), strip.getHeight()));
  1236. if (pc == 0 && x1 - x0 > 2.0f)
  1237. {
  1238. g.setColour (colour::bg0);
  1239. g.drawText ("C" + juce::String (n / 12 - 1), juce::Rectangle<float> (x0 - 2.0f, strip.getY(), 30.0f, strip.getHeight()), juce::Justification::centredLeft);
  1240. }
  1241. }
  1242. }
  1243. // gain lines with their labels (warm), the analyser's dBFS beside them (grey)
  1244. const auto rangeDb = viewRange();
  1245. const auto step = rangeDb <= 3.0 ? 1.0 : rangeDb <= 6.0 ? 2.0 : rangeDb <= 12.0 ? 3.0 : 6.0;
  1246. for (double db = -rangeDb; db <= rangeDb + 0.01; db += step)
  1247. {
  1248. const auto y = yForDb (db);
  1249. g.setColour (std::abs (db) < 0.01 ? colour::gridBar : colour::gridBeat);
  1250. g.drawHorizontalLine ((int) y, plot.getX(), plot.getRight());
  1251. g.setColour (std::abs (db) < 0.01 ? colour::textPrimary : colour::accentHover.withAlpha (0.85f));
  1252. if (y > plot.getY() + 22.0f)
  1253. g.drawText ((db > 0 ? "+" : "") + juce::String (db, step < 1.5 && std::abs (db - std::round (db)) > 0.01 ? 1 : 0),
  1254. juce::Rectangle<float> (plot.getRight() + 4.0f, y - 7.0f, 28.0f, 14.0f), juce::Justification::centredRight);
  1255. }
  1256. g.setColour (colour::textDisabled);
  1257. for (double db = -10.0; db >= specFloor() + 5.0; db -= 10.0)
  1258. {
  1259. const auto y = yForSpectrum (db);
  1260. if (y > plot.getY() + 24.0f)
  1261. g.drawText (juce::String ((int) db), juce::Rectangle<float> (plot.getRight() + 34.0f, y - 7.0f, 28.0f, 14.0f), juce::Justification::centredRight);
  1262. }
  1263. {
  1264. const auto r = juce::Rectangle<float> (plot.getRight() + 6.0f, plot.getY() + 2.0f, 50.0f, 18.0f);
  1265. g.setColour (r.contains (mouse) ? colour::bg5 : colour::bg4);
  1266. g.fillRoundedRectangle (r, radius::md);
  1267. g.setColour (colour::accentHover);
  1268. g.setFont (font (type::small, true));
  1269. g.drawText (juce::String ((int) rangeDb) + " dB", r, juce::Justification::centred);
  1270. }
  1271. // analyser (behind the curves)
  1272. const auto mode = analyzer (param01 (analyzerParam));
  1273. if (mode != Analyzer::off && ! specPre.empty())
  1274. {
  1275. auto spectrumPath = [&] (const std::vector<float>& db, bool closed) {
  1276. juce::Path p;
  1277. const auto n = (int) db.size();
  1278. for (int i = 0; i < n; ++i)
  1279. {
  1280. const auto x = plot.getX() + plot.getWidth() * (float) i / (float) (n - 1);
  1281. const auto y = yForSpectrum (db[(std::size_t) i]);
  1282. if (i == 0) p.startNewSubPath (x, closed ? plot.getBottom() : y);
  1283. if (i == 0 && closed) p.lineTo (x, y);
  1284. else p.lineTo (x, y);
  1285. }
  1286. if (closed)
  1287. {
  1288. p.lineTo (plot.getRight(), plot.getBottom());
  1289. p.closeSubPath();
  1290. }
  1291. return p;
  1292. };
  1293. g.saveState();
  1294. g.reduceClipRegion (plot.toNearestInt());
  1295. if (mode == Analyzer::pre || mode == Analyzer::both)
  1296. {
  1297. g.setGradientFill (juce::ColourGradient (colour::bg5.withAlpha (0.7f), 0.0f, plot.getY(), colour::bg5.withAlpha (0.25f), 0.0f, plot.getBottom(), false));
  1298. g.fillPath (spectrumPath (specPre, true));
  1299. }
  1300. if (mode == Analyzer::post || mode == Analyzer::both)
  1301. {
  1302. const auto p = spectrumPath (specPost, true);
  1303. // round 126: the analyser glows — a deep fill of the accent rising to a lit edge
  1304. juce::ColourGradient fillGrad (specColour.withAlpha (0.34f), 0.0f, plot.getY() + plot.getHeight() * 0.25f, specColour.withAlpha (0.02f), 0.0f,
  1305. plot.getBottom(), false);
  1306. fillGrad.addColour (0.5, specColour.darker (0.3f).withAlpha (0.14f));
  1307. g.setGradientFill (fillGrad);
  1308. g.fillPath (p);
  1309. const auto edge = spectrumPath (specPost, false);
  1310. if (! skin::reducedEffects())
  1311. {
  1312. g.setColour (specColour.withAlpha (0.18f));
  1313. g.strokePath (edge, juce::PathStrokeType (4.0f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  1314. }
  1315. g.setColour (specColour.brighter (0.2f).withAlpha (0.85f));
  1316. g.strokePath (edge, juce::PathStrokeType (1.2f));
  1317. }
  1318. // round 166f: the comparative target's spectrum, a cool line over the source's
  1319. if (showTarget && hasTarget() && ! specSide.empty())
  1320. {
  1321. const auto edge = spectrumPath (specSide, false);
  1322. g.setColour (colour::info.withAlpha (0.25f));
  1323. g.strokePath (edge, juce::PathStrokeType (3.5f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  1324. g.setColour (colour::info.withAlpha (0.9f));
  1325. g.strokePath (edge, juce::PathStrokeType (1.3f));
  1326. }
  1327. g.restoreState();
  1328. }
  1329. // curves: each band (its own colour, faint), the dynamic span, then the sum
  1330. const auto columns = juce::jmax (64, (int) plot.getWidth() / 2);
  1331. std::vector<double> freqs ((std::size_t) columns + 1), sum ((std::size_t) columns + 1, outputDb (fx.params[(std::size_t) outputParam])), band ((std::size_t) columns + 1),
  1332. span ((std::size_t) columns + 1);
  1333. for (int i = 0; i <= columns; ++i)
  1334. freqs[(std::size_t) i] = hzForX (plot.getX() + plot.getWidth() * (float) i / (float) columns);
  1335. const auto sr = displayRate();
  1336. const auto solo = soloBand (fx.params[(std::size_t) soloParam]);
  1337. g.saveState();
  1338. g.reduceClipRegion (plot.toNearestInt());
  1339. for (int b = 0; b < bands; ++b)
  1340. {
  1341. if (! bandOn (b)) continue;
  1342. const auto dyn = isDynamic (fx.params, b);
  1343. const auto live = dyn ? liveDynamic (b) : 0.0;
  1344. bandCurveDb (fx.params, b, sr, freqs.data(), columns + 1, band.data(), live);
  1345. for (int i = 0; i <= columns; ++i) sum[(std::size_t) i] += band[(std::size_t) i];
  1346. const auto colour = bandColour (b);
  1347. const auto isSel = b == selected || b == hover;
  1348. juce::Path p;
  1349. for (int i = 0; i <= columns; ++i)
  1350. {
  1351. const auto x = plot.getX() + plot.getWidth() * (float) i / (float) columns;
  1352. const auto y = yForDb (band[(std::size_t) i]);
  1353. if (i == 0) p.startNewSubPath (x, y);
  1354. else p.lineTo (x, y);
  1355. }
  1356. if (isSel)
  1357. {
  1358. auto fill = p;
  1359. fill.lineTo (plot.getRight(), yForDb (0.0));
  1360. fill.lineTo (plot.getX(), yForDb (0.0));
  1361. fill.closeSubPath();
  1362. g.setGradientFill (juce::ColourGradient (colour.withAlpha (solo == b ? 0.36f : 0.22f), 0.0f, plot.getY(), colour.withAlpha (solo == b ? 0.18f : 0.06f), 0.0f,
  1363. plot.getBottom(), false));
  1364. g.fillPath (fill);
  1365. }
  1366. if (dyn) // the range the band can move through: static curve ↔ static + range
  1367. {
  1368. std::vector<double> lo ((std::size_t) columns + 1), hi ((std::size_t) columns + 1);
  1369. bandCurveDb (fx.params, b, sr, freqs.data(), columns + 1, lo.data(), 0.0);
  1370. bandCurveDb (fx.params, b, sr, freqs.data(), columns + 1, hi.data(), rangeDb_ (b));
  1371. juce::Path area2;
  1372. for (int i = 0; i <= columns; ++i)
  1373. {
  1374. const auto x = plot.getX() + plot.getWidth() * (float) i / (float) columns;
  1375. if (i == 0) area2.startNewSubPath (x, yForDb (lo[0]));
  1376. else area2.lineTo (x, yForDb (lo[(std::size_t) i]));
  1377. }
  1378. for (int i = columns; i >= 0; --i)
  1379. area2.lineTo (plot.getX() + plot.getWidth() * (float) i / (float) columns, yForDb (hi[(std::size_t) i]));
  1380. area2.closeSubPath();
  1381. g.setColour (colour.withAlpha (0.12f));
  1382. g.fillPath (area2);
  1383. }
  1384. g.setColour (colour.withAlpha (isSel ? 0.9f : 0.45f));
  1385. g.strokePath (p, juce::PathStrokeType (isSel ? 1.6f : 1.0f));
  1386. }
  1387. // the sum
  1388. juce::Path total;
  1389. for (int i = 0; i <= columns; ++i)
  1390. {
  1391. const auto x = plot.getX() + plot.getWidth() * (float) i / (float) columns;
  1392. const auto y = yForDb (sum[(std::size_t) i]);
  1393. if (i == 0) total.startNewSubPath (x, y);
  1394. else total.lineTo (x, y);
  1395. }
  1396. const auto curveColour = fx.enabled ? colour::textPrimary : colour::textDisabled;
  1397. if (fx.enabled)
  1398. {
  1399. // round 126: the area between the sum and 0 dB lit in the accent, the sum a white-hot line in a red glow
  1400. auto area0 = total;
  1401. area0.lineTo (plot.getRight(), yForDb (0.0));
  1402. area0.lineTo (plot.getX(), yForDb (0.0));
  1403. area0.closeSubPath();
  1404. g.setColour (sumGlow.withAlpha (0.16f));
  1405. g.fillPath (area0);
  1406. if (! skin::reducedEffects())
  1407. {
  1408. g.setColour (sumGlow.withAlpha (0.12f));
  1409. g.strokePath (total, juce::PathStrokeType (12.0f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  1410. }
  1411. g.setColour (sumGlow.withAlpha (0.4f));
  1412. g.strokePath (total, juce::PathStrokeType (5.0f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  1413. }
  1414. g.setColour (curveColour);
  1415. g.strokePath (total, juce::PathStrokeType (2.2f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  1416. g.restoreState();
  1417. // nodes
  1418. for (int b = 0; b < bands; ++b)
  1419. {
  1420. if (! bandOn (b)) continue;
  1421. const auto pos = nodePosition (b);
  1422. const auto colour = bandColour (b);
  1423. const auto isSel = b == selected;
  1424. if (hasGain (bandShape (b)) && isDynamic (fx.params, b))
  1425. {
  1426. // the live position and the range handle
  1427. const auto scale = gainScale (param01 (scaleParam));
  1428. const auto liveY = yForDb ((gainDb (param01 (param (b, 2))) + liveDynamic (b)) * scale);
  1429. const auto rh = rangeHandle (b);
  1430. g.setColour (colour.withAlpha (0.5f));
  1431. g.drawLine (pos.x, pos.y, rh.x, rh.y, 1.0f);
  1432. juce::Path diamond;
  1433. diamond.addPolygon (rh, 4, 5.0f, juce::MathConstants<float>::pi * 0.25f);
  1434. g.setColour (isSel ? colour : colour.withAlpha (0.6f));
  1435. g.fillPath (diamond);
  1436. g.setColour (colour.withAlpha (0.9f));
  1437. g.drawEllipse (juce::Rectangle<float> (8.0f, 8.0f).withCentre ({ pos.x, liveY }), 1.5f);
  1438. }
  1439. const auto r = isSel ? 9.0f : 7.5f;
  1440. skin::bloom (g, pos, r * (isSel ? 3.4f : 2.4f), colour, isSel || b == hover ? 0.55f : 0.35f); // round 126: every node glows
  1441. if (isSel)
  1442. {
  1443. g.setColour (colour.withAlpha (0.3f));
  1444. g.drawEllipse (juce::Rectangle<float> (r * 3.2f, r * 3.2f).withCentre (pos), 1.5f);
  1445. }
  1446. const auto nodeColour = soloBand (fx.params[(std::size_t) soloParam]) == b ? colour::warning : colour;
  1447. const auto node = juce::Rectangle<float> (r * 2.0f, r * 2.0f).withCentre (pos);
  1448. g.setColour (juce::Colours::black.withAlpha (0.45f));
  1449. g.fillEllipse (node.translated (0.0f, 1.5f).expanded (1.0f));
  1450. if (bandPowered (b))
  1451. {
  1452. g.setGradientFill (juce::ColourGradient (nodeColour.brighter (0.35f), pos.x, node.getY(), nodeColour.darker (0.15f), pos.x, node.getBottom(), false));
  1453. g.fillEllipse (node);
  1454. g.setColour (juce::Colours::white.withAlpha (0.35f));
  1455. g.drawEllipse (node.reduced (0.5f), 1.0f);
  1456. g.setColour (colour::bg0);
  1457. }
  1458. else // switched off: a hollow node in the band's colour (round 128)
  1459. {
  1460. g.setColour (colour::bg1);
  1461. g.fillEllipse (node);
  1462. g.setColour (nodeColour.withAlpha (0.8f));
  1463. g.drawEllipse (node.reduced (0.75f), 1.5f);
  1464. }
  1465. g.setFont (font (type::caption, true));
  1466. g.drawText (juce::String (b + 1), juce::Rectangle<float> (r * 2.0f, r * 2.0f).withCentre (pos), juce::Justification::centred);
  1467. if (const auto pl = bandPlacement (b); pl != Placement::stereo) // L / R / M / S beside the node
  1468. {
  1469. static const char* tags[] = { "", "L", "R", "M", "S" };
  1470. const auto tag = juce::Rectangle<float> (13.0f, 12.0f).withCentre ({ pos.x + r + 7.0f, pos.y - r - 2.0f });
  1471. g.setColour (colour::bg0.withAlpha (0.85f));
  1472. g.fillRoundedRectangle (tag, 3.0f);
  1473. g.setColour (colour);
  1474. g.setFont (font (type::caption, true));
  1475. g.drawText (tags[(int) pl], tag, juce::Justification::centred);
  1476. }
  1477. if (b == hover || isSel)
  1478. {
  1479. g.setColour (colour::textPrimary);
  1480. g.drawEllipse (juce::Rectangle<float> (r * 2.0f + 3.0f, r * 2.0f + 3.0f).withCentre (pos), 1.2f);
  1481. }
  1482. }
  1483. // what is under the mouse: frequency, note, gain of the curve
  1484. if (plot.contains (mouse))
  1485. {
  1486. const auto f = hzForX (mouse.x);
  1487. g.setColour (colour::textSecondary);
  1488. g.setFont (font (type::caption));
  1489. g.drawText (hzText (f) + " " + noteName (f) + " " + juce::String (dbForY (mouse.y), 1) + " dB",
  1490. juce::Rectangle<float> (plot.getX() + 8.0f, plot.getY() + 6.0f, 260.0f, 14.0f), juce::Justification::centredLeft);
  1491. }
  1492. bool any = false;
  1493. for (int b = 0; b < bands; ++b) any = any || bandOn (b);
  1494. if (! any)
  1495. {
  1496. g.setColour (colour::textDisabled);
  1497. g.setFont (font (type::body));
  1498. g.drawText (tr ("Double-click to add a band"), plot, juce::Justification::centred);
  1499. }
  1500. }
  1501. // the selected band's floating panel (over the bottom of the graph)
  1502. void paintPanel (juce::Graphics& g, const EffectSlot& fx, const std::vector<Control>& all)
  1503. {
  1504. using namespace theme;
  1505. const auto panel = panelArea();
  1506. const auto colourB = bandColour (selected);
  1507. skin::floatingPanel (g, panel, colourB);
  1508. g.setColour (colourB.withAlpha (0.35f));
  1509. g.fillRoundedRectangle (juce::Rectangle<float> (panel.getX() + 12.0f, panel.getY() - 2.0f, 50.0f, 7.0f), 3.5f);
  1510. g.setColour (colourB);
  1511. g.fillRoundedRectangle (juce::Rectangle<float> (panel.getX() + 14.0f, panel.getY() - 1.0f, 46.0f, 3.0f), 1.5f);
  1512. const auto solo = soloBand (fx.params[(std::size_t) soloParam]);
  1513. auto drawButton = [&] (juce::Rectangle<float> r, const juce::String& text, bool on, juce::Colour onColour) {
  1514. skin::button (g, r, on, r.contains (mouse), onColour);
  1515. g.setColour (on ? colour::textPrimary : colour::textSecondary);
  1516. g.setFont (font (type::small, on));
  1517. g.drawText (text, r.reduced (6.0f, 0.0f), juce::Justification::centred, true);
  1518. };
  1519. auto drawArrow = [&] (juce::Rectangle<float> r, bool left) {
  1520. juce::Path a;
  1521. const auto c = r.getCentre();
  1522. if (left) a.addTriangle (c.x + 3.0f, c.y - 5.0f, c.x + 3.0f, c.y + 5.0f, c.x - 3.0f, c.y);
  1523. else a.addTriangle (c.x - 3.0f, c.y - 5.0f, c.x - 3.0f, c.y + 5.0f, c.x + 3.0f, c.y);
  1524. g.setColour (r.contains (mouse) ? colour::textPrimary : colour::textSecondary);
  1525. g.fillPath (a);
  1526. };
  1527. // the band's power button in its colour (round 128: Pro-Q's band power)
  1528. {
  1529. const auto pr = juce::Rectangle<float> (20.0f, 20.0f).withCentre ({ panel.getX() + 22.0f, panel.getY() + 16.0f });
  1530. const auto on = bandPowered (selected);
  1531. g.setColour (on ? colourB : colour::bg2);
  1532. g.fillEllipse (pr);
  1533. if (! on)
  1534. {
  1535. g.setColour (colourB.withAlpha (0.7f));
  1536. g.drawEllipse (pr.reduced (0.75f), 1.5f);
  1537. }
  1538. drawPowerGlyph (g, pr.reduced (2.0f), on ? colour::bg0 : (pr.contains (mouse) ? colour::textPrimary : colour::textSecondary));
  1539. }
  1540. g.setColour (colour::textSecondary);
  1541. g.setFont (font (type::small));
  1542. g.drawText (tr ("Band {0}", { juce::String (selected + 1) }), juce::Rectangle<float> (panel.getX() + 36.0f, panel.getY() + 6.0f, 90.0f, 20.0f), juce::Justification::centredLeft);
  1543. for (const auto& c : all)
  1544. {
  1545. switch (c.type)
  1546. {
  1547. case Ctl::shapeMenu:
  1548. drawButton (c.r, tr (shapeNames[(int) bandShape (selected)]) + juce::String::fromUTF8 (" \xe2\x96\xbe"), true, colourB);
  1549. break;
  1550. case Ctl::slopeMenu:
  1551. drawButton (c.r, juce::String (slopeDb (param01 (c.param))) + " dB/oct" + juce::String::fromUTF8 (" \xe2\x96\xbe"), false, colourB);
  1552. break;
  1553. case Ctl::remove: drawButton (c.r, tr ("Remove"), false, colour::accent); break;
  1554. case Ctl::solo: drawButton (c.r, tr ("Solo"), solo == selected, colour::warning); break;
  1555. case Ctl::placementMenu:
  1556. drawButton (c.r, placementLabel (bandPlacement (selected)) + juce::String::fromUTF8 (" \xe2\x96\xbe"), bandPlacement (selected) != Placement::stereo, colourB);
  1557. break;
  1558. case Ctl::dynamic: drawButton (c.r, tr ("Dynamic"), isDynamic (fx.params, selected), colourB); break;
  1559. case Ctl::spectral: drawButton (c.r, tr ("Spectral"), isSpectral (fx.params, selected), colourB); break;
  1560. case Ctl::spectralTilt: drawButton (c.r, tr ("Tilt"), param01 (c.param) >= 0.5f, colourB); break;
  1561. case Ctl::prevBand: drawArrow (c.r, true); break;
  1562. case Ctl::nextBand: drawArrow (c.r, false); break;
  1563. case Ctl::close:
  1564. {
  1565. g.setColour (c.r.contains (mouse) ? colour::textPrimary : colour::textSecondary);
  1566. const auto r = c.r.reduced (5.0f);
  1567. g.drawLine (r.getX(), r.getY(), r.getRight(), r.getBottom(), 1.5f);
  1568. g.drawLine (r.getRight(), r.getY(), r.getX(), r.getBottom(), 1.5f);
  1569. // the band number between the arrows
  1570. g.setColour (colour::textPrimary);
  1571. g.setFont (font (type::small, true));
  1572. g.drawText (juce::String (selected + 1), c.r.translated (-44.0f, 0.0f).withWidth (22.0f), juce::Justification::centred);
  1573. break;
  1574. }
  1575. case Ctl::freq: case Ctl::gain: case Ctl::q: case Ctl::range: case Ctl::threshold: case Ctl::attack: case Ctl::release:
  1576. {
  1577. static const std::map<Ctl, const char*> captions { { Ctl::freq, TR_NOOP ("FREQ") }, { Ctl::gain, TR_NOOP ("GAIN") }, { Ctl::q, "Q" },
  1578. { Ctl::range, TR_NOOP ("RANGE") }, { Ctl::threshold, TR_NOOP ("THRESHOLD") },
  1579. { Ctl::attack, TR_NOOP ("ATTACK") }, { Ctl::release, TR_NOOP ("RELEASE") } };
  1580. const auto bipolar = c.type == Ctl::gain || c.type == Ctl::range;
  1581. const auto active = dragging == Drag::control && dragParam == c.param;
  1582. const auto global = c.type == Ctl::attack || c.type == Ctl::release;
  1583. const auto knob = knobCircle (c);
  1584. skin::KnobLook look;
  1585. look.value = param01 (c.param);
  1586. look.bipolar = bipolar;
  1587. look.accent = global ? colour::accent : colourB;
  1588. look.hover = c.r.contains (mouse) ? 1.0f : 0.0f;
  1589. look.active = active;
  1590. look.enabled = fx.enabled;
  1591. skin::knob (g, knob, look);
  1592. if (c.type == Ctl::gain && isDynamic (fx.params, selected))
  1593. {
  1594. // the dynamic range as an outer arc from the gain, and the live position as a dot
  1595. const auto scale = (float) (juce::MathConstants<float>::pi * 1.5f);
  1596. const auto start = -juce::MathConstants<float>::pi * 0.75f;
  1597. const auto g0 = param01 (param (selected, 2));
  1598. const auto g1 = juce::jlimit (0.0f, 1.0f, gainValue (gainDb (g0) + rangeDb_ (selected)));
  1599. const auto rr = knob.getWidth() * 0.5f + 4.0f;
  1600. juce::Path arc;
  1601. arc.addCentredArc (knob.getCentreX(), knob.getCentreY(), rr, rr, 0.0f, start + scale * juce::jmin (g0, g1), start + scale * juce::jmax (g0, g1), true);
  1602. g.setColour (colourB.withAlpha (0.8f));
  1603. g.strokePath (arc, juce::PathStrokeType (2.0f));
  1604. const auto live = juce::jlimit (0.0f, 1.0f, gainValue (gainDb (g0) + liveDynamic (selected)));
  1605. const auto ang = start + scale * live;
  1606. g.setColour (colour::textPrimary);
  1607. g.fillEllipse (juce::Rectangle<float> (5.0f, 5.0f).withCentre ({ knob.getCentreX() + rr * std::sin (ang), knob.getCentreY() - rr * std::cos (ang) }));
  1608. }
  1609. g.setColour (active ? colourB.brighter (0.3f) : skin::captionColour());
  1610. g.setFont (font (type::caption, true));
  1611. g.drawText (tr (captions.at (c.type)), c.r.withY (c.r.getBottom() - 34.0f).withHeight (14.0f), juce::Justification::centred);
  1612. g.setColour (skin::valueColour (c.r.contains (mouse) || active));
  1613. g.setFont (font (type::small));
  1614. g.drawText (valueText (c.param), c.r.withTop (c.r.getBottom() - 20.0f), juce::Justification::centred, true);
  1615. break;
  1616. }
  1617. default: break;
  1618. }
  1619. }
  1620. if (hasGain (bandShape (selected)) && ! isDynamic (fx.params, selected))
  1621. {
  1622. g.setColour (colour::textDisabled);
  1623. g.setFont (font (type::caption));
  1624. g.drawFittedText (tr ("Dynamic EQ: the gain follows the level of the band's region (threshold, range)"),
  1625. juce::Rectangle<float> (panel.getX() + 306.0f, panel.getY() + 40.0f, 200.0f, 60.0f).toNearestInt(), juce::Justification::centredLeft, 3);
  1626. }
  1627. }
  1628. private:
  1629. enum class Drag { none, node, range, control };
  1630. int tapKey() const { return track * maxEffectSlots + slot; }
  1631. void claimTap()
  1632. {
  1633. if (auto* times = engine.getProcessTimes()) times->analyzer.key.store (tapKey());
  1634. }
  1635. void changeListenerCallback (juce::ChangeBroadcaster*) override
  1636. {
  1637. if (current() == nullptr) selected = -1;
  1638. repaint();
  1639. }
  1640. void timerCallback() override
  1641. {
  1642. if (! isShowing()) return; // a hidden editor costs nothing
  1643. if (shownSlot.changed (engine.getAutomationDisplay(), track, slot)) // the bands and knobs follow their automation
  1644. repaint();
  1645. updateSpectrum();
  1646. // redraw the graph only when the analyser or a dynamic band moved (an idle editor stops drawing)
  1647. double signature = 0.0;
  1648. for (const auto* v : { &specPre, &specPost, &specSide })
  1649. for (const auto x : *v) signature += x;
  1650. for (int b = 0; b < bands; ++b) signature += liveDynamic (b) * (b + 1);
  1651. if (signature != lastSignature || demo) repaint (graphArea().toNearestInt());
  1652. lastSignature = signature;
  1653. }
  1654. double lastSignature = 0.0;
  1655. // round 170c (issue #11: "the visualiser is unstable"): the analyser lives on a fixed grid of log-spaced points
  1656. // over the graph's whole axis (10 Hz … 30 kHz), not one per pixel column: resizing the window no longer resets it
  1657. // to the floor or changes its look. Below one FFT bin per point the magnitude is interpolated between bins (no
  1658. // staircase in the lows), above it the band's strongest bin is kept (a tone reads its true level); then a light
  1659. // smoothing across neighbours and a short rise (≈ 3 frames) instead of jumping to every frame's value: no flicker.
  1660. static constexpr int specPoints = 480;
  1661. static double specHz (int i) { return minHz * std::pow (maxHz / minHz, (double) i / (double) (specPoints - 1)); }
  1662. void updateSpectrum()
  1663. {
  1664. constexpr int columns = specPoints;
  1665. if ((int) specPre.size() != columns)
  1666. {
  1667. specPre.assign ((std::size_t) columns, (float) specFloor());
  1668. specPost.assign ((std::size_t) columns, (float) specFloor());
  1669. }
  1670. if (analyzer (param01 (analyzerParam)) == Analyzer::off || frozen) return;
  1671. if (demo)
  1672. {
  1673. for (int i = 0; i < columns; ++i)
  1674. {
  1675. const auto f = specHz (i);
  1676. // a mix's typical spectrum: pink-ish slope, a kick bump, a presence region, roll-off
  1677. const auto db = -40.0 - 9.0 * std::log2 (juce::jmax (20.0, f) / 1000.0) * 0.5 + 9.0 * std::exp (-std::pow (std::log2 (f / 60.0), 2.0))
  1678. + 4.0 * std::sin (f * 0.0021) - (f > 14000.0 ? (f - 14000.0) * 0.004 : 0.0) + tiltDb (f);
  1679. specPre[(std::size_t) i] = (float) db;
  1680. const auto* fx = current();
  1681. specPost[(std::size_t) i] = (float) (db + (fx != nullptr ? responseDb (fx->params, 48000.0, f) : 0.0));
  1682. }
  1683. return;
  1684. }
  1685. auto* times = engine.getProcessTimes();
  1686. if (times == nullptr) return;
  1687. auto& tap = times->analyzer;
  1688. if (tap.key.load (std::memory_order_relaxed) != tapKey())
  1689. {
  1690. if (hasKeyboardFocus (true) || isMouseOver (true)) claimTap(); // the editor in use takes the analyser
  1691. decay();
  1692. return;
  1693. }
  1694. const auto written = tap.written.load (std::memory_order_acquire);
  1695. if (written == lastWritten) { decay(); return; }
  1696. lastWritten = written;
  1697. const auto sr = displayRate();
  1698. const bool side = tap.sideOn.load (std::memory_order_relaxed) && hasTarget(); // round 166f: the comparative target
  1699. if ((int) specSide.size() != columns) specSide.assign ((std::size_t) columns, (float) specFloor());
  1700. std::vector<float> fresh ((std::size_t) columns);
  1701. for (int which = 0; which < (side ? 3 : 2); ++which)
  1702. {
  1703. auto& ring = which == 0 ? tap.pre : which == 1 ? tap.post : tap.side;
  1704. std::fill (fftData.begin(), fftData.end(), 0.0f);
  1705. for (int i = 0; i < fftSize; ++i)
  1706. fftData[(std::size_t) i] = ring[(std::size_t) ((written - (std::uint32_t) fftSize + (std::uint32_t) i) & (ProcessTimes::AnalyzerTap::size - 1))]
  1707. .load (std::memory_order_relaxed) * window[(std::size_t) i];
  1708. fft.performFrequencyOnlyForwardTransform (fftData.data(), true);
  1709. // round 152: EQ match — the input's long-term spectrum (round 166f: with Capture::both, the target's too)
  1710. if ((which == 0 && capture != Capture::none) || (which == 2 && capture == Capture::both))
  1711. {
  1712. const bool ref = which == 2 || capture == Capture::reference;
  1713. auto& acc = ref ? matchRef : matchCur;
  1714. if (acc.size() != (std::size_t) (fftSize / 2)) acc.assign ((std::size_t) (fftSize / 2), 0.0);
  1715. for (int k = 0; k < fftSize / 2; ++k) acc[(std::size_t) k] += (double) fftData[(std::size_t) k] * fftData[(std::size_t) k];
  1716. ++(ref ? refFrames : curFrames);
  1717. }
  1718. auto& out = which == 0 ? specPre : which == 1 ? specPost : specSide;
  1719. computeSpectrumDb (fftData.data(), sr, fresh.data());
  1720. for (int i = 0; i < columns; ++i)
  1721. {
  1722. // neighbours smoothed (¼ ½ ¼ in dB), then a short rise and the analyser speed's fall
  1723. const auto db = 0.25f * fresh[(std::size_t) juce::jmax (0, i - 1)] + 0.5f * fresh[(std::size_t) i]
  1724. + 0.25f * fresh[(std::size_t) juce::jmin (columns - 1, i + 1)];
  1725. auto& v = out[(std::size_t) i];
  1726. v = db > v ? v + (db - v) * 0.6f : juce::jmax (db, v - fallPerFrame());
  1727. }
  1728. }
  1729. }
  1730. // one frame's spectrum (dBFS, a full-scale sine = 0 dB, tilted) on the fixed grid, from FFT magnitudes
  1731. void computeSpectrumDb (const float* magnitudes, double sr, float* out) const
  1732. {
  1733. const auto norm = 4.0f / (float) fftSize; // Hann (coherent gain ½) and one side
  1734. const auto binHz = sr / (double) fftSize;
  1735. const auto ratio = std::pow (maxHz / minHz, 0.5 / (double) (specPoints - 1)); // half a point's width
  1736. for (int i = 0; i < specPoints; ++i)
  1737. {
  1738. const auto f = specHz (i);
  1739. float m = 0.0f;
  1740. if (f >= sr * 0.5) m = 0.0f;
  1741. else
  1742. {
  1743. const auto k0 = f / ratio / binHz, k1 = f * ratio / binHz;
  1744. if (std::floor (k1) <= std::ceil (k0)) // narrower than a bin: interpolate between the two around f
  1745. {
  1746. const auto k = juce::jlimit (0.0, (double) (fftSize / 2 - 2), f / binHz);
  1747. const auto lo = (int) k;
  1748. const auto t = (float) (k - lo);
  1749. m = magnitudes[lo] + (magnitudes[lo + 1] - magnitudes[lo]) * t;
  1750. }
  1751. else
  1752. for (int k = juce::jlimit (1, fftSize / 2 - 1, (int) std::ceil (k0)); k <= juce::jlimit (1, fftSize / 2 - 1, (int) std::floor (k1)); ++k)
  1753. m = juce::jmax (m, magnitudes[k]);
  1754. }
  1755. out[i] = (float) (20.0 * std::log10 (m * norm + 1e-9) + tiltDb (f));
  1756. }
  1757. }
  1758. double tiltDb (double f) const { return analyzerTiltDb (param01 (specTiltParam)) * std::log2 (juce::jmax (10.0, f) / 1000.0); }
  1759. // dB per 30 Hz frame the analyser falls: very slow 0.3 … very fast 6 (medium 1.5 = ~45 dB/s, the old speed)
  1760. float fallPerFrame() const
  1761. {
  1762. static constexpr float fall[] { 0.3f, 0.75f, 1.5f, 3.0f, 6.0f };
  1763. return fall[analyzerSpeed (param01 (specSpeedParam))];
  1764. }
  1765. void decay()
  1766. {
  1767. if (frozen) return;
  1768. for (auto* v : { &specPre, &specPost, &specSide })
  1769. for (auto& x : *v) x = juce::jmax ((float) specFloor(), x - fallPerFrame());
  1770. }
  1771. AudioEngine& engine;
  1772. int track, slot;
  1773. mutable ShownEffectSlot shownSlot;
  1774. bool demo = false;
  1775. int selected = -1, hover = -1, tipBand = -1, dragBand = -1, dragParam = -1;
  1776. bool piano = false;
  1777. bool frozen = false; // analyser freeze (editor only)
  1778. Capture capture = Capture::none; // round 152: EQ match
  1779. std::vector<double> matchRef, matchCur;
  1780. int refFrames = 0, curFrames = 0, matchBands = 8, matchSmoothing = 6;
  1781. pluginheader::PresetBar bar { engine, track, slot };
  1782. Drag dragging = Drag::none;
  1783. juce::Point<float> dragOffset, dragStart, mouse { -1.0f, -1.0f };
  1784. juce::Rectangle<float> dragRect;
  1785. ui::ValueDrag valueDrag;
  1786. juce::dsp::FFT fft;
  1787. std::vector<float> fftData, window, specPre, specPost, specSide;
  1788. std::uint32_t lastWritten = 0;
  1789. };
  1790. class FacetEqWindow final : public juce::DocumentWindow
  1791. {
  1792. public:
  1793. FacetEqWindow (AudioEngine& engine, int track, int slot, const juce::String& title)
  1794. : DocumentWindow (title, theme::colour::bg1, DocumentWindow::closeButton | DocumentWindow::minimiseButton),
  1795. gone (engine, track, slot, [this] { closeButtonPressed(); })
  1796. {
  1797. setUsingNativeTitleBar (true);
  1798. setContentOwned (new FacetEqEditor (engine, track, slot, false), true);
  1799. setResizable (true, false);
  1800. if (auto* c = getConstrainer())
  1801. {
  1802. c->setMinimumSize (860, 440);
  1803. c->setMaximumSize (2400, 1400);
  1804. }
  1805. centreWithSize (getWidth(), getHeight());
  1806. setVisible (true);
  1807. keepAboveMainWindow (*this);
  1808. if (auto* c = getContentComponent()) skin::playOpenAnimation (*c, skin::styleFor (EffectKind::facetEq)); // round 126: fade / scale in
  1809. }
  1810. void closeButtonPressed() override;
  1811. private:
  1812. ui::SlotGoneWatcher gone;
  1813. };
  1814. std::map<std::pair<int, int>, std::unique_ptr<FacetEqWindow>>& windows()
  1815. {
  1816. static std::map<std::pair<int, int>, std::unique_ptr<FacetEqWindow>> open;
  1817. return open;
  1818. }
  1819. void FacetEqWindow::closeButtonPressed()
  1820. {
  1821. for (auto& [key, window] : windows())
  1822. if (window.get() == this)
  1823. {
  1824. juce::MessageManager::callAsync ([k = key] { windows().erase (k); });
  1825. return;
  1826. }
  1827. }
  1828. } // namespace
  1829. void drawFacetEqCurve (juce::Graphics& g, juce::Rectangle<float> area, const EffectParams& params, double sampleRate, bool enabled)
  1830. {
  1831. using namespace theme;
  1832. g.setColour (colour::bg0);
  1833. g.fillRoundedRectangle (area, radius::md);
  1834. const auto xFor = [&] (double f) { return area.getX() + area.getWidth() * (float) (std::log (f / 20.0) / std::log (1000.0)); };
  1835. g.setColour (colour::gridBeat);
  1836. for (double f : { 100.0, 1000.0, 10000.0 }) g.drawVerticalLine ((int) xFor (f), area.getY(), area.getBottom());
  1837. g.setColour (colour::gridBar);
  1838. g.drawHorizontalLine ((int) area.getCentreY(), area.getX(), area.getRight());
  1839. const int n = juce::jmax (32, (int) area.getWidth() / 2);
  1840. std::vector<double> freqs ((std::size_t) n + 1), sum ((std::size_t) n + 1, outputDb (params[(std::size_t) outputParam])), band ((std::size_t) n + 1);
  1841. for (int i = 0; i <= n; ++i) freqs[(std::size_t) i] = 20.0 * std::pow (1000.0, (double) i / n);
  1842. for (int b = 0; b < bands; ++b)
  1843. {
  1844. if (shape (params[(std::size_t) param (b, 0)]) == Shape::off) continue;
  1845. bandCurveDb (params, b, sampleRate, freqs.data(), n + 1, band.data());
  1846. for (int i = 0; i <= n; ++i) sum[(std::size_t) i] += band[(std::size_t) i];
  1847. }
  1848. juce::Path p;
  1849. for (int i = 0; i <= n; ++i)
  1850. {
  1851. const auto x = area.getX() + area.getWidth() * (float) i / (float) n;
  1852. const auto y = area.getCentreY() - area.getHeight() * 0.45f * (float) (juce::jlimit (-24.0, 24.0, sum[(std::size_t) i]) / 24.0);
  1853. if (i == 0) p.startNewSubPath (x, y);
  1854. else p.lineTo (x, y);
  1855. }
  1856. g.setColour (enabled ? colour::accent : colour::textDisabled);
  1857. g.strokePath (p, juce::PathStrokeType (2.0f));
  1858. for (int b = 0; b < bands; ++b)
  1859. {
  1860. const auto sh = shape (params[(std::size_t) param (b, 0)]);
  1861. if (sh == Shape::off) continue;
  1862. const auto f = juce::jlimit (20.0, 20000.0, hz (params[(std::size_t) param (b, 1)]));
  1863. const auto db = hasGain (sh) ? gainDb (params[(std::size_t) param (b, 2)]) * gainScale (params[(std::size_t) scaleParam]) : 0.0;
  1864. g.setColour (bandColour (b));
  1865. g.fillEllipse (juce::Rectangle<float> (7.0f, 7.0f).withCentre ({ xFor (f), area.getCentreY() - area.getHeight() * 0.45f * (float) (juce::jlimit (-24.0, 24.0, db) / 24.0) }));
  1866. }
  1867. }
  1868. void tickFacetEqEditor (juce::Component& editor)
  1869. {
  1870. if (auto* facetEditor = dynamic_cast<FacetEqEditor*> (&editor))
  1871. {
  1872. facetEditor->claimTap();
  1873. facetEditor->updateSpectrum();
  1874. }
  1875. }
  1876. std::unique_ptr<juce::Component> makeFacetEqEditor (AudioEngine& engine, int track, int slot, bool demo)
  1877. {
  1878. return std::make_unique<FacetEqEditor> (engine, track, slot, demo);
  1879. }
  1880. namespace FacetEqWindows
  1881. {
  1882. void open (AudioEngine& engine, int track, int slot)
  1883. {
  1884. const auto& mixer = engine.getSong().mixer;
  1885. if (track < 0 || track >= (int) mixer.size() || slot < 0 || slot >= (int) mixer[(std::size_t) track].effects.size())
  1886. return;
  1887. auto& open = windows();
  1888. const auto key = std::make_pair (track, slot);
  1889. if (auto it = open.find (key); it != open.end())
  1890. {
  1891. if (auto* editor = dynamic_cast<FacetEqEditor*> (it->second->getContentComponent()); editor != nullptr && editor->current() != nullptr)
  1892. {
  1893. it->second->toFront (true);
  1894. return;
  1895. }
  1896. open.erase (it);
  1897. }
  1898. const auto name = track == 0 ? tr ("Master") : juce::String::fromUTF8 (mixer[(std::size_t) track].name.c_str());
  1899. const auto title = juce::String ("Facet EQ - ") + (name.isNotEmpty() ? name : tr ("Insert {0}", { juce::String (track) })) + " ("
  1900. + tr ("slot {0}", { juce::String (slot + 1) }) + ")";
  1901. open[key] = std::make_unique<FacetEqWindow> (engine, track, slot, title);
  1902. }
  1903. void closeAll()
  1904. {
  1905. windows().clear();
  1906. }
  1907. } // namespace FacetEqWindows
  1908. } // namespace lynx