]> git.sesse.net Git - kdenlive/blob - src/audiosignal.cpp
audiosignal now dockable and horizontal-/verticalable
[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 <QVBoxLayout>
23 #include <QLabel>
24 #include <QPainter>
25 #include <QDebug>
26 #include <QList>
27
28
29 AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
30 {
31     //QVBoxLayout *vbox=new QVBoxLayout(this);
32     //label=new QLabel();
33     //vbox->addWidget(label);
34     setMinimumHeight(10);
35     setMinimumWidth(10);
36     col << Qt::green <<  Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green ;
37     col << Qt::yellow <<  Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow  ;
38     col << Qt::darkYellow << Qt::darkYellow << Qt::darkYellow;
39     col << Qt::red << Qt::red;
40 }
41
42
43 void AudioSignal::showAudio(const QByteArray arr)
44 {
45     channels = arr;
46     if (peeks.count()!=channels.count()){
47         peeks=QByteArray(channels.count(),0);
48         peekage=QByteArray(channels.count(),0);
49     }
50     for (int chan=0;chan<peeks.count();chan++)
51     {
52         peekage[chan]=peekage[chan]+1;
53         if (  peeks.at(chan)<arr.at(chan) ||  peekage.at(chan)>50 )
54         {
55             peekage[chan]=0;
56             peeks[chan]=arr[chan];
57         }
58     }
59     update();
60 }
61 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
62 {
63     QPainter p(this);
64     //p.begin();
65     //p.fillRect(0,0,(unsigned char)channels[0]*width()/255,height()/2,QBrush(Qt::SolidPattern));
66     //p.fillRect(0,height()/2,(unsigned char)channels[1]*width()/255,height()/2,QBrush(Qt::SolidPattern));
67     int numchan = channels.size();
68     bool horiz=width() > height();
69     for (int i = 0; i < numchan; i++) {
70         int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
71         int xdelta=(horiz ? width():height() )  /20 ;
72         int _y2= (horiz ?  height() :width () ) / numchan - 1  ;
73         int _y1=(horiz ? height():width() ) *i/numchan;
74         int _x2=maxx >  xdelta ? xdelta - (horiz?1:3) : maxx - (horiz ?1 :3 );
75
76         for (int x = 0; x < 20; x++) {
77             int _x1= x *xdelta;
78             if (maxx > 0) {
79                 //p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
80                 p.fillRect(horiz?_x1:_y1, horiz?_y1:height()-_x1, horiz?_x2:_y2,horiz? _y2:-_x2, QBrush(col.at(x), Qt::SolidPattern));
81                 maxx -= xdelta;
82             }
83         }
84         int xp=peeks.at(i)*(horiz?width():height())/127-2;
85         p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
86     }
87     p.end();
88 }
89 #include "audiosignal.moc"