]> git.sesse.net Git - kdenlive/blob - src/scopes/audioscopes/audiosignal.cpp
Continue merging of scopemanager from Granjow
[kdenlive] / src / scopes / audioscopes / 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 #include <math.h>
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     dbscale << 0 << -1 << -2 << -3 << -4 << -5 << -6 << -8 << -10 << -20 << -40 ;
41     setContextMenuPolicy(Qt::ActionsContextMenu);
42     m_aMonitoringEnabled = new QAction(i18n("Monitor audio signal"), this);
43     m_aMonitoringEnabled->setCheckable(true);
44     m_aMonitoringEnabled->setChecked(true);
45     connect(m_aMonitoringEnabled, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
46     connect(&m_timer,SIGNAL(timeout()),this,SLOT(slotNoAudioTimeout()));
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
63     int num_samples = samples > 200 ? 200 : samples;
64
65     QByteArray channels;
66     int num_oversample=0;
67     for (int i = 0; i < num_channels; i++) {
68         long val = 0;
69         double over1=0.0;
70         double over2=0.0;
71         for (int s = 0; s < num_samples; s ++) {
72             int sample=abs(data[i+s*num_channels] / 128);
73             val += sample;
74             if (sample==128){
75                 num_oversample++;
76             }else{
77                 num_oversample=0;
78             }
79             //if 3 samples over max => 1 peak over 0 db (0db=40.0)
80             if (num_oversample>3){
81                 over1=41.0/42.0*127;
82             }
83             // 10 samples @max => show max signal
84             if (num_oversample>10){
85                 over2=127;
86             }
87
88
89
90         }
91         //max amplitude = 40/42, 3to10  oversamples=41, more then 10 oversamples=42 
92         if (over2>0.0){
93             channels.append(over2);             
94         } else if (over1>0.0){
95             channels.append(over1);             
96         }else
97             channels.append(val / num_samples*40.0/42.0);
98     }
99     showAudio(channels);
100     m_timer.start(1000);
101 }
102
103 void AudioSignal::slotNoAudioTimeout(){
104     peeks.fill(0);
105     showAudio(QByteArray(2,0));
106     m_timer.stop();
107 }
108
109 void AudioSignal::showAudio(const QByteArray arr)
110 {
111     channels = arr;
112     if (peeks.count()!=channels.count()){
113         peeks=QByteArray(channels.count(),0);
114         peekage=QByteArray(channels.count(),0);
115     }
116     for (int chan=0;chan<peeks.count();chan++)
117     {
118         peekage[chan]=peekage[chan]+1;
119         if (  peeks.at(chan)<arr.at(chan) ||  peekage.at(chan)>50 )
120         {
121             peekage[chan]=0;
122             peeks[chan]=arr[chan];
123         }
124     }
125     update();
126 }
127
128 double AudioSignal::valueToPixel(double in)
129 {
130         //in=0 -> return 0 (null length from max), in=127/127 return 1 (max length )
131         return 1.0- log10( in)/log10(1.0/127.0);
132 }
133
134 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
135 {
136     if (!m_aMonitoringEnabled->isChecked()) {
137         return;
138     }
139     QPainter p(this);
140     int numchan = channels.size();
141     bool horiz=width() > height();
142     int dbsize=20;
143     bool showdb=width()>(dbsize+40);
144     //valpixel=1.0 for 127, 1.0+(1/40) for 1 short oversample, 1.0+(2/40) for longer oversample
145     for (int i = 0; i < numchan; i++) {
146         //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
147         double valpixel=valueToPixel((double)(unsigned char)channels[i]/127.0);
148         int maxx=  height()  * valpixel; 
149         int xdelta= height()   /42 ;
150         int _y2= (showdb?width()-dbsize:width () ) / numchan - 1  ;
151         int _y1= (showdb?width()-dbsize:width() ) *i/numchan;
152         int _x2= maxx >  xdelta ? xdelta - 3 : maxx - 3 ;
153         if (horiz){
154             dbsize=9;
155             showdb=height()>(dbsize);
156             maxx=width()*valpixel; 
157             xdelta = width() / 42;
158             _y2=( showdb?height()-dbsize:height() ) / numchan - 1 ;
159             _y1= (showdb?height()-dbsize:height() ) * i/numchan;
160             _x2= maxx >  xdelta ? xdelta - 1 : maxx - 1;
161         }
162
163         for (int x = 0; x <= 42; x++) {
164             int _x1= x *xdelta;
165             QColor sig=Qt::green;
166             //value of actual painted digit
167             double ival=(double)_x1/(double)xdelta/42.0;
168             if (ival > 40.0/42.0){
169                 sig=Qt::red;
170             }else if ( ival > 37.0/42.0){
171                 sig=Qt::darkYellow;
172             }else if ( ival >30.0/42.0){
173                 sig=Qt::yellow;
174             }
175             if (maxx > 0) {
176                 if (horiz){
177                     p.fillRect(_x1, _y1, _x2, _y2, QBrush(sig, Qt::SolidPattern) );
178                 }else{
179                     p.fillRect(_y1, height()-_x1, _y2,-_x2, QBrush(sig, Qt::SolidPattern) );
180                 }
181                 maxx -= xdelta;
182             }
183         }
184         int xp=valueToPixel((double)peeks.at(i)/127.0)*(horiz?width():height())-2;
185         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
186
187     }
188     if (showdb){
189         //draw db value at related pixel
190         for (int l=0;l<dbscale.size();l++){
191             if (!horiz){
192                 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)height();
193                 p.drawText(width()-20,height()-xf*40.0/42.0+20, QString().sprintf("%d",dbscale.at(l)));
194             }else{
195                 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)width();
196                 p.drawText(xf*40/42-10,height()-2, QString().sprintf("%d",dbscale.at(l)));
197             }
198         }
199     }
200     p.end();
201 }
202
203 void AudioSignal::slotSwitchAudioMonitoring(bool)
204 {
205     emit updateAudioMonitoring();
206 }
207
208 #include "audiosignal.moc"