demos/spectrum/app/levelmeter.cpp
changeset 25 e24348a560a6
child 29 b72c6db6890b
equal deleted inserted replaced
23:89e065397ea6 25:e24348a560a6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "levelmeter.h"
       
    43 
       
    44 #include <math.h>
       
    45 
       
    46 #include <QPainter>
       
    47 #include <QTimer>
       
    48 #include <QDebug>
       
    49 
       
    50 
       
    51 // Constants
       
    52 const int RedrawInterval = 100; // ms
       
    53 const qreal PeakDecayRate = 0.001;
       
    54 const int PeakHoldLevelDuration = 2000; // ms
       
    55 
       
    56 
       
    57 LevelMeter::LevelMeter(QWidget *parent)
       
    58     :   QWidget(parent)
       
    59     ,   m_rmsLevel(0.0)
       
    60     ,   m_peakLevel(0.0)
       
    61     ,   m_decayedPeakLevel(0.0)
       
    62     ,   m_peakDecayRate(PeakDecayRate)
       
    63     ,   m_peakHoldLevel(0.0)
       
    64     ,   m_redrawTimer(new QTimer(this))
       
    65     ,   m_rmsColor(Qt::red)
       
    66     ,   m_peakColor(255, 200, 200, 255)
       
    67 {
       
    68     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
       
    69     setMinimumWidth(30);
       
    70 
       
    71     connect(m_redrawTimer, SIGNAL(timeout()), this, SLOT(redrawTimerExpired()));
       
    72     m_redrawTimer->start(RedrawInterval);
       
    73 }
       
    74 
       
    75 LevelMeter::~LevelMeter()
       
    76 {
       
    77 
       
    78 }
       
    79 
       
    80 void LevelMeter::reset()
       
    81 {
       
    82     m_rmsLevel = 0.0;
       
    83     m_peakLevel = 0.0;
       
    84     update();
       
    85 }
       
    86 
       
    87 void LevelMeter::levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples)
       
    88 {
       
    89     // Smooth the RMS signal
       
    90     const qreal smooth = pow(qreal(0.9), static_cast<qreal>(numSamples) / 256); // TODO: remove this magic number
       
    91     m_rmsLevel = (m_rmsLevel * smooth) + (rmsLevel * (1.0 - smooth));
       
    92 
       
    93     if (peakLevel > m_decayedPeakLevel) {
       
    94         m_peakLevel = peakLevel;
       
    95         m_decayedPeakLevel = peakLevel;
       
    96         m_peakLevelChanged.start();
       
    97     }
       
    98 
       
    99     if (peakLevel > m_peakHoldLevel) {
       
   100         m_peakHoldLevel = peakLevel;
       
   101         m_peakHoldLevelChanged.start();
       
   102     }
       
   103 
       
   104     update();
       
   105 }
       
   106 
       
   107 void LevelMeter::redrawTimerExpired()
       
   108 {
       
   109     // Decay the peak signal
       
   110     const int elapsedMs = m_peakLevelChanged.elapsed();
       
   111     const qreal decayAmount = m_peakDecayRate * elapsedMs;
       
   112     if (decayAmount < m_peakLevel)
       
   113         m_decayedPeakLevel = m_peakLevel - decayAmount;
       
   114     else
       
   115         m_decayedPeakLevel = 0.0;
       
   116 
       
   117     // Check whether to clear the peak hold level
       
   118     if (m_peakHoldLevelChanged.elapsed() > PeakHoldLevelDuration)
       
   119         m_peakHoldLevel = 0.0;
       
   120 
       
   121     update();
       
   122 }
       
   123 
       
   124 void LevelMeter::paintEvent(QPaintEvent *event)
       
   125 {
       
   126     Q_UNUSED(event)
       
   127 
       
   128     QPainter painter(this);
       
   129     painter.fillRect(rect(), Qt::black);
       
   130 
       
   131     QRect bar = rect();
       
   132 
       
   133     bar.setTop(rect().top() + (1.0 - m_peakHoldLevel) * rect().height());
       
   134     bar.setBottom(bar.top() + 5);
       
   135     painter.fillRect(bar, m_rmsColor);
       
   136     bar.setBottom(rect().bottom());
       
   137 
       
   138     bar.setTop(rect().top() + (1.0 - m_decayedPeakLevel) * rect().height());
       
   139     painter.fillRect(bar, m_peakColor);
       
   140 
       
   141     bar.setTop(rect().top() + (1.0 - m_rmsLevel) * rect().height());
       
   142     painter.fillRect(bar, m_rmsColor);
       
   143 }