]> git.sesse.net Git - kdenlive/blobdiff - src/audiosignal.cpp
Fix possible crash on exit
[kdenlive] / src / audiosignal.cpp
index 4a147efbbfd3555f65b165fe8bc0e9cb50c91bb8..6a935869b0d97e99ce857711eecef9efc2e38e8f 100644 (file)
  ***************************************************************************/
 
 #include "audiosignal.h"
+#include "kdenlivesettings.h"
+
+#include <KLocale>
 
 #include <QVBoxLayout>
 #include <QLabel>
+#include <QAction>
 #include <QPainter>
 #include <QDebug>
 #include <QList>
@@ -32,13 +36,34 @@ AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
     //label=new QLabel();
     //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;
+    setContextMenuPolicy(Qt::ActionsContextMenu);
+    QAction *showSignal = new QAction(i18n("Monitor audio signal"), this);
+    showSignal->setCheckable(true);
+    showSignal->setChecked(KdenliveSettings::monitor_audio());
+    connect(showSignal, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
+    addAction(showSignal);
 }
 
 
+void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int freq ,int num_channels ,int samples){
+
+    int num_samples = samples > 200 ? 200 : samples;
+
+    QByteArray channels;
+    for (int i = 0; i < num_channels; i++) {
+        long val = 0;
+        for (int s = 0; s < num_samples; s ++) {
+            val += abs(data[i+s*num_channels] / 128);
+        }
+        channels.append(val / num_samples);
+    }
+    showAudio(channels);
+}
 void AudioSignal::showAudio(const QByteArray arr)
 {
     channels = arr;
@@ -64,19 +89,32 @@ void AudioSignal::paintEvent(QPaintEvent* /*e*/)
     //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 horiz=width() > height();
     for (int i = 0; i < numchan; i++) {
-        int maxx = (unsigned char)channels[i] * width() / 255;
-        int xdelta = width() / 20;
-        int y1 = height() * i / numchan;
-        int _h = height() / numchan - 1;
+        int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
+        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 );
+
         for (int x = 0; x < 20; x++) {
+            int _x1= x *xdelta;
             if (maxx > 0) {
-                p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
+                //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));
                 maxx -= xdelta;
             }
         }
-        p.fillRect(peeks.at(i)*width()/255-2,y1,3,_h,QBrush(Qt::black,Qt::SolidPattern));
+        int xp=peeks.at(i)*(horiz?width():height())/127-2;
+        p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
     }
     p.end();
 }
+
+void AudioSignal::slotSwitchAudioMonitoring(bool isOn)
+{
+    KdenliveSettings::setMonitor_audio(isOn);
+    emit updateAudioMonitoring();
+}
+
 #include "audiosignal.moc"