From 3645c8a2d88de28430bbf9874f6119ddd3c22e24 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 12 Apr 2024 23:07:04 +0200 Subject: [PATCH 1/7] Scalable LFO graph Make the rendering of the LFO graph scalable. Change the fixed size to a minimum size. Adjust the rendering code such that it uses the width and height of the widget instead of the background pixmap. Only draw only poly line once instead of many line segments. Collect the points in the for-loop to be able to do so. This makes the code a bit more understandable because we now compute exacly one point per iteration in the for-loop. Use the same interpolation for the line color like in the envelope graph. Rename some variables to make them consistent with the other code. Remove the member `m_params` which is not used anyway. This also allows for the removal of the overridden `modelChanged` method. --- include/LfoGraph.h | 4 -- src/gui/instrument/LfoGraph.cpp | 87 +++++++++++++++++---------------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/include/LfoGraph.h b/include/LfoGraph.h index 733db3a349c..94c6bf0d556 100644 --- a/include/LfoGraph.h +++ b/include/LfoGraph.h @@ -45,8 +45,6 @@ class LfoGraph : public QWidget, public ModelView LfoGraph(QWidget* parent); protected: - void modelChanged() override; - void mousePressEvent(QMouseEvent* me) override; void paintEvent(QPaintEvent* pe) override; @@ -56,8 +54,6 @@ class LfoGraph : public QWidget, public ModelView private: QPixmap m_lfoGraph = embed::getIconPixmap("lfo_graph"); - EnvelopeAndLfoParameters* m_params = nullptr; - float m_randomGraph {0.}; }; diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index d02f583d0c7..357fe399559 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -30,6 +30,7 @@ #include "EnvelopeAndLfoParameters.h" #include "Oscillator.h" +#include "ColorHelper.h" #include "gui_templates.h" @@ -45,12 +46,7 @@ LfoGraph::LfoGraph(QWidget* parent) : QWidget(parent), ModelView(nullptr, this) { - setFixedSize(m_lfoGraph.size()); -} - -void LfoGraph::modelChanged() -{ - m_params = castModel(); + setMinimumSize(m_lfoGraph.size()); } void LfoGraph::mousePressEvent(QMouseEvent* me) @@ -78,81 +74,90 @@ void LfoGraph::paintEvent(QPaintEvent*) const f_cnt_t attackFrames = params->getLfoAttackFrames(); const f_cnt_t oscillationFrames = params->getLfoOscillationFrames(); const bool x100 = params->getX100Model().value(); - const int waveModel = params->getLfoWaveModel().value(); + const int lfoWaveModel = params->getLfoWaveModel().value(); + const auto * userWave = params->getLfoUserWave().get(); - int LFO_GRAPH_W = m_lfoGraph.width() - 3; // subtract border - int LFO_GRAPH_H = m_lfoGraph.height() - 6; // subtract border - int graph_x_base = 2; - int graph_y_base = 3 + LFO_GRAPH_H / 2; + const int margin = 3; + const int lfoGraphWidth = width() - margin; // subtract margin + const int lfoGraphHeight = height() - 2 * margin; // subtract margin + int graphBaseX = 2; + int graphBaseY = margin + lfoGraphHeight / 2; - const float frames_for_graph = + const float framesForGraph = SECS_PER_LFO_OSCILLATION * Engine::audioEngine()->baseSampleRate() / 10; - const float gray = 1.0 - fabsf(amount); - const auto red = static_cast(96 * gray); - const auto green = static_cast(255 - 159 * gray); - const auto blue = static_cast(128 - 32 * gray); - const QColor penColor(red, green, blue); - p.setPen(QPen(penColor, 1.5)); + float oscFrames = oscillationFrames * (x100 ? 100. : 1.); - float osc_frames = oscillationFrames; + QPolygonF polyLine; + polyLine << QPointF(graphBaseX - 1, graphBaseY); - if (x100) { osc_frames *= 100.0f; } - - float old_y = 0; - for (int x = 0; x <= LFO_GRAPH_W; ++x) + // Collect the points for the poly line by sampling the LFO according to its shape + for (int x = 0; x <= lfoGraphWidth; ++x) { - float val = 0.0; - float cur_sample = x * frames_for_graph / LFO_GRAPH_W; - if (static_cast(cur_sample) > predelayFrames) + float value = 0.0; + float currentSample = x * framesForGraph / lfoGraphWidth; + const f_cnt_t sampleAsFrameCount = static_cast(currentSample); + if (sampleAsFrameCount > predelayFrames) { - float phase = (cur_sample -= predelayFrames) / osc_frames; - switch (static_cast(waveModel)) + const float phase = (currentSample -= predelayFrames) / oscFrames; + + const auto lfoShape = static_cast(lfoWaveModel); + switch (lfoShape) { case EnvelopeAndLfoParameters::LfoShape::SineWave: default: - val = Oscillator::sinSample(phase); + value = Oscillator::sinSample(phase); break; case EnvelopeAndLfoParameters::LfoShape::TriangleWave: - val = Oscillator::triangleSample(phase); + value = Oscillator::triangleSample(phase); break; case EnvelopeAndLfoParameters::LfoShape::SawWave: - val = Oscillator::sawSample(phase); + value = Oscillator::sawSample(phase); break; case EnvelopeAndLfoParameters::LfoShape::SquareWave: - val = Oscillator::squareSample(phase); + value = Oscillator::squareSample(phase); break; case EnvelopeAndLfoParameters::LfoShape::RandomWave: if (x % (int)(900 * lfoSpeed + 1) == 0) { m_randomGraph = Oscillator::noiseSample(0.0); } - val = m_randomGraph; + value = m_randomGraph; break; case EnvelopeAndLfoParameters::LfoShape::UserDefinedWave: - val = Oscillator::userWaveSample(m_params->getLfoUserWave().get(), phase); + value = Oscillator::userWaveSample(userWave, phase); break; } - if (static_cast(cur_sample) <= attackFrames) + if (sampleAsFrameCount <= attackFrames) { - val *= cur_sample / attackFrames; + value *= currentSample / attackFrames; } } - float cur_y = -LFO_GRAPH_H / 2.0f * val; - p.drawLine(QLineF(graph_x_base + x - 1, graph_y_base + old_y, graph_x_base + x, graph_y_base + cur_y)); - old_y = cur_y; + const float currentY = -lfoGraphHeight / 2.0f * value; + + polyLine << QPointF(graphBaseX + x, graphBaseY + currentY); } + // Compute the color of the lines based on the amount of the LFO + const float absAmount = std::abs(amount); + const QColor noAmountColor{96, 91, 96}; + const QColor fullAmountColor{0, 255, 128}; + const QColor lineColor{ColorHelper::interpolateInRgb(noAmountColor, fullAmountColor, absAmount)}; + + p.setPen(QPen(lineColor, 1.5)); + + p.drawPolyline(polyLine); + // Draw the info text - int ms_per_osc = static_cast(SECS_PER_LFO_OSCILLATION * lfoSpeed * 1000.0); + int msPerOsc = static_cast(SECS_PER_LFO_OSCILLATION * lfoSpeed * 1000.f); QFont f = p.font(); f.setPixelSize(height() * 0.2); p.setFont(f); p.setPen(QColor(201, 201, 225)); - p.drawText(4, m_lfoGraph.height() - 6, tr("%1 ms/LFO").arg(ms_per_osc)); + p.drawText(4, height() - 6, tr("%1 ms/LFO").arg(msPerOsc)); } void LfoGraph::toggleAmountModel() From aa8a6da7f9b60df133fbf19de709e9a1b317c483 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 12 Apr 2024 23:14:29 +0200 Subject: [PATCH 2/7] Use "Hz" instead of "ms/LFO" Use the more common unit "Hz" to display the frequency of the LFO instead of "ms/LFO". The frequency is always displayed with three digits after the decimal separator to prevent "jumps". --- src/gui/instrument/LfoGraph.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 357fe399559..8f51b298fff 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -151,13 +151,13 @@ void LfoGraph::paintEvent(QPaintEvent*) p.drawPolyline(polyLine); // Draw the info text - int msPerOsc = static_cast(SECS_PER_LFO_OSCILLATION * lfoSpeed * 1000.f); + const float hertz = 1. / (SECS_PER_LFO_OSCILLATION * lfoSpeed); QFont f = p.font(); f.setPixelSize(height() * 0.2); p.setFont(f); p.setPen(QColor(201, 201, 225)); - p.drawText(4, height() - 6, tr("%1 ms/LFO").arg(msPerOsc)); + p.drawText(4, height() - 6, tr("%1 Hz").arg(hertz, 0, 'f', 3)); } void LfoGraph::toggleAmountModel() From ee72f46f0f978d00cda91530285ad6f2a72cfe02 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 12 Apr 2024 23:16:34 +0200 Subject: [PATCH 3/7] Take "Freq * 100" into account This commit fixes a bug where the "Freq * 100" option was not taken into account when computing and displaying the frequency of the LFO. --- src/gui/instrument/LfoGraph.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 8f51b298fff..8414e03c7a2 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -151,7 +151,7 @@ void LfoGraph::paintEvent(QPaintEvent*) p.drawPolyline(polyLine); // Draw the info text - const float hertz = 1. / (SECS_PER_LFO_OSCILLATION * lfoSpeed); + const float hertz = 1. / (SECS_PER_LFO_OSCILLATION * lfoSpeed) * (x100 ? 100. : 1.); QFont f = p.font(); f.setPixelSize(height() * 0.2); From 1027bc71949e5665d7cff543bec5dbf72b223f96 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 12 Apr 2024 23:26:02 +0200 Subject: [PATCH 4/7] Keep info text legible Draw a slightly transparent black rectangle underneath the text to keep it legible, e.g. for high frequencies with an LFO amount of 1 which results in a very bright and dense graph. --- src/gui/instrument/LfoGraph.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 8414e03c7a2..745aee0ad3b 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -152,12 +152,26 @@ void LfoGraph::paintEvent(QPaintEvent*) // Draw the info text const float hertz = 1. / (SECS_PER_LFO_OSCILLATION * lfoSpeed) * (x100 ? 100. : 1.); + const auto infoText = tr("%1 Hz").arg(hertz, 0, 'f', 3); + // First configure the font so that we get correct results for the font metrics used below QFont f = p.font(); f.setPixelSize(height() * 0.2); p.setFont(f); + + // This is the position where the text and its rectangle will be rendered + const QPoint textPosition(4, height() - 6); + + // Draw a slightly transparent black rectangle underneath the text to keep it legible + const QFontMetrics fontMetrics(f); + // This gives the bounding rectangle if the text was rendered at the origin ... + const auto boundingRect = fontMetrics.boundingRect(infoText); + // ... so we translate it to the actual position where the text will be rendered. + p.fillRect(boundingRect.translated(textPosition), QColor{0, 0, 0, 192}); + + // Now draw the actual info text p.setPen(QColor(201, 201, 225)); - p.drawText(4, height() - 6, tr("%1 Hz").arg(hertz, 0, 'f', 3)); + p.drawText(textPosition, infoText); } void LfoGraph::toggleAmountModel() From 49868d4b3ff996f6cc0826b9ea8e3337a5e0707a Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 12 Apr 2024 23:31:37 +0200 Subject: [PATCH 5/7] Extract drawing of info text into method Extract the drawing of the info text into its own private method `drawInfoText`. --- include/LfoGraph.h | 1 + src/gui/instrument/LfoGraph.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/LfoGraph.h b/include/LfoGraph.h index 94c6bf0d556..9d566770f09 100644 --- a/include/LfoGraph.h +++ b/include/LfoGraph.h @@ -49,6 +49,7 @@ class LfoGraph : public QWidget, public ModelView void paintEvent(QPaintEvent* pe) override; private: + void drawInfoText(const EnvelopeAndLfoParameters&); void toggleAmountModel(); private: diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 745aee0ad3b..470bea51079 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -150,7 +150,16 @@ void LfoGraph::paintEvent(QPaintEvent*) p.drawPolyline(polyLine); - // Draw the info text + drawInfoText(*params); +} + +void LfoGraph::drawInfoText(const EnvelopeAndLfoParameters& params) +{ + QPainter p(this); + + const float lfoSpeed = params.getLfoSpeedModel().value(); + const bool x100 = params.getX100Model().value(); + const float hertz = 1. / (SECS_PER_LFO_OSCILLATION * lfoSpeed) * (x100 ? 100. : 1.); const auto infoText = tr("%1 Hz").arg(hertz, 0, 'f', 3); From 79f0b14221e631d2e345755ee814f5f4fb892b05 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sat, 13 Apr 2024 10:13:52 +0200 Subject: [PATCH 6/7] Code review changes --- src/gui/instrument/LfoGraph.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 470bea51079..76670823761 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -96,7 +96,7 @@ void LfoGraph::paintEvent(QPaintEvent*) { float value = 0.0; float currentSample = x * framesForGraph / lfoGraphWidth; - const f_cnt_t sampleAsFrameCount = static_cast(currentSample); + const auto sampleAsFrameCount = static_cast(currentSample); if (sampleAsFrameCount > predelayFrames) { const float phase = (currentSample -= predelayFrames) / oscFrames; From f73b909c45ffd6e8dcc824635bdf396680fde9e8 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sat, 13 Apr 2024 19:26:06 +0200 Subject: [PATCH 7/7] Remove inline subtraction of the predelay The subtraction of the predelay from the current sample was inlined in a statement. This is removed with this commit. --- src/gui/instrument/LfoGraph.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gui/instrument/LfoGraph.cpp b/src/gui/instrument/LfoGraph.cpp index 76670823761..7edbacb09ea 100644 --- a/src/gui/instrument/LfoGraph.cpp +++ b/src/gui/instrument/LfoGraph.cpp @@ -99,7 +99,8 @@ void LfoGraph::paintEvent(QPaintEvent*) const auto sampleAsFrameCount = static_cast(currentSample); if (sampleAsFrameCount > predelayFrames) { - const float phase = (currentSample -= predelayFrames) / oscFrames; + currentSample -= predelayFrames; + const float phase = currentSample / oscFrames; const auto lfoShape = static_cast(lfoWaveModel); switch (lfoShape)