summaryrefslogtreecommitdiffstats
path: root/amarok/src/analyzers/glanalyzer.cpp
blob: a338773456e17ea2b1b55106f30064bc5f494497 (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
/***************************************************************************
                          gloscope.cpp  -  description
                             -------------------
    begin                : Jan 17 2004
    copyright            : (C) 2004 by Adam Pigg
    email                : adam@piggz.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <config.h>

#ifdef HAVE_TQGLWIDGET

#include <cmath>
#include "glanalyzer.h"
#include <kdebug.h>


GLAnalyzer::GLAnalyzer( TQWidget *tqparent )
        : Analyzer::Base3D(tqparent, 15)
        , m_oldy(32, -10.0f)
        , m_peaks(32)
{}

GLAnalyzer::~GLAnalyzer()
{}

// METHODS =====================================================

void GLAnalyzer::analyze( const Scope &s )
{
	//kdDebug() << "Scope Size: " << s.size() << endl;
        /* Scope t(32);
	if (s.size() != 32)
	{
		Analyzer::interpolate(s, t);
	}
	else
	{
		t = s;
	}*/
        uint offset = 0;
        static float peak;
        float mfactor = 0.0;
        static int drawcount;

        if (s.size() == 64)
        {
            offset=8;
        }

	glRotatef(0.25f, 0.0f, 1.0f, 0.5f); //Rotate the scene
	drawFloor();
	drawcount++;
        if (drawcount > 25)
        {
            drawcount = 0;
            peak = 0.0;
        }

        for ( uint i = 0; i < 32; i++ )
	{
            if (s[i] > peak)
            {
                peak = s[i];
            }
        }

        mfactor = 20 / peak;
        for ( uint i = 0; i < 32; i++ )
	{

                //kdDebug() << "Scope item " << i << " value: " << s[i] << endl;

                // Calculate new horizontal position (x) depending on number of samples
		x = -16.0f + i;

		// Calculating new vertical position (y) depending on the data passed by amarok
		y = float(s[i+offset] * mfactor); //This make it kinda dynamically resize depending on the data

		//Some basic bounds checking
		if (y > 30)
			y = 30;
		else if (y < 0)
			y = 0;

		if((y - m_oldy[i]) < -0.6f) // Going Down Too Much
		{
			y = m_oldy[i] - 0.7f;
		}
		if (y < 0.0f)
		{
			y = 0.0f;
		}

		m_oldy[i] = y; //Save value as last value

		//Peak Code
		if (m_oldy[i] > m_peaks[i].level)
		{
			m_peaks[i].level = m_oldy[i];
			m_peaks[i].delay = 30;
		}

		if (m_peaks[i].delay > 0)
		{
			m_peaks[i].delay--;
		}

		if (m_peaks[i].level > 1.0f)
		{
			if (m_peaks[i].delay <= 0)
			{
				m_peaks[i].level-=0.4f;
			}
		}
		// Draw the bar
		drawBar(x,y);
		drawPeak(x, m_peaks[i].level);

  	}

	updateGL();
}

void GLAnalyzer::initializeGL()
{
	// Clear frame (next fading will be preferred to clearing)
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// Set clear color to black
        glClear( GL_COLOR_BUFFER_BIT );

	// Set the shading model
	glShadeModel(GL_SMOOTH);

	// Set the polygon mode to fill
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	// Enable depth testing for hidden line removal
	glEnable(GL_DEPTH_TEST);

	// Set blend parameters for 'composting alpha'
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
}

void GLAnalyzer::resizeGL( int w, int h )
{
    glViewport( 0, 0, (GLint)w, (GLint)h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho(-16.0f, 16.0f, -10.0f, 10.0f, -50.0f, 100.0f);
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

void GLAnalyzer::paintGL()
{
        glMatrixMode( GL_MODELVIEW );
#if 0
        glClear( GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT );
#else
        glEnable( GL_DEPTH_TEST );
        glEnable( GL_BLEND );
        glPushMatrix();
        glLoadIdentity();
        glBegin( GL_TRIANGLE_STRIP );
          glColor4f( 0.0f, 0.0f, 0.1f, 0.08f );
          glVertex2f( 20.0f, 10.0f );
          glVertex2f( -20.0f, 10.0f );
          glVertex2f( 20.0f, -10.0f );
          glVertex2f( -20.0f, -10.0f );
        glEnd();
        glPopMatrix();
        glDisable( GL_BLEND );
        glEnable( GL_DEPTH_TEST );
        glClear( GL_DEPTH_BUFFER_BIT );
#endif
	//swapBuffers();

        glFlush();

}

void GLAnalyzer::drawBar(float xPos, float height)
{
        glPushMatrix();

        //Sets color to blue
	//Set the colour depending on the height of the bar
	glColor3f((height/40) + 0.5f, (height/40) + 0.625f, 1.0f);
        glTranslatef(xPos, -10.0f, 0.0f);

        glScalef(1.0f, height, 3.0f);
        drawCube();

	//Set colour to full blue
	//glColor3f(0.0f, 0.0f, 1.0f);
	//drawFrame();
        glPopMatrix();
}

void GLAnalyzer::drawFloor()
{
        glPushMatrix();

        //Sets color to amarok blue
	glColor3f( 0.5f, 0.625f, 1.0f);
        glTranslatef(-16.0f,-11.0f, -4.0f);

        glScalef(32.0f, 1.0f, 10.0f);
        drawCube();

	//Set colour to full blue
	glColor3f(0.0f, 0.0f, 1.0f);
        drawFrame();
        glPopMatrix();
}

void GLAnalyzer::drawPeak(float xPos, float ypos)
{
        glPushMatrix();

        //Set the colour to red
	glColor3f(1.0f, 0.0f, 0.0f);
        glTranslatef(xPos, ypos - 10.0f, 0.0f);

        glScalef(1.0f, 1.0f, 3.0f);
        drawCube();

        glPopMatrix();
}

void GLAnalyzer::drawCube()
{
        glPushMatrix();
        glBegin(GL_POLYGON);

	//This is the top face
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);

	//This is the front face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the right face
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);

	//This is the left face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the bottom face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the back face
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);

        glEnd();
        glPopMatrix();
}
void GLAnalyzer::drawFrame()
{
        glPushMatrix();
        glBegin(GL_LINES);


	//This is the top face
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);

	//This is the front face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the right face
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);

	//This is the left face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the bottom face
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	//This is the back face
	glVertex3f(0.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 0.0f, 1.0f);
	glVertex3f(1.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 1.0f);

        glEnd();
        glPopMatrix();
}

#endif