playermanager.cpp 16.7 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>
Laurent Montel's avatar
Laurent Montel committed
21 22
//Added by qt3to4:
#include <QPixmap>
23

24 25
#include <math.h>

26
#include "artsplayer.h"
27
#include "akodeplayer.h"
28
#include "gstreamerplayer.h"
29
#include "playermanager.h"
30
#include "playlistinterface.h"
31
#include "slideraction.h"
32
#include "statuslabel.h"
33
#include "actioncollection.h"
34
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
35
#include "coverinfo.h"
36
#include "tag.h"
37

38 39
#include "config.h"

40
using namespace ActionCollection;
41 42 43

PlayerManager *PlayerManager::m_instance = 0;

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

46 47 48 49
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

50
enum SoundSystem { ArtsBackend = 0, GStreamerBackend = 1, AkodeBackend = 2 };
51 52 53 54 55 56

static Player *createPlayer(int system = ArtsBackend)
{

    Player *p = 0;
    switch(system) {
57
#ifdef HAVE_AKODE
58 59 60
    case AkodeBackend:
        p = new aKodePlayer;
        break;
61
#endif
62
#if HAVE_ARTS
63 64 65
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
66
#endif
67
#if HAVE_GSTREAMER
68 69 70
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
71
#endif 
72
    default:
73
#if HAVE_ARTS
74
        p = new ArtsPlayer;
İsmail Dönmez's avatar
 
İsmail Dönmez committed
75
#elif HAVE_GSTREAMER
76
        p = new GStreamerPlayer;
77
#else
İsmail Dönmez's avatar
 
İsmail Dönmez committed
78
        p = new aKodePlayer;
79
#endif
80 81
        break;
    }
82 83 84
    return p;
}

85 86 87 88 89
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
90
    Player(),
91
    m_sliderAction(0),
92
    m_playlistInterface(0),
93
    m_statusLabel(0),
94 95
    m_player(0),
    m_timer(0),
96 97
    m_noSeek(false),
    m_muted(false)
98
{
Michael Pyne's avatar
Michael Pyne committed
99 100 101 102 103
// 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();
104 105 106 107
}

PlayerManager::~PlayerManager()
{
108
    delete m_player;
109 110 111 112 113 114 115 116 117 118 119 120 121
}

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

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

122 123
bool PlayerManager::playing() const
{
124
    if(!player())
125 126
        return false;

127
    return player()->playing();
128 129 130 131
}

bool PlayerManager::paused() const
{
132
    if(!player())
133 134
        return false;

135
    return player()->paused();
136 137 138 139
}

float PlayerManager::volume() const
{
140
    if(!player())
141 142
        return 0;

143
    return player()->volume();
144 145
}

146 147 148
int PlayerManager::status() const
{
    if(!player())
149
        return StatusStopped;
150 151

    if(player()->paused())
152
        return StatusPaused;
153 154

    if(player()->playing())
155
        return StatusPlaying;
156

157 158 159
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
160
int PlayerManager::totalTime() const
161
{
162
    if(!player())
163 164
        return 0;

165
    return player()->totalTime();
166 167
}

Scott Wheeler's avatar
Scott Wheeler committed
168
int PlayerManager::currentTime() const
169
{
170
    if(!player())
171 172
        return 0;

173
    return player()->currentTime();
174 175 176 177
}

int PlayerManager::position() const
{
178
    if(!player())
179 180
        return 0;

181
    return player()->position();
182 183
}

184 185 186 187 188 189 190
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
191
    if(!playing() && !paused())
192 193 194 195 196
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
197 198 199 200 201 202 203 204 205 206 207 208 209
QPixmap PlayerManager::trackCover(const QString &size) const
{
    if(!playing() && !paused())
        return QPixmap();

    if(size.lower() == "small")
        return m_file.coverInfo()->pixmap(CoverInfo::Thumbnail);
    if(size.lower() == "large")
        return m_file.coverInfo()->pixmap(CoverInfo::FullSize);

    return QPixmap();
}

210 211 212 213 214
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

215 216 217 218 219
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

220 221 222 223 224
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
225 226
}

