summaryrefslogtreecommitdiffstats
path: root/noatun/modules/voiceprint/voiceprint.cpp
blob: 234aafd0a9967abe07e9cb384685bdfae0d0cce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "voiceprint.h"
#include <noatun/player.h>
#include <noatun/app.h>
#include <math.h>
#include <tqpainter.h>
#include "prefs.h"
#include <klocale.h>
#include <stdio.h>

extern "C"
{
	KDE_EXPORT Plugin *create_plugin()
	{
		return new VoicePrint();
	}
}

VoicePrint *VoicePrint::voicePrint=0;

VoicePrint::VoicePrint() : TQWidget(0,0,WRepaintNoErase), MonoFFTScope(50), Plugin()
{
	voicePrint=this;
	mOffset=0;
	mSegmentWidth=2;
	setCaption(i18n("Voiceprint"));
	resize(320, 240);
	MonoFFTScope::start();
	show();
	setMaximumHeight(1024);
}

VoicePrint::~VoicePrint()
{
}

void VoicePrint::init()
{
	Prefs *p=new Prefs(TQT_TQOBJECT(this));
	p->reopen();
	p->save();
	resizeEvent(0);
}

void VoicePrint::setColors(const TQColor &bg, const TQColor &fg, const TQColor &l)
{
	mProgress=l;
	mLowColor=bg.rgb();
	mHighColor=fg.rgb();
	setBackgroundColor(mLowColor);
}

void VoicePrint::closeEvent(TQCloseEvent *)
{
	unload();
}

void VoicePrint::resizeEvent(TQResizeEvent *)
{
	mOffset=0;
	mBuffer.resize(size());
	TQPainter paint(&mBuffer);
	paint.fillRect(TQRect(0,0, TQWidget::width(), height()), TQColor(mLowColor));
	setBands(magic(height()/mSegmentWidth));
}

#define COLOR(color, bgcolor, fgcolor, foctet) \
        (int)( color(bgcolor) + (foctet) * (color(fgcolor) - color(bgcolor)) )

inline static TQRgb averageByIntensity(TQRgb bgcolor, TQRgb fgcolor, int octet)
{
	float foctet = octet / 255.0;

	return tqRgb(COLOR(tqRed, bgcolor, fgcolor, foctet),
	            COLOR(tqGreen, bgcolor, fgcolor, foctet),
	            COLOR(tqBlue, bgcolor, fgcolor, foctet)
	           );
}

#undef COLOR

void VoicePrint::paintEvent(TQPaintEvent *e)
{
	bitBlt(this, e->rect().topLeft(), &mBuffer, e->rect(), TQt::CopyROP);
}

void VoicePrint::scopeEvent(float *data, int bands)
{
	// save cpu
	if(isHidden()) return;

	TQPainter paint(&mBuffer);
	// each square has a width of mSegmentWidth
	float brightness = float(bands * mSegmentWidth);
	for (int i=0; i<bands ; i++)
	{
		float b=data[bands-i-1]+1.0;
		// the more bands there are, the dimmer each becomes
		b=log10(b)/log(2) * 16 * brightness;
		int band=int(b);
		if (band>255) band=255;
		else if (band<0) band=0;
		
		TQColor area(averageByIntensity(mLowColor, mHighColor, band));

		int bandTop=i*height()/bands, bandBottom=(i+1)*height()/bands;
		paint.fillRect(mOffset, bandTop, mSegmentWidth,bandBottom-bandTop,area);
	}
	
	int newOffset = mOffset+mSegmentWidth;
	if (newOffset>TQWidget::width()) newOffset=0;
	paint.fillRect(newOffset, 0, mSegmentWidth, height(), mProgress);

	// redraw changes with the minimum amount of work
	if(newOffset != 0)
	{
		tqrepaint(mOffset,0,mSegmentWidth*2,height(),false);
	}
	else
	{
		tqrepaint(mOffset,0,mSegmentWidth,height(),false);
		tqrepaint(newOffset,0,mSegmentWidth,height(),false);
	}
	mOffset = newOffset;
}

#include "voiceprint.moc"