| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- #include "ui/Toolbar.h"
- #include "ui/HintBar.h"
- #include "ui/I18n.h"
- #include "ui/Icons.h"
- #include "ui/Theme.h"
- namespace lynx::toolbar
- {
- namespace
- {
- juce::String barBeatStep (const Song& song, double ticks, int beatsPerBar)
- {
- const auto beats = ticks / ppq;
- int bar = 0, beat = 0;
- if (song.mode == PlayMode::song) // the Playlist ruler applies (with time signature changes)
- {
- const auto current = song.barAt ((std::int64_t) ticks);
- bar = current.number;
- beat = (int) ((ticks - (double) current.tick) / ppq) + 1;
- }
- else
- {
- const auto perBar = juce::jmax (1, beatsPerBar);
- bar = (int) (beats / perBar) + 1;
- beat = (int) std::fmod (beats, (double) perBar) + 1;
- }
- const auto step = (int) (std::fmod (beats, 1.0) * stepsPerBeat) + 1;
- return juce::String::formatted ("%d : %d : %d", bar, beat, step);
- }
- double loopLength (const Song& song)
- {
- if (song.patterns.empty())
- return 1.0;
- const auto length = song.mode == PlayMode::song
- ? song.songLength()
- : song.patterns[(std::size_t) juce::jlimit (0, (int) song.patterns.size() - 1, song.currentPattern)].effectiveLength (song.beatsPerBar);
- return (double) juce::jmax<std::int64_t> (1, length);
- }
- double playheadTicks (AudioEngine& engine)
- {
- return std::fmod (juce::jmax (0.0, engine.getTransport().positionTicks.load()), loopLength (engine.getSong()));
- }
- juce::String centsText (double cents)
- {
- const auto c = juce::roundToInt (cents);
- return (c > 0 ? "+" : "") + juce::String (c) + " " + tr ("cents");
- }
- } // namespace
- // --- song position -------------------------------------------------------------------------------
- SongPositionBar::SongPositionBar (AudioEngine& e) : engine (e)
- {
- setMouseCursor (juce::MouseCursor::PointingHandCursor);
- }
- double SongPositionBar::length() const { return loopLength (engine.getSong()); }
- double SongPositionBar::position() const { return playheadTicks (engine); }
- juce::Rectangle<float> SongPositionBar::track() const
- {
- return getLocalBounds().toFloat().reduced (5.0f, 0.0f).withSizeKeepingCentre ((float) getWidth() - 10.0f, 4.0f);
- }
- void SongPositionBar::update()
- {
- const auto fraction = position() / length();
- if (std::abs (fraction - shownFraction) * getWidth() >= 0.5)
- {
- shownFraction = fraction;
- repaint();
- }
- }
- void SongPositionBar::paint (juce::Graphics& g)
- {
- using namespace theme;
- const auto t = track();
- const auto& song = engine.getSong();
- const auto active = dragging || isMouseOver();
- g.setColour (colour::bg0);
- g.fillRoundedRectangle (t.expanded (0.0f, 1.0f), 2.5f);
- // the Playlist loop (Song mode)
- if (song.mode == PlayMode::song && song.hasLoop())
- {
- const auto x0 = t.getX() + t.getWidth() * (float) ((double) song.loopStart / length());
- const auto x1 = t.getX() + t.getWidth() * (float) juce::jmin (1.0, (double) song.loopEnd / length());
- g.setColour (colour::info.withAlpha (0.35f));
- g.fillRect (juce::Rectangle<float> (x0, t.getY() - 1.0f, juce::jmax (1.0f, x1 - x0), t.getHeight() + 2.0f));
- }
- const auto fraction = (float) juce::jlimit (0.0, 1.0, position() / length());
- const auto x = t.getX() + t.getWidth() * fraction;
- g.setColour (engine.getTransport().playing.load() ? colour::accent : colour::accent.withMultipliedSaturation (0.55f).withMultipliedBrightness (0.8f));
- g.fillRoundedRectangle (t.withRight (juce::jmax (t.getX() + 1.0f, x)), 2.0f);
- const auto radius = active ? 5.0f : 3.5f;
- g.setColour (active ? colour::textPrimary : colour::textSecondary);
- g.fillEllipse (juce::Rectangle<float> (radius * 2.0f, radius * 2.0f).withCentre ({ x, t.getCentreY() }));
- }
- void SongPositionBar::seekTo (int x)
- {
- const auto t = track();
- const auto fraction = juce::jlimit (0.0, 1.0, (double) ((float) x - t.getX()) / juce::jmax (1.0f, t.getWidth()));
- auto ticks = fraction * length();
- if (! juce::ModifierKeys::currentModifiers.isAltDown()) // Alt: no snap (as everywhere)
- ticks = std::round (ticks / ticksPerStep) * ticksPerStep;
- engine.seek (juce::jlimit (0.0, length() - 1.0, ticks));
- update();
- repaint();
- }
- void SongPositionBar::mouseDown (const juce::MouseEvent& e)
- {
- if (! e.mods.isLeftButtonDown())
- return;
- dragging = true;
- seekTo (e.x);
- }
- void SongPositionBar::mouseDrag (const juce::MouseEvent& e)
- {
- if (dragging)
- seekTo (e.x);
- }
- void SongPositionBar::mouseUp (const juce::MouseEvent&)
- {
- dragging = false;
- repaint();
- }
- void SongPositionBar::mouseWheelMove (const juce::MouseEvent&, const juce::MouseWheelDetails& wheel)
- {
- const auto delta = std::abs (wheel.deltaY) >= std::abs (wheel.deltaX) ? wheel.deltaY : -wheel.deltaX;
- if (delta == 0.0f)
- return;
- const auto bar = (double) ppq * juce::jmax (1, engine.getSong().beatsPerBar);
- const auto current = std::round (position() / bar) * bar;
- engine.seek (juce::jlimit (0.0, juce::jmax (0.0, length() - bar), current + (delta > 0.0f ? -bar : bar) * (wheel.isReversed ? -1.0 : 1.0)));
- update();
- }
- ui::Hint SongPositionBar::hintAt (juce::Point<int>)
- {
- const auto& song = engine.getSong();
- ui::Hint h;
- h.text = (song.mode == PlayMode::song ? tr ("Song position") : tr ("Pattern position"))
- + ": " + tr ("[LMB] click or drag to move the playhead [Wheel] one bar [Alt] no snap");
- const auto bars = juce::jmax (1, juce::roundToInt (length() / ((double) ppq * juce::jmax (1, song.beatsPerBar))));
- h.value = tr ("Bar {0} of {1}", { barBeatStep (song, position(), song.beatsPerBar).upToFirstOccurrenceOf (" ", false, false), juce::String (bars) });
- return h;
- }
- // --- time display --------------------------------------------------------------------------------
- juce::String TimeDisplay::text() const
- {
- const auto& song = engine.getSong();
- const auto ticks = playheadTicks (engine);
- if (! clockMode)
- return barBeatStep (song, ticks, engine.getTransport().beatsPerBar.load());
- const auto seconds = song.secondsForTicks ((std::int64_t) ticks);
- const auto whole = (int) seconds;
- return juce::String::formatted ("%d : %02d : %02d", whole / 60, whole % 60, (int) ((seconds - whole) * 100.0));
- }
- void TimeDisplay::update()
- {
- if (const auto t = text(); t != shown)
- {
- shown = t;
- repaint();
- }
- }
- void TimeDisplay::paint (juce::Graphics& g)
- {
- using namespace theme;
- const auto area = getLocalBounds();
- g.setColour (colour::bg0);
- g.fillRoundedRectangle (area.toFloat(), radius::md);
- if (isMouseOver())
- {
- g.setColour (colour::borderStrong);
- g.drawRoundedRectangle (area.toFloat().reduced (0.5f), radius::md, 1.0f);
- }
- g.setColour (colour::textPrimary);
- g.setFont (monoFont (juce::jmin (19.0f, (float) area.getHeight() * 0.5f)));
- g.drawText (shown.isEmpty() ? text() : shown, area.withTrimmedBottom (12), juce::Justification::centred, false);
- g.setColour (colour::textDisabled);
- g.setFont (font (type::caption - 1.0f));
- g.drawText (clockMode ? tr ("MIN : SEC : CS") : tr ("BAR : BEAT : STEP"), area.withTrimmedTop (area.getHeight() - 14).withTrimmedBottom (2),
- juce::Justification::centred, false);
- }
- void TimeDisplay::mouseUp (const juce::MouseEvent& e)
- {
- if (e.mods.isPopupMenu() || ! getLocalBounds().contains (e.getPosition()))
- return;
- clockMode = ! clockMode;
- shown = {};
- update();
- repaint();
- }
- ui::Hint TimeDisplay::hintAt (juce::Point<int>)
- {
- ui::Hint h;
- h.text = (clockMode ? tr ("Time: minutes : seconds : hundredths") : tr ("Time: bar : beat : step"))
- + " " + tr ("[LMB] switch between bars and minutes");
- h.value = text();
- return h;
- }
- // --- master volume and pitch ---------------------------------------------------------------------
- MasterKnob::MasterKnob (AudioEngine& e, Kind k) : engine (e), kind (k)
- {
- setRepaintsOnMouseActivity (true);
- }
- bool MasterKnob::available() const
- {
- return kind == Kind::pitch || ! engine.getSong().mixer.empty();
- }
- double MasterKnob::value() const
- {
- const auto& song = engine.getSong();
- if (kind == Kind::pitch)
- return song.masterPitch;
- return song.mixer.empty() ? (double) unityFader : (double) song.mixer.front().volume;
- }
- void MasterKnob::setValue (double v)
- {
- const auto s = spec();
- v = juce::jlimit (s.minimum, s.maximum, v);
- if (! available() || std::abs (v - value()) < 1e-9)
- return;
- if (kind == Kind::pitch)
- engine.editSong ([v] (Song& song) { song.masterPitch = (float) v; });
- else
- engine.editSong ([v] (Song& song) {
- if (! song.mixer.empty())
- song.mixer.front().volume = (float) v;
- });
- update();
- }
- ui::ValueSpec MasterKnob::spec() const
- {
- if (kind == Kind::volume)
- {
- auto s = ui::faderSpec (tr ("Master volume"), unityFader);
- s.target = AutomationTarget { AutomationTargetKind::mixerVolume, 0 };
- return s;
- }
- ui::ValueSpec s;
- s.name = tr ("Master pitch");
- s.minimum = -1200.0;
- s.maximum = 1200.0;
- s.defaultValue = 0.0;
- s.pixelsForRange = 360.0;
- s.toText = [] (double v) { return centsText (v); };
- s.fromText = [] (const juce::String& t) -> std::optional<double> {
- const auto number = t.retainCharacters ("+-0123456789.");
- if (number.isEmpty())
- return std::nullopt;
- return number.getDoubleValue();
- };
- return s;
- }
- void MasterKnob::update()
- {
- if (const auto v = value(); v != shown)
- {
- shown = v;
- repaint();
- }
- }
- void MasterKnob::paint (juce::Graphics& g)
- {
- using namespace theme;
- auto area = getLocalBounds().toFloat();
- const auto knob = area.removeFromLeft (juce::jmin (area.getHeight(), 26.0f));
- const auto s = spec();
- const auto v = value();
- const auto hover = isMouseOver() || drag.isActive();
- const auto value01 = (float) ((v - s.minimum) / (s.maximum - s.minimum));
- drawKnob (g, knob, value01, kind == Kind::pitch, available() ? colour::knobArc : colour::textDisabled, hover && available());
- area.removeFromLeft (5.0f);
- auto caption = area.removeFromTop (area.getHeight() / 2.0f);
- g.setColour (colour::textDisabled);
- g.setFont (font (type::caption - 1.0f, true));
- g.drawText (kind == Kind::volume ? tr ("MASTER") : tr ("PITCH"), caption.withTrimmedTop (1.0f), juce::Justification::bottomLeft, true);
- g.setColour (drag.isActive() ? colour::textPrimary : colour::textSecondary);
- g.setFont (font (type::caption));
- g.drawText (available() ? s.text (v) : tr ("no mixer"), area, juce::Justification::topLeft, true);
- }
- void MasterKnob::mouseDown (const juce::MouseEvent& e)
- {
- if (! available())
- return;
- if (e.mods.isPopupMenu())
- {
- ui::showValueMenu (*this, kind == Kind::volume ? &engine : nullptr, spec(), value(), [this] (double v) { setValue (v); });
- return;
- }
- if (ui::isResetClick (e))
- {
- setValue (spec().defaultValue);
- return;
- }
- drag.begin (e, value());
- repaint();
- }
- void MasterKnob::mouseDrag (const juce::MouseEvent& e)
- {
- if (! drag.isActive())
- return;
- auto v = drag.update (e, spec());
- if (kind == Kind::pitch && ! e.mods.isShiftDown()) // semitone steps; Shift: free (as FL's master pitch)
- v = std::round (v / 100.0) * 100.0;
- setValue (v);
- }
- void MasterKnob::mouseUp (const juce::MouseEvent&)
- {
- drag.end();
- repaint();
- }
- void MasterKnob::mouseDoubleClick (const juce::MouseEvent& e)
- {
- if (available() && ! e.mods.isPopupMenu() && ! ui::isResetClick (e))
- ui::typeValue (*this, spec(), value(), [this] (double v) { setValue (v); });
- }
- void MasterKnob::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel)
- {
- if (! available())
- return;
- if (kind == Kind::pitch)
- {
- const auto delta = std::abs (wheel.deltaY) >= std::abs (wheel.deltaX) ? wheel.deltaY : -wheel.deltaX;
- if (delta != 0.0f)
- setValue (value() + (delta > 0.0f ? 1.0 : -1.0) * (wheel.isReversed ? -1.0 : 1.0) * (e.mods.isCtrlDown() ? 1.0 : 100.0));
- return;
- }
- if (const auto v = ui::wheelValue (value(), e, wheel, spec()))
- setValue (*v);
- }
- ui::Hint MasterKnob::hintAt (juce::Point<int>)
- {
- if (! available())
- return { tr ("Master volume: this project has no mixer"), {}, false, false };
- const auto gesture = kind == Kind::volume
- ? tr ("level of everything you hear (the Master insert's fader)") + " " + ui::valueGestures()
- : tr ("tunes the built-in instruments [LMB] drag (semitones) [Shift] cents [Wheel] semitone [Ctrl][Wheel] cent [Alt][LMB] reset [RMB] menu");
- return ui::valueHint (spec(), value(), gesture);
- }
- // --- CPU, memory, polyphony ----------------------------------------------------------------------
- void PerformanceMeter::setValues (double cpu01, std::int64_t memoryBytes, int polyphony)
- {
- cpu = cpu01;
- cpuSmoothed = cpuSmoothed * 0.8 + cpu01 * 0.2;
- memory = memoryBytes;
- voices = polyphony;
- peakVoices = juce::jmax (peakVoices, polyphony);
- repaint();
- }
- void PerformanceMeter::paint (juce::Graphics& g)
- {
- using namespace theme;
- auto area = getLocalBounds();
- auto row1 = area.removeFromTop (area.getHeight() / 2).reduced (0, 4);
- auto row2 = area.reduced (0, 4);
- const juce::Font caption (font (type::caption - 1.0f, true)), number (font (type::caption));
- // CPU: caption, bar, percent
- g.setFont (caption);
- g.setColour (colour::textDisabled);
- g.drawText (tr ("CPU"), row1.removeFromLeft (30), juce::Justification::centredLeft, false);
- g.setFont (number);
- g.setColour (colour::textSecondary);
- g.drawText (juce::String (juce::roundToInt (cpuSmoothed * 100.0)) + "%", row1.removeFromRight (34), juce::Justification::centredRight, false);
- const auto bar = row1.reduced (2, 3).toFloat();
- g.setColour (colour::bg0);
- g.fillRoundedRectangle (bar, 2.0f);
- const auto load = (float) juce::jlimit (0.0, 1.0, cpuSmoothed);
- g.setColour (load < 0.6f ? colour::meterLow : load < 0.85f ? colour::meterMid : colour::meterClip);
- if (load > 0.0f)
- g.fillRoundedRectangle (bar.withWidth (juce::jmax (2.0f, bar.getWidth() * load)), 2.0f);
- // memory and polyphony
- auto left = row2.removeFromLeft (row2.getWidth() * 58 / 100);
- g.setFont (caption);
- g.setColour (colour::textDisabled);
- g.drawText (tr ("RAM"), left.removeFromLeft (30), juce::Justification::centredLeft, false);
- g.setFont (number);
- g.setColour (colour::textSecondary);
- g.drawText (memory > 0 ? juce::String (juce::roundToInt ((double) memory / (1024.0 * 1024.0))) + " MB" : juce::String ("-"), left,
- juce::Justification::centredLeft, false);
- g.setFont (caption);
- g.setColour (colour::textDisabled);
- g.drawText (tr ("POLY"), row2.removeFromLeft (32), juce::Justification::centredLeft, false);
- g.setFont (number);
- g.setColour (voices > 0 ? colour::textPrimary : colour::textSecondary);
- g.drawText (juce::String (voices), row2, juce::Justification::centredRight, false);
- }
- ui::Hint PerformanceMeter::hintAt (juce::Point<int> p)
- {
- ui::Hint h;
- if (p.y < getHeight() / 2)
- {
- h.text = tr ("CPU: how much of the audio deadline the processing takes; near 100% the sound breaks up");
- h.value = juce::String (cpu * 100.0, 1) + "%";
- }
- else if (p.x < getWidth() * 58 / 100)
- {
- h.text = tr ("RAM: memory used by LYNX (samples, plugins, undo history)");
- h.value = juce::String ((double) memory / (1024.0 * 1024.0), 0) + " MB";
- }
- else
- {
- h.text = tr ("Polyphony: notes sounding now in the song and from the keyboard (voices still releasing are not counted)");
- h.value = juce::String (voices) + " (" + tr ("max {0}", { juce::String (peakVoices) }) + ")";
- }
- return h;
- }
- // --- output monitor ------------------------------------------------------------------------------
- void OutputMonitor::update()
- {
- if (! isShowing() || getWidth() <= 0)
- return;
- engine.copyScope (samples.data(), (int) samples.size());
- float now = 0.0f;
- for (auto s : samples)
- now = juce::jmax (now, std::abs (s));
- peak = juce::jmax (now, peak * 0.82f); // fast attack, smooth fall
- if (now >= held || holdFrames <= 0)
- {
- held = now;
- holdFrames = 30; // about a second at 30 Hz
- }
- else
- --holdFrames;
- if (now >= 0.999f)
- clipFrames = 60;
- else if (clipFrames > 0)
- --clipFrames;
- repaint();
- }
- void OutputMonitor::paint (juce::Graphics& g)
- {
- using namespace theme;
- auto area = getLocalBounds().toFloat();
- g.setColour (colour::bg0);
- g.fillRoundedRectangle (area, radius::md);
- // peak meter on the right: -60..0 dB, the top segment red while it clipped recently
- auto meter = area.removeFromRight (8.0f).reduced (1.5f, 4.0f);
- auto level = [] (float gain) { return juce::jlimit (0.0f, 1.0f, (juce::Decibels::gainToDecibels (gain, -60.0f) + 60.0f) / 60.0f); };
- g.setColour (colour::bg3);
- g.fillRoundedRectangle (meter, 1.5f);
- const auto filled = meter.withTop (meter.getBottom() - meter.getHeight() * level (peak));
- g.setGradientFill (juce::ColourGradient::vertical (colour::meterMid, meter.getY() + meter.getHeight() * 0.1f, colour::meterLow,
- meter.getY() + meter.getHeight() * 0.4f));
- g.fillRect (filled);
- if (held > 0.0f)
- {
- g.setColour (colour::textSecondary);
- g.fillRect (meter.withHeight (1.0f).withY (meter.getBottom() - meter.getHeight() * level (held)));
- }
- if (clipFrames > 0)
- {
- g.setColour (colour::meterClip);
- g.fillRoundedRectangle (meter.withHeight (4.0f), 1.0f);
- }
- // oscilloscope
- auto scope = area.reduced (4.0f, 3.0f);
- g.setColour (colour::border);
- g.drawHorizontalLine ((int) scope.getCentreY(), scope.getX(), scope.getRight());
- juce::Path wave;
- const auto mid = scope.getCentreY();
- const auto half = scope.getHeight() * 0.47f;
- const auto width = juce::jmax (1, (int) scope.getWidth());
- for (int x = 0; x < width; ++x)
- {
- const auto index = (std::size_t) (x * (int) samples.size() / width);
- const auto y = mid - juce::jlimit (-1.0f, 1.0f, samples[index]) * half;
- if (x == 0)
- wave.startNewSubPath (scope.getX(), y);
- else
- wave.lineTo (scope.getX() + (float) x, y);
- }
- g.setColour ((clipFrames > 0 ? colour::meterClip : colour::accent).withAlpha (0.9f));
- g.strokePath (wave, juce::PathStrokeType (1.3f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
- }
- ui::Hint OutputMonitor::hintAt (juce::Point<int>)
- {
- ui::Hint h;
- h.text = tr ("Output: the waveform and peak level of what you hear (the meter's top turns red when it clipped)");
- h.value = held <= 1.0e-5f ? juce::String ("-inf dB") : juce::String (juce::Decibels::gainToDecibels (held), 1) + " dB";
- return h;
- }
- } // namespace lynx::toolbar
- // The process memory comes from the OS; its headers go last (windows.h defines macros such as `small`).
- #if JUCE_WINDOWS
- #ifndef NOMINMAX
- #define NOMINMAX
- #endif
- #define PSAPI_VERSION 2 // GetProcessMemoryInfo from kernel32 (K32GetProcessMemoryInfo): no extra library
- #include <windows.h>
- #include <psapi.h>
- #endif
- std::int64_t lynx::toolbar::PerformanceMeter::processMemoryBytes()
- {
- #if JUCE_WINDOWS
- PROCESS_MEMORY_COUNTERS counters {};
- if (GetProcessMemoryInfo (GetCurrentProcess(), &counters, sizeof (counters)))
- return (std::int64_t) counters.WorkingSetSize;
- return 0;
- #elif JUCE_LINUX
- // resident pages, from /proc
- const auto fields = juce::StringArray::fromTokens (juce::File ("/proc/self/statm").loadFileAsString(), " ", "");
- return fields.size() > 1 ? (std::int64_t) fields[1].getLargeIntValue() * 4096 : 0;
- #else
- return 0;
- #endif
- }
|