]> git.sesse.net Git - kdenlive/blob - src/scopes/audioscopes/audiosignal.cpp
const'ref. REmove not necessary virtual keyword. Fix indent.
[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 #include <KDebug>
24
25 #include <QVBoxLayout>
26 #include <QAction>
27 #include <QMenu>
28 #include <QPainter>
29 #include <QDebug>
30 #include <QList>
31
32 #include <math.h>
33
34 AudioSignal::AudioSignal(QWidget *parent): AbstractAudioScopeWidget(false, parent)
35 {
36     setMinimumHeight(10);
37     setMinimumWidth(10);
38     dbscale << 0 << -1 << -2 << -3 << -4 << -5 << -6 << -8 << -10 << -20 << -40 ;
39     m_menu->removeAction(m_aRealtime);
40     connect(&m_timer,SIGNAL(timeout()),this,SLOT(slotNoAudioTimeout()));
41     init();
42 }
43
44 AudioSignal::~AudioSignal()
45 {
46 }
47
48 QImage AudioSignal::renderAudioScope(uint, const QVector<int16_t> &audioFrame,
49                                      const int, const int num_channels, const int samples, const int)
50 {
51     QTime start = QTime::currentTime();
52
53     int num_samples = samples > 200 ? 200 : samples;
54
55     QByteArray channels;
56     for (int i = 0; i < num_channels; ++i) {
57         long val = 0;
58         for (int s = 0; s < num_samples; s ++) {
59             val += abs(audioFrame[i+s*num_channels] / 128);
60         }
61         channels.append(val / num_samples);
62     }
63
64     if (peeks.count()!=channels.count()){
65         peeks=QByteArray(channels.count(),0);
66         peekage=QByteArray(channels.count(),0);
67     }
68     for (int chan=0;chan<peeks.count();chan++)
69     {
70         peekage[chan] = peekage[chan]+1;
71         if (  peeks.at(chan)<channels.at(chan) ||  peekage.at(chan)>50 )
72         {
73             peekage[chan]=0;
74             peeks[chan]=channels[chan];
75         }
76     }
77
78     QImage image(m_scopeRect.size(), QImage::Format_ARGB32);
79     image.fill(Qt::transparent);
80     QPainter p(&image);
81     p.setPen(Qt::white);
82     p.setRenderHint(QPainter::TextAntialiasing, false);
83     p.setRenderHint(QPainter::Antialiasing, false);
84     
85     int numchan = channels.size();
86     bool horiz=width() > height();
87     int dbsize=20;
88     bool showdb=width()>(dbsize+40);
89     //valpixel=1.0 for 127, 1.0+(1/40) for 1 short oversample, 1.0+(2/40) for longer oversample
90     for (int i = 0; i < numchan; ++i) {
91         //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
92         double valpixel=valueToPixel((double)(unsigned char)channels[i]/127.0);
93         int maxx=  height()  * valpixel;
94         int xdelta= height()   /42 ;
95         int _y2= (showdb?width()-dbsize:width () ) / numchan - 1  ;
96         int _y1= (showdb?width()-dbsize:width() ) *i/numchan;
97         int _x2= maxx >  xdelta ? xdelta - 3 : maxx - 3 ;
98         if (horiz){
99             dbsize=9;
100             showdb=height()>(dbsize);
101             maxx=width()*valpixel;
102             xdelta = width() / 42;
103             _y2=( showdb?height()-dbsize:height() ) / numchan - 1 ;
104             _y1= (showdb?height()-dbsize:height() ) * i/numchan;
105             _x2= maxx >  xdelta ? xdelta - 1 : maxx - 1;
106         }
107
108         for (int x = 0; x <= 42; x++) {
109             int _x1= x *xdelta;
110             QColor sig=Qt::green;
111             //value of actual painted digit
112             double ival=(double)_x1/(double)xdelta/42.0;
113             if (ival > 40.0/42.0){
114                 sig=Qt::red;
115             }else if ( ival > 37.0/42.0){
116                 sig=Qt::darkYellow;
117             }else if ( ival >30.0/42.0){
118                 sig=Qt::yellow;
119             }
120             if (maxx > 0) {
121                 if (horiz){
122                     p.fillRect(_x1, _y1, _x2, _y2, QBrush(sig, Qt::SolidPattern) );
123                 }else{
124                     p.fillRect(_y1, height()-_x1, _y2,-_x2, QBrush(sig, Qt::SolidPattern) );
125                 }
126                 maxx -= xdelta;
127             }
128         }
129         int xp=valueToPixel((double)peeks.at(i)/127.0)*(horiz?width():height())-2;
130         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::gray,Qt::SolidPattern));
131
132     }
133     if (showdb){
134         //draw db value at related pixel
135         for (int l=0;l<dbscale.size();l++){
136             if (!horiz){
137                 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)height();
138                 p.drawText(width()-20,height()-xf*40.0/42.0+20, QString().sprintf("%d",dbscale.at(l)));
139             }else{
140                 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)width();
141                 p.drawText(xf*40/42-10,height()-2, QString().sprintf("%d",dbscale.at(l)));
142             }
143         }
144     }
145     p.end();
146     emit signalScopeRenderingFinished((uint) start.elapsed(), 1);
147     return image;
148 }
149
150 QRect AudioSignal::scopeRect() { return QRect(0, 0, width(), height()); }
151
152 QImage AudioSignal::renderHUD(uint) { return QImage(); }
153 QImage AudioSignal::renderBackground(uint) { return QImage(); }
154
155 void AudioSignal::slotReceiveAudio(QVector<int16_t> data, int, int num_channels, int samples)
156 {
157
158     int num_samples = samples > 200 ? 200 : samples;
159
160     QByteArray channels;
161     int num_oversample=0;
162     for (int i = 0; i < num_channels; ++i) {
163         long val = 0;
164         double over1=0.0;
165         double over2=0.0;
166         for (int s = 0; s < num_samples; s ++) {
167             int sample=abs(data[i+s*num_channels] / 128);
168             val += sample;
169             if (sample==128){
170                 num_oversample++;
171             }else{
172                 num_oversample=0;
173             }
174             //if 3 samples over max => 1 peak over 0 db (0db=40.0)
175             if (num_oversample>3){
176                 over1=41.0/42.0*127;
177             }
178             // 10 samples @max => show max signal
179             if (num_oversample>10){
180                 over2=127;
181             }
182
183
184
185         }
186         //max amplitude = 40/42, 3to10  oversamples=41, more then 10 oversamples=42
187         if (over2>0.0){
188             channels.append(over2);
189         } else if (over1>0.0){
190             channels.append(over1);
191         }else
192             channels.append(val / num_samples*40.0/42.0);
193     }
194     showAudio(channels);
195     m_timer.start(1000);
196 }
197
198 void AudioSignal::slotNoAudioTimeout(){
199     peeks.fill(0);
200     showAudio(QByteArray(2,0));
201     m_timer.stop();
202 }
203
204 void AudioSignal::showAudio(const QByteArray &arr)
205 {
206     channels = arr;
207     if (peeks.count()!=channels.count()){
208         peeks=QByteArray(channels.count(),0);
209         peekage=QByteArray(channels.count(),0);
210     }
211     for (int chan=0;chan<peeks.count();chan++)
212     {
213         peekage[chan]=peekage[chan]+1;
214         if (  peeks.at(chan)<arr.at(chan) ||  peekage.at(chan)>50 )
215         {
216             peekage[chan]=0;
217             peeks[chan]=arr[chan];
218         }
219     }
220     update();
221 }
222
223 double AudioSignal::valueToPixel(double in)
224 {
225     //in=0 -> return 0 (null length from max), in=127/127 return 1 (max length )
226     return 1.0- log10( in)/log10(1.0/127.0);
227 }
228
229 #include "audiosignal.moc"