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