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