]> git.sesse.net Git - kdenlive/blob - src/audiosignal.cpp
Audio scopes: Only deliver audio when required for better performance when scopes...
[kdenlive] / src / audiosignal.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Marco Gittler (g.marco@freenet.de)              *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "audiosignal.h"
21
22 #include <KLocale>
23
24 #include <QVBoxLayout>
25 #include <QLabel>
26 #include <QAction>
27 #include <QPainter>
28 #include <QDebug>
29 #include <QList>
30
31
32 AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
33 {
34     //QVBoxLayout *vbox=new QVBoxLayout(this);
35     //label=new QLabel();
36     //vbox->addWidget(label);
37     setMinimumHeight(10);
38     setMinimumWidth(10);
39     col << Qt::green <<  Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green ;
40     col << Qt::yellow <<  Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow  ;
41     col << Qt::darkYellow << Qt::darkYellow << Qt::darkYellow;
42     col << Qt::red << Qt::red;
43     setContextMenuPolicy(Qt::ActionsContextMenu);
44     m_aMonitoringEnabled = new QAction(i18n("Monitor audio signal"), this);
45     m_aMonitoringEnabled->setCheckable(true);
46     connect(m_aMonitoringEnabled, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
47     addAction(m_aMonitoringEnabled);
48 }
49
50 AudioSignal::~AudioSignal()
51 {
52     delete m_aMonitoringEnabled;
53 }
54
55 bool AudioSignal::monitoringEnabled() const
56 {
57     return m_aMonitoringEnabled->isChecked();
58 }
59
60 void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int, int num_channels, int samples){
61
62     int num_samples = samples > 200 ? 200 : samples;
63
64     QByteArray channels;
65     for (int i = 0; i < num_channels; i++) {
66         long val = 0;
67         for (int s = 0; s < num_samples; s ++) {
68             val += abs(data[i+s*num_channels] / 128);
69         }
70         channels.append(val / num_samples);
71     }
72     showAudio(channels);
73 }
74 void AudioSignal::showAudio(const QByteArray arr)
75 {
76     channels = arr;
77     if (peeks.count()!=channels.count()){
78         peeks=QByteArray(channels.count(),0);
79         peekage=QByteArray(channels.count(),0);
80     }
81     for (int chan=0;chan<peeks.count();chan++)
82     {
83         peekage[chan]=peekage[chan]+1;
84         if (  peeks.at(chan)<arr.at(chan) ||  peekage.at(chan)>50 )
85         {
86             peekage[chan]=0;
87             peeks[chan]=arr[chan];
88         }
89     }
90     update();
91 }
92 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
93 {
94     if (!m_aMonitoringEnabled->isChecked()) {
95         return;
96     }
97     QPainter p(this);
98     //p.begin();
99     //p.fillRect(0,0,(unsigned char)channels[0]*width()/255,height()/2,QBrush(Qt::SolidPattern));
100     //p.fillRect(0,height()/2,(unsigned char)channels[1]*width()/255,height()/2,QBrush(Qt::SolidPattern));
101     int numchan = channels.size();
102     bool horiz=width() > height();
103     for (int i = 0; i < numchan; i++) {
104         int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
105         int xdelta=(horiz ? width():height() )  /20 ;
106         int _y2= (horiz ?  height() :width () ) / numchan - 1  ;
107         int _y1=(horiz ? height():width() ) *i/numchan;
108         int _x2=maxx >  xdelta ? xdelta - (horiz?1:3) : maxx - (horiz ?1 :3 );
109
110         for (int x = 0; x < 20; x++) {
111             int _x1= x *xdelta;
112             if (maxx > 0) {
113                 //p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
114                 p.fillRect(horiz?_x1:_y1, horiz?_y1:height()-_x1, horiz?_x2:_y2,horiz? _y2:-_x2, QBrush(col.at(x), Qt::SolidPattern));
115                 maxx -= xdelta;
116             }
117         }
118         int xp=peeks.at(i)*(horiz?width():height())/127-2;
119         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
120     }
121     p.end();
122 }
123
124 void AudioSignal::slotSwitchAudioMonitoring(bool)
125 {
126     emit updateAudioMonitoring();
127 }
128
129 #include "audiosignal.moc"