Toolbar.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include "ui/Toolbar.h"
  2. #include "ui/HintBar.h"
  3. #include "ui/I18n.h"
  4. #include "ui/Icons.h"
  5. #include "ui/Theme.h"
  6. namespace lynx::toolbar
  7. {
  8. namespace
  9. {
  10. juce::String barBeatStep (const Song& song, double ticks, int beatsPerBar)
  11. {
  12. const auto beats = ticks / ppq;
  13. int bar = 0, beat = 0;
  14. if (song.mode == PlayMode::song) // the Playlist ruler applies (with time signature changes)
  15. {
  16. const auto current = song.barAt ((std::int64_t) ticks);
  17. bar = current.number;
  18. beat = (int) ((ticks - (double) current.tick) / ppq) + 1;
  19. }
  20. else
  21. {
  22. const auto perBar = juce::jmax (1, beatsPerBar);
  23. bar = (int) (beats / perBar) + 1;
  24. beat = (int) std::fmod (beats, (double) perBar) + 1;
  25. }
  26. const auto step = (int) (std::fmod (beats, 1.0) * stepsPerBeat) + 1;
  27. return juce::String::formatted ("%d : %d : %d", bar, beat, step);
  28. }
  29. double loopLength (const Song& song)
  30. {
  31. if (song.patterns.empty())
  32. return 1.0;
  33. const auto length = song.mode == PlayMode::song
  34. ? song.songLength()
  35. : song.patterns[(std::size_t) juce::jlimit (0, (int) song.patterns.size() - 1, song.currentPattern)].effectiveLength (song.beatsPerBar);
  36. return (double) juce::jmax<std::int64_t> (1, length);
  37. }
  38. double playheadTicks (AudioEngine& engine)
  39. {
  40. return std::fmod (juce::jmax (0.0, engine.getTransport().positionTicks.load()), loopLength (engine.getSong()));
  41. }
  42. juce::String centsText (double cents)
  43. {
  44. const auto c = juce::roundToInt (cents);
  45. return (c > 0 ? "+" : "") + juce::String (c) + " " + tr ("cents");
  46. }
  47. } // namespace
  48. // --- song position -------------------------------------------------------------------------------
  49. SongPositionBar::SongPositionBar (AudioEngine& e) : engine (e)
  50. {
  51. setMouseCursor (juce::MouseCursor::PointingHandCursor);
  52. }
  53. double SongPositionBar::length() const { return loopLength (engine.getSong()); }
  54. double SongPositionBar::position() const { return playheadTicks (engine); }
  55. juce::Rectangle<float> SongPositionBar::track() const
  56. {
  57. return getLocalBounds().toFloat().reduced (5.0f, 0.0f).withSizeKeepingCentre ((float) getWidth() - 10.0f, 4.0f);
  58. }
  59. void SongPositionBar::update()
  60. {
  61. const auto fraction = position() / length();
  62. if (std::abs (fraction - shownFraction) * getWidth() >= 0.5)
  63. {
  64. shownFraction = fraction;
  65. repaint();
  66. }
  67. }
  68. void SongPositionBar::paint (juce::Graphics& g)
  69. {
  70. using namespace theme;
  71. const auto t = track();
  72. const auto& song = engine.getSong();
  73. const auto active = dragging || isMouseOver();
  74. g.setColour (colour::bg0);
  75. g.fillRoundedRectangle (t.expanded (0.0f, 1.0f), 2.5f);
  76. // the Playlist loop (Song mode)
  77. if (song.mode == PlayMode::song && song.hasLoop())
  78. {
  79. const auto x0 = t.getX() + t.getWidth() * (float) ((double) song.loopStart / length());
  80. const auto x1 = t.getX() + t.getWidth() * (float) juce::jmin (1.0, (double) song.loopEnd / length());
  81. g.setColour (colour::info.withAlpha (0.35f));
  82. g.fillRect (juce::Rectangle<float> (x0, t.getY() - 1.0f, juce::jmax (1.0f, x1 - x0), t.getHeight() + 2.0f));
  83. }
  84. const auto fraction = (float) juce::jlimit (0.0, 1.0, position() / length());
  85. const auto x = t.getX() + t.getWidth() * fraction;
  86. g.setColour (engine.getTransport().playing.load() ? colour::accent : colour::accent.withMultipliedSaturation (0.55f).withMultipliedBrightness (0.8f));
  87. g.fillRoundedRectangle (t.withRight (juce::jmax (t.getX() + 1.0f, x)), 2.0f);
  88. const auto radius = active ? 5.0f : 3.5f;
  89. g.setColour (active ? colour::textPrimary : colour::textSecondary);
  90. g.fillEllipse (juce::Rectangle<float> (radius * 2.0f, radius * 2.0f).withCentre ({ x, t.getCentreY() }));
  91. }
  92. void SongPositionBar::seekTo (int x)
  93. {
  94. const auto t = track();
  95. const auto fraction = juce::jlimit (0.0, 1.0, (double) ((float) x - t.getX()) / juce::jmax (1.0f, t.getWidth()));
  96. auto ticks = fraction * length();
  97. if (! juce::ModifierKeys::currentModifiers.isAltDown()) // Alt: no snap (as everywhere)
  98. ticks = std::round (ticks / ticksPerStep) * ticksPerStep;
  99. engine.seek (juce::jlimit (0.0, length() - 1.0, ticks));
  100. update();
  101. repaint();
  102. }
  103. void SongPositionBar::mouseDown (const juce::MouseEvent& e)
  104. {
  105. if (! e.mods.isLeftButtonDown())
  106. return;
  107. dragging = true;
  108. seekTo (e.x);
  109. }
  110. void SongPositionBar::mouseDrag (const juce::MouseEvent& e)
  111. {
  112. if (dragging)
  113. seekTo (e.x);
  114. }
  115. void SongPositionBar::mouseUp (const juce::MouseEvent&)
  116. {
  117. dragging = false;
  118. repaint();
  119. }
  120. void SongPositionBar::mouseWheelMove (const juce::MouseEvent&, const juce::MouseWheelDetails& wheel)
  121. {
  122. const auto delta = std::abs (wheel.deltaY) >= std::abs (wheel.deltaX) ? wheel.deltaY : -wheel.deltaX;
  123. if (delta == 0.0f)
  124. return;
  125. const auto bar = (double) ppq * juce::jmax (1, engine.getSong().beatsPerBar);
  126. const auto current = std::round (position() / bar) * bar;
  127. engine.seek (juce::jlimit (0.0, juce::jmax (0.0, length() - bar), current + (delta > 0.0f ? -bar : bar) * (wheel.isReversed ? -1.0 : 1.0)));
  128. update();
  129. }
  130. ui::Hint SongPositionBar::hintAt (juce::Point<int>)
  131. {
  132. const auto& song = engine.getSong();
  133. ui::Hint h;
  134. h.text = (song.mode == PlayMode::song ? tr ("Song position") : tr ("Pattern position"))
  135. + ": " + tr ("[LMB] click or drag to move the playhead [Wheel] one bar [Alt] no snap");
  136. const auto bars = juce::jmax (1, juce::roundToInt (length() / ((double) ppq * juce::jmax (1, song.beatsPerBar))));
  137. h.value = tr ("Bar {0} of {1}", { barBeatStep (song, position(), song.beatsPerBar).upToFirstOccurrenceOf (" ", false, false), juce::String (bars) });
  138. return h;
  139. }
  140. // --- time display --------------------------------------------------------------------------------
  141. juce::String TimeDisplay::text() const
  142. {
  143. const auto& song = engine.getSong();
  144. const auto ticks = playheadTicks (engine);
  145. if (! clockMode)
  146. return barBeatStep (song, ticks, engine.getTransport().beatsPerBar.load());
  147. const auto seconds = song.secondsForTicks ((std::int64_t) ticks);
  148. const auto whole = (int) seconds;
  149. return juce::String::formatted ("%d : %02d : %02d", whole / 60, whole % 60, (int) ((seconds - whole) * 100.0));
  150. }
  151. void TimeDisplay::update()
  152. {
  153. if (const auto t = text(); t != shown)
  154. {
  155. shown = t;
  156. repaint();
  157. }
  158. }
  159. void TimeDisplay::paint (juce::Graphics& g)
  160. {
  161. using namespace theme;
  162. const auto area = getLocalBounds();
  163. g.setColour (colour::bg0);
  164. g.fillRoundedRectangle (area.toFloat(), radius::md);
  165. if (isMouseOver())
  166. {
  167. g.setColour (colour::borderStrong);
  168. g.drawRoundedRectangle (area.toFloat().reduced (0.5f), radius::md, 1.0f);
  169. }
  170. g.setColour (colour::textPrimary);
  171. g.setFont (monoFont (juce::jmin (19.0f, (float) area.getHeight() * 0.5f)));
  172. g.drawText (shown.isEmpty() ? text() : shown, area.withTrimmedBottom (12), juce::Justification::centred, false);
  173. g.setColour (colour::textDisabled);
  174. g.setFont (font (type::caption - 1.0f));
  175. g.drawText (clockMode ? tr ("MIN : SEC : CS") : tr ("BAR : BEAT : STEP"), area.withTrimmedTop (area.getHeight() - 14).withTrimmedBottom (2),
  176. juce::Justification::centred, false);
  177. }
  178. void TimeDisplay::mouseUp (const juce::MouseEvent& e)
  179. {
  180. if (e.mods.isPopupMenu() || ! getLocalBounds().contains (e.getPosition()))
  181. return;
  182. clockMode = ! clockMode;
  183. shown = {};
  184. update();
  185. repaint();
  186. }
  187. ui::Hint TimeDisplay::hintAt (juce::Point<int>)
  188. {
  189. ui::Hint h;
  190. h.text = (clockMode ? tr ("Time: minutes : seconds : hundredths") : tr ("Time: bar : beat : step"))
  191. + " " + tr ("[LMB] switch between bars and minutes");
  192. h.value = text();
  193. return h;
  194. }
  195. // --- master volume and pitch ---------------------------------------------------------------------
  196. MasterKnob::MasterKnob (AudioEngine& e, Kind k) : engine (e), kind (k)
  197. {
  198. setRepaintsOnMouseActivity (true);
  199. }
  200. bool MasterKnob::available() const
  201. {
  202. return kind == Kind::pitch || ! engine.getSong().mixer.empty();
  203. }
  204. double MasterKnob::value() const
  205. {
  206. const auto& song = engine.getSong();
  207. if (kind == Kind::pitch)
  208. return song.masterPitch;
  209. return song.mixer.empty() ? (double) unityFader : (double) song.mixer.front().volume;
  210. }
  211. void MasterKnob::setValue (double v)
  212. {
  213. const auto s = spec();
  214. v = juce::jlimit (s.minimum, s.maximum, v);
  215. if (! available() || std::abs (v - value()) < 1e-9)
  216. return;
  217. if (kind == Kind::pitch)
  218. engine.editSong ([v] (Song& song) { song.masterPitch = (float) v; });
  219. else
  220. engine.editSong ([v] (Song& song) {
  221. if (! song.mixer.empty())
  222. song.mixer.front().volume = (float) v;
  223. });
  224. update();
  225. }
  226. ui::ValueSpec MasterKnob::spec() const
  227. {
  228. if (kind == Kind::volume)
  229. {
  230. auto s = ui::faderSpec (tr ("Master volume"), unityFader);
  231. s.target = AutomationTarget { AutomationTargetKind::mixerVolume, 0 };
  232. return s;
  233. }
  234. ui::ValueSpec s;
  235. s.name = tr ("Master pitch");
  236. s.minimum = -1200.0;
  237. s.maximum = 1200.0;
  238. s.defaultValue = 0.0;
  239. s.pixelsForRange = 360.0;
  240. s.toText = [] (double v) { return centsText (v); };
  241. s.fromText = [] (const juce::String& t) -> std::optional<double> {
  242. const auto number = t.retainCharacters ("+-0123456789.");
  243. if (number.isEmpty())
  244. return std::nullopt;
  245. return number.getDoubleValue();
  246. };
  247. return s;
  248. }
  249. void MasterKnob::update()
  250. {
  251. if (const auto v = value(); v != shown)
  252. {
  253. shown = v;
  254. repaint();
  255. }
  256. }
  257. void MasterKnob::paint (juce::Graphics& g)
  258. {
  259. using namespace theme;
  260. auto area = getLocalBounds().toFloat();
  261. const auto knob = area.removeFromLeft (juce::jmin (area.getHeight(), 26.0f));
  262. const auto s = spec();
  263. const auto v = value();
  264. const auto hover = isMouseOver() || drag.isActive();
  265. const auto value01 = (float) ((v - s.minimum) / (s.maximum - s.minimum));
  266. drawKnob (g, knob, value01, kind == Kind::pitch, available() ? colour::knobArc : colour::textDisabled, hover && available());
  267. area.removeFromLeft (5.0f);
  268. auto caption = area.removeFromTop (area.getHeight() / 2.0f);
  269. g.setColour (colour::textDisabled);
  270. g.setFont (font (type::caption - 1.0f, true));
  271. g.drawText (kind == Kind::volume ? tr ("MASTER") : tr ("PITCH"), caption.withTrimmedTop (1.0f), juce::Justification::bottomLeft, true);
  272. g.setColour (drag.isActive() ? colour::textPrimary : colour::textSecondary);
  273. g.setFont (font (type::caption));
  274. g.drawText (available() ? s.text (v) : tr ("no mixer"), area, juce::Justification::topLeft, true);
  275. }
  276. void MasterKnob::mouseDown (const juce::MouseEvent& e)
  277. {
  278. if (! available())
  279. return;
  280. if (e.mods.isPopupMenu())
  281. {
  282. ui::showValueMenu (*this, kind == Kind::volume ? &engine : nullptr, spec(), value(), [this] (double v) { setValue (v); });
  283. return;
  284. }
  285. if (ui::isResetClick (e))
  286. {
  287. setValue (spec().defaultValue);
  288. return;
  289. }
  290. drag.begin (e, value());
  291. repaint();
  292. }
  293. void MasterKnob::mouseDrag (const juce::MouseEvent& e)
  294. {
  295. if (! drag.isActive())
  296. return;
  297. auto v = drag.update (e, spec());
  298. if (kind == Kind::pitch && ! e.mods.isShiftDown()) // semitone steps; Shift: free (as FL's master pitch)
  299. v = std::round (v / 100.0) * 100.0;
  300. setValue (v);
  301. }
  302. void MasterKnob::mouseUp (const juce::MouseEvent&)
  303. {
  304. drag.end();
  305. repaint();
  306. }
  307. void MasterKnob::mouseDoubleClick (const juce::MouseEvent& e)
  308. {
  309. if (available() && ! e.mods.isPopupMenu() && ! ui::isResetClick (e))
  310. ui::typeValue (*this, spec(), value(), [this] (double v) { setValue (v); });
  311. }
  312. void MasterKnob::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
  313. {
  314. if (! available())
  315. return;
  316. if (kind == Kind::pitch)
  317. {
  318. const auto delta = std::abs (wheel.deltaY) >= std::abs (wheel.deltaX) ? wheel.deltaY : -wheel.deltaX;
  319. if (delta != 0.0f)
  320. setValue (value() + (delta > 0.0f ? 1.0 : -1.0) * (wheel.isReversed ? -1.0 : 1.0) * (e.mods.isCtrlDown() ? 1.0 : 100.0));
  321. return;
  322. }
  323. if (const auto v = ui::wheelValue (value(), e, wheel, spec()))
  324. setValue (*v);
  325. }
  326. ui::Hint MasterKnob::hintAt (juce::Point<int>)
  327. {
  328. if (! available())
  329. return { tr ("Master volume: this project has no mixer"), {}, false, false };
  330. const auto gesture = kind == Kind::volume
  331. ? tr ("level of everything you hear (the Master insert's fader)") + " " + ui::valueGestures()
  332. : tr ("tunes the built-in instruments [LMB] drag (semitones) [Shift] cents [Wheel] semitone [Ctrl][Wheel] cent [Alt][LMB] reset [RMB] menu");
  333. return ui::valueHint (spec(), value(), gesture);
  334. }
  335. // --- CPU, memory, polyphony ----------------------------------------------------------------------
  336. void PerformanceMeter::setValues (double cpu01, std::int64_t memoryBytes, int polyphony)
  337. {
  338. cpu = cpu01;
  339. cpuSmoothed = cpuSmoothed * 0.8 + cpu01 * 0.2;
  340. memory = memoryBytes;
  341. voices = polyphony;
  342. peakVoices = juce::jmax (peakVoices, polyphony);
  343. repaint();
  344. }
  345. void PerformanceMeter::paint (juce::Graphics& g)
  346. {
  347. using namespace theme;
  348. auto area = getLocalBounds();
  349. auto row1 = area.removeFromTop (area.getHeight() / 2).reduced (0, 4);
  350. auto row2 = area.reduced (0, 4);
  351. const juce::Font caption (font (type::caption - 1.0f, true)), number (font (type::caption));
  352. // CPU: caption, bar, percent
  353. g.setFont (caption);
  354. g.setColour (colour::textDisabled);
  355. g.drawText (tr ("CPU"), row1.removeFromLeft (30), juce::Justification::centredLeft, false);
  356. g.setFont (number);
  357. g.setColour (colour::textSecondary);
  358. g.drawText (juce::String (juce::roundToInt (cpuSmoothed * 100.0)) + "%", row1.removeFromRight (34), juce::Justification::centredRight, false);
  359. const auto bar = row1.reduced (2, 3).toFloat();
  360. g.setColour (colour::bg0);
  361. g.fillRoundedRectangle (bar, 2.0f);
  362. const auto load = (float) juce::jlimit (0.0, 1.0, cpuSmoothed);
  363. g.setColour (load < 0.6f ? colour::meterLow : load < 0.85f ? colour::meterMid : colour::meterClip);
  364. if (load > 0.0f)
  365. g.fillRoundedRectangle (bar.withWidth (juce::jmax (2.0f, bar.getWidth() * load)), 2.0f);
  366. // memory and polyphony
  367. auto left = row2.removeFromLeft (row2.getWidth() * 58 / 100);
  368. g.setFont (caption);
  369. g.setColour (colour::textDisabled);
  370. g.drawText (tr ("RAM"), left.removeFromLeft (30), juce::Justification::centredLeft, false);
  371. g.setFont (number);
  372. g.setColour (colour::textSecondary);
  373. g.drawText (memory > 0 ? juce::String (juce::roundToInt ((double) memory / (1024.0 * 1024.0))) + " MB" : juce::String ("-"), left,
  374. juce::Justification::centredLeft, false);
  375. g.setFont (caption);
  376. g.setColour (colour::textDisabled);
  377. g.drawText (tr ("POLY"), row2.removeFromLeft (32), juce::Justification::centredLeft, false);
  378. g.setFont (number);
  379. g.setColour (voices > 0 ? colour::textPrimary : colour::textSecondary);
  380. g.drawText (juce::String (voices), row2, juce::Justification::centredRight, false);
  381. }
  382. ui::Hint PerformanceMeter::hintAt (juce::Point<int> p)
  383. {
  384. ui::Hint h;
  385. if (p.y < getHeight() / 2)
  386. {
  387. h.text = tr ("CPU: how much of the audio deadline the processing takes; near 100% the sound breaks up");
  388. h.value = juce::String (cpu * 100.0, 1) + "%";
  389. }
  390. else if (p.x < getWidth() * 58 / 100)
  391. {
  392. h.text = tr ("RAM: memory used by LYNX (samples, plugins, undo history)");
  393. h.value = juce::String ((double) memory / (1024.0 * 1024.0), 0) + " MB";
  394. }
  395. else
  396. {
  397. h.text = tr ("Polyphony: notes sounding now in the song and from the keyboard (voices still releasing are not counted)");
  398. h.value = juce::String (voices) + " (" + tr ("max {0}", { juce::String (peakVoices) }) + ")";
  399. }
  400. return h;
  401. }
  402. // --- output monitor ------------------------------------------------------------------------------
  403. void OutputMonitor::update()
  404. {
  405. if (! isShowing() || getWidth() <= 0)
  406. return;
  407. engine.copyScope (samples.data(), (int) samples.size());
  408. float now = 0.0f;
  409. for (auto s : samples)
  410. now = juce::jmax (now, std::abs (s));
  411. peak = juce::jmax (now, peak * 0.82f); // fast attack, smooth fall
  412. if (now >= held || holdFrames <= 0)
  413. {
  414. held = now;
  415. holdFrames = 30; // about a second at 30 Hz
  416. }
  417. else
  418. --holdFrames;
  419. if (now >= 0.999f)
  420. clipFrames = 60;
  421. else if (clipFrames > 0)
  422. --clipFrames;
  423. repaint();
  424. }
  425. void OutputMonitor::paint (juce::Graphics& g)
  426. {
  427. using namespace theme;
  428. auto area = getLocalBounds().toFloat();
  429. g.setColour (colour::bg0);
  430. g.fillRoundedRectangle (area, radius::md);
  431. // peak meter on the right: -60..0 dB, the top segment red while it clipped recently
  432. auto meter = area.removeFromRight (8.0f).reduced (1.5f, 4.0f);
  433. auto level = [] (float gain) { return juce::jlimit (0.0f, 1.0f, (juce::Decibels::gainToDecibels (gain, -60.0f) + 60.0f) / 60.0f); };
  434. g.setColour (colour::bg3);
  435. g.fillRoundedRectangle (meter, 1.5f);
  436. const auto filled = meter.withTop (meter.getBottom() - meter.getHeight() * level (peak));
  437. g.setGradientFill (juce::ColourGradient::vertical (colour::meterMid, meter.getY() + meter.getHeight() * 0.1f, colour::meterLow,
  438. meter.getY() + meter.getHeight() * 0.4f));
  439. g.fillRect (filled);
  440. if (held > 0.0f)
  441. {
  442. g.setColour (colour::textSecondary);
  443. g.fillRect (meter.withHeight (1.0f).withY (meter.getBottom() - meter.getHeight() * level (held)));
  444. }
  445. if (clipFrames > 0)
  446. {
  447. g.setColour (colour::meterClip);
  448. g.fillRoundedRectangle (meter.withHeight (4.0f), 1.0f);
  449. }
  450. // oscilloscope
  451. auto scope = area.reduced (4.0f, 3.0f);
  452. g.setColour (colour::border);
  453. g.drawHorizontalLine ((int) scope.getCentreY(), scope.getX(), scope.getRight());
  454. juce::Path wave;
  455. const auto mid = scope.getCentreY();
  456. const auto half = scope.getHeight() * 0.47f;
  457. const auto width = juce::jmax (1, (int) scope.getWidth());
  458. for (int x = 0; x < width; ++x)
  459. {
  460. const auto index = (std::size_t) (x * (int) samples.size() / width);
  461. const auto y = mid - juce::jlimit (-1.0f, 1.0f, samples[index]) * half;
  462. if (x == 0)
  463. wave.startNewSubPath (scope.getX(), y);
  464. else
  465. wave.lineTo (scope.getX() + (float) x, y);
  466. }
  467. g.setColour ((clipFrames > 0 ? colour::meterClip : colour::accent).withAlpha (0.9f));
  468. g.strokePath (wave, juce::PathStrokeType (1.3f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  469. }
  470. ui::Hint OutputMonitor::hintAt (juce::Point<int>)
  471. {
  472. ui::Hint h;
  473. h.text = tr ("Output: the waveform and peak level of what you hear (the meter's top turns red when it clipped)");
  474. h.value = held <= 1.0e-5f ? juce::String ("-inf dB") : juce::String (juce::Decibels::gainToDecibels (held), 1) + " dB";
  475. return h;
  476. }
  477. } // namespace lynx::toolbar
  478. // The process memory comes from the OS; its headers go last (windows.h defines macros such as `small`).
  479. #if JUCE_WINDOWS
  480. #ifndef NOMINMAX
  481. #define NOMINMAX
  482. #endif
  483. #define PSAPI_VERSION 2 // GetProcessMemoryInfo from kernel32 (K32GetProcessMemoryInfo): no extra library
  484. #include <windows.h>
  485. #include <psapi.h>
  486. #endif
  487. std::int64_t lynx::toolbar::PerformanceMeter::processMemoryBytes()
  488. {
  489. #if JUCE_WINDOWS
  490. PROCESS_MEMORY_COUNTERS counters {};
  491. if (GetProcessMemoryInfo (GetCurrentProcess(), &counters, sizeof (counters)))
  492. return (std::int64_t) counters.WorkingSetSize;
  493. return 0;
  494. #elif JUCE_LINUX
  495. // resident pages, from /proc
  496. const auto fields = juce::StringArray::fromTokens (juce::File ("/proc/self/statm").loadFileAsString(), " ", "");
  497. return fields.size() > 1 ? (std::int64_t) fields[1].getLargeIntValue() * 4096 : 0;
  498. #else
  499. return 0;
  500. #endif
  501. }