]> git.sesse.net Git - kdenlive/blob - src/audiosignal.cpp
6a935869b0d97e99ce857711eecef9efc2e38e8f
[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 #include "kdenlivesettings.h"
22
23 #include <KLocale>
24
25 #include <QVBoxLayout>
26 #include <QLabel>
27 #include <QAction>
28 #include <QPainter>
29 #include <QDebug>
30 #include <QList>
31
32
33 AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
34 {
35     //QVBoxLayout *vbox=new QVBoxLayout(this);
36     //label=new QLabel();
37     //vbox->addWidget(label);
38     setMinimumHeight(10);
39     setMinimumWidth(10);
40     col << Qt::green <<  Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green ;
41     col << Qt::yellow <<  Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow  ;
42     col << Qt::darkYellow << Qt::darkYellow << Qt::darkYellow;
43     col << Qt::red << Qt::red;
44     setContextMenuPolicy(Qt::ActionsContextMenu);
45     QAction *showSignal = new QAction(i18n("Monitor audio signal"), this);
46     showSignal->setCheckable(true);
47     showSignal->setChecked(KdenliveSettings::monitor_audio());
48     connect(showSignal, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
49     addAction(showSignal);
50 }
51
52
53 void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int freq ,int num_channels ,int samples){
54
55     int num_samples = samples > 200 ? 200 : samples;
56
57     QByteArray channels;
58     for (int i = 0; i < num_channels; i++) {
59         long val = 0;
60         for (int s = 0; s < num_samples; s ++) {
61             val += abs(data[i+s*num_channels] / 128);
62         }
63         channels.append(val / num_samples);
64     }
65     showAudio(channels);
66 }
67 void AudioSignal::showAudio(const QByteArray arr)
68 {
69     channels = arr;
70     if (peeks.count()!=channels.count()){
71         peeks=QByteArray(channels.count(),0);
72         peekage=QByteArray(channels.count(),0);
73     }
74     for (int chan=0;chan<peeks.count();chan++)
75     {
76         peekage[chan]=peekage[chan]+1;
77         if (  peeks.at(chan)<arr.at(chan) ||  peekage.at(chan)>50 )
78         {
79             peekage[chan]=0;
80             peeks[chan]=arr[chan];
81         }
82     }
83     update();
84 }
85 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
86 {
87     QPainter p(this);
88     //p.begin();
89     //p.fillRect(0,0,(unsigned char)channels[0]*width()/255,height()/2,QBrush(Qt::SolidPattern));
90     //p.fillRect(0,height()/2,(unsigned char)channels[1]*width()/255,height()/2,QBrush(Qt::SolidPattern));
91     int numchan = channels.size();
92     bool horiz=width() > height();
93     for (int i = 0; i < numchan; i++) {
94         int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
95         int xdelta=(horiz ? width():height() )  /20 ;
96         int _y2= (horiz ?  height() :width () ) / numchan - 1  ;
97         int _y1=(horiz ? height():width() ) *i/numchan;
98         int _x2=maxx >  xdelta ? xdelta - (horiz?1:3) : maxx - (horiz ?1 :3 );
99
100         for (int x = 0; x < 20; x++) {
101             int _x1= x *xdelta;
102             if (maxx > 0) {
103                 //p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
104                 p.fillRect(horiz?_x1:_y1, horiz?_y1:height()-_x1, horiz?_x2:_y2,horiz? _y2:-_x2, QBrush(col.at(x), Qt::SolidPattern));
105                 maxx -= xdelta;
106             }
107         }
108         int xp=peeks.at(i)*(horiz?width():height())/127-2;
109         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
110     }
111     p.end();
112 }
113
114 void AudioSignal::slotSwitchAudioMonitoring(bool isOn)
115 {
116     KdenliveSettings::setMonitor_audio(isOn);
117     emit updateAudioMonitoring();
118 }
119
120 #include "audiosignal.moc"