10 double lufs_to_pos(float level_lu, int height, float min_level, float max_level)
12 // Note: “max” is the loudest level, but y=0 is top of screen.
15 if (level_lu < min_level) {
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);
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) {
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)
35 painter.fillRect(horizontal_margin, y_offset, width - 2 * horizontal_margin, height, Qt::black);
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;
50 double on_r, on_g, on_b;
52 double t = double(y) / height;
58 on_r = 1.0 - 2.0 * (t - 0.5);
63 on_r = on_g = on_b = 0.05;
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));