227
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
228 229 230 231
{
    m_playlistInterface = interface;
}

232 233 234 235 236
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

237 238 239 240 241
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
242 243 244 245 246 247

#if HAVE_ARTS
    l.append(i18n("aRts"));
#endif
#if HAVE_GSTREAMER
    l.append(i18n("GStreamer"));
248
#endif
249 250
#ifdef HAVE_AKODE
    l.append(i18n("aKode"));
251
#endif
252

253
    action->setItems(l);
254 255 256
    return action;
}

257 258 259 260
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

261
void PlayerManager::play(const FileHandle &file)
262
{
263
    if(!player() || !m_playlistInterface)
264 265
        return;

266 267
    if(file.isNull()) {
        if(player()->paused())
268
            player()->play();
269
        else if(player()->playing()) {
270 271
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
272
            player()->seekPosition(0);
273
        }
274
        else {
275
            m_playlistInterface->playNext();
276
            m_file = m_playlistInterface->currentFile();
277

278
            if(!m_file.isNull())
279
                player()->play(m_file);
280
        }
281
    }
282
    else {
283
        m_file = file;
284
        player()->play(file);
285
    }
286

287
    // Make sure that the player() actually starts before doing anything.
288

289
    if(!player()->playing()) {
290 291 292
        stop();
        return;
    }
293

294 295 296
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
297 298
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
299
    action("back")->setEnabled(true);
300

301 302
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
303 304

    m_timer->start(m_pollInterval);
305 306

    emit signalPlay();
307 308
}

309 310 311 312 313 314 315 316 317
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
318 319 320 321 322
void PlayerManager::play()
{
    play(FileHandle::null());
}

323 324
void PlayerManager::pause()
{
325
    if(!player())
326 327
        return;

328 329 330 331 332 333
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
334
    action("pause")->setEnabled(false);
335 336

    player()->pause();
337 338

    emit signalPause();
339 340 341 342
}

void PlayerManager::stop()
{
343
    if(!player() || !m_playlistInterface)
344 345
        return;

346 347
    m_timer->stop();

348 349 350 351
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
352
    action("forwardAlbum")->setEnabled(false);
353

354 355 356 357
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
358 359

    player()->stop();
360
    m_playlistInterface->stop();
361

362 363
    m_file = FileHandle::null();

364
    emit signalStop();
365 366
}

Laurent Montel's avatar
Laurent Montel committed
367
void PlayerManager::setVolume(float volume)
368
{
369
    if(!player())
370 371
        return;

372
    player()->setVolume(volume);
373 374
}

Scott Wheeler's avatar
Scott Wheeler committed
375
void PlayerManager::seek(int seekTime)
376
{
377
    if(!player())
378
        return;
379

380
    player()->seek(seekTime);
381 382
}

383
void PlayerManager::seekPosition(int position)
384
{
385 386 387 388
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
389
        return;
390

391 392
    slotUpdateTime(position);
    player()->seekPosition(position);
393 394 395

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
396 397 398 399
}

void PlayerManager::seekForward()
{
400
    seekPosition(kMin(SliderAction::maxPosition, position() + 10));
401 402 403 404
}

void PlayerManager::seekBack()
{
405
    seekPosition(kMax(SliderAction::minPosition, position() - 10));
406 407
}

408 409
void PlayerManager::playPause()
{
410
    playing() ? action("pause")->activate() : action("play")->activate();
411 412
}

413 414
void PlayerManager::forward()
{
415 416
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
417

418 419 420 421 422 423 424 425
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
426 427
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
428

429 430 431 432 433 434
    if(!file.isNull())
        play(file);
    else
        stop();
}

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

440
    int volume = m_sliderAction->volumeSlider()->volume() +
441
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
442 443

    slotSetVolume(volume);
