playermanager.cpp 16.6 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
#include "artsplayer.h"
25
#include "akodeplayer.h"
26
#include "gstreamerplayer.h"
27
#include "playermanager.h"
28
#include "playlistinterface.h"
29
#include "slideraction.h"
30
#include "statuslabel.h"
31
#include "actioncollection.h"
32
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
33
#include "coverinfo.h"
34
#include "tag.h"
35

36 37
#include "config.h"

38
using namespace ActionCollection;
39 40 41

PlayerManager *PlayerManager::m_instance = 0;

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

44 45 46 47
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

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

static Player *createPlayer(int system = ArtsBackend)
{

    Player *p = 0;
    switch(system) {
55 56 57 58
    case AkodeBackend:
        p = new aKodePlayer;
        break;
#if HAVE_ARTS
59 60 61
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
62 63
#endif
#if HAVE_GSTREAMER
64 65 66
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
67
#endif
68
    default:
69
#if HAVE_ARTS
70
        p = new ArtsPlayer;
İsmail Dönmez's avatar
 
İsmail Dönmez committed
71
#elif HAVE_GSTREAMER
72
        p = new GStreamerPlayer;
73
#else
İsmail Dönmez's avatar
 
İsmail Dönmez committed
74
        p = new aKodePlayer;
75
#endif
76 77
        break;
    }
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
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
187
    if(!playing() && !paused())
188 189 190 191 192
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
193 194 195 196 197 198 199 200 201 202 203 204 205
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();
}

206 207 208 209 210
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

211 212 213 214 215
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

216 217 218 219 220
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
221 222
}

223
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
224 225 226 227
{
    m_playlistInterface = interface;
}

228 229 230 231 232
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

233 234 235 236 237
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
238
    l <<
239
#if HAVE_ARTS
240
        i18n("aRts") <<
241 242
#endif
#if HAVE_GSTREAMER
243
        i18n("GStreamer") <<
244
#endif
245
        i18n("aKode");
246
    action->setItems(l);
247 248 249
    return action;
}

250 251 252 253
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

254
void PlayerManager::play(const FileHandle &file)
255
{
256
    if(!player() || !m_playlistInterface)
257 258
        return;

259 260
    if(file.isNull()) {
        if(player()->paused())
261
            player()->play();
262
        else if(player()->playing()) {
263 264
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
265
            player()->seekPosition(0);
266
        }
267
        else {
268
            m_playlistInterface->playNext();
269
            m_file = m_playlistInterface->currentFile();
270

271
            if(!m_file.isNull())
272
                player()->play(m_file);
273
        }
274
    }
275
    else {
276
        m_file = file;
277
        player()->play(file);
278
    }
279

280
    // Make sure that the player() actually starts before doing anything.
281

282
    if(!player()->playing()) {
283 284 285
        stop();
        return;
    }
286

287 288 289
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
290 291
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
292
    action("back")->setEnabled(true);
293

294 295
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
296 297

    m_timer->start(m_pollInterval);
298 299

    emit signalPlay();
300 301
}

302 303 304 305 306 307 308 309 310
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
311 312 313 314 315
void PlayerManager::play()
{
    play(FileHandle::null());
}

316 317
void PlayerManager::pause()
{
318
    if(!player())
319 320
        return;

321 322 323 324 325 326
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
327
    action("pause")->setEnabled(false);
328 329

    player()->pause();
330 331

    emit signalPause();
332 333 334 335
}

void PlayerManager::stop()
{
336
    if(!player() || !m_playlistInterface)
337 338
        return;

339 340
    m_timer->stop();

341 342 343 344
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
345
    action("forwardAlbum")->setEnabled(false);
346

347 348 349 350
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
351 352

    player()->stop();
353
    m_playlistInterface->stop();
354

355 356
    m_file = FileHandle::null();

357
    emit signalStop();
358 359
}

Laurent Montel's avatar
Laurent Montel committed
360
void PlayerManager::setVolume(float volume)
361
{
362
    if(!player())
363 364
        return;

365
    player()->setVolume(volume);
366 367
}

Scott Wheeler's avatar
Scott Wheeler committed
368
void PlayerManager::seek(int seekTime)
369
{
370
    if(!player())
371
        return;
372

373
    player()->seek(seekTime);
374 375
}

