summaryrefslogtreecommitdiffstats
path: root/juk/artsplayer.cpp
blob: fc8269dd877a8739f9c3d0f84eae95e99d2e4057 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/***************************************************************************
    begin                : Sun Feb 17 2002
    copyright            : (C) 2002 - 2004 by Scott Wheeler
    email                : [email protected]

    copyright            : (C) 2003 by Matthias Kretz
    email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "artsplayer.h"

#if HAVE_ARTS

#include <kdebug.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>

#include <tqdir.h>

#include <kartsserver.h>
#include <kartsdispatcher.h>
#include <kplayobject.h>
#include <kplayobjectfactory.h>

#include <cstdlib>
#include <sys/wait.h>

#include <tdemessagebox.h>
#include <kaudiomanagerplay.h>
#include <tdelocale.h>

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

ArtsPlayer::ArtsPlayer() : Player(),
                           m_dispatcher(0),
                           m_server(0),
                           m_factory(0),
                           m_playobject(0),
                           m_amanPlay(0),
                           m_volumeControl(Arts::StereoVolumeControl::null()),
                           m_currentVolume(1.0)
{
    setupPlayer();
}

ArtsPlayer::~ArtsPlayer()
{
    delete m_playobject;
    delete m_factory;
    delete m_amanPlay;
    delete m_server;
    delete m_dispatcher;
}

void ArtsPlayer::play(const FileHandle &file)
{
    // kdDebug(65432) << k_funcinfo << endl;
    // Make sure that the server still exists, if it doesn't a new one should
    // be started automatically and the factory and amanPlay are created again.

    if(!file.isNull())
        m_currentURL.setPath(file.absFilePath());

    if(m_server->server().isNull()) {
        KMessageBox::error(0, i18n("Cannot find the aRts soundserver."));
        return;
    }

    if(!m_playobject || !file.isNull()) {
        stop();

        delete m_playobject;
        m_playobject = m_factory->createPlayObject(m_currentURL, false);

        if(m_playobject->object().isNull())
            connect(m_playobject, TQT_SIGNAL(playObjectCreated()), TQT_SLOT(playObjectCreated()));
        else
            playObjectCreated();
    }

    m_playobject->play();
}

void ArtsPlayer::pause()
{
    // kdDebug(65432) << k_funcinfo << endl;
    if(m_playobject)
        m_playobject->pause();
}

void ArtsPlayer::stop()
{
    // kdDebug(65432) << k_funcinfo << endl;
    if(m_playobject) {
        m_playobject->halt();
        delete m_playobject;
        m_playobject = 0;
    }
    if(!m_volumeControl.isNull()) {
        m_volumeControl.stop();
        m_volumeControl = Arts::StereoVolumeControl::null();
    }
}

void ArtsPlayer::setVolume(float volume)
{
    // kdDebug( 65432 ) << k_funcinfo << endl;

    m_currentVolume = volume;

    if(serverRunning() && m_playobject && !m_playobject->isNull()) {
        if(m_volumeControl.isNull())
            setupVolumeControl();
        if(!m_volumeControl.isNull()) {
            m_volumeControl.scaleFactor(volume);
            // kdDebug( 65432 ) << "set volume to " << volume << endl;
        }
    }
}

float ArtsPlayer::volume() const
{
    return m_currentVolume;
}

/////////////////////////////////////////////////////////////////////////////////
// player status functions
/////////////////////////////////////////////////////////////////////////////////

bool ArtsPlayer::playing() const
{
    if(serverRunning() && m_playobject && m_playobject->state() == Arts::posPlaying)
        return true;
    else
        return false;
}

bool ArtsPlayer::paused() const
{
    if(serverRunning() && m_playobject && m_playobject->state() == Arts::posPaused)
        return true;
    else
        return false;
}

int ArtsPlayer::totalTime() const
{
    if(serverRunning() && m_playobject)
        return m_playobject->overallTime().seconds;
    else
        return -1;
}

int ArtsPlayer::currentTime() const
{
    if(serverRunning() && m_playobject &&
       (m_playobject->state() == Arts::posPlaying ||
        m_playobject->state() == Arts::posPaused))
    {
        return m_playobject->currentTime().seconds;
    }
    else
        return -1;
}

int ArtsPlayer::position() const
{
    if(serverRunning() && m_playobject && m_playobject->state() == Arts::posPlaying) {
        long total = m_playobject->overallTime().seconds * 1000 + m_playobject->overallTime().ms;
        long current = m_playobject->currentTime().seconds * 1000 + m_playobject->currentTime().ms;

        // add .5 to make rounding happen properly

        return int(double(current) * 1000 / total + .5);
    }
    else
        return -1;
}

/////////////////////////////////////////////////////////////////////////////////
// player seek functions
/////////////////////////////////////////////////////////////////////////////////

void ArtsPlayer::seek(int seekTime)
{
    if(serverRunning() && m_playobject) {
        Arts::poTime poSeekTime;
        poSeekTime.custom = 0;
        poSeekTime.ms = 0;
        poSeekTime.seconds = seekTime;
        m_playobject->object().seek(poSeekTime);
    }
}

void ArtsPlayer::seekPosition(int position)
{
    if(serverRunning() && m_playobject) {
        Arts::poTime poSeekTime;
        long total = m_playobject->overallTime().seconds;
        poSeekTime.custom = 0;
        poSeekTime.ms = 0;
        poSeekTime.seconds = long(double(total) * position / 1000 + .5);
        m_playobject->object().seek(poSeekTime);
    }
}

/////////////////////////////////////////////////////////////////////////////////
// private
/////////////////////////////////////////////////////////////////////////////////

void ArtsPlayer::setupArtsObjects()
{
    // kdDebug( 65432 ) << k_funcinfo << endl;
    delete m_factory;
    delete m_amanPlay;
    m_volumeControl = Arts::StereoVolumeControl::null();
    m_factory = new KDE::PlayObjectFactory(m_server);
    m_amanPlay = new KAudioManagerPlay(m_server);

    if(m_amanPlay->isNull() || !m_factory) {
        KMessageBox::error(0, i18n("Connecting/starting aRts soundserver failed. "
                                   "Make sure that artsd is configured properly."));
        exit(1);
    }

    m_amanPlay->setTitle(i18n("JuK"));
    m_amanPlay->setAutoRestoreID("JuKAmanPlay");

    m_factory->setAudioManagerPlay(m_amanPlay);
}

void ArtsPlayer::playObjectCreated()
{
    // kdDebug(65432) << k_funcinfo << endl;
    setVolume(m_currentVolume);
}

void ArtsPlayer::setupPlayer()
{
    m_dispatcher = new KArtsDispatcher;
    m_server = new KArtsServer;
    setupArtsObjects();
    connect(m_server, TQT_SIGNAL(restartedServer()), TQT_SLOT(setupArtsObjects()));
}

void ArtsPlayer::setupVolumeControl()
{
    // kdDebug( 65432 ) << k_funcinfo << endl;
    m_volumeControl = Arts::DynamicCast(m_server->server().createObject("Arts::StereoVolumeControl"));
    if(!m_volumeControl.isNull() && !m_playobject->isNull() && !m_playobject->object().isNull()) {
        Arts::Synth_AMAN_PLAY ap = m_amanPlay->amanPlay();
        Arts::PlayObject po = m_playobject->object();
        ap.stop();
        Arts::disconnect(po, "left" , ap, "left" );
        Arts::disconnect(po, "right", ap, "right");

        m_volumeControl.start();
        ap.start();

        Arts::connect(po, "left" , m_volumeControl, "inleft" );
        Arts::connect(po, "right", m_volumeControl, "inright");
        Arts::connect(m_volumeControl, "outleft" , ap, "left" );
        Arts::connect(m_volumeControl, "outright", ap, "right");
        // kdDebug( 65432 ) << "connected volume control" << endl;
    }
    else {
        m_volumeControl = Arts::StereoVolumeControl::null();
        kdDebug(65432) << "Could not initialize volume control!" << endl;
    }
}

bool ArtsPlayer::serverRunning() const
{
    if(m_server)
        return !(m_server->server().isNull());
    else
        return false;
}

#include "artsplayer.moc"

#endif 

// vim: sw=4 ts=8 et