UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
vuMeterWidget.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <cassert>
3#include <cmath>
4#include <thread>
5#include <functional>
6#include <QPainter>
7#include <QBrush>
8#include <QLinearGradient>
9#include <QRect>
10#include <QString>
11#include <QStyle>
12#include "vuMeterWidget.hpp"
13#include "control_port.hpp"
15#include "astat.h"
16
17namespace{
18
19 static constexpr int meterVerticalPad = 5;
20 static constexpr int meterBarPad = 2;
21 static constexpr double zeroLevel = -40.0;
22
23}//anon namespace
24
26 QWidget(parent),
27 peak {0.0},
28 rms {0.0},
29 barLevel {0.0},
30 rmsLevel {0.0},
31 updatesPerSecond(24)
32{
33 connect(&timer, SIGNAL(timeout()), this, SLOT(updateVal()));
34 timer.start(1000/updatesPerSecond);
35}
36
37void VuMeterWidget::parseLine(std::string_view line){
38 if(!sv_is_prefix(line, parsePrefix))
39 return;
40
41 line.remove_prefix(parsePrefix.size());
42
43 for(channels = 0; !line.empty() && channels < max_channels; channels++){
44 auto rmsTag = tokenize(line, ' ');
45 if(!sv_is_prefix(rmsTag, "volrms"))
46 return;
47 auto rms_sv = tokenize(line, ' ');
48
49 auto peakTag = tokenize(line, ' ');
50 if(!sv_is_prefix(peakTag, "volpeak"))
51 return;
52 auto peak_sv = tokenize(line, ' ');
53
54 double ch_rms = 0;
55 double ch_peak = 0;
56 if(!parse_num(rms_sv, ch_rms)
57 || !parse_num(peak_sv, ch_peak))
58 {
59 return;
60 }
61
62 peak[channels] = ch_peak;
63 rms[channels] = ch_rms;
64 }
65
66 lastUpdate = clock::now();
67}
68
70 this->controlPort = controlPort;
71
72 using namespace std::placeholders;
75}
76
77void VuMeterWidget::setParsePrefix(std::string_view prefix){
78 parsePrefix = prefix;
79}
80
82 const double fallSpeed = 200.0;
83 const double maximumPeakFallSpeed = 10.0;
84 auto maximumPeakHoldTime = std::chrono::seconds(2);
85
86 connected = controlPort->getState() == QAbstractSocket::ConnectedState;
87
88 for(int i = 0; i < channels; i++){
89 auto& maxPeak = maximumLevel[i];
90 auto now = clock::now();
91
92 barLevel[i] = std::max(barLevel[i] - fallSpeed / updatesPerSecond, 0.0);
93 rmsLevel[i] = std::max(rmsLevel[i] - fallSpeed / updatesPerSecond, 0.0);
94 if(now - maxPeak.ts > maximumPeakHoldTime){
95 maxPeak.val = std::max(maxPeak.val - maximumPeakFallSpeed / updatesPerSecond, 0.0);
96 }
97
98 double newPeakHeight = 100 - std::max(peak[i], zeroLevel) * (100 / zeroLevel);
99 double newRmsHeight = 100 - std::max(rms[i], zeroLevel) * (100 / zeroLevel);
100
101 barLevel[i] = std::max(barLevel[i], newPeakHeight);
102 rmsLevel[i] = std::max(rmsLevel[i], newRmsHeight);
103
104 if(barLevel[i] >= maxPeak.val){
105 maxPeak.val = barLevel[i];
106 maxPeak.ts = now;
107 }
108 }
109
110 update();
111}
112
114 assert(controlPort);
115 controlPort->writeLine("stats on\r\n");
116 lastUpdate = clock::time_point();
117}
118
119void VuMeterWidget::paintMeter(QPainter& painter,
120 int x,
121 int y,
122 int width,
123 int height,
124 double peak,
125 double rms,
126 double maxPeak)
127{
128 const int meterBarPadHorizontal = 1;
129 const int in_width = width - meterBarPadHorizontal * 2;
130 const int in_full_height = height - meterBarPad * 2;
131
132 int barHeight = in_full_height * (peak / 100);
133 int rmsHeight = in_full_height * (rms / 100);
134 int maxPeakHeight = in_full_height * (maxPeak / 100);
135
136 QLinearGradient gradient(0, 0, in_width, in_full_height);
137 gradient.setColorAt(0, Qt::red);
138 gradient.setColorAt(0.25, Qt::yellow);
139 gradient.setColorAt(0.5, Qt::green);
140 gradient.setColorAt(1, Qt::green);
141 QBrush brush(gradient);
142
143 using namespace std::chrono_literals;
144 bool enabled = connected && (clock::now() - lastUpdate <= 2s);
145
146 painter.fillRect(x, y + 1, width, height - 2, enabled ? Qt::black : Qt::gray);
147 if(!enabled)
148 return;
149
150 painter.fillRect(x + meterBarPadHorizontal,
151 y + meterBarPad + (in_full_height - barHeight),
152 in_width, barHeight,
153 brush);
154
155 int maxPos = y + meterBarPad + (in_full_height - maxPeakHeight);
156 QPen maxLevelPen(brush, 1);
157 painter.setPen(maxLevelPen);
158 painter.drawLine(x + meterBarPadHorizontal, maxPos, x + in_width, maxPos);
159
160 QPen rmsPen(Qt::blue);
161 rmsPen.setWidth(4);
162 painter.setPen(rmsPen);
163 int pos = y + meterBarPad + (in_full_height - rmsHeight);
164 painter.drawLine(x + meterBarPadHorizontal, pos, x + in_width, pos);
165}
166
167void VuMeterWidget::paintScale(QPainter& painter,
168 int x,
169 int y,
170 int width,
171 int height,
172 bool leftTicks,
173 bool rightTicks)
174{
175 const int barHeight = height - meterVerticalPad * 2 - meterBarPad * 2;
176 const int barStart = y + meterVerticalPad + meterBarPad;
177 const float stepPx = (float) barHeight / std::fabs(zeroLevel);
178
179 painter.setPen(connected ? Qt::black : Qt::gray);
180 int i = 0;
181
182 int textPosX;
183 if(leftTicks && rightTicks){
184 textPosX = x + width / 2 - 8;
185 } else if(leftTicks){
186 textPosX = x + width - 16;
187 } else {
188 textPosX = x;
189 }
190
191 for(float y = barStart; y <= barStart + barHeight + 0.1; y += stepPx){
192 static const int lineLenghtSmall = 2;
193 static const int lineLenghtMid = 4;
194 static const int lineLenghtLarge = 6;
195 static const int drawThreshold = 4;
196 if(i % 10 == 0){
197 if(leftTicks) painter.drawLine(x, y, x + lineLenghtLarge, y);
198 if(rightTicks) painter.drawLine(x + width - lineLenghtLarge, y, x + width, y);
199 painter.drawText(QRect(textPosX, y - 6, 16, 12), Qt::AlignCenter,QString::number(i));
200 } else if(i % 5 == 0){
201 if(leftTicks) painter.drawLine(x, y, x + lineLenghtMid, y);
202 if(rightTicks) painter.drawLine(x + width - lineLenghtMid, y, x + width, y);
203 } else if(stepPx >= drawThreshold){
204 if(leftTicks) painter.drawLine(x, y, x + lineLenghtSmall, y);
205 if(rightTicks) painter.drawLine(x + width - lineLenghtSmall, y, x + width, y);
206 }
207
208 i++;
209 }
210
211 if(!connected){
212 int iconDim = 32;
213 QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxWarning);
214 QPixmap pixmap = icon.pixmap(iconDim,iconDim);
215 painter.eraseRect(x+width/2 - (iconDim+4)/2, y + height / 2 - (iconDim+4)/2,
216 iconDim+4, iconDim+4);
217 painter.drawPixmap(x+width/2 - iconDim/2, y + height / 2 - iconDim/2,
218 iconDim, iconDim,
219 pixmap,
220 0, 0,
221 iconDim, iconDim);
222 }
223}
224
225void VuMeterWidget::paintEvent(QPaintEvent * /*paintEvent*/){
226 const int scaleWidth = 30;
227
228 const int meterCount = std::max(1, channels);
229
230 const int meterWidth = (width() - scaleWidth) / meterCount;
231
232 QPainter painter(this);
233
234 if(meterCount % 2 != 0){
235 int scaleX = onRightSide ? 0 : meterCount * meterWidth;
236 int metersStartX = onRightSide ? scaleWidth : 0;
237
238 for(int i = 0; i < meterCount; i++){
239 paintMeter(painter,
240 metersStartX + i * meterWidth,
241 meterVerticalPad,
242 meterWidth,
243 height() - meterVerticalPad * 2,
244 barLevel[i],
245 rmsLevel[i],
246 maximumLevel[i].val);
247 }
248
249 paintScale(painter,
250 scaleX,
251 0,
252 scaleWidth,
253 height(),
256 } else {
257 int i = 0;
258 for(; i < meterCount / 2; i++){
259 paintMeter(painter,
260 i * meterWidth,
261 meterVerticalPad,
262 meterWidth,
263 height() - meterVerticalPad * 2,
264 barLevel[i],
265 rmsLevel[i],
266 maximumLevel[i].val);
267 }
268
269 paintScale(painter,
270 i * meterWidth,
271 0,
272 scaleWidth,
273 height(),
274 true,
275 true);
276
277 for(; i < meterCount; i++){
278 paintMeter(painter,
279 i * meterWidth + scaleWidth,
280 meterVerticalPad,
281 meterWidth,
282 height() - meterVerticalPad * 2,
283 barLevel[i],
284 rmsLevel[i],
285 maximumLevel[i].val);
286 }
287
288 }
289
290}
int void * val
void addLineCallback(LineCallback callback)
void connected()
QAbstractSocket::SocketState getState() const
void writeLine(std::string_view line)
static const int max_channels
void setParsePrefix(std::string_view prefix)
double rmsLevel[max_channels]
std::string parsePrefix
void parseLine(std::string_view line)
void paintMeter(QPainter &, int x, int y, int width, int height, double peak, double rms, double maxPeak)
double rms[max_channels]
ControlPort * controlPort
VuMeterWidget(QWidget *parent)
double peak[max_channels]
void onControlPortConnect()
void setControlPort(ControlPort *controlPort)
void paintScale(QPainter &, int x, int y, int width, int height, bool leftTicks, bool rightTicks)
clock::time_point lastUpdate
void paintEvent(QPaintEvent *paintEvent)
PeakMaximum maximumLevel[max_channels]
double barLevel[max_channels]
const char * parent
Definition settings.cpp:198
bool enabled
Definition settings.cpp:197
std::string_view tokenize(std::string_view &str, char delim, char quot)
Tokenizer for string_view.
bool parse_num(std::string_view sv, T &res, int base=10)
bool sv_is_prefix(std::string_view haystack, std::string_view needle)