| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2014 Matej Repinc <[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 Pulic 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 "ASCIIAnalyzer.h" |
| 18 |
|
| 19 |
#include "PaletteHandler.h" |
| 20 |
|
| 21 |
#include <cmath> |
| 22 |
|
| 23 |
#include <QPainter> |
| 24 |
#include <QResizeEvent> |
| 25 |
|
| 26 |
|
| 27 |
ASCIIAnalyzer* ASCIIAnalyzer::instance = 0; |
| 28 |
|
| 29 |
ASCIIAnalyzer::ASCIIAnalyzer( QWidget *parent ) |
| 30 |
: Analyzer::Base( parent ) |
| 31 |
, m_columns( 0 ) //int |
| 32 |
, m_rows( 0 ) //int |
| 33 |
{ |
| 34 |
instance = this; |
| 35 |
setObjectName( "ASCII" ); |
| 36 |
|
| 37 |
setMaximumWidth( MAX_COLUMNS * ( BLOCK_WIDTH + 1 ) - 1 ); |
| 38 |
setFps( 30 ); |
| 39 |
} |
| 40 |
|
| 41 |
void |
| 42 |
ASCIIAnalyzer::initializeGL() |
| 43 |
{ |
| 44 |
// Disable depth test (all is drawn on a 2d plane) |
| 45 |
glDisable( GL_DEPTH_TEST ); |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
ASCIIAnalyzer::resizeGL( int w, int h ) |
| 50 |
{ |
| 51 |
glViewport( 0, 0, (GLint)w, (GLint)h ); |
| 52 |
|
| 53 |
// Set up a 2D projection matrix |
| 54 |
glMatrixMode( GL_PROJECTION ); |
| 55 |
glLoadIdentity(); |
| 56 |
glOrtho( 0.0, (GLdouble)w, (GLdouble)h, 0.0, 0.0, 1.0 ); |
| 57 |
|
| 58 |
const int oldRows = m_rows; |
| 59 |
|
| 60 |
// Rounded up so that the last column/line is covered if partially visible |
| 61 |
m_columns = std::min( std::floor( (double)width() / ( BLOCK_WIDTH + 1 ) ), (double)MAX_COLUMNS ); |
| 62 |
m_rows = std::ceil( (double)height() / ( BLOCK_HEIGHT + 1 ) ); |
| 63 |
|
| 64 |
m_scope.resize( m_columns ); |
| 65 |
m_store.resize( m_columns ); |
| 66 |
|
| 67 |
if( m_rows != oldRows ) |
| 68 |
{ |
| 69 |
m_barPixmap = QPixmap( BLOCK_WIDTH, m_rows * ( BLOCK_HEIGHT + 1 ) ); |
| 70 |
|
| 71 |
m_yscale.resize( m_rows + 1 ); |
| 72 |
|
| 73 |
const float PRE = 1, PRO = 1; //PRE and PRO allow us to restrict the range somewhat |
| 74 |
|
| 75 |
for( int z = 0; z < m_rows; ++z ) |
| 76 |
m_yscale[z] = 1 - ( log10( PRE + z ) / log10( PRE + m_rows + PRO ) ); |
| 77 |
|
| 78 |
m_yscale[m_rows] = 0; |
| 79 |
|
| 80 |
determineStep(); |
| 81 |
paletteChange( palette() ); |
| 82 |
} |
| 83 |
|
| 84 |
drawBackground(); |
| 85 |
analyze( m_scope ); |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
ASCIIAnalyzer::determineStep() |
| 90 |
{ |
| 91 |
// Based on Mark Kretschmann's work in BlockAnalyzer |
| 92 |
// falltime is dependent on rowcount due to our digital resolution (ie we have boxes/blocks of pixels) |
| 93 |
// I calculated the value 50 based on some trial and error |
| 94 |
|
| 95 |
const double fallTime = 50 * m_rows; |
| 96 |
m_step = double( m_rows * 80 ) / fallTime; //80 = ~milliseconds between signals with audio data |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
ASCIIAnalyzer::transform( QVector<float> &s ) //pure virtual |
| 101 |
{ |
| 102 |
// Based on Mark Kretschmann's work in BlockAnalyzer |
| 103 |
for( int x = 0; x < s.size(); ++x ) |
| 104 |
s[x] *= 2; |
| 105 |
|
| 106 |
float *front = static_cast<float*>( &s.front() ); |
| 107 |
|
| 108 |
m_fht->spectrum( front ); |
| 109 |
m_fht->scale( front, 1.0 / 20 ); |
| 110 |
|
| 111 |
//the second half is pretty dull, so only show it if the user has a large analyzer |
| 112 |
//by setting to m_scope.size() if large we prevent interpolation of large analyzers, this is good! |
| 113 |
s.resize( m_scope.size() <= MAX_COLUMNS / 2 ? MAX_COLUMNS / 2 : m_scope.size() ); |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
ASCIIAnalyzer::analyze( const QVector<float> &s ) |
| 118 |
{ |
| 119 |
interpolate( s, m_scope ); |
| 120 |
} |
| 121 |
|
| 122 |
void |
| 123 |
ASCIIAnalyzer::paintGL() |
| 124 |
{ |
| 125 |
// Based largely on Mark Kretschmann's work in BlockAnalyzer, |
| 126 |
// however a bit simplified since we don't need fancy transitions |
| 127 |
// and textures. |
| 128 |
// y = 2 3 2 1 0 2 |
| 129 |
// . . . . # . |
| 130 |
// . . . # # . |
| 131 |
// # . # # # # |
| 132 |
// # # # # # # |
| 133 |
// |
| 134 |
// visual aid for how this analyzer works. |
| 135 |
// y represents the number of blanks |
| 136 |
// y starts from the top and increases in units of blocks |
| 137 |
|
| 138 |
// m_yscale looks similar to: { 0.7, 0.5, 0.25, 0.15, 0.1, 0 } |
| 139 |
// if it contains 6 elements there are 5 rows in the analyzer |
| 140 |
|
| 141 |
glMatrixMode( GL_MODELVIEW ); |
| 142 |
glLoadIdentity(); |
| 143 |
|
| 144 |
// Paint the background |
| 145 |
drawTexture( m_background.data(), 0, 0, 0, 0 ); |
| 146 |
|
| 147 |
for( uint y, x = 0; x < (uint)m_scope.size(); ++x ) |
| 148 |
{ |
| 149 |
// determine y |
| 150 |
for( y = 0; m_scope[x] < m_yscale[y]; ++y ) |
| 151 |
; |
| 152 |
|
| 153 |
// the higher the y, the lower the bar physically is. |
| 154 |
if( ( float )y > m_store[x] ) |
| 155 |
y = uint( m_store[x] += m_step ); |
| 156 |
else |
| 157 |
m_store[x] = y; |
| 158 |
|
| 159 |
// Don't draw top two #'s |
| 160 |
y += 2; |
| 161 |
|
| 162 |
int xpos = x * ( BLOCK_WIDTH + 1 ); |
| 163 |
// REMEMBER: y is a number from 0 to m_rows, 0 means all blocks are glowing, m_rows means none are |
| 164 |
drawTexture( m_barTexture.data(), xpos, y * ( BLOCK_HEIGHT + 1 ), 0, y * ( BLOCK_HEIGHT + 1 ) ); |
| 165 |
|
| 166 |
// Draw second top bar to "ease" transition |
| 167 |
int top_ypos = int( m_store[x] ) * ( BLOCK_HEIGHT + 1 ); |
| 168 |
drawTexture( m_topSecondBarTexture.data(), xpos, top_ypos + BLOCK_HEIGHT + 1, 0, 0 ); |
| 169 |
|
| 170 |
// Draw top bar |
| 171 |
drawTexture( m_topBarTexture.data(), xpos, top_ypos, 0, 0 ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
ASCIIAnalyzer::drawTexture( Texture* texture, int x, int y, int sx, int sy ) |
| 177 |
{ |
| 178 |
const GLfloat xf = x; |
| 179 |
const GLfloat yf = y; |
| 180 |
const GLfloat wf = texture->size.width() - sx; |
| 181 |
const GLfloat hf = texture->size.height() - sy; |
| 182 |
const GLfloat sxf = (GLfloat)sx / texture->size.width(); |
| 183 |
const GLfloat syf = (GLfloat)sy / texture->size.height(); |
| 184 |
|
| 185 |
glEnable( GL_TEXTURE_2D ); |
| 186 |
glBindTexture( GL_TEXTURE_2D, texture->id ); |
| 187 |
|
| 188 |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 189 |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 190 |
|
| 191 |
// Draw a textured quad |
| 192 |
glBegin(GL_QUADS); |
| 193 |
glTexCoord2f( sxf, syf ); glVertex2f( xf, yf ); |
| 194 |
glTexCoord2f( sxf, 1.0 ); glVertex2f( xf, yf + hf ); |
| 195 |
glTexCoord2f( 1.0, 1.0 ); glVertex2f( xf + wf, yf + hf ); |
| 196 |
glTexCoord2f( 1.0, syf ); glVertex2f( xf + wf, yf ); |
| 197 |
glEnd(); |
| 198 |
|
| 199 |
glDisable( GL_TEXTURE_2D ); |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
ASCIIAnalyzer::paletteChange( const QPalette& ) //virtual |
| 204 |
{ |
| 205 |
const QColor bg = palette().background().color(); |
| 206 |
const QFont font ("Cantarell", 10); |
| 207 |
|
| 208 |
QPixmap topBar( BLOCK_WIDTH, BLOCK_HEIGHT ); |
| 209 |
topBar.fill( bg ); |
| 210 |
QPainter tbp ( &topBar ); |
| 211 |
tbp.setPen(Qt::red); |
| 212 |
tbp.setBackground(palette().background().color()); |
| 213 |
tbp.setFont(font); |
| 214 |
tbp.drawText(topBar.rect(), Qt::AlignCenter, "."); |
| 215 |
m_topBarTexture = QSharedPointer<Texture>( new Texture( topBar ) ); |
| 216 |
|
| 217 |
QPixmap topSecondBar( BLOCK_WIDTH, BLOCK_HEIGHT ); |
| 218 |
// red on top, black on bottom |
| 219 |
QLinearGradient gradient (BLOCK_WIDTH/2, 0, BLOCK_WIDTH/2, BLOCK_HEIGHT); |
| 220 |
gradient.setColorAt(0.3, Qt::red); |
| 221 |
gradient.setColorAt(1.0, Qt::darkGreen); |
| 222 |
topSecondBar.fill( bg ); |
| 223 |
QPainter tsbp ( &topSecondBar ); |
| 224 |
tsbp.setPen( QPen(gradient, BLOCK_WIDTH) ); |
| 225 |
tsbp.setBrush( gradient ); |
| 226 |
tsbp.setFont(font); |
| 227 |
tsbp.drawText(topSecondBar.rect(), Qt::AlignCenter, "o"); |
| 228 |
m_topSecondBarTexture = QSharedPointer<Texture>( new Texture( topSecondBar ) ); |
| 229 |
|
| 230 |
m_barPixmap.fill( bg ); |
| 231 |
QPainter p( &m_barPixmap ); |
| 232 |
p.setPen(Qt::darkGreen); |
| 233 |
p.setFont(font); |
| 234 |
|
| 235 |
for( int y = 0; y < m_rows; ++y ) { |
| 236 |
QRect rect (0, y * ( BLOCK_HEIGHT + 1 ), BLOCK_WIDTH, BLOCK_HEIGHT); |
| 237 |
p.drawText(rect, Qt::AlignCenter, "#"); |
| 238 |
} |
| 239 |
|
| 240 |
m_barTexture = QSharedPointer<Texture>( new Texture( m_barPixmap ) ); |
| 241 |
drawBackground(); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
ASCIIAnalyzer::drawBackground() |
| 246 |
{ |
| 247 |
const QColor bg = palette().background().color(); |
| 248 |
QPixmap background( size() ); |
| 249 |
background.fill( bg ); |
| 250 |
|
| 251 |
m_background = QSharedPointer<Texture>( new Texture( background ) ); |
| 252 |
} |