]> git.sesse.net Git - nageru/commitdiff
Some refactoring of common VU functions.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 31 Oct 2015 11:38:51 +0000 (12:38 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 31 Oct 2015 11:38:51 +0000 (12:38 +0100)
Makefile
lrameter.cpp
vu_common.cpp [new file with mode: 0644]
vu_common.h [new file with mode: 0644]
vumeter.cpp

index d8dd820ea978d5ce16e586716e46e42b0b775234..f20a3df6a6997575960da84b39eb2bc6c7ee9270 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CXXFLAGS := -O2 -march=native -g -std=gnu++11 -Wall -Wno-deprecated-declarations
 LDFLAGS=$(shell pkg-config --libs $(PKG_MODULES)) -lEGL -lGL -pthread -lva -lva-drm -lva-x11 -lX11 -lavformat -lavcodec -lavutil -lzita-resampler -lebur128
 
 # Qt objects
-OBJS=glwidget.o main.o mainwindow.o vumeter.o lrameter.o
+OBJS=glwidget.o main.o mainwindow.o vumeter.o lrameter.o vu_common.o
 OBJS += glwidget.moc.o mainwindow.moc.o vumeter.moc.o lrameter.moc.o
 
 # Mixer objects
index da651014ec268e5a5283ae1851cbce8c6c59e11d..7f7aaf1cd672de81a60920b427c68fe7726b9c1f 100644 (file)
@@ -1,29 +1,10 @@
 #include <QPainter>
 
 #include "lrameter.h"
+#include "vu_common.h"
 
 using namespace std;
 
-namespace {
-
-int lufs_to_pos(float level_lu, int height)
-{
-       const float min_level = 9.0f;    // y=0 is top of screen, so “min” is the loudest level.
-       const float max_level = -18.0f;
-
-       // Handle -inf.
-       if (level_lu < max_level) {
-               return height - 1;
-       }
-
-       int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
-       y = std::max(y, 0);
-       y = std::min(y, height - 1);
-       return y;
-}
-
-}  // namespace
-
 LRAMeter::LRAMeter(QWidget *parent)
        : QWidget(parent)
 {
@@ -32,17 +13,8 @@ LRAMeter::LRAMeter(QWidget *parent)
 void LRAMeter::paintEvent(QPaintEvent *event)
 {
        QPainter painter(this);
-       const int margin = 5;
 
        painter.fillRect(0, 0, width(), height(), parentWidget()->palette().window());
-       painter.fillRect(margin, 0, width() - 2 * margin, height(), Qt::black);
-
-       // TODO: QLinearGradient is not gamma-correct; we might want to correct for that.
-       QLinearGradient on(0, 0, 0, height());
-        on.setColorAt(0.0f, QColor(255, 0, 0));
-        on.setColorAt(0.5f, QColor(255, 255, 0));
-        on.setColorAt(1.0f, QColor(0, 255, 0));
-       QColor off(80, 80, 80);
 
        float level_lufs;
        {
@@ -51,22 +23,8 @@ void LRAMeter::paintEvent(QPaintEvent *event)
        }
 
        float level_lu = level_lufs + 23.0f;
-       int y = lufs_to_pos(level_lu, height());
-
-       // Draw bars colored up until the level, then gray from there.
-       for (int level = -18; level < 9; ++level) {
-               int min_y = lufs_to_pos(level + 1.0f, height()) + 1;
-               int max_y = lufs_to_pos(level, height()) - 1;
-
-               if (y > max_y) {
-                       painter.fillRect(margin, min_y, width() - 2 * margin, max_y - min_y, off);
-               } else if (y < min_y) {
-                       painter.fillRect(margin, min_y, width() - 2 * margin, max_y - min_y, on);
-               } else {
-                       painter.fillRect(margin, min_y, width() - 2 * margin, y - min_y, off);
-                       painter.fillRect(margin, y, width() - 2 * margin, max_y - y, on);
-               }
-       }
+       const int margin = 5;
+       draw_vu_meter(painter, level_lu, width(), height(), margin);
 
        // Draw the target area (+/-1 LU is allowed EBU range).
        int min_y = lufs_to_pos(1.0f, height());
diff --git a/vu_common.cpp b/vu_common.cpp
new file mode 100644 (file)
index 0000000..9a6a35a
--- /dev/null
@@ -0,0 +1,50 @@
+#include "vu_common.h"
+#include <math.h>
+#include <algorithm>
+
+using namespace std;
+
+int lufs_to_pos(float level_lu, int height)
+{       
+       const float min_level = 9.0f;    // y=0 is top of screen, so “min” is the loudest level.
+       const float max_level = -18.0f;
+
+       // Handle -inf.
+       if (level_lu < max_level) {
+               return height - 1;
+       }
+
+       int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
+       y = std::max(y, 0);
+       y = std::min(y, height - 1);
+       return y;
+}
+
+void draw_vu_meter(QPainter &painter, float level_lu, int width, int height, int margin)
+{
+       painter.fillRect(margin, 0, width - 2 * margin, height, Qt::black);
+
+       // TODO: QLinearGradient is not gamma-correct; we might want to correct for that.
+       QLinearGradient on(0, 0, 0, height);
+       on.setColorAt(0.0f, QColor(255, 0, 0));
+       on.setColorAt(0.5f, QColor(255, 255, 0));
+       on.setColorAt(1.0f, QColor(0, 255, 0));
+       QColor off(80, 80, 80);
+
+       int y = lufs_to_pos(level_lu, height);
+
+       // Draw bars colored up until the level, then gray from there.
+       for (int level = -18; level < 9; ++level) {
+               int min_y = lufs_to_pos(level + 1.0f, height) + 1;
+               int max_y = lufs_to_pos(level, height) - 1;
+
+               if (y > max_y) {
+                       painter.fillRect(margin, min_y, width - 2 * margin, max_y - min_y, off);
+               } else if (y < min_y) {
+                       painter.fillRect(margin, min_y, width - 2 * margin, max_y - min_y, on);
+               } else {
+                       painter.fillRect(margin, min_y, width - 2 * margin, y - min_y, off);
+                       painter.fillRect(margin, y, width - 2 * margin, max_y - y, on);
+               }
+       }
+}
diff --git a/vu_common.h b/vu_common.h
new file mode 100644 (file)
index 0000000..4a50552
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _VU_COMMON_H
+#define _VU_COMMON_H 1
+
+#include <QPainter>
+
+int lufs_to_pos(float level_lu, int height);
+void draw_vu_meter(QPainter &painter, float level_lu, int width, int height, int margin);
+
+#endif // !defined(_VU_COMMON_H)
index e377211fe1371c0abb2c28bec5b65ec14985119e..da93c1e380732535c18ee38d038e2c9996184fe0 100644 (file)
@@ -1,29 +1,10 @@
 #include <QPainter>
 
 #include "vumeter.h"
+#include "vu_common.h"
 
 using namespace std;
 
-namespace {
-
-int lufs_to_pos(float level_lu, int height)
-{
-       const float min_level = 9.0f;    // y=0 is top of screen, so “min” is the loudest level.
-       const float max_level = -18.0f;
-
-       // Handle -inf.
-       if (level_lu < max_level) {
-               return height - 1;
-       }
-
-       int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
-       y = std::max(y, 0);
-       y = std::min(y, height - 1);
-       return y;
-}
-
-}  // namespace
-
 VUMeter::VUMeter(QWidget *parent)
        : QWidget(parent)
 {