summaryrefslogtreecommitdiffstats
path: root/noatun/modules/kjofol-skin/kjvis.cpp
blob: 712460896963f3def4fefe8fffe3d13c4bdc5dba (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/***************************************************************************
	kjvis.cpp  -  Visualizations used in the KJöfol-GUI
	--------------------------------------
	Maintainer: Stefan Gehn <metz AT gehn.net>

 ***************************************************************************/

// local includes
#include "kjvis.h"
#include "kjprefs.h"

// system includes
#include <math.h>

//qt includes
#include <qpainter.h>
#include <qsize.h>

//kde includes
#include <kdebug.h>
#include <kglobal.h>
#include <kconfig.h>
#include <kpixmapeffect.h>
#include <kpixmap.h>

// noatun includes
#include <noatun/player.h>

#define _KJ_GRADIENT_DIFF 130

/*******************************************
 * KJFFTScope
 *******************************************/

void KJVisScope::swapScope(Visuals newOne)
{
	//kdDebug(66666) << k_funcinfo << endl;
	QStringList line = parent()->item("analyzerwindow");
	KJLoader *p=parent();
	p->removeChild(this);
	delete this;

	KJLoader::kjofol->prefs()->setVisType ( newOne );

	KJWidget *w = 0;
	switch (newOne)
	{
	case Null:
		w = new KJNullScope(line, p);
		break;
	case FFT:
		w = new KJFFT(line, p);
		break;
	case StereoFFT:
		w = new KJStereoFFT(line, p);
		break;
	case Mono:
		w = new KJScope(line, p);
		break;
	};

	p->addChild(w);
}

/*******************************************
 * KJNullScope
 *******************************************/

KJNullScope::KJNullScope(const QStringList &l, KJLoader *parent)
	: KJVisScope(parent)
{
	int x  = l[1].toInt();
	int y  = l[2].toInt();
	int xs = l[3].toInt() - x;
	int ys = l[4].toInt() - y;

	// background under vis
	QPixmap tmp = parent->pixmap(parent->item("backgroundimage")[1]);
	mBack = new KPixmap ( QSize(xs,ys) );
	bitBlt( mBack, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );
	setRect ( x, y, xs, ys );
	repaint();
}

void KJNullScope::paint(QPainter *p, const QRect &)
{
	// just redraw the background
	bitBlt ( p->device(), rect().topLeft(), mBack, QRect(0,0,-1,-1), Qt::CopyROP );
}

bool KJNullScope::mousePress(const QPoint &)
{
	return true;
}

void KJNullScope::mouseRelease(const QPoint &, bool in)
{
	if (!in) // only do something if users is still inside the button
		return;

	parent()->repaint(rect(), false);
	swapScope(FFT);
}

void KJNullScope::readConfig()
{
//	kdDebug(66666) << "[KJNullScope] readConfig()" << endl;
	Visuals v = (Visuals) KJLoader::kjofol->prefs()->visType();
	if ( v != Null )
	{
		parent()->repaint(rect(), false);
		swapScope ( v );
	}
}


/*************************************************
 * KJFFT - Analyzer like visualization, mono
 *************************************************/

KJFFT::KJFFT(const QStringList &l, KJLoader *parent)
	: KJVisScope(parent), MonoFFTScope(50), mGradient(0)
{
	int x = l[1].toInt();
	int y = l[2].toInt();
	int xs = l[3].toInt()-x;
	int ys = l[4].toInt()-y;

	// each bar will be 1px wide
	mMultiples=1;

	if ( parent->exist("analyzercolor") )
	{
		QStringList &col = parser()["analyzercolor"];
		mColor.setRgb ( col[1].toInt(), col[2].toInt(), col[3].toInt() );
	}
	else // TODO: what should be default colors for Vis?
	{
		mColor.setRgb ( 255, 255, 255 ); // white is default
	}

	// background under vis
	QPixmap tmp  = parent->pixmap(parent->item("backgroundimage")[1]);
	mBack = new KPixmap ( QSize(xs,ys) );
	bitBlt( mBack, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	mAnalyzer = new KPixmap ( QSize(xs,ys) );
	bitBlt( mAnalyzer, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	// create a gradient for the bars going from 30% lighter to 30% darker than mColor
	mGradient = new KPixmap ( QSize(xs,ys) );
	KPixmapEffect::gradient ( *mGradient, mColor.light(_KJ_GRADIENT_DIFF),
		mColor.dark(_KJ_GRADIENT_DIFF), KPixmapEffect::VerticalGradient );

	setRect (x,y,xs,ys);
	setBands(magic(xs/mMultiples));
	readConfig(); // read our config settings
	start();
}

void KJFFT::scopeEvent(float *d, int size)
{
	if ( !napp->player()->isPlaying() ) // don't draw if we aren't playing (either paused or stopped)
	{
		if ( napp->player()->isStopped() ) // clear vis-window if playing has been stopped
			parent()->repaint(rect(), false);
		return;
	}

	int x = 0;
	int h = rect().height();

	QBitmap mGradientMask ( rect().width(), h, true );
	QPainter mask( &mGradientMask );

	float *start = d ;
	float *end = d + size /*- 1*/;

	// loop creating the mask for vis-gradient
	for ( ; start < end; ++start )
	{
		// 5 has been 8 before and I have no idea how this scaling works :/
		// FIXME: somebody please make it scale to 100% of height,
		//        I guess that would be nicer to look at
//		float n = log((*start)+1) * (float)h * 5;
		float n = log((*start)+1) * (float)h * 5;
		int amp=(int)n;

		// range check
		if ( amp < 0 ) amp = 0;
		else if ( amp > h ) amp = h;

		// make a part of the analyzer-gradient visible
		mask.fillRect ( x, (h-amp), mMultiples, amp, Qt::color1 );
		x += mMultiples;
	}
	// done creating our mask

	// draw background of vis into it
	bitBlt ( mAnalyzer, 0, 0, mBack, 0, 0, -1, -1, Qt::CopyROP );

	// draw the analyzer
	mGradient->setMask(mGradientMask);
	bitBlt ( mAnalyzer, 0, 0, mGradient, 0, 0, -1, -1, Qt::CopyROP );

	repaint();
}

void KJFFT::paint(QPainter *p, const QRect &)
{
	// put that thing on screen
	if ( !napp->player()->isStopped() )
		bitBlt ( p->device(), rect().topLeft(), mAnalyzer, QRect(0,0,-1,-1), Qt::CopyROP );
}


bool KJFFT::mousePress(const QPoint &)
{
	return true;
}

void KJFFT::mouseRelease(const QPoint &, bool in)
{
	if (!in) // only do something if users is still inside the button
		return;

	stop();
	parent()->repaint(rect(), false);
	swapScope(Mono);
}

void KJFFT::readConfig()
{
//	kdDebug(66666) << "[KJFFT] readConfig()" << endl;
	Visuals v = (Visuals) KJLoader::kjofol->prefs()->visType();
	if ( v != FFT )
	{
		stop();
		parent()->repaint(rect(), false);
		swapScope ( v );
		return;
	}

	mTimerValue = KJLoader::kjofol->prefs()->visTimerValue();
	setInterval( mTimerValue );
}


/*************************************************
 * KJStereoFFT - Analyzer like visualization, stereo
 *************************************************/

KJStereoFFT::KJStereoFFT(const QStringList &l, KJLoader *parent)
	: KJVisScope(parent), StereoFFTScope(50), mGradient(0)
{
	//kdDebug(66666) << k_funcinfo << endl;

	int x = l[1].toInt();
	int y = l[2].toInt();
	int xs = l[3].toInt()-x;
	int ys = l[4].toInt()-y;

	// each bar will be 1px wide
	mMultiples=1;

	if ( parent->exist("analyzercolor") )
	{
		QStringList &col = parser()["analyzercolor"];
		mColor.setRgb ( col[1].toInt(), col[2].toInt(), col[3].toInt() );
	}
	else // TODO: what should be default colors for Vis?
	{
		mColor.setRgb ( 255, 255, 255 ); // white is default
	}

	// background under vis
	QPixmap tmp  = parent->pixmap(parent->item("backgroundimage")[1]);
	mBack = new KPixmap ( QSize(xs,ys) );
	bitBlt( mBack, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	mAnalyzer = new KPixmap ( QSize(xs,ys) );
	bitBlt( mAnalyzer, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	// create a gradient for the bars going from 30% lighter to 30% darker than mColor
	mGradient = new KPixmap ( QSize(xs,ys) );
	KPixmapEffect::gradient ( *mGradient, mColor.light(_KJ_GRADIENT_DIFF),
		mColor.dark(_KJ_GRADIENT_DIFF), KPixmapEffect::VerticalGradient );

	setRect (x,y,xs,ys);
	setBands(magic(xs/mMultiples));
	readConfig(); // read our config settings
	start();
}

void KJStereoFFT::scopeEvent(float *left, float *right, int len)
{
	if ( !napp->player()->isPlaying() ) // don't draw if we aren't playing (either paused or stopped)
	{
		if ( napp->player()->isStopped() ) // clear vis-window if playing has been stopped
			parent()->repaint(rect(), false);
		return;
	}

	unsigned int h = rect().height();
	int hh = (int)(rect().height()/2);

	QBitmap mGradientMask ( rect().width(), h, true );
	QPainter mask( &mGradientMask );

	float *start = left;
	float *end = left + len;
	float n = 0.0;
	int amp = 0;
	int x = 0;

	// loop creating the mask for vis-gradient
	for ( ; start < end; ++start )
	{
		n = log((*start)+1) * (float)hh * 5;
		amp = (int)n;

		// range check
		if ( amp < 0 ) amp = 0;
		else if ( amp > hh ) amp = hh;

		// make a part of the analyzer-gradient visible
		mask.fillRect ( x, (h-amp), mMultiples, amp, Qt::color1 );
		x += mMultiples;
	}
	// done creating our mask


	start = right;
	end = right + len;
	x = 0;
	// loop creating the mask for vis-gradient
	for ( ; start < end; ++start )
	{
		n = log((*start)+1) * (float)hh * 5;
		amp = (int)n;

		// range check
		if ( amp < 0 ) amp = 0;
		else if ( amp > hh ) amp = hh;

		// make a part of the analyzer-gradient visible
		mask.fillRect ( x, 0, mMultiples, amp, Qt::color1 );
		x += mMultiples;
	}


	// draw background of vis into it
	bitBlt ( mAnalyzer, 0, 0, mBack, 0, 0, -1, -1, Qt::CopyROP );

	// draw the analyzer
	mGradient->setMask(mGradientMask);
	bitBlt ( mAnalyzer, 0, 0, mGradient, 0, 0, -1, -1, Qt::CopyROP );

	repaint();
}

void KJStereoFFT::paint(QPainter *p, const QRect &)
{
	// put that thing on screen
	if ( !napp->player()->isStopped() )
		bitBlt ( p->device(), rect().topLeft(), mAnalyzer, QRect(0,0,-1,-1), Qt::CopyROP );
}

bool KJStereoFFT::mousePress(const QPoint &)
{
	return true;
}

void KJStereoFFT::mouseRelease(const QPoint &, bool in)
{
	if (!in) // only do something if users is still inside the button
		return;
	stop();
	parent()->repaint(rect(), false);
	swapScope(Null);
}

void KJStereoFFT::readConfig()
{
	//kdDebug(66666) << k_funcinfo << endl;
	Visuals v = (Visuals) KJLoader::kjofol->prefs()->visType();
	if ( v != StereoFFT )
	{
		stop();
		parent()->repaint(rect(), false);
		swapScope ( v );
		return;
	}
	setInterval(KJLoader::kjofol->prefs()->visTimerValue());
}


/*************************************************
 * KJScope - oscilloscope like visualization
 *************************************************/

KJScope::KJScope(const QStringList &l, KJLoader *parent)
	: KJVisScope(parent), MonoScope(50)/*, blurnum(0), mOsci(0)*/
{
	int x=l[1].toInt();
	int y=l[2].toInt();
	int xs = mWidth = l[3].toInt()-x;
	int ys = mHeight = l[4].toInt()-y;

	blurnum = 0;

//	kdDebug(66666) << "Analyzer Window " << x << "," << y << " " << mWidth << "," << mHeight << endl;

	if ( parent->exist("analyzercolor") )
	{
		QStringList &col = parser()["analyzercolor"];
		mColor.setRgb ( col[1].toInt(), col[2].toInt(), col[3].toInt() );
	}
	else // FIXME: what should be default colors for Vis?
		mColor.setRgb ( 255, 255, 255 );

	// background under vis
	QPixmap tmp  = parent->pixmap(parent->item("backgroundimage")[1]);
	mBack = new KPixmap ( QSize(xs,ys) );
	bitBlt( mBack, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	mOsci = new KPixmap ( QSize(xs,ys) );
	bitBlt( mOsci, 0, 0, &tmp, x, y, xs, ys, Qt::CopyROP );

	// create a gradient
	mGradient = new KPixmap ( QSize(xs,ys) );
	KPixmapEffect::gradient ( *mGradient, mColor.light(_KJ_GRADIENT_DIFF),
		mColor.dark(_KJ_GRADIENT_DIFF), KPixmapEffect::VerticalGradient );

	setRect ( x, y, xs, ys );

	// set the samplewidth to the largest integer divisible by mWidth
	setSamples ( xs );

	readConfig();
	start();
}

void KJScope::scopeEvent(float *d, int size)
{
	if ( !napp->player()->isPlaying() )
	{
		if ( napp->player()->isStopped() )
		{
			bitBlt ( mOsci, 0, 0, mBack, 0, 0, -1, -1, Qt::CopyROP );
			repaint();
		}
		return;
	}

	float *start = d;
	float *end = d + size;

	int heightHalf = rect().height()/2 /* -1 */;
	int x = 0;

	QPainter tempP( mOsci );

	if ( blurnum == 3 )
	{  // clear whole Vis
		bitBlt ( mOsci, 0, 0, mBack, 0, 0, -1, -1, Qt::CopyROP );
		tempP.setPen( mColor.light(110) ); // 10% lighter than mColor
		blurnum=0;
	}
	else
	{
		blurnum++;
		// reduce color for blur-effect
		tempP.setPen( mColor.dark(90+(10*blurnum)) ); // darken color
	}

	for ( ; start < end; ++start )
	{
		float n = (*start) * (float)heightHalf;
		int amp = (int)n;

		// range check
		if ( amp > heightHalf ) amp = heightHalf;
		else if ( amp < -heightHalf) amp = -heightHalf;

		// draw
//		tempP.drawLine(x, heightHalf, x, heightHalf+amp);
		if ( amp > 0 )
		{
			bitBlt ( tempP.device(), QPoint(x,heightHalf), mGradient, QRect(x,heightHalf,1,amp), Qt::CopyROP );
		}
		else
		{
			amp = -amp;
			bitBlt ( tempP.device(), QPoint(x,heightHalf-amp), mGradient, QRect(x,(heightHalf-amp),1,amp), Qt::CopyROP );
		}
		x++;
	}

	repaint();
}

void KJScope::paint(QPainter *p, const QRect &)
{
	// put that thing on screen
	bitBlt ( p->device(), rect().topLeft(), mOsci, QRect(0,0,-1,-1), Qt::CopyROP );
}

bool KJScope::mousePress(const QPoint &)
{
	return true;
}

void KJScope::mouseRelease(const QPoint &, bool in)
{
	if (!in) // only do something if users is still inside the button
		return;

	stop();
	parent()->repaint(rect(), false);
	swapScope(/*Null*/ StereoFFT);
}

void KJScope::readConfig()
{
//	kdDebug(66666) << "[KJScope] readConfig()" << endl;
	Visuals v = (Visuals) KJLoader::kjofol->prefs()->visType();
	if ( v != Mono )
	{
		stop();
		parent()->repaint(rect(), false);
		swapScope ( v );
		return;
	}

	mTimerValue = KJLoader::kjofol->prefs()->visTimerValue();
	setInterval( mTimerValue );
}