]> git.sesse.net Git - nageru/blob - vu_common.cpp
87254dae16c764a9381ecadf168dee586df6733a
[nageru] / vu_common.cpp
1 #include "vu_common.h"
2
3 #include <math.h>
4 #include <algorithm>
5
6 #include <QBrush>
7 #include <QColor>
8 #include <QPainter>
9
10 using namespace std;
11
12 double lufs_to_pos(float level_lu, int height, float min_level, float max_level)
13 {
14         // Note: “max” is the loudest level, but y=0 is top of screen.
15
16         // Handle -inf.
17         if (level_lu < min_level) {
18                 return height - 1;
19         }
20
21         double y = height * (level_lu - max_level) / (min_level - max_level);
22         y = max<double>(y, 0);
23         y = min<double>(y, height - 1);
24
25         // If we are big enough, snap to pixel grid instead of antialiasing
26         // the edges; the unevenness will be less noticeable than the blurriness.
27         double height_per_level = height / (max_level - min_level) - 2.0;
28         if (height_per_level >= 10.0) {
29                 y = round(y);
30         }
31
32         return y;
33 }
34
35 void draw_vu_meter(QPainter &painter, int width, int height, int margin, bool is_on, float min_level, float max_level, bool flip)
36 {
37         painter.fillRect(margin, 0, width - 2 * margin, height, Qt::black);
38
39         for (int y = 0; y < height; ++y) {
40                 // Find coverage of “on” rectangles in this pixel row.
41                 double coverage = 0.0;
42                 for (int level = floor(min_level); level <= ceil(max_level); ++level) {
43                         double min_y = lufs_to_pos(level + 1.0, height, min_level, max_level) + 1.0;
44                         double max_y = lufs_to_pos(level, height, min_level, max_level) - 1.0;
45                         min_y = std::max<double>(min_y, y);
46                         min_y = std::min<double>(min_y, y + 1);
47                         max_y = std::max<double>(max_y, y);
48                         max_y = std::min<double>(max_y, y + 1);
49                         coverage += max_y - min_y;
50                 }
51
52                 double on_r, on_g, on_b;
53                 if (is_on) {
54                         double t = double(y) / height;
55                         if (t <= 0.5) {
56                                 on_r = 1.0;
57                                 on_g = 2.0 * t;
58                                 on_b = 0.0;
59                         } else {
60                                 on_r = 1.0 - 2.0 * (t - 0.5);
61                                 on_g = 1.0;
62                                 on_b = 0.0;
63                         }
64                 } else {
65                         on_r = on_g = on_b = 0.05;
66                 }
67
68                 // Correct for coverage and do a simple gamma correction.
69                 int r = lrintf(255 * pow(on_r * coverage, 1.0 / 2.2));
70                 int g = lrintf(255 * pow(on_g * coverage, 1.0 / 2.2));
71                 int b = lrintf(255 * pow(on_b * coverage, 1.0 / 2.2));
72                 int draw_y = flip ? (height - y - 1) : y;
73                 painter.fillRect(margin, draw_y, width - 2 * margin, 1, QColor(r, g, b));
74         }
75 }