summaryrefslogtreecommitdiffstats
path: root/amarok/src/analyzers/analyzerbase.cpp
blob: b4ff9ac0ec662c391ab701be477e6552774fadf2 (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
/***************************************************************************
                          viswidget.cpp  -  description
                             -------------------
    begin                : Die Jan 7 2003
    copyright            : (C) 2003 by Max Howell
    email                : markey@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "analyzerbase.h"
#include <cmath>        //interpolate()
#include "enginecontroller.h"
#include <tqevent.h>     //event()


// INSTRUCTIONS Base2D
// 1. do anything that depends on height() in init(), Base2D will call it before you are shown
// 2. otherwise you can use the constructor to initialise things
// 3. reimplement analyze(), and paint to canvas(), Base2D will update the widget when you return control to it
// 4. if you want to manipulate the scope, reimplement transform()
// 5. for convenience <vector> <tqpixmap.h> <qwdiget.h> are pre-included
// TODO make an INSTRUCTIONS file
//can't mod scope in analyze you have to use transform


//TODO for 2D use setErasePixmap TQt function insetead of m_background

// make the linker happy only for gcc < 4.0
#if !( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 0 ) )
template class Analyzer::Base<TQWidget>;
#endif


template<class W>
Analyzer::Base<W>::Base( TQWidget *parent, uint timeout, uint scopeSize )
        : W( parent )
        , m_timeout( timeout )
        , m_fht( new FHT(scopeSize) )
{}

template<class W> bool
Analyzer::Base<W>::event( TQEvent *e )
{
    switch( e->type() ) {
/*    case TQEvent::Paint:
        if( !canvas()->isNull() )
            bitBlt( this, 0, 0, canvas() );
        return true; //no propagate event*/
    case TQEvent::Hide:
        m_timer.stop();
        break;

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

    default:
        break;
    }

    return TQWidget::event( e );
}

template<class W> void
Analyzer::Base<W>::transform( Scope &scope ) //virtual
{
    //this is a standard transformation that should give
    //an FFT scope that has bands for pretty analyzers

    //NOTE resizing here is redundant as FHT routines only calculate FHT::size() values
    //scope.resize( m_fht->size() );

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

    float* f = new float[ m_fht->size() ];
    m_fht->copy( &f[0], front );
    m_fht->logSpectrum( front, &f[0] );
    m_fht->scale( front, 1.0 / 20 );

    scope.resize( m_fht->size() / 2 ); //second half of values are rubbish
    delete [] f;
}

template<class W> void
Analyzer::Base<W>::drawFrame()
{
    EngineBase *engine = EngineController::engine();

    switch( engine->state() )
    {
    case Engine::Playing:
    {
        const Engine::Scope &thescope = engine->scope();
        static Analyzer::Scope scope( 512 );
        int i = 0;

        // convert to mono here - our built in analyzers need mono, but we the engines provide interleaved pcm
        for( uint x = 0; (int)x < m_fht->size(); ++x ) 
        {
           scope[x] = double(thescope[i] + thescope[i+1]) / (2*(1<<15));
           i += 2;
        }

        transform( scope );
        analyze( scope );

        scope.resize( m_fht->size() );

        break;
    }
    case Engine::Paused:
        paused();
        break;

    default:
        demo();
    }
}

template<class W> int
Analyzer::Base<W>::resizeExponent( int exp )
{
    if ( exp < 3 )
        exp = 3;
    else if ( exp > 9 )
        exp = 9;

    if ( exp != m_fht->sizeExp() ) {
        delete m_fht;
        m_fht = new FHT( exp );
    }
    return exp;
}

template<class W> int
Analyzer::Base<W>::resizeForBands( int bands )
{
    int exp;
    if ( bands <= 8 )
        exp = 4;
    else if ( bands <= 16 )
        exp = 5;
    else if ( bands <= 32 )
        exp = 6;
    else if ( bands <= 64 )
        exp = 7;
    else if ( bands <= 128 )
        exp = 8;
    else
        exp = 9;

    resizeExponent( exp );
    return m_fht->size() / 2;
}

template<class W> void
Analyzer::Base<W>::paused() //virtual
{}

template<class W> void
Analyzer::Base<W>::demo() //virtual
{
    static int t = 201; //FIXME make static to namespace perhaps

    if( t > 999 ) t = 1; //0 = wasted calculations
    if( t < 201 )
    {
        Scope s( 32 );

        const double dt = double(t) / 200;
        for( uint i = 0; i < s.size(); ++i )
            s[i] = dt * (sin( M_PI + (i * M_PI) / s.size() ) + 1.0);

        analyze( s );
    }
    else analyze( Scope( 32, 0 ) );

    ++t;
}



Analyzer::Base2D::Base2D( TQWidget *parent, uint timeout, uint scopeSize )
   : Base<TQWidget>( parent, timeout, scopeSize )
{
    setWFlags( TQt::WNoAutoErase ); //no flicker

    connect( &m_timer, TQT_SIGNAL( timeout() ), TQT_SLOT( draw() ) );
}

void
Analyzer::Base2D::polish()
{
    //TODO is there much point in this anymore?

    //we use polish for initialzing (instead of ctor)
    //because we need to know the widget's final size
    TQWidget::polish();

    init(); //virtual
}

void
Analyzer::Base2D::resizeEvent( TQResizeEvent *e )
{
    m_background.resize( size() );
    m_canvas.resize( size() );
    m_background.fill( backgroundColor() );
    eraseCanvas(); //this is necessary

    TQWidget::resizeEvent( e );
}

void
Analyzer::Base2D::paletteChange( const TQPalette& )
{
    m_background.fill( backgroundColor() );
    eraseCanvas();
}



#ifdef HAVE_TQGLWIDGET
Analyzer::Base3D::Base3D( TQWidget *parent, uint timeout, uint scopeSize )
        : Base<TQGLWidget>( parent, timeout, scopeSize )
{
    connect( &m_timer, TQT_SIGNAL( timeout() ), TQT_SLOT( draw() ) );
}
#endif


void
Analyzer::interpolate( const Scope &inVec, Scope &outVec ) //static
{
    double pos = 0.0;
    const double step = (double)inVec.size() / outVec.size();

    for ( uint i = 0; i < outVec.size(); ++i, pos += step )
    {
        const double error = pos - std::floor( pos );
        const unsigned long offset = (unsigned long)pos;

        unsigned long indexLeft = offset + 0;

        if ( indexLeft >= inVec.size() )
            indexLeft = inVec.size() - 1;

        unsigned long indexRight = offset + 1;

        if ( indexRight >= inVec.size() )
            indexRight = inVec.size() - 1;

        outVec[i] = inVec[indexLeft ] * ( 1.0 - error ) +
                    inVec[indexRight] * error;
    }
}

void
Analyzer::initSin( Scope &v, const uint size ) //static
{
    double step = ( M_PI * 2 ) / size;
    double radian = 0;

    for ( uint i = 0; i < size; i++ )
    {
        v.push_back( sin( radian ) );
        radian += step;
    }
}

#include "analyzerbase.moc"