1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "levelmeter.h"
42
43 #include <math.h>
44
45 #include <QPainter>
46 #include <QTimer>
47 #include <QDebug>
48
49
50 // Constants
51 const int RedrawInterval = 100; // ms
52 const qreal PeakDecayRate = 0.001;
53 const int PeakHoldLevelDuration = 2000; // ms
54
55
LevelMeter(QWidget * parent)56 LevelMeter::LevelMeter(QWidget *parent)
57 : QWidget(parent)
58 , m_rmsLevel(0.0)
59 , m_peakLevel(0.0)
60 , m_decayedPeakLevel(0.0)
61 , m_peakDecayRate(PeakDecayRate)
62 , m_peakHoldLevel(0.0)
63 , m_redrawTimer(new QTimer(this))
64 , m_rmsColor(Qt::red)
65 , m_peakColor(255, 200, 200, 255)
66 {
67 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
68 setMinimumWidth(30);
69
70 connect(m_redrawTimer, SIGNAL(timeout()), this, SLOT(redrawTimerExpired()));
71 m_redrawTimer->start(RedrawInterval);
72 }
73
~LevelMeter()74 LevelMeter::~LevelMeter()
75 {
76
77 }
78
reset()79 void LevelMeter::reset()
80 {
81 m_rmsLevel = 0.0;
82 m_peakLevel = 0.0;
83 update();
84 }
85
levelChanged(qreal rmsLevel,qreal peakLevel,int numSamples)86 void LevelMeter::levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples)
87 {
88 // Smooth the RMS signal
89 const qreal smooth = pow(qreal(0.9), static_cast<qreal>(numSamples) / 256); // TODO: remove this magic number
90 m_rmsLevel = (m_rmsLevel * smooth) + (rmsLevel * (1.0 - smooth));
91
92 if (peakLevel > m_decayedPeakLevel) {
93 m_peakLevel = peakLevel;
94 m_decayedPeakLevel = peakLevel;
95 m_peakLevelChanged.start();
96 }
97
98 if (peakLevel > m_peakHoldLevel) {
99 m_peakHoldLevel = peakLevel;
100 m_peakHoldLevelChanged.start();
101 }
102
103 update();
104 }
105
redrawTimerExpired()106 void LevelMeter::redrawTimerExpired()
107 {
108 // Decay the peak signal
109 const int elapsedMs = m_peakLevelChanged.elapsed();
110 const qreal decayAmount = m_peakDecayRate * elapsedMs;
111 if (decayAmount < m_peakLevel)
112 m_decayedPeakLevel = m_peakLevel - decayAmount;
113 else
114 m_decayedPeakLevel = 0.0;
115
116 // Check whether to clear the peak hold level
117 if (m_peakHoldLevelChanged.elapsed() > PeakHoldLevelDuration)
118 m_peakHoldLevel = 0.0;
119
120 update();
121 }
122
paintEvent(QPaintEvent * event)123 void LevelMeter::paintEvent(QPaintEvent *event)
124 {
125 Q_UNUSED(event)
126
127 QPainter painter(this);
128 painter.fillRect(rect(), Qt::black);
129
130 QRect bar = rect();
131
132 bar.setTop(rect().top() + (1.0 - m_peakHoldLevel) * rect().height());
133 bar.setBottom(bar.top() + 5);
134 painter.fillRect(bar, m_rmsColor);
135 bar.setBottom(rect().bottom());
136
137 bar.setTop(rect().top() + (1.0 - m_decayedPeakLevel) * rect().height());
138 painter.fillRect(bar, m_peakColor);
139
140 bar.setTop(rect().top() + (1.0 - m_rmsLevel) * rect().height());
141 painter.fillRect(bar, m_rmsColor);
142 }
143