/
qsynthmeter.cpp
475 lines (365 loc) · 12 KB
/
qsynthmeter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// qsynthMeter.cpp
//
/****************************************************************************
Copyright (C) 2004-2015, rncbc aka Rui Nuno Capela. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*****************************************************************************/
#include "qsynthAbout.h"
#include "qsynthMeter.h"
#include <QPainter>
#include <QToolTip>
#include <QLabel>
#include <QHBoxLayout>
#include <math.h>
// Meter level limits (in dB).
#define QSYNTH_METER_MAXDB (+3.0f)
#define QSYNTH_METER_MINDB (-70.0f)
// The decay rates (magic goes here :).
// - value decay rate (faster)
#define QSYNTH_METER_DECAY_RATE1 (1.0f - 3E-2f)
// - peak decay rate (slower)
#define QSYNTH_METER_DECAY_RATE2 (1.0f - 3E-6f)
// Number of cycles the peak stays on hold before fall-off.
#define QSYNTH_METER_PEAK_FALLOFF 16
//----------------------------------------------------------------------------
// qsynthMeterScale -- Meter bridge scale widget.
// Constructor.
qsynthMeterScale::qsynthMeterScale( qsynthMeter *pMeter )
: QWidget(pMeter), m_pMeter(pMeter)
{
m_iLastY = 0;
QWidget::setMinimumWidth(16);
// QWidget::setBackgroundRole(QPalette::Mid);
const QFont& font = QWidget::font();
QWidget::setFont(QFont(font.family(), font.pointSize() - 4));
}
// Draw IEC scale line and label; assumes labels drawed from top to bottom.
void qsynthMeterScale::drawLineLabel (
QPainter *p, int y, const QString& sLabel )
{
const int iCurrY = QWidget::height() - y;
const int iWidth = QWidget::width() - 2;
const QFontMetrics& fm = p->fontMetrics();
const int iMidHeight = (fm.height() >> 1);
if (iCurrY < iMidHeight || iCurrY > m_iLastY + iMidHeight) {
if (fm.width(sLabel) < iWidth - 5) {
p->drawLine(0, iCurrY, 2, iCurrY);
if (m_pMeter->portCount() > 1)
p->drawLine(iWidth - 3, iCurrY, iWidth - 1, iCurrY);
}
p->drawText(0, iCurrY - iMidHeight, iWidth - 2, fm.height(),
Qt::AlignHCenter | Qt::AlignVCenter, sLabel);
m_iLastY = iCurrY + 1;
}
}
// Paint event handler.
void qsynthMeterScale::paintEvent ( QPaintEvent * )
{
QPainter p(this);
m_iLastY = 0;
const QPalette& pal = QWidget::palette();
const bool bDark = (pal.base().color().value() < 0x7f);
const QColor& color = pal.midlight().color();
p.setPen(bDark ? color.lighter() : color.darker());
drawLineLabel(&p, m_pMeter->iec_level(qsynthMeter::Color0dB), "0");
drawLineLabel(&p, m_pMeter->iec_level(qsynthMeter::Color3dB), "3");
drawLineLabel(&p, m_pMeter->iec_level(qsynthMeter::Color6dB), "6");
drawLineLabel(&p, m_pMeter->iec_level(qsynthMeter::Color10dB), "10");
for (float dB = -20.0f; dB > QSYNTH_METER_MINDB; dB -= 10.0f)
drawLineLabel(&p, m_pMeter->iec_scale(dB), QString::number(-int(dB)));
}
//----------------------------------------------------------------------------
// qsynthMeterValue -- Meter bridge value widget.
// Constructor.
qsynthMeterValue::qsynthMeterValue ( qsynthMeter *pMeter )
: QFrame(pMeter), m_pMeter(pMeter)
{
m_fValue = 0.0f;
m_iValue = 0;
m_fValueDecay = QSYNTH_METER_DECAY_RATE1;
m_iPeak = 0;
m_fPeakDecay = QSYNTH_METER_DECAY_RATE2;
m_iPeakHold = 0;
m_iPeakColor = qsynthMeter::Color6dB;
QWidget::setMinimumWidth(12);
QFrame::setBackgroundRole(QPalette::NoRole);
QFrame::setFrameShape(QFrame::StyledPanel);
QFrame::setFrameShadow(QFrame::Sunken);
}
// Frame value one-way accessors.
void qsynthMeterValue::setValue ( float fValue )
{
if (m_fValue < fValue)
m_fValue = fValue;
refresh();
}
// Reset peak holder.
void qsynthMeterValue::peakReset (void)
{
m_iPeak = 0;
}
// Value refreshment.
void qsynthMeterValue::refresh (void)
{
if (m_fValue < 0.001f && m_iPeak < 1)
return;
float dB = QSYNTH_METER_MINDB;
if (m_fValue > 0.0f) {
dB = 20.0f * ::log10f(m_fValue);
m_fValue = 0.0f;
}
if (dB < QSYNTH_METER_MINDB)
dB = QSYNTH_METER_MINDB;
else if (dB > QSYNTH_METER_MAXDB)
dB = QSYNTH_METER_MAXDB;
int iValue = m_pMeter->iec_scale(dB);
if (iValue < m_iValue) {
iValue = int(m_fValueDecay * float(m_iValue));
m_fValueDecay *= m_fValueDecay;
} else {
m_fValueDecay = QSYNTH_METER_DECAY_RATE1;
}
int iPeak = m_iPeak;
if (iPeak < iValue) {
iPeak = iValue;
m_iPeakHold = 0;
m_fPeakDecay = QSYNTH_METER_DECAY_RATE2;
m_iPeakColor = qsynthMeter::Color10dB;
for (; m_iPeakColor > qsynthMeter::ColorOver
&& iPeak >= m_pMeter->iec_level(m_iPeakColor); --m_iPeakColor)
/* empty body loop */;
} else if (++m_iPeakHold > m_pMeter->peakFalloff()) {
iPeak = int(m_fPeakDecay * float(iPeak));
if (iPeak < iValue) {
iPeak = iValue;
} else {
m_fPeakDecay *= m_fPeakDecay;
}
}
if (iValue == m_iValue && iPeak == m_iPeak)
return;
m_iValue = iValue;
m_iPeak = iPeak;
update();
}
// Paint event handler.
void qsynthMeterValue::paintEvent ( QPaintEvent * )
{
QPainter painter(this);
const int w = QWidget::width();
const int h = QWidget::height();
int y;
if (isEnabled()) {
painter.fillRect(0, 0, w, h,
m_pMeter->color(qsynthMeter::ColorBack));
y = m_pMeter->iec_level(qsynthMeter::Color0dB);
painter.setPen(m_pMeter->color(qsynthMeter::ColorFore));
painter.drawLine(0, h - y, w, h - y);
} else {
painter.fillRect(0, 0, w, h, QWidget::palette().dark().color());
}
#ifdef CONFIG_GRADIENT
painter.drawPixmap(0, h - m_iValue,
m_pMeter->pixmap(), 0, h - m_iValue, w, m_iValue);
#else
y = m_iValue;
int y_over = 0;
int y_curr = 0;
for (int i = qsynthMeter::Color10dB;
i > qsynthMeter::ColorOver && y >= y_over; --i) {
y_curr = m_pMeter->iec_level(i);
if (y < y_curr) {
painter.fillRect(0, h - y, w, y - y_over,
m_pMeter->color(i));
} else {
painter.fillRect(0, h - y_curr, w, y_curr - y_over,
m_pMeter->color(i));
}
y_over = y_curr;
}
if (y > y_over) {
painter.fillRect(0, h - y, w, y - y_over,
m_pMeter->color(qsynthMeter::ColorOver));
}
#endif
painter.setPen(m_pMeter->color(m_iPeakColor));
painter.drawLine(0, h - m_iPeak, w, h - m_iPeak);
}
// Resize event handler.
void qsynthMeterValue::resizeEvent ( QResizeEvent *pResizeEvent )
{
m_iPeak = 0;
QWidget::resizeEvent(pResizeEvent);
// QWidget::repaint(true);
}
//----------------------------------------------------------------------------
// qsynthMeter -- Meter bridge slot widget.
// Constructor.
qsynthMeter::qsynthMeter ( QWidget *pParent )
: QWidget(pParent)
{
m_iPortCount = 2; // FIXME: Default port count.
m_iScaleCount = m_iPortCount;
m_ppValues = NULL;
m_ppScales = NULL;
m_fScale = 0.0f;
#ifdef CONFIG_GRADIENT
m_pPixmap = new QPixmap();
#endif
m_iPeakFalloff = QSYNTH_METER_PEAK_FALLOFF;
for (int i = 0; i < LevelCount; i++)
m_levels[i] = 0;
m_colors[ColorOver] = QColor(240, 0, 20);
m_colors[Color0dB] = QColor(240,160, 20);
m_colors[Color3dB] = QColor(220,220, 20);
m_colors[Color6dB] = QColor(160,220, 20);
m_colors[Color10dB] = QColor( 40,160, 40);
m_colors[ColorBack] = QColor( 20, 40, 20);
m_colors[ColorFore] = QColor( 80, 80, 80);
m_pHBoxLayout = new QHBoxLayout();
m_pHBoxLayout->setMargin(0);
m_pHBoxLayout->setSpacing(0);
QWidget::setLayout(m_pHBoxLayout);
QWidget::setBackgroundRole(QPalette::NoRole);
if (m_iPortCount > 0) {
if (m_iPortCount > 1)
m_iScaleCount--;
m_ppValues = new qsynthMeterValue *[m_iPortCount];
m_ppScales = new qsynthMeterScale *[m_iScaleCount];
for (int iPort = 0; iPort < m_iPortCount; iPort++) {
m_ppValues[iPort] = new qsynthMeterValue(this);
m_pHBoxLayout->addWidget(m_ppValues[iPort]);
if (iPort < m_iScaleCount) {
m_ppScales[iPort] = new qsynthMeterScale(this);
m_pHBoxLayout->addWidget(m_ppScales[iPort]);
}
}
int iStripCount = 2 * m_iPortCount;
if (m_iPortCount > 1)
--iStripCount;
QWidget::setMinimumSize(12 * iStripCount, 120);
QWidget::setMaximumWidth(16 * iStripCount);
} else {
QWidget::setMinimumSize(2, 120);
QWidget::setMaximumWidth(4);
}
QWidget::setSizePolicy(
QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding));
}
// Default destructor.
qsynthMeter::~qsynthMeter (void)
{
#ifdef CONFIG_GRADIENT
delete m_pPixmap;
#endif
for (int iPort = 0; iPort < m_iPortCount; iPort++) {
delete m_ppValues[iPort];
if (iPort < m_iScaleCount)
delete m_ppScales[iPort];
}
delete [] m_ppScales;
delete [] m_ppValues;
delete m_pHBoxLayout;
}
// Child widget accessors.
int qsynthMeter::iec_scale ( float dB ) const
{
float fDef = 1.0;
if (dB < -70.0)
fDef = 0.0;
else if (dB < -60.0)
fDef = (dB + 70.0) * 0.0025;
else if (dB < -50.0)
fDef = (dB + 60.0) * 0.005 + 0.025;
else if (dB < -40.0)
fDef = (dB + 50.0) * 0.0075 + 0.075;
else if (dB < -30.0)
fDef = (dB + 40.0) * 0.015 + 0.15;
else if (dB < -20.0)
fDef = (dB + 30.0) * 0.02 + 0.3;
else /* if (dB < 0.0) */
fDef = (dB + 20.0) * 0.025 + 0.5;
return (int) (fDef * m_fScale);
}
int qsynthMeter::iec_level ( int iIndex ) const
{
return m_levels[iIndex];
}
int qsynthMeter::portCount (void) const
{
return m_iPortCount;
}
// Peak falloff mode setting.
void qsynthMeter::setPeakFalloff ( int iPeakFalloff )
{
m_iPeakFalloff = iPeakFalloff;
}
int qsynthMeter::peakFalloff (void) const
{
return m_iPeakFalloff;
}
// Reset peak holder.
void qsynthMeter::peakReset (void)
{
for (int iPort = 0; iPort < m_iPortCount; iPort++)
m_ppValues[iPort]->peakReset();
}
#ifdef CONFIG_GRADIENT
// Gradient pixmap accessor.
const QPixmap& qsynthMeter::pixmap (void) const
{
return *m_pPixmap;
}
void qsynthMeter::updatePixmap (void)
{
const int w = QWidget::width();
const int h = QWidget::height();
QLinearGradient grad(0, 0, 0, h);
grad.setColorAt(0.2f, color(ColorOver));
grad.setColorAt(0.3f, color(Color0dB));
grad.setColorAt(0.4f, color(Color3dB));
grad.setColorAt(0.6f, color(Color6dB));
grad.setColorAt(0.8f, color(Color10dB));
*m_pPixmap = QPixmap(w, h);
QPainter(m_pPixmap).fillRect(0, 0, w, h, grad);
}
#endif
// Slot refreshment.
void qsynthMeter::refresh (void)
{
for (int iPort = 0; iPort < m_iPortCount; iPort++)
m_ppValues[iPort]->refresh();
}
// Resize event handler.
void qsynthMeter::resizeEvent ( QResizeEvent * )
{
m_fScale = 0.85f * float(QWidget::height());
m_levels[Color0dB] = iec_scale( 0.0f);
m_levels[Color3dB] = iec_scale( -3.0f);
m_levels[Color6dB] = iec_scale( -6.0f);
m_levels[Color10dB] = iec_scale(-10.0f);
#ifdef CONFIG_GRADIENT
updatePixmap();
#endif
}
// Meter value proxy.
void qsynthMeter::setValue ( int iPort, float fValue )
{
m_ppValues[iPort]->setValue(fValue);
}
// Common resource accessor.
const QColor& qsynthMeter::color ( int iIndex ) const
{
return m_colors[iIndex];
}
// end of qsynthMeter.cpp