]> git.sesse.net Git - nageru/blob - nageru/vumeter.cpp
Add support for V4L2 output.
[nageru] / nageru / vumeter.cpp
1 #include "vumeter.h"
2
3 #include <QPainter>
4 #include <QRect>
5 #include "vu_common.h"
6
7 class QPaintEvent;
8 class QResizeEvent;
9
10 using namespace std;
11
12 VUMeter::VUMeter(QWidget *parent)
13         : QWidget(parent)
14 {
15 }
16
17 void VUMeter::resizeEvent(QResizeEvent *event)
18 {
19         recalculate_pixmaps();
20 }
21
22 void VUMeter::paintEvent(QPaintEvent *event)
23 {
24         QPainter painter(this);
25
26         float level_lufs[2], peak_lufs[2];
27         {
28                 lock_guard<mutex> lock(level_mutex);
29                 level_lufs[0] = this->level_lufs[0];
30                 level_lufs[1] = this->level_lufs[1];
31                 peak_lufs[0] = this->peak_lufs[0];
32                 peak_lufs[1] = this->peak_lufs[1];
33         }
34
35         int mid = width() / 2;
36
37         for (unsigned channel = 0; channel < 2; ++channel) {
38                 int left = (channel == 0) ? 0 : mid;
39                 int right = (channel == 0) ? mid : width();
40                 float level_lu = level_lufs[channel] - ref_level_lufs;
41                 int on_pos = lrint(lufs_to_pos(level_lu, height()));
42
43                 QRect off_rect(left, 0, right - left, on_pos);
44                 QRect on_rect(left, on_pos, right - left, height() - on_pos);
45
46                 painter.drawPixmap(off_rect, off_pixmap, off_rect);
47                 painter.drawPixmap(on_rect, on_pixmap, on_rect);
48
49                 float peak_lu = peak_lufs[channel] - ref_level_lufs;
50                 if (peak_lu >= min_level && peak_lu <= max_level) {
51                         int peak_pos = lrint(lufs_to_pos(peak_lu, height()));
52                         QRect peak_rect(left, peak_pos - 1, right, 2);
53                         painter.drawPixmap(peak_rect, full_on_pixmap, peak_rect);
54                 }
55         }
56 }
57
58 void VUMeter::recalculate_pixmaps()
59 {
60         full_on_pixmap = QPixmap(width(), height());
61         QPainter full_on_painter(&full_on_pixmap);
62         full_on_painter.fillRect(0, 0, width(), height(), parentWidget()->palette().window());
63         draw_vu_meter(full_on_painter, width(), height(), 0, 0.0, true, min_level, max_level, /*flip=*/false);
64
65         float margin = 0.5 * (width() - 20);
66
67         on_pixmap = QPixmap(width(), height());
68         QPainter on_painter(&on_pixmap);
69         on_painter.fillRect(0, 0, width(), height(), parentWidget()->palette().window());
70         draw_vu_meter(on_painter, width(), height(), margin, 2.0, true, min_level, max_level, /*flip=*/false);
71
72         off_pixmap = QPixmap(width(), height());
73         QPainter off_painter(&off_pixmap);
74         off_painter.fillRect(0, 0, width(), height(), parentWidget()->palette().window());
75         draw_vu_meter(off_painter, width(), height(), margin, 2.0, false, min_level, max_level, /*flip=*/false);
76 }