]> git.sesse.net Git - kdenlive/commitdiff
audiosignal shows db
authorMarco Gittler <marco@gitma.de>
Thu, 10 Nov 2011 12:27:42 +0000 (13:27 +0100)
committerMarco Gittler <marco@gitma.de>
Thu, 10 Nov 2011 22:04:39 +0000 (23:04 +0100)
src/audiosignal.cpp
src/audiosignal.h

index 6ccd2dd5ea30e0b627a30f5da3372b47e055854c..00c5946541c70f71cd954e46087e76acb7a9cc46 100644 (file)
@@ -37,14 +37,13 @@ AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
     //vbox->addWidget(label);
     setMinimumHeight(10);
     setMinimumWidth(10);
-    col << Qt::green <<  Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green ;
-    col << Qt::yellow <<  Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow  ;
-    col << Qt::darkYellow << Qt::darkYellow << Qt::darkYellow;
-    col << Qt::red << Qt::red;
+    dbscale << 0 << -1 << -2 << -3 << -4 << -5 << -6 << -8 << -10 << -20 << -40 ;
     setContextMenuPolicy(Qt::ActionsContextMenu);
     m_aMonitoringEnabled = new QAction(i18n("Monitor audio signal"), this);
     m_aMonitoringEnabled->setCheckable(true);
+    m_aMonitoringEnabled->setChecked(true);
     connect(m_aMonitoringEnabled, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
+    connect(&m_timer,SIGNAL(timeout()),this,SLOT(slotNoAudioTimeout()));
     addAction(m_aMonitoringEnabled);
 }
 
@@ -63,15 +62,49 @@ void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int, int num_ch
     int num_samples = samples > 200 ? 200 : samples;
 
     QByteArray channels;
+    int num_oversample=0;
     for (int i = 0; i < num_channels; i++) {
         long val = 0;
+        double over1=0.0;
+        double over2=0.0;
         for (int s = 0; s < num_samples; s ++) {
-            val += abs(data[i+s*num_channels] / 128);
+            int sample=abs(data[i+s*num_channels] / 128);
+            val += sample;
+            if (sample==128){
+                num_oversample++;
+            }else{
+                num_oversample=0;
+            }
+            //if 3 samples over max => 1 peak over 0 db (0db=40.0)
+            if (num_oversample>3){
+                over1=41.0/42.0*127;
+            }
+            // 10 samples @max => show max signal
+            if (num_oversample>10){
+                over2=127;
+            }
+
+
+
         }
-        channels.append(val / num_samples);
+        //max amplitude = 40/42, 3to10  oversamples=41, more then 10 oversamples=42 
+        if (over2>0.0){
+            channels.append(over2);            
+        } else if (over1>0.0){
+            channels.append(over1);            
+        }else
+            channels.append(val / num_samples*40.0/42.0);
     }
     showAudio(channels);
+    m_timer.start(1000);
+}
+
+void AudioSignal::slotNoAudioTimeout(){
+    peeks.fill(0);
+    showAudio(QByteArray(2,0));
+    m_timer.stop();
 }
+
 void AudioSignal::showAudio(const QByteArray arr)
 {
     channels = arr;
@@ -91,17 +124,10 @@ void AudioSignal::showAudio(const QByteArray arr)
     update();
 }
 
-double AudioSignal::valueToPixel(double in,bool db)
+double AudioSignal::valueToPixel(double in)
 {
-    if (db)
-    {
-        // ratio db(in)/db(0.01) (means: min db value = -40.0 )
-        return 1.0- log10( in)/log10(0.01);
-    }
-    else
-    {
-        return in;
-    }
+       //in=0 -> return 0 (null length from max), in=127/127 return 1 (max length )
+       return 1.0- log10( in)/log10(1.0/127.0);
 }
 
 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
@@ -110,30 +136,65 @@ void AudioSignal::paintEvent(QPaintEvent* /*e*/)
         return;
     }
     QPainter p(this);
-    //p.begin();
-    //p.fillRect(0,0,(unsigned char)channels[0]*width()/255,height()/2,QBrush(Qt::SolidPattern));
-    //p.fillRect(0,height()/2,(unsigned char)channels[1]*width()/255,height()/2,QBrush(Qt::SolidPattern));
     int numchan = channels.size();
-    bool db=true; // show values in db(i)
     bool horiz=width() > height();
