7 int lufs_to_pos(float level_lu, int height)
9 const float min_level = 9.0f; // y=0 is top of screen, so “min” is the loudest level.
10 const float max_level = -18.0f;
13 if (level_lu < max_level) {
17 int y = lrintf(height * (level_lu - min_level) / (max_level - min_level));
19 y = std::min(y, height - 1);
23 void draw_vu_meter(QPainter &painter, float range_low_lu, float range_high_lu, int width, int height, int margin)
25 painter.fillRect(margin, 0, width - 2 * margin, height, Qt::black);
27 // TODO: QLinearGradient is not gamma-correct; we might want to correct for that.
28 QLinearGradient on(0, 0, 0, height);
29 on.setColorAt(0.0f, QColor(255, 0, 0));
30 on.setColorAt(0.5f, QColor(255, 255, 0));
31 on.setColorAt(1.0f, QColor(0, 255, 0));
32 QColor off(80, 80, 80);
34 int min_on_y = lufs_to_pos(range_high_lu, height);
35 int max_on_y = lufs_to_pos(range_low_lu, height);
37 // Draw bars colored up until the level, then gray from there.
38 for (int level = -18; level < 9; ++level) {
39 int min_y = lufs_to_pos(level + 1.0f, height) + 1;
40 int max_y = lufs_to_pos(level, height) - 1;
42 painter.fillRect(margin, min_y, width - 2 * margin, max_y - min_y, off);
43 int min_draw_y = std::max(min_y, min_on_y);
44 int max_draw_y = std::min(max_y, max_on_y);
45 if (min_draw_y < max_draw_y) {
46 painter.fillRect(margin, min_draw_y, width - 2 * margin, max_draw_y - min_draw_y, on);