refs #387, preparation for level meter

* not yet connected with audio
This commit is contained in:
Klaus Basan
2015-02-15 17:18:06 +01:00
parent 7201d09c9a
commit 496d63cf11
3 changed files with 212 additions and 11 deletions

View File

@@ -162,7 +162,7 @@
<item>
<widget class="QPushButton" name="pb_Volume100">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
@@ -179,17 +179,14 @@
</widget>
</item>
<item>
<spacer name="hs_AudioSoundButtons">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="BlackGui::CLevelMeter" name="wip_LevelMeter" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</widget>
</item>
</layout>
</widget>
@@ -218,6 +215,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>BlackGui::CLevelMeter</class>
<extends>QWidget</extends>
<header>blackgui/levelmeter.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../blackmisc/blackmisc.qrc"/>
</resources>

111
src/blackgui/levelmeter.cpp Normal file
View File

@@ -0,0 +1,111 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Class based on qt example: Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
*/
#include "levelmeter.h"
#include <math.h>
#include <QPainter>
#include <QTimer>
#include <QDebug>
namespace BlackGui
{
CLevelMeter::CLevelMeter(QWidget *parent)
: QWidget(parent)
, m_peakDecayRate(PeakDecayRate)
, m_redrawTimer(new QTimer(this))
, m_rmsColor(Qt::red)
, m_peakColor(255, 200, 200, 255)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
setMinimumWidth(30);
connect(m_redrawTimer, &QTimer::timeout, this, &CLevelMeter::ps_redrawTimerExpired);
m_redrawTimer->start(RedrawInterval);
}
CLevelMeter::~CLevelMeter()
{ }
void CLevelMeter::reset()
{
m_rmsLevel = 0.0;
m_peakLevel = 0.0;
update();
}
void CLevelMeter::levelChanged(double rmsLevel, double peakLevel, int numSamples)
{
// Smooth the RMS signal
const double smooth = pow(double(0.9), static_cast<double>(numSamples) / 256); // TODO: remove this magic number
m_rmsLevel = (m_rmsLevel * smooth) + (rmsLevel * (1.0 - smooth));
if (peakLevel > m_decayedPeakLevel)
{
m_peakLevel = peakLevel;
m_decayedPeakLevel = peakLevel;
m_peakLevelChanged.start();
}
if (peakLevel > m_peakHoldLevel)
{
m_peakHoldLevel = peakLevel;
m_peakHoldLevelChanged.start();
}
update();
}
void CLevelMeter::ps_redrawTimerExpired()
{
// Decay the peak signal
const int elapsedMs = m_peakLevelChanged.elapsed();
const double decayAmount = m_peakDecayRate * elapsedMs;
if (decayAmount < m_peakLevel)
{
m_decayedPeakLevel = m_peakLevel - decayAmount;
}
else
{
m_decayedPeakLevel = 0.0;
}
// Check whether to clear the peak hold level
if (m_peakHoldLevelChanged.elapsed() > PeakHoldLevelDuration)
m_peakHoldLevel = 0.0;
update();
}
void CLevelMeter::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
QRect bar = rect();
bar.setTop(rect().top() + (1.0 - m_peakHoldLevel) * rect().height());
bar.setBottom(bar.top() + 5);
painter.fillRect(bar, m_rmsColor);
bar.setBottom(rect().bottom());
bar.setTop(rect().top() + (1.0 - m_decayedPeakLevel) * rect().height());
painter.fillRect(bar, m_peakColor);
bar.setTop(rect().top() + (1.0 - m_rmsLevel) * rect().height());
painter.fillRect(bar, m_rmsColor);
}
} // namespace

85
src/blackgui/levelmeter.h Normal file
View File

@@ -0,0 +1,85 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Class based on qt example: Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
*/
//! \file
#ifndef BLACKGUI_LEVELMETER_H
#define BLACKGUI_LEVELMETER_H
#include <QTime>
#include <QWidget>
namespace BlackGui
{
/**
* Widget which displays a vertical audio level meter, indicating the
* RMS and peak levels of the window of audio samples most recently analyzed
* by the Engine.
*/
class CLevelMeter : public QWidget
{
Q_OBJECT
public:
//! Constructor
CLevelMeter(QWidget *parent = 0);
//! Destructor
~CLevelMeter();
//! \copydoc QWidget::paintEvent
void paintEvent(QPaintEvent *event) override;
public slots:
//! Clean up
void reset();
//! Values
void levelChanged(double rmsLevel, double peakLevel, int numSamples);
private slots:
void ps_redrawTimerExpired();
private:
const int RedrawInterval = 100; // ms
const double PeakDecayRate = 0.001;
const int PeakHoldLevelDuration = 2000; // ms
//! Height of RMS level bar, range 0.0 - 1.0.
double m_rmsLevel = 0.0;
//! Most recent peak level, range 0.0 - 1.0.
double m_peakLevel = 0.0;
//! Height of peak level bar.
//! This is calculated by decaying m_peakLevel depending on the elapsed time since m_peakLevelChanged, and the value of m_decayRate.
double m_decayedPeakLevel = 0.0;
//! Time at which m_peakLevel was last changed.
QTime m_peakLevelChanged;
//! Rate at which peak level bar decays. Expressed in level units / millisecond.
double m_peakDecayRate;
//! High watermark of peak level. Range 0.0 - 1.0.
double m_peakHoldLevel = 0.0;
//! Time at which m_peakHoldLevel was last changed.
QTime m_peakHoldLevelChanged;
QTimer *m_redrawTimer;
QColor m_rmsColor;
QColor m_peakColor;
};
}
#endif // guard