source: trunk/demos/spectrum/app/levelmeter.cpp@ 822

Last change on this file since 822 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 4.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
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
51const int RedrawInterval = 100; // ms
52const qreal PeakDecayRate = 0.001;
53const int PeakHoldLevelDuration = 2000; // ms
54
55
56LevelMeter::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
74LevelMeter::~LevelMeter()
75{
76
77}
78
79void LevelMeter::reset()
80{
81 m_rmsLevel = 0.0;
82 m_peakLevel = 0.0;
83 update();
84}
85
86void 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
106void 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
123void 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}
Note: See TracBrowser for help on using the repository browser.