Level Meter: changed rms to avg to be generic

This commit is contained in:
f4exb 2016-12-04 06:44:37 +01:00
parent 373a99e575
commit 76932e6627
2 changed files with 11 additions and 9 deletions

View File

@ -4,6 +4,7 @@
* - use the widget horizontally * - use the widget horizontally
* - differentiate each area with a different color * - differentiate each area with a different color
* - allow overload by 25% with indication of 100% threshold and overload * - allow overload by 25% with indication of 100% threshold and overload
* - make it generic to fit many cases: VU, signal strength ...
** **
** Copyright (C) 2015 The Qt Company Ltd. ** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/ ** Contact: http://www.qt.io/licensing/
@ -58,13 +59,13 @@ const int PeakHoldLevelDuration = 2000; // ms
LevelMeter::LevelMeter(QWidget *parent) LevelMeter::LevelMeter(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, m_rmsLevel(0.0) , m_avgLevel(0.0)
, m_peakLevel(0.0) , m_peakLevel(0.0)
, m_decayedPeakLevel(0.0) , m_decayedPeakLevel(0.0)
, m_peakDecayRate(PeakDecayRate) , m_peakDecayRate(PeakDecayRate)
, m_peakHoldLevel(0.0) , m_peakHoldLevel(0.0)
, m_redrawTimer(new QTimer(this)) , m_redrawTimer(new QTimer(this))
, m_rmsColor(QColor(0x97, 0x54, 0x00)) // color mapper 59% , m_avgColor(QColor(0x97, 0x54, 0x00)) // color mapper 59%
, m_decayedPeakColor(QColor(0xff, 0x8b, 0x00)) // color mapper foreground , m_decayedPeakColor(QColor(0xff, 0x8b, 0x00)) // color mapper foreground
, m_peakColor(255, 0, 0, 255) // just red , m_peakColor(255, 0, 0, 255) // just red
{ {
@ -82,16 +83,16 @@ LevelMeter::~LevelMeter()
void LevelMeter::reset() void LevelMeter::reset()
{ {
m_rmsLevel = 0.0; m_avgLevel = 0.0;
m_peakLevel = 0.0; m_peakLevel = 0.0;
update(); update();
} }
void LevelMeter::levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples) void LevelMeter::levelChanged(qreal avgLevel, qreal peakLevel, int numSamples)
{ {
// Smooth the RMS signal // Smooth the RMS signal
const qreal smooth = pow(qreal(0.9), static_cast<qreal>(numSamples) / 256); // TODO: remove this magic number const qreal smooth = pow(qreal(0.9), static_cast<qreal>(numSamples) / 256); // TODO: remove this magic number
m_rmsLevel = (m_rmsLevel * smooth) + (rmsLevel * (1.0 - smooth)); m_avgLevel = (m_avgLevel * smooth) + (avgLevel * (1.0 - smooth));
if (peakLevel > m_decayedPeakLevel) { if (peakLevel > m_decayedPeakLevel) {
m_peakLevel = peakLevel; m_peakLevel = peakLevel;
@ -172,6 +173,6 @@ void LevelMeterVU::render(QPainter *painter)
bar.setRight(rect().right() - (1.0 - 0.75*m_decayedPeakLevel) * rect().width()); bar.setRight(rect().right() - (1.0 - 0.75*m_decayedPeakLevel) * rect().width());
painter->fillRect(bar, m_decayedPeakColor); painter->fillRect(bar, m_decayedPeakColor);
bar.setRight(rect().right() - (1.0 - 0.75*m_rmsLevel) * rect().width()); bar.setRight(rect().right() - (1.0 - 0.75*m_avgLevel) * rect().width());
painter->fillRect(bar, m_rmsColor); painter->fillRect(bar, m_avgColor);
} }

View File

@ -4,6 +4,7 @@
* - use the widget horizontally * - use the widget horizontally
* - differentiate each area with a different color * - differentiate each area with a different color
* - allow overload by 25% with indication of 100% threshold and overload * - allow overload by 25% with indication of 100% threshold and overload
* - make it generic to fit many cases: VU, signal strength ...
** **
** Copyright (C) 2015 The Qt Company Ltd. ** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/ ** Contact: http://www.qt.io/licensing/
@ -77,7 +78,7 @@ protected:
* Height of RMS level bar. * Height of RMS level bar.
* Range 0.0 - 1.0. * Range 0.0 - 1.0.
*/ */
qreal m_rmsLevel; qreal m_avgLevel;
/** /**
* Most recent peak level. * Most recent peak level.
@ -116,7 +117,7 @@ protected:
QTimer *m_redrawTimer; QTimer *m_redrawTimer;
QColor m_rmsColor; QColor m_avgColor;
QColor m_peakColor; QColor m_peakColor;
QColor m_decayedPeakColor; QColor m_decayedPeakColor;