+    int dbsize=20;
+    bool showdb=width()>(dbsize+40);
+    //valpixel=1.0 for 127, 1.0+(1/40) for 1 short oversample, 1.0+(2/40) for longer oversample
     for (int i = 0; i < numchan; i++) {
         //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
-        int maxx= (horiz ? width() : height() ) * valueToPixel((double)(unsigned char)channels[i]/127.0,db);
-        int xdelta=(horiz ? width():height() )  /20 ;
-        int _y2= (horiz ?  height() :width () ) / numchan - 1  ;
-        int _y1=(horiz ? height():width() ) *i/numchan;
-        int _x2=maxx >  xdelta ? xdelta - (horiz?1:3) : maxx - (horiz ?1 :3 );
+        double valpixel=valueToPixel((double)(unsigned char)channels[i]/127.0);
+        int maxx=  height()  * valpixel; 
+        int xdelta= height()   /42 ;
+        int _y2= (showdb?width()-dbsize:width () ) / numchan - 1  ;
+        int _y1= (showdb?width()-dbsize:width() ) *i/numchan;
+        int _x2= maxx >  xdelta ? xdelta - 3 : maxx - 3 ;
+        if (horiz){
+            dbsize=9;
+            showdb=height()>(dbsize);
+            maxx=width()*valpixel; 
+            xdelta = width() / 42;
+            _y2=( showdb?height()-dbsize:height() ) / numchan - 1 ;
+            _y1= (showdb?height()-dbsize:height() ) * i/numchan;
+            _x2= maxx >  xdelta ? xdelta - 1 : maxx - 1;
+        }
 
-        for (int x = 0; x < 20; x++) {
+        for (int x = 0; x <= 42; x++) {
             int _x1= x *xdelta;
+            QColor sig=Qt::green;
+            //value of actual painted digit
+            double ival=(double)_x1/(double)xdelta/42.0;
+            if (ival > 40.0/42.0){
+                sig=Qt::red;
+            }else if ( ival > 37.0/42.0){
+                sig=Qt::darkYellow;
+            }else if ( ival >30.0/42.0){
+                sig=Qt::yellow;
+            }
             if (maxx > 0) {
-                //p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
-                p.fillRect(horiz?_x1:_y1, horiz?_y1:height()-_x1, horiz?_x2:_y2,horiz? _y2:-_x2, QBrush(col.at(x), Qt::SolidPattern));
+                if (horiz){
+                    p.fillRect(_x1, _y1, _x2, _y2, QBrush(sig, Qt::SolidPattern) );
+                }else{
+                    p.fillRect(_y1, height()-_x1, _y2,-_x2, QBrush(sig, Qt::SolidPattern) );
+                }
                 maxx -= xdelta;
             }
         }
-        int xp=valueToPixel((double)peeks.at(i)/127.0,db)*(horiz?width():height())-2;
+        int xp=valueToPixel((double)peeks.at(i)/127.0)*(horiz?width():height())-2;
         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
+
+    }
+    if (showdb){
+        //draw db value at related pixel
+        for (int l=0;l<dbscale.size();l++){
+            if (!horiz){
+                double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)height();
+                p.drawText(width()-20,height()-xf*40.0/42.0+20, QString().sprintf("%d",dbscale.at(l)));
+            }else{
+                double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)width();
+                p.drawText(xf*40/42-10,height()-2, QString().sprintf("%d",dbscale.at(l)));
+            }
+        }
     }
     p.end();
 }
index 2fbaaf1c994884184fb9efcad48097dd4f0fa291..6cb9b071f963ffdea8bd4553c5656b58d82ea070 100644 (file)
@@ -23,6 +23,7 @@
 #include <QByteArray>
 #include <QList>
 #include <QColor>
+#include <QTimer>
 class QLabel;
 
 #include  <QWidget>
@@ -39,10 +40,11 @@ public:
     bool monitoringEnabled() const;
 
 private:
-    double valueToPixel(double in,bool db);
+    double valueToPixel(double in);
+       QTimer m_timer;
     QLabel* label;
     QByteArray channels,peeks,peekage;
-    QList<QColor> col;
+       QList<int> dbscale;
     QAction *m_aMonitoringEnabled;
 
 protected:
@@ -53,6 +55,7 @@ public slots:
     void slotReceiveAudio(const QVector<int16_t>&,int,int,int);
 private slots:
      void slotSwitchAudioMonitoring(bool isOn);
+       void slotNoAudioTimeout();
 
 signals:
     void updateAudioMonitoring();