~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to noatun/modules/voiceprint/voiceprint.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "voiceprint.h"
 
2
#include <noatun/player.h>
 
3
#include <noatun/app.h>
 
4
#include <math.h>
 
5
#include <qpainter.h>
 
6
#include "prefs.h"
 
7
#include <klocale.h>
 
8
#include <stdio.h>
 
9
 
 
10
extern "C"
 
11
{
 
12
        KDE_EXPORT Plugin *create_plugin()
 
13
        {
 
14
                return new VoicePrint();
 
15
        }
 
16
}
 
17
 
 
18
VoicePrint *VoicePrint::voicePrint=0;
 
19
 
 
20
VoicePrint::VoicePrint() : QWidget(0,0,WRepaintNoErase), MonoFFTScope(50), Plugin()
 
21
{
 
22
        voicePrint=this;
 
23
        mOffset=0;
 
24
        mSegmentWidth=2;
 
25
        setCaption(i18n("Voiceprint"));
 
26
        resize(320, 240);
 
27
        MonoFFTScope::start();
 
28
        show();
 
29
        setMaximumHeight(1024);
 
30
}
 
31
 
 
32
VoicePrint::~VoicePrint()
 
33
{
 
34
}
 
35
 
 
36
void VoicePrint::init()
 
37
{
 
38
        Prefs *p=new Prefs(this); 
 
39
        p->reopen();
 
40
        p->save();
 
41
        resizeEvent(0);
 
42
}
 
43
 
 
44
void VoicePrint::setColors(const QColor &bg, const QColor &fg, const QColor &l)
 
45
{
 
46
        mProgress=l;
 
47
        mLowColor=bg.rgb();
 
48
        mHighColor=fg.rgb();
 
49
        setBackgroundColor(mLowColor);
 
50
}
 
51
 
 
52
void VoicePrint::closeEvent(QCloseEvent *)
 
53
{
 
54
        unload();
 
55
}
 
56
 
 
57
void VoicePrint::resizeEvent(QResizeEvent *)
 
58
{
 
59
        mOffset=0;
 
60
        mBuffer.resize(size());
 
61
        QPainter paint(&mBuffer);
 
62
        paint.fillRect(QRect(0,0, QWidget::width(), height()), QColor(mLowColor));
 
63
        setBands(magic(height()/mSegmentWidth));
 
64
}
 
65
 
 
66
#define COLOR(color, bgcolor, fgcolor, foctet) \
 
67
        (int)( color(bgcolor) + (foctet) * (color(fgcolor) - color(bgcolor)) )
 
68
 
 
69
inline static QRgb averageByIntensity(QRgb bgcolor, QRgb fgcolor, int octet)
 
70
{
 
71
        float foctet = octet / 255.0;
 
72
 
 
73
        return qRgb(COLOR(qRed, bgcolor, fgcolor, foctet),
 
74
                    COLOR(qGreen, bgcolor, fgcolor, foctet),
 
75
                    COLOR(qBlue, bgcolor, fgcolor, foctet)
 
76
                   );
 
77
}
 
78
 
 
79
#undef COLOR
 
80
 
 
81
void VoicePrint::paintEvent(QPaintEvent *e)
 
82
{
 
83
        bitBlt(this, e->rect().topLeft(), &mBuffer, e->rect(), Qt::CopyROP);
 
84
}
 
85
 
 
86
void VoicePrint::scopeEvent(float *data, int bands)
 
87
{
 
88
        // save cpu
 
89
        if(isHidden()) return;
 
90
 
 
91
        QPainter paint(&mBuffer);
 
92
        // each square has a width of mSegmentWidth
 
93
        float brightness = float(bands * mSegmentWidth);
 
94
        for (int i=0; i<bands ; i++)
 
95
        {
 
96
                float b=data[bands-i-1]+1.0;
 
97
                // the more bands there are, the dimmer each becomes
 
98
                b=log10(b)/log(2) * 16 * brightness;
 
99
                int band=int(b);
 
100
                if (band>255) band=255;
 
101
                else if (band<0) band=0;
 
102
                
 
103
                QColor area(averageByIntensity(mLowColor, mHighColor, band));
 
104
 
 
105
                int bandTop=i*height()/bands, bandBottom=(i+1)*height()/bands;
 
106
                paint.fillRect(mOffset, bandTop, mSegmentWidth,bandBottom-bandTop,area);
 
107
        }
 
108
        
 
109
        int newOffset = mOffset+mSegmentWidth;
 
110
        if (newOffset>QWidget::width()) newOffset=0;
 
111
        paint.fillRect(newOffset, 0, mSegmentWidth, height(), mProgress);
 
112
 
 
113
        // redraw changes with the minimum amount of work
 
114
        if(newOffset != 0)
 
115
        {
 
116
                repaint(mOffset,0,mSegmentWidth*2,height(),false);
 
117
        }
 
118
        else
 
119
        {
 
120
                repaint(mOffset,0,mSegmentWidth,height(),false);
 
121
                repaint(newOffset,0,mSegmentWidth,height(),false);
 
122
        }
 
123
        mOffset = newOffset;
 
124
}
 
125
 
 
126
#include "voiceprint.moc"