12 double lufs_to_pos(float level_lu, int height, float min_level, float max_level)
14 // Note: “max” is the loudest level, but y=0 is top of screen.
17 if (level_lu < min_level) {
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);
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) {
35 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)
37 painter.fillRect(horizontal_margin, 0, width - 2 * horizontal_margin, height, Qt::black);
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) + segment_margin * 0.5;
44 double max_y = lufs_to_pos(level, height, min_level, max_level) - segment_margin * 0.5;
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;
52 double on_r, on_g, on_b;
54 double t = double(y) / height;
60 on_r = 1.0 - 2.0 * (t - 0.5);
65 on_r = on_g = on_b = 0.05;
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(horizontal_margin, draw_y, width - 2 * horizontal_margin, 1, QColor(r, g, b));