playermanager.cpp 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/***************************************************************************
    begin                : Sat Feb 14 2004
    copyright            : (C) 2004 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <kdebug.h>
17
#include <klocale.h>
18 19

#include <qslider.h>
20
#include <qtimer.h>
21

22 23
#include <math.h>

24 25
#include "artsplayer.h"
#include "gstreamerplayer.h"
26
#include "playermanager.h"
27
#include "playlistinterface.h"
28
#include "slideraction.h"
29
#include "statuslabel.h"
30
#include "actioncollection.h"
31
#include "collectionlist.h"
32
#include "tag.h"
33

34 35
#include "config.h"

36
using namespace ActionCollection;
37 38 39

PlayerManager *PlayerManager::m_instance = 0;

Stephan Kulow's avatar
Stephan Kulow committed
40
enum PlayerManagerStatus { StatusStopped = -1, StatusPaused = 1, StatusPlaying = 2 };
41

42 43 44 45 46 47 48 49 50 51
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

enum SoundSystem { ArtsBackend = 0, GStreamerBackend = 1 };

static Player *createPlayer(int system = ArtsBackend)
{

    Player *p = 0;
52

53
#if HAVE_ARTS && HAVE_GSTREAMER
54 55 56 57 58 59 60
    switch(system) {
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
61 62 63
    default:
        p = new ArtsPlayer;
        break;
64 65
    }
#else
66
    Q_UNUSED(system)
67
#if HAVE_ARTS
68 69 70
    p = new ArtsPlayer;
#endif

71
#if HAVE_GSTREAMER
72 73 74 75 76 77
    p = new GStreamerPlayer;
#else
#warning No Player Backend Available
#endif
#endif

78 79 80
    return p;
}

81 82 83 84 85
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
86
    Player(),
87
    m_sliderAction(0),
88
    m_playlistInterface(0),
89
    m_statusLabel(0),
90 91
    m_player(0),
    m_timer(0),
92 93
    m_noSeek(false),
    m_muted(false)
94
{
Michael Pyne's avatar
Michael Pyne committed
95 96 97 98 99
// This class is the first thing constructed during program startup, and
// therefore has no access to the widgets needed by the setup() method.
// Since the setup() method will be called indirectly by the player() method
// later, just disable it here. -- mpyne
//    setup();
100 101 102 103
}

PlayerManager::~PlayerManager()
{
104
    delete m_player;
105 106 107 108 109 110 111 112 113 114 115 116 117
}

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

PlayerManager *PlayerManager::instance() // static
{
    if(!m_instance)
        m_instance = new PlayerManager;
    return m_instance;
}

118 119
bool PlayerManager::playing() const
{
120
    if(!player())
121 122
        return false;

123
    return player()->playing();
124 125 126 127
}

bool PlayerManager::paused() const
{
128
    if(!player())
129 130
        return false;

131
    return player()->paused();
132 133 134 135
}

float PlayerManager::volume() const
{
136
    if(!player())
137 138
        return 0;

139
    return player()->volume();
140 141
}

142 143 144
int PlayerManager::status() const
{
    if(!player())
145
        return StatusStopped;
146 147

    if(player()->paused())
148
        return StatusPaused;
149 150

    if(player()->playing())
151
        return StatusPlaying;
152 153 154 155
    
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
156
int PlayerManager::totalTime() const
157
{
158
    if(!player())
159 160
        return 0;

161
    return player()->totalTime();
162 163
}

Scott Wheeler's avatar
Scott Wheeler committed
164
int PlayerManager::currentTime() const
165
{
166
    if(!player())
167 168
        return 0;

169
    return player()->currentTime();
170 171 172 173
}

int PlayerManager::position() const
{
174
    if(!player())
175 176
        return 0;

177
    return player()->position();
178 179
}

180 181 182 183 184 185 186 187 188 189 190 191 192
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
    if(!playing())
        return QString::null;

    return m_file.property(property);
}

193 194 195 196 197
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

198 199 200 201 202 203 204 205
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

    return m_file.tag()->artist() + " - " + m_file.tag()->title();
}

206
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
207 208 209 210
{
    m_playlistInterface = interface;
}

211 212 213 214 215
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

216 217 218 219 220
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
221 222 223

#if defined(HAVE_ARTS) && defined(HAVE_GSTREAMER)
    l << i18n("aRts") << i18n("GStreamer");
224 225
    action->setItems(l);
#else
226
    Q_UNUSED(parent)
227 228 229 230
#endif
    return action;
}

231 232 233 234
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

235
void PlayerManager::play(const FileHandle &file)
236
{
237
    if(!player() || !m_playlistInterface)
238 239
        return;

240 241
    if(file.isNull()) {
        if(player()->paused())
242
            player()->play();
243
        else if(player()->playing()) {
244 245
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
246
            player()->seekPosition(0);
247
        }
248
        else {
249
	    m_playlistInterface->playNext();
250
            m_file = m_playlistInterface->currentFile();
251

252
            if(!m_file.isNull())
253
                player()->play(m_file);
254
        }
255
    }
256
    else {
257
        m_file = file;
258
        player()->play(file);
259
    }
260

261
    // Make sure that the player() actually starts before doing anything.
262

263
    if(!player()->playing()) {
264 265 266
        stop();
        return;
    }
267

268 269 270
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
271 272
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
273
    action("back")->setEnabled(true);
274

275 276
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
277 278

    m_timer->start(m_pollInterval);
279 280

    emit signalPlay();
281 282
}

283 284 285 286 287 288 289 290 291
void PlayerManager::play(const QString &file)
{
    CollectionListItem *item = CollectionList::instance()->lookup(file);
    if(item) {
        Playlist::setPlaying(item);
        play(item->file());
    }
}

Scott Wheeler's avatar
Scott Wheeler committed
292 293 294 295 296
void PlayerManager::play()
{
    play(FileHandle::null());
}

297 298
void PlayerManager::pause()
{
299
    if(!player())
300 301
        return;

302 303 304 305 306 307
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
308
    action("pause")->setEnabled(false);
309 310

    player()->pause();
311 312

    emit signalPause();
313 314 315 316
}

void PlayerManager::stop()
{
317
    if(!player() || !m_playlistInterface)
318 319
        return;

320 321
    m_timer->stop();

322 323 324 325
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
326
    action("forwardAlbum")->setEnabled(false);
327

328 329 330 331
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
332 333

    player()->stop();
334
    m_playlistInterface->stop();
335

336 337
    m_file = FileHandle::null();

338
    emit signalStop();
339 340
}

Laurent Montel's avatar
Laurent Montel committed
341
void PlayerManager::setVolume(float volume)
342
{
343
    if(!player())
344 345
        return;

346
    player()->setVolume(volume);
347 348
}

Scott Wheeler's avatar
Scott Wheeler committed
349
void PlayerManager::seek(int seekTime)
350
{
351
    if(!player())
352
        return;
353

354
    player()->seek(seekTime);
355 356
}

357
void PlayerManager::seekPosition(int position)
358
{
359 360 361 362
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
363
        return;
364

365 366
    slotUpdateTime(position);
    player()->seekPosition(position);
367 368 369

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
370 371 372 373
}

void PlayerManager::seekForward()
{
374
    seekPosition(kMin(SliderAction::maxPosition, position() + 10));
375 376 377 378
}

void PlayerManager::seekBack()
{
379
    seekPosition(kMax(SliderAction::minPosition, position() - 10));
380 381
}

382 383
void PlayerManager::playPause()
{
384
    playing() ? action("pause")->activate() : action("play")->activate();
385 386
}

387 388
void PlayerManager::forward()
{
389 390
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
391

392 393 394 395 396 397 398 399
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
400 401
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
402

403 404 405 406 407 408
    if(!file.isNull())
        play(file);
    else
        stop();
}

409 410
void PlayerManager::volumeUp()
{
411
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
412
        return;
413

414
    int volume = m_sliderAction->volumeSlider()->volume() +
415
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
416 417

    slotSetVolume(volume);
418
    m_sliderAction->volumeSlider()->setVolume(volume);
419 420 421 422
}

void PlayerManager::volumeDown()
{
423
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
424
        return;
425 426

    int volume = m_sliderAction->volumeSlider()->value() -
427
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
428 429

    slotSetVolume(volume);
430
    m_sliderAction->volumeSlider()->setVolume(volume);
431 432 433 434
}

void PlayerManager::mute()
{
435
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
436 437 438 439 440 441
        return;

    slotSetVolume(m_muted ? m_sliderAction->volumeSlider()->value() : 0);
    m_muted = !m_muted;
}

442 443 444
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
445

446
void PlayerManager::slotPollPlay()
447
{
448
    if(!player() || !m_playlistInterface)
449
        return;
450

451
    m_noSeek = true;
452

453
    if(!player()->playing()) {
454
        m_timer->stop();
455

456 457
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
458
        if(!nextFile.isNull())
459
            play(nextFile);
460 461
        else
            stop();
462 463
    }
    else if(!m_sliderAction->dragging()) {
464 465
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
466

467 468 469 470
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
471
    }
472

473 474 475 476 477 478 479 480
    // This call is done because when the user adds the slider to the toolbar
    // while playback is occuring the volume slider generally defaults to 0,
    // and doesn't get updated to the correct volume.  It might be better to
    // have the SliderAction class fill in the correct volume, but I'm trying
    // to avoid having it depend on PlayerManager since it may not be
    // constructed in time during startup. -mpyne

    if(!m_sliderAction->volumeDragging() && m_sliderAction->volumeSlider())
481 482 483 484 485 486 487 488
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
        float v = sqrtf(sqrtf(volume())); // Cancel out exponential scaling

        m_sliderAction->volumeSlider()->blockSignals(true);
        m_sliderAction->volumeSlider()->setVolume((int)((v) * maxV));
        m_sliderAction->volumeSlider()->blockSignals(false);
    }
489

490 491 492 493
    // Ok, this is weird stuff, but it works pretty well.  Ordinarily we don't
    // need to check up on our playing time very often, but in the span of the
    // last interval, we want to check a lot -- to figure out that we've hit the
    // end of the song as soon as possible.
494

495 496 497
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
498 499 500
    {
        m_timer->changeInterval(50);
    }
501

502
    m_noSeek = false;
503 504
}

505 506 507 508
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
509
    m_player = createPlayer(system);
510
    setup();
511 512 513 514
}

void PlayerManager::slotSetVolume(int volume)
{
515 516
    float scaledVolume;

517
    if(m_sliderAction->volumeSlider())
518 519 520 521 522 523 524 525 526 527 528 529
        scaledVolume = float(volume) / m_sliderAction->volumeSlider()->maxValue();
    else {
        scaledVolume = float(volume) / 100.0; // Hopefully this is accurate
        scaledVolume = kMin(1.0f, scaledVolume);
    }

    // Perform exponential scaling to counteract the fact that humans perceive
    // volume changes logarithmically.

    scaledVolume *= scaledVolume;
    scaledVolume *= scaledVolume;
    setVolume(scaledVolume); // scaledVolume ^ 4
530 531 532 533 534 535 536
}

void PlayerManager::slotUpdateTime(int position)
{
    if(!m_statusLabel)
        return;

537
    float positionFraction = float(position) / SliderAction::maxPosition;
538
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
539
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
540 541 542 543

    m_statusLabel->setItemCurrentTime(seekTime);
}

544 545 546 547
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

548 549 550 551 552 553 554 555
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

556 557 558 559
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

560 561 562
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
563
       !action("forwardAlbum") ||
564 565
       !action("forward") ||
       !action("trackPositionAction"))
566 567 568 569 570
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

571 572
    // initialize action states

573 574 575 576
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
577
    action("forwardAlbum")->setEnabled(false);
578

579 580
    // setup sliders

581
    m_sliderAction = action<SliderAction>("trackPositionAction");
582

583 584 585 586
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
587
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
588 589
            this, SLOT(slotSetVolume(int)));

590 591 592
    // Call this method manually to avoid warnings.
    
    KAction *outputAction = actions()->action("outputSelect");
593 594 595

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
596
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
597
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
598 599
    }
    else
600
        m_player = createPlayer();
601

602 603 604 605 606 607 608 609 610
    float volume;
    
    if(m_sliderAction->volumeSlider()) {
        volume = 
            float(m_sliderAction->volumeSlider()->volume()) /
            float(m_sliderAction->volumeSlider()->maxValue());
    }
    else
        volume = 1; // Assume user wants full volume
611 612 613

    m_player->setVolume(volume);

614 615
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
616 617 618
}

#include "playermanager.moc"
619 620

// vim: set et ts=4 sw=4: