summaryrefslogtreecommitdiffstats
path: root/src/app/analyzer.cpp
blob: 593bd16fcfa66379bc4dcd4a33835fea58c5349c (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
// (c) 2004 Max Howell (max.howell@methylblue.com)
// See COPYING file for licensing information

#include "analyzer.h"
#include "codeine.h"
#include "debug.h"
#include <math.h>       //interpolate()
#include <ntqevent.h>     //event()
#include "xineEngine.h"

#include "fht.cpp"

template<class W>
Analyzer::Base<W>::Base( TQWidget *parent, uint timeout )
      : W( parent, "Analyzer" )
      , m_timeout( timeout )
{}

template<class W> bool
Analyzer::Base<W>::event( TQEvent *e )
{
   switch( e->type() ) {
   case TQEvent::Hide:
      m_timer.stop();
      break;

   case TQEvent::Show:
      m_timer.start( timeout() );
      break;

   default:
      ;
   }

   return TQWidget::event( e );
}


Analyzer::Base2D::Base2D( TQWidget *parent, uint timeout )
      : Base<TQWidget>( parent, timeout )
{
   setWFlags( TQt::WNoAutoErase ); //no flicker
   connect( &m_timer, SIGNAL(timeout()), SLOT(draw()) );
}

void
Analyzer::Base2D::draw()
{
   switch( Codeine::engine()->state() ) {
   case Engine::Playing:
   {
      const Engine::Scope &thescope = Codeine::engine()->scope();
      static Analyzer::Scope scope( Analyzer::SCOPE_SIZE );

      for( int x = 0; x < Analyzer::SCOPE_SIZE; ++x )
         scope[x] = double(thescope[x]) / (1<<15);

      transform( scope );
      analyze( scope );

      scope.resize( Analyzer::SCOPE_SIZE );

      bitBlt( this, 0, 0, canvas() );
      break;
   }
   case Engine::Paused:
      break;

   default:
      erase();
   }
}

void
Analyzer::Base2D::resizeEvent( TQResizeEvent* )
{
   m_canvas.resize( size() );
   m_canvas.fill( colorGroup().background() );
}



// Author:    Max Howell <max.howell@methylblue.com>, (C) 2003
// Copyright: See COPYING file that comes with this distribution

#include <ntqpainter.h>

Analyzer::Block::Block( TQWidget *parent )
      : Analyzer::Base2D( parent, 20 )
{
   setMinimumWidth( 64 ); //-1 is padding, no drawing takes place there
   setMaximumWidth( 128 );

   //TODO yes, do height for width
}

void
Analyzer::Block::transform( Analyzer::Scope &scope ) //pure virtual
{
   static FHT fht( Analyzer::SCOPE_SIZE_EXP );

   for( uint x = 0; x < scope.size(); ++x )
      scope[x] *= 2;

   float *front = static_cast<float*>( &scope.front() );

   fht.spectrum( front );
   fht.scale( front, 1.0 / 40 );
}

#include <math.h>
void
Analyzer::Block::analyze( const Analyzer::Scope &s )
{
   canvas()->fill( colorGroup().foreground().light() );

   TQPainter p( canvas() );
   p.setPen( colorGroup().background() );

   const double F = double(height()) / (log10( 256 ) * 1.1 /*<- max. amplitude*/);

   for( uint x = 0; x < s.size(); ++x )
      //we draw the blank bit
      p.drawLine( x, 0, x, int(height() - log10( s[x] * 256.0 ) * F) );
}

int
Analyzer::Block::heightForWidth( int w ) const
{
   return w / 2;
}