444
    m_sliderAction->volumeSlider()->setVolume(volume);
445 446 447 448
}

void PlayerManager::volumeDown()
{
449
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
450
        return;
451 452

    int volume = m_sliderAction->volumeSlider()->value() -
453
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
454 455

    slotSetVolume(volume);
456
    m_sliderAction->volumeSlider()->setVolume(volume);
457 458 459 460
}

void PlayerManager::mute()
{
461
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
462 463 464 465 466 467
        return;

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

468 469 470
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
471

472
void PlayerManager::slotPollPlay()
473
{
474
    if(!player() || !m_playlistInterface)
475
        return;
476

477
    m_noSeek = true;
478

479
    if(!player()->playing()) {
480
        m_timer->stop();
481

482 483
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
484
        if(!nextFile.isNull())
485
            play(nextFile);
486 487
        else
            stop();
488 489
    }
    else if(!m_sliderAction->dragging()) {
490 491
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
492

493 494 495 496
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
497
    }
498

499 500 501 502 503 504 505 506
    // 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())
507 508
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
509
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
510 511 512 513 514

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

516 517 518 519
    // 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.
520

521 522 523
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
524 525 526
    {
        m_timer->changeInterval(50);
    }
527

528
    m_noSeek = false;
529 530
}

531
void PlayerManager::slotSetOutput(const QString &system)
532 533
{
    stop();
534 535 536 537 538 539
    setOutput(system);
    setup();
}

void PlayerManager::setOutput(const QString &system)
{
540
    delete m_player;
541
    if(system == i18n("aRts"))
542
        m_player = createPlayer(ArtsBackend);
543
    else if(system == i18n("GStreamer"))
544
        m_player = createPlayer(GStreamerBackend);
545
    else if(system == i18n("aKode"))
546
        m_player = createPlayer(AkodeBackend);
547 548 549 550
}

void PlayerManager::slotSetVolume(int volume)
{
551 552
    float scaledVolume;

553
    if(m_sliderAction->volumeSlider())
554 555 556 557 558 559 560 561 562 563 564 565
        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
566 567 568 569 570 571 572
}

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

573
    float positionFraction = float(position) / SliderAction::maxPosition;
574
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
575
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
576 577 578 579

    m_statusLabel->setItemCurrentTime(seekTime);
}

580 581 582 583
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

584 585 586 587 588 589 590 591
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

592 593 594 595
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

596 597 598
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
599
       !action("forwardAlbum") ||
600 601
       !action("forward") ||
       !action("trackPositionAction"))
602 603 604 605 606
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

607 608
    // initialize action states

609 610 611 612
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
613
    action("forwardAlbum")->setEnabled(false);
614

615 616
    // setup sliders

617
    m_sliderAction = action<SliderAction>("trackPositionAction");
618

619 620 621 622
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
623
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
624 625
            this, SLOT(slotSetVolume(int)));

626
    // Call this method manually to avoid warnings.
627

628
    KAction *outputAction = actions()->action("outputSelect");
629 630

    if(outputAction) {
631
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
632
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
633 634
    }
    else
635
        m_player = createPlayer();
636

637 638 639 640 641 642 643 644 645
    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
646 647 648

    m_player->setVolume(volume);

649 650
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
651 652
}

Michael Pyne's avatar
Michael Pyne committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
QString PlayerManager::randomPlayMode() const
{
    if(action<KToggleAction>("randomPlay")->isChecked())
        return "Random";
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        return "AlbumRandom";
    return "NoRandom";
}

void PlayerManager::setRandomPlayMode(const QString &randomMode)
{
    if(randomMode.lower() == "random")
        action<KToggleAction>("randomPlay")->setChecked(true);
    if(randomMode.lower() == "albumrandom")
        action<KToggleAction>("albumRandomPlay")->setChecked(true);
    if(randomMode.lower() == "norandom")
        action<KToggleAction>("disableRandomPlay")->setChecked(true);
}

672
#include "playermanager.moc"
673 674

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