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