]> git.sesse.net Git - nageru/blob - lrameter.cpp
Add the beginnings of a loudness range meter.
[nageru] / lrameter.cpp
1 #include <QPainter>
2
3 #include "lrameter.h"
4
5 using namespace std;
6
7 namespace {
8
9 int lufs_to_pos(float level_lu, int height)
10 {
11         const float min_level = 9.0f;    // y=0 is top of screen, so “min” is the loudest level.
12         const float max_level = -18.0f;
13         int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
14         y = std::max(y, 0);
15         y = std::min(y, height - 1);
16         return y;
17 }
18
19 }  // namespace
20
21 LRAMeter::LRAMeter(QWidget *parent)
22         : QWidget(parent)
23 {
24 }
25
26 void LRAMeter::paintEvent(QPaintEvent *event)
27 {
28         QPainter painter(this);
29         const int margin = 5;
30
31         painter.fillRect(0, 0, width(), height(), parentWidget()->palette().window());
32         painter.fillRect(margin, 0, width() - 2 * margin, height(), Qt::black);
33
34         // TODO: QLinearGradient is not gamma-correct; we might want to correct for that.
35         QLinearGradient on(0, 0, 0, height());
36         on.setColorAt(0.0f, QColor(255, 0, 0));
37         on.setColorAt(0.5f, QColor(255, 255, 0));
38         on.setColorAt(1.0f, QColor(0, 255, 0));
39         QColor off(80, 80, 80);
40
41         float level_lufs;
42         {
43                 unique_lock<mutex> lock(level_mutex);
44                 level_lufs = this->level_lufs;
45         }
46
47         float level_lu = level_lufs + 23.0f;
48         int y = lufs_to_pos(level_lu, height());
49
50         // Draw bars colored up until the level, then gray from there.
51         for (int level = -18; level < 9; ++level) {
52                 int min_y = lufs_to_pos(level + 1.0f, height()) + 1;
53                 int max_y = lufs_to_pos(level, height()) - 1;
54
55                 if (y > max_y) {
56                         painter.fillRect(margin, min_y, width() - 2 * margin, max_y - min_y, off);
57                 } else if (y < min_y) {
58                         painter.fillRect(margin, min_y, width() - 2 * margin, max_y - min_y, on);
59                 } else {
60                         painter.fillRect(margin, min_y, width() - 2 * margin, y - min_y, off);
61                         painter.fillRect(margin, y, width() - 2 * margin, max_y - y, on);
62                 }
63         }
64
65         // Draw the target area (+/-1 LU is allowed EBU range).
66         int min_y = lufs_to_pos(1.0f, height());
67         int max_y = lufs_to_pos(-1.0f, height());
68
69         // FIXME: This outlining isn't so pretty.
70         {
71                 QPen pen(Qt::black);
72                 pen.setWidth(5);
73                 painter.setPen(pen);
74                 painter.drawRect(2, min_y, width() - 5, max_y - min_y);
75         }
76         {
77                 QPen pen;
78                 if (level_lu >= -1.0f && level_lu <= 1.0f) {
79                         pen.setColor(Qt::green);
80                 } else {
81                         pen.setColor(Qt::red);
82                 }
83                 pen.setWidth(3);
84                 painter.setPen(pen);
85                 painter.drawRect(2, min_y, width() - 5, max_y - min_y);
86         }
87 }