376
void PlayerManager::seekPosition(int position)
377
{
378 379 380 381
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
382
        return;
383

384 385
    slotUpdateTime(position);
    player()->seekPosition(position);
386 387 388

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
389 390 391 392
}

void PlayerManager::seekForward()
{
393
    seekPosition(kMin(SliderAction::maxPosition, position() + 10));
394 395 396 397
}

void PlayerManager::seekBack()
{
398
    seekPosition(kMax(SliderAction::minPosition, position() - 10));
399 400
}

401 402
void PlayerManager::playPause()
{
403
    playing() ? action("pause")->activate() : action("play")->activate();
404 405
}

406 407
void PlayerManager::forward()
{
408 409
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
410

411 412 413 414 415 416 417 418
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
419 420
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
421

422 423 424 425 426 427
    if(!file.isNull())
        play(file);
    else
        stop();
}

428 429
void PlayerManager::volumeUp()
{
430
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
431
        return;
432

433
    int volume = m_sliderAction->volumeSlider()->volume() +
434
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
435 436

    slotSetVolume(volume);
437
    m_sliderAction->volumeSlider()->setVolume(volume);
438 439 440 441
}

void PlayerManager::volumeDown()
{
442
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
443
        return;
444 445

    int volume = m_sliderAction->volumeSlider()->value() -
446
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
447 448

    slotSetVolume(volume);
449
    m_sliderAction->volumeSlider()->setVolume(volume);
450 451 452 453
}

void PlayerManager::mute()
{
454
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
455 456 457 458 459 460
        return;

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

461 462 463
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
464

465
void PlayerManager::slotPollPlay()
466
{
467
    if(!player() || !m_playlistInterface)
468
        return;
469

470
    m_noSeek = true;
471

472
    if(!player()->playing()) {
473
        m_timer->stop();
474

475 476
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
477
        if(!nextFile.isNull())
478
            play(nextFile);
479 480
        else
            stop();
481 482
    }
    else if(!m_sliderAction->dragging()) {
483 484
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
485

486 487 488 489
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
490
    }
491

492 493 494 495 496 497 498 499
    // 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())
500 501
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
502
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
503 504 505 506 507

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

509 510 511 512
    // 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.
513

514 515 516
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
517 518 519
    {
        m_timer->changeInterval(50);
    }
520

521
    m_noSeek = false;
522 523
}

524
void PlayerManager::slotSetOutput(const QString &system)
525 526
{
    stop();
527 528 529 530 531 532
    setOutput(system);
    setup();
}

void PlayerManager::setOutput(const QString &system)
{
533
    delete m_player;
534
    if(system == i18n("aRts"))
535
        m_player = createPlayer(ArtsBackend);
536
    else if(system == i18n("GStreamer"))
537
        m_player = createPlayer(GStreamerBackend);
538
    else if(system == i18n("aKode"))
539
        m_player = createPlayer(AkodeBackend);
540 541 542 543
}

void PlayerManager::slotSetVolume(int volume)
{
544 545
    float scaledVolume;

546
    if(m_sliderAction->volumeSlider())
547 548 549 550 551 552 553 554 555 556 557 558
        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
559 560 561 562 563 564 565
}

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

566
    float positionFraction = float(position) / SliderAction::maxPosition;
567
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
568
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
569 570 571 572

    m_statusLabel->setItemCurrentTime(seekTime);
}

573 574 575 576
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

577 578 579 580 581 582 583 584
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

585 586 587 588
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

589 590 591
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
592
       !action("forwardAlbum") ||
593 594
       !action("forward") ||
       !action("trackPositionAction"))
595 596 597 598 599
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

600 601
    // initialize action states

602 603 604 605
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
606
    action("forwardAlbum")->setEnabled(false);
607

608 609
    // setup sliders

610
    m_sliderAction = action<SliderAction>("trackPositionAction");
611

612 613 614 615
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
616
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
617 618
            this, SLOT(slotSetVolume(int)));

619
    // Call this method manually to avoid warnings.
620

621
    KAction *outputAction = actions()->action("outputSelect");
622 623

    if(outputAction) {
624
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
625
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
626 627
    }
    else
628
        m_player = createPlayer();
629

630 631 632 633 634 635 636 637 638
    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
639 640 641

    m_player->setVolume(volume);

642 643
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
644 645
}

Michael Pyne's avatar
Michael Pyne committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
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);
}

665
#include "playermanager.moc"
666 667

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