| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2004 Enrico Ros <[email protected]> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#include "DiscoAnalyzer.h" |
| 18 |
|
| 19 |
#include <QImage> |
| 20 |
#include <QStandardPaths> |
| 21 |
|
| 22 |
#include <cmath> |
| 23 |
#include <cstdlib> |
| 24 |
#include <sys/time.h> |
| 25 |
|
| 26 |
|
| 27 |
DiscoAnalyzer::DiscoAnalyzer( QWidget *parent ): |
| 28 |
Analyzer::Base( parent ) |
| 29 |
{ |
| 30 |
setObjectName( "Disco" ); |
| 31 |
|
| 32 |
m_dotTexture = bindTexture( QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/dot.png" ) ) ); |
| 33 |
m_w1Texture = bindTexture( QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/wirl1.png" ) ) ); |
| 34 |
m_w2Texture = bindTexture( QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/wirl2.png" ) ) ); |
| 35 |
|
| 36 |
m_show.paused = true; |
| 37 |
m_show.pauseTimer = 0.0; |
| 38 |
m_show.rotDegrees = 0.0; |
| 39 |
m_frame.rotDegrees = 0.0; |
| 40 |
} |
| 41 |
|
| 42 |
DiscoAnalyzer::~DiscoAnalyzer() |
| 43 |
{ |
| 44 |
deleteTexture( m_dotTexture ); |
| 45 |
deleteTexture( m_w1Texture ); |
| 46 |
deleteTexture( m_w2Texture ); |
| 47 |
} |
| 48 |
|
| 49 |
void DiscoAnalyzer::demo() |
| 50 |
{ |
| 51 |
static int t = 0; |
| 52 |
static bool forward = true; |
| 53 |
|
| 54 |
QVector<float> s( 200 ); |
| 55 |
const double dt = double( t ) / 200; |
| 56 |
|
| 57 |
for( int i = 0; i < s.size(); ++i ) |
| 58 |
s[i] = dt * ( sin( M_PI + ( i * M_PI ) / s.size() ) + 1.0 ); |
| 59 |
|
| 60 |
analyze( s ); |
| 61 |
|
| 62 |
if( t == 0 ) |
| 63 |
forward = true; |
| 64 |
if( t == 200 ) |
| 65 |
forward = false; |
| 66 |
|
| 67 |
t = forward ? t + 2 : t - 2; |
| 68 |
} |
| 69 |
|
| 70 |
void DiscoAnalyzer::initializeGL() |
| 71 |
{ |
| 72 |
// Set a smooth shade model |
| 73 |
glShadeModel( GL_SMOOTH ); |
| 74 |
|
| 75 |
// Disable depth test (all is drawn on a 2d plane) |
| 76 |
glDisable( GL_DEPTH_TEST ); |
| 77 |
|
| 78 |
// Set blend parameters for 'composting alpha' |
| 79 |
glBlendFunc( GL_SRC_ALPHA, GL_ONE ); //superpose |
| 80 |
//glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); //fade |
| 81 |
glEnable( GL_BLEND ); |
| 82 |
|
| 83 |
// Clear frame with a black background |
| 84 |
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); |
| 85 |
glClear( GL_COLOR_BUFFER_BIT ); |
| 86 |
} |
| 87 |
|
| 88 |
void DiscoAnalyzer::resizeGL( int w, int h ) |
| 89 |
{ |
| 90 |
// Setup screen. We're going to manually do the perspective projection |
| 91 |
glViewport( 0, 0, ( GLint )w, ( GLint )h ); |
| 92 |
glMatrixMode( GL_PROJECTION ); |
| 93 |
glLoadIdentity(); |
| 94 |
glOrtho( -10.0f, 10.0f, -10.0f, 10.0f, -5.0f, 5.0f ); |
| 95 |
|
| 96 |
// Get the aspect ratio of the screen to draw 'cicular' particles |
| 97 |
const float ratio = ( float )w / ( float )h, |
| 98 |
eqPixH = 60, |
| 99 |
eqPixW = 80; |
| 100 |
if( ratio >= ( 4.0 / 3.0 ) ) |
| 101 |
{ |
| 102 |
m_unitX = 10.0 / ( eqPixH * ratio ); |
| 103 |
m_unitY = 10.0 / eqPixH; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
m_unitX = 10.0 / eqPixW; |
| 108 |
m_unitY = 10.0 / ( eqPixW / ratio ); |
| 109 |
} |
| 110 |
|
| 111 |
// Get current timestamp. |
| 112 |
timeval tv; |
| 113 |
gettimeofday( &tv, NULL ); |
| 114 |
m_show.timeStamp = ( double )tv.tv_sec + ( double )tv.tv_usec / 1000000.0; |
| 115 |
} |
| 116 |
|
| 117 |
void DiscoAnalyzer::analyze( const QVector<float> &s ) |
| 118 |
{ |
| 119 |
bool haveNoData = s.empty(); |
| 120 |
|
| 121 |
// if we're going into pause mode, clear timers. |
| 122 |
if( !m_show.paused && haveNoData ) |
| 123 |
m_show.pauseTimer = 0.0; |
| 124 |
|
| 125 |
// if we have got data, interpolate it (asking myself why I'm doing it here..) |
| 126 |
if( !( m_show.paused = haveNoData ) ) |
| 127 |
{ |
| 128 |
int bands = s.size(), |
| 129 |
lowbands = bands / 4, |
| 130 |
hibands = bands / 3, |
| 131 |
midbands = bands - lowbands - hibands; Q_UNUSED( midbands ); |
| 132 |
float currentEnergy = 0, |
| 133 |
currentMeanBand = 0, |
| 134 |
maxValue = 0; |
| 135 |
for( int i = 0; i < bands; i++ ) |
| 136 |
{ |
| 137 |
float value = s[i]; |
| 138 |
currentEnergy += value; |
| 139 |
currentMeanBand += ( float )i * value; |
| 140 |
if( value > maxValue ) |
| 141 |
maxValue = value; |
| 142 |
} |
| 143 |
m_frame.silence = currentEnergy < 0.001; |
| 144 |
if( !m_frame.silence ) |
| 145 |
{ |
| 146 |
m_frame.meanBand = 100.0 * currentMeanBand / ( currentEnergy * bands ); |
| 147 |
currentEnergy = 100.0 * currentEnergy / ( float )bands; |
| 148 |
m_frame.dEnergy = currentEnergy - m_frame.energy; |
| 149 |
m_frame.energy = currentEnergy; |
| 150 |
// printf( "%d [%f :: %f ]\t%f \n", bands, frame.energy, frame.meanBand, maxValue ); |
| 151 |
} |
| 152 |
else |
| 153 |
m_frame.energy = 0.0; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void DiscoAnalyzer::paintGL() |
| 158 |
{ |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
void DiscoAnalyzer::drawDot( float x, float y, float size ) |
| 163 |
{ |
| 164 |
float sizeX = size * m_unitX, |
| 165 |
sizeY = size * m_unitY, |
| 166 |
pLeft = x - sizeX, |
| 167 |
pTop = y + sizeY, |
| 168 |
pRight = x + sizeX, |
| 169 |
pBottom = y - sizeY; |
| 170 |
glTexCoord2f( 0, 0 ); // Bottom Left |
| 171 |
glVertex2f( pLeft, pBottom ); |
| 172 |
glTexCoord2f( 0, 1 ); // Top Left |
| 173 |
glVertex2f( pLeft, pTop ); |
| 174 |
glTexCoord2f( 1, 1 ); // Top Right |
| 175 |
glVertex2f( pRight, pTop ); |
| 176 |
glTexCoord2f( 1, 0 ); // Bottom Right |
| 177 |
glVertex2f( pRight, pBottom ); |
| 178 |
} |
| 179 |
|
| 180 |
void DiscoAnalyzer::drawFullDot( float r, float g, float b, float a ) |
| 181 |
{ |
| 182 |
glBindTexture( GL_TEXTURE_2D, m_dotTexture ); |
| 183 |
glEnable( GL_TEXTURE_2D ); |
| 184 |
glColor4f( r, g, b, a ); |
| 185 |
glBegin( GL_TRIANGLE_STRIP ); |
| 186 |
glTexCoord2f( 1.0, 1.0 ); |
| 187 |
glVertex2f( 10.0f, 10.0f ); |
| 188 |
glTexCoord2f( 0.0, 1.0 ); |
| 189 |
glVertex2f( -10.0f, 10.0f ); |
| 190 |
glTexCoord2f( 1.0, 0.0 ); |
| 191 |
glVertex2f( 10.0f, -10.0f ); |
| 192 |
glTexCoord2f( 0.0 , 0.0 ); |
| 193 |
glVertex2f( -10.0f, -10.0f ); |
| 194 |
glEnd(); |
| 195 |
glDisable( GL_TEXTURE_2D ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
void DiscoAnalyzer::setTextureMatrix( float rot, float scale ) |
| 200 |
{ |
| 201 |
glMatrixMode( GL_TEXTURE ); |
| 202 |
glLoadIdentity(); |
| 203 |
if( rot != 0.0 || scale != 0.0 ) |
| 204 |
{ |
| 205 |
glTranslatef( 0.5f, 0.5f, 0.0f ); |
| 206 |
glRotatef( rot, 0.0f, 0.0f, 1.0f ); |
| 207 |
glScalef( scale, scale, 1.0f ); |
| 208 |
glTranslatef( -0.5f, -0.5f, 0.0f ); |
| 209 |
} |
| 210 |
glMatrixMode( GL_MODELVIEW ); |
| 211 |
} |
| 212 |
|