summaryrefslogtreecommitdiffstats
path: root/amarok/src/engine/akode/akode-engine.cpp
blob: 268405899e49ec60c9978597b00ed44dc33cb7ed (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
/***************************************************************************
 *   Copyright (C) 2005 Max Howell <max.howell@methylblue.com>             *
 *                                                                         *
 *   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 <akode-engine.h>
#include <akode/decoder.h>
#include <akode/player.h>
#include <klocale.h>
#include <tqapplication.h>

AMAROK_EXPORT_PLUGIN( AkodeEngine )


namespace Amarok
{
    class Manager : public aKode::Player::Manager
    {
        AkodeEngine *m_engine;

        /// Called for all stateChanges
        virtual void stateChangeEvent( aKode::Player::State )
        {
            TQApplication::postEvent( m_engine, new TQCustomEvent( 3000 ) );
        }

        /// Called when a decoder reaches end of file
        virtual void eofEvent()
        {
            TQApplication::postEvent( m_engine, new TQCustomEvent( 3001 ) );
        }

        /// Called when a decoder encounters a fatal error
        virtual void errorEvent()
        {
            TQApplication::postEvent( m_engine, new TQCustomEvent( 3002 ) );
        }

    public:
        Manager( AkodeEngine *engine ) : m_engine( engine ) {}
    };
}


AkodeEngine::AkodeEngine()
        : m_player( 0 )
{}

AkodeEngine::~AkodeEngine()
{
    if( m_player )
        m_player->close();
}

bool
AkodeEngine::init()
{
//    startTimer( 20 );

    m_player = new aKode::Player();
    m_player->setManager( new Amarok::Manager( this ) );
    m_player->setMonitor( &m_scope );

    return m_player->open( "auto" );
}

bool
AkodeEngine::load( const KURL &url, bool isStream )
{
    Engine::Base::load( url, isStream );

    return m_player->load( url.path().local8Bit().data() );
}

bool
AkodeEngine::play( uint /*offset*/ )
{
    //FIXME this seemed to crash Amarok
    //m_player->decoder()->seek( offset );
    m_player->play();

    return true;
}

void
AkodeEngine::unpause()
{
    m_player->play();
}

bool
AkodeEngine::canDecode( const KURL &url ) const
{
    const TQString ext = url.path().right( 4 ).lower();

    return ext == ".mp3" || ext == ".ogg" || ext == ".wav" || ext ==".mpc" || ext == "flac";
}

uint
AkodeEngine::position() const
{
    if( !m_player->decoder() )
        return 0;

    const int pos = m_player->decoder()->position();

    return pos >= 0 ? pos : 0;
}

void
AkodeEngine::stop()
{
    m_player->stop();
    m_player->unload();
}

void
AkodeEngine::pause()
{
    switch( m_player->state() ) {
        case aKode::Player::Playing: m_player->pause(); break;
        case aKode::Player::Paused: m_player->play(); break;
        default: ;
    }
}

void
AkodeEngine::setVolumeSW( uint v )
{
    m_player->setVolume( (float)v / 100.0 );
}

void
AkodeEngine::seek( uint ms )
{
    m_player->decoder()->seek( ms );
}

Engine::State
AkodeEngine::state() const
{
    switch( m_player->state() )
    {
        case aKode::Player::Open:
        case aKode::Player::Closed:  return Engine::Empty;
        default:
        case aKode::Player::Loaded:  return Engine::Idle;
        case aKode::Player::Playing: return Engine::Playing;
        case aKode::Player::Paused:  return Engine::Paused;
    }
}

bool
AkodeEngine::event( TQEvent *e )
{
    switch( e->type() )
    {
    /*
    case TQEvent::Timer:
        if( m_player->decoder() && m_player->decoder()->eof() ) {
            m_player->stop();
            emit trackEnded();
        }
        break;
    */
    case 3000:
        emit stateChanged( state() );
        break;

    case 3001:
        m_player->stop();
        emit trackEnded();
        break;

    case 3002:
        m_player->stop();
        emit trackEnded();
        emit infoMessage( i18n("Unable to decode <i>%1</i>").arg( m_url.prettyURL()) );
        break;

    default:
        return false;
    }

    return true;
}

const Engine::Scope& AkodeEngine::scope()
{
    return m_scope.scope();
}