playermanager.cpp 17.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***************************************************************************
    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.                                   *
 *                                                                         *
 ***************************************************************************/

16 17 18 19 20 21 22 23 24
/**
 * Note to those who work here.  The preprocessor variables HAVE_ARTS and HAVE_GSTREAMER
 * are ::ALWAYS DEFINED::.  You can't use #ifdef to see if they're present, you should just
 * use #if.
 *
 * However, HAVE_AKODE is #define'd if present, and undefined if not present.
 * - mpyne
 */

25
#include <kdebug.h>
26
#include <klocale.h>
27 28

#include <qslider.h>
29
#include <qtimer.h>
Laurent Montel's avatar
Laurent Montel committed
30 31
//Added by qt3to4:
#include <QPixmap>
32

33 34
#include <math.h>

35
#include "artsplayer.h"
36
#include "akodeplayer.h"
37
#include "gstreamerplayer.h"
38
#include "playermanager.h"
39
#include "playlistinterface.h"
40
#include "slideraction.h"
41
#include "statuslabel.h"
42
#include "actioncollection.h"
43
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
44
#include "coverinfo.h"
45
#include "tag.h"
46

47 48
#include "config.h"

49
using namespace ActionCollection;
50

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

53 54 55 56
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

57
enum SoundSystem { ArtsBackend = 0, GStreamerBackend = 1, AkodeBackend = 2 };
58 59 60 61 62 63

static Player *createPlayer(int system = ArtsBackend)
{

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

92 93 94 95 96
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
97
    Player(),
98
    m_sliderAction(0),
99
    m_playlistInterface(0),
100
    m_statusLabel(0),
101 102
    m_player(0),
    m_timer(0),
103
    m_noSeek(false),
104 105
    m_muted(false),
    m_setup(false)
106
{
Michael Pyne's avatar
Michael Pyne committed
107 108 109 110 111
// 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();
112 113 114 115
}

PlayerManager::~PlayerManager()
{
116
    delete m_player;
117 118 119 120 121 122 123 124
}

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

PlayerManager *PlayerManager::instance() // static
{
125 126
    static PlayerManager manager;
    return &manager;
127 128
}

129 130
bool PlayerManager::playing() const
{
131
    if(!player())
132 133
        return false;

134
    return player()->playing();
135 136 137 138
}

bool PlayerManager::paused() const
{
139
    if(!player())
140 141
        return false;

142
    return player()->paused();
143 144 145 146
}

float PlayerManager::volume() const
{
147
    if(!player())
148 149
        return 0;

150
    return player()->volume();
151 152
}

153 154 155
int PlayerManager::status() const
{
    if(!player())
156
        return StatusStopped;
157 158

    if(player()->paused())
159
        return StatusPaused;
160 161

    if(player()->playing())
162
        return StatusPlaying;
163

164 165 166
    return 0;
}

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

172
    return player()->totalTime();
173 174
}

Scott Wheeler's avatar
Scott Wheeler committed
175
int PlayerManager::currentTime() const
176
{
177
    if(!player())
178 179
        return 0;

180
    return player()->currentTime();
181 182 183 184
}

int PlayerManager::position() const
{
185
    if(!player())
186 187
        return 0;

188
    return player()->position();
189 190
}

191 192 193 194 195 196 197
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
198
    if(!playing() && !paused())
199 200 201 202 203
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
204 205 206 207 208 209 210 211 212 213 214 215 216
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();
}

217 218 219 220 221
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

222 223 224 225 226
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

227 228 229 230 231
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
232 233
}

234
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
235 236 237 238
{
    m_playlistInterface = interface;
}

239 240 241 242 243
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

244 245 246 247 248
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
249 250 251 252 253 254

#if HAVE_ARTS
    l.append(i18n("aRts"));
#endif
#if HAVE_GSTREAMER
    l.append(i18n("GStreamer"));
255
#endif
256 257
#ifdef HAVE_AKODE
    l.append(i18n("aKode"));
258
#endif
259

260
    if(l.isEmpty()) {
261
        kError(65432) << "Your JuK seems to have no output backend possibilities.\n";
262 263 264
        l.append(i18n("aKode")); // Looks like akode is the default backend.
    }

265
    action->setItems(l);
266 267 268
    return action;
}

269 270 271 272
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

273
void PlayerManager::play(const FileHandle &file)
274
{
275
    if(!player() || !m_playlistInterface)
276 277
        return;

278 279
    if(file.isNull()) {
        if(player()->paused())
280
            player()->play();
281
        else if(player()->playing()) {
282 283
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
284
            player()->seekPosition(0);
285
        }
286
        else {
287
            m_playlistInterface->playNext();
288
            m_file = m_playlistInterface->currentFile();
289

290
            if(!m_file.isNull())
291
                player()->play(m_file);
292
        }
293
    }
294
    else {
295
        m_file = file;
296
        player()->play(file);
297
    }
298

299
    // Make sure that the player() actually starts before doing anything.
300

301
    if(!player()->playing()) {
Scott Wheeler's avatar
Scott Wheeler committed
302
        kWarning(65432) << "Unable to play " << file.absFilePath() << endl;
303 304 305
        stop();
        return;
    }
306

307 308 309
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
310 311
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
312
    action("back")->setEnabled(true);
313

314 315
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
316 317

    m_timer->start(m_pollInterval);
318 319

    emit signalPlay();
320 321
}

322 323 324 325 326 327 328 329 330
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
331 332 333 334 335
void PlayerManager::play()
{
    play(FileHandle::null());
}

336 337
void PlayerManager::pause()
{
338
    if(!player())
339 340
        return;

341 342 343 344 345 346
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
347
    action("pause")->setEnabled(false);
348 349

    player()->pause();
350 351

    emit signalPause();
352 353 354 355
}

void PlayerManager::stop()
{
356
    if(!player() || !m_playlistInterface)
357 358
        return;

359 360
    m_timer->stop();

361 362 363 364
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
365
    action("forwardAlbum")->setEnabled(false);
366

367 368 369 370
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
371 372

    player()->stop();
373
    m_playlistInterface->stop();
374

375 376
    m_file = FileHandle::null();

377
    emit signalStop();
378 379
}

Laurent Montel's avatar
Laurent Montel committed
380
void PlayerManager::setVolume(float volume)
381
{
382
    if(!player())
383 384
        return;

385
    player()->setVolume(volume);
386 387
}

Scott Wheeler's avatar
Scott Wheeler committed
388
void PlayerManager::seek(int seekTime)
389
{
390
    if(!player())
391
        return;
392

393
    player()->seek(seekTime);
394 395
}

396
void PlayerManager::seekPosition(int position)
397
{
398 399 400 401
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
402
        return;
403

404 405
    slotUpdateTime(position);
    player()->seekPosition(position);
406 407 408

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
409 410 411 412
}

void PlayerManager::seekForward()
{
Laurent Montel's avatar
Laurent Montel committed
413
    seekPosition(qMin(SliderAction::maxPosition, position() + 10));
414 415 416 417
}

void PlayerManager::seekBack()
{
Laurent Montel's avatar
Laurent Montel committed
418
    seekPosition(qMax(SliderAction::minPosition, position() - 10));
419 420
}

421 422
void PlayerManager::playPause()
{
423
    playing() ? action("pause")->activate() : action("play")->activate();
424 425
}

426 427
void PlayerManager::forward()
{
428 429
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
430

431 432 433 434 435 436 437 438
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
439 440
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
441

442 443 444 445 446 447
    if(!file.isNull())
        play(file);
    else
        stop();
}

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

453
    int volume = m_sliderAction->volumeSlider()->volume() +
454
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
455 456

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

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

    int volume = m_sliderAction->volumeSlider()->value() -
466
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
467 468

    slotSetVolume(volume);
469
    m_sliderAction->volumeSlider()->setVolume(volume);
470 471 472 473
}

void PlayerManager::mute()
{
474
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
475 476 477 478 479 480
        return;

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

481 482 483
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
484

485
void PlayerManager::slotPollPlay()
486
{
487
    if(!player() || !m_playlistInterface)
488
        return;
489

490
    m_noSeek = true;
491

492
    if(!player()->playing()) {
493
        m_timer->stop();
494

495 496
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
497
        if(!nextFile.isNull())
498
            play(nextFile);
499 500
        else
            stop();
501 502
    }
    else if(!m_sliderAction->dragging()) {
503 504
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
505

506 507 508 509
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
510
    }
511

512 513 514 515 516 517 518 519
    // 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())
520 521
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
522
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
523 524 525 526 527

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

529 530 531 532
    // 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.
533

534 535 536
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
537 538 539
    {
        m_timer->changeInterval(50);
    }
540

541
    m_noSeek = false;
542 543
}

544
void PlayerManager::slotSetOutput(const QString &system)
545 546
{
    stop();
547 548 549 550 551 552
    setOutput(system);
    setup();
}

void PlayerManager::setOutput(const QString &system)
{
553
    delete m_player;
554
    if(system == i18n("aRts"))
555
        m_player = createPlayer(ArtsBackend);
556
    else if(system == i18n("GStreamer"))
557
        m_player = createPlayer(GStreamerBackend);
558
    else if(system == i18n("aKode"))
559
        m_player = createPlayer(AkodeBackend);
560 561 562 563
}

void PlayerManager::slotSetVolume(int volume)
{
564 565
    float scaledVolume;

566
    if(m_sliderAction->volumeSlider())
567 568 569
        scaledVolume = float(volume) / m_sliderAction->volumeSlider()->maxValue();
    else {
        scaledVolume = float(volume) / 100.0; // Hopefully this is accurate
Laurent Montel's avatar
Laurent Montel committed
570
        scaledVolume = qMin(1.0f, scaledVolume);
571 572 573 574 575 576 577 578
    }

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

    scaledVolume *= scaledVolume;
    scaledVolume *= scaledVolume;
    setVolume(scaledVolume); // scaledVolume ^ 4
579 580 581 582 583 584 585
}

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

586
    float positionFraction = float(position) / SliderAction::maxPosition;
587
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
588
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
589 590 591 592

    m_statusLabel->setItemCurrentTime(seekTime);
}

593 594 595 596
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

597 598 599 600 601 602 603 604
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

605 606 607 608
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

609 610 611
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
612
       !action("forwardAlbum") ||
613 614
       !action("forward") ||
       !action("trackPositionAction"))
615
    {
Laurent Montel's avatar
Laurent Montel committed
616
        kWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
617 618 619
        return;
    }

620 621 622 623
    if(m_setup)
        return;
    m_setup = true;

624 625
    // initialize action states

626 627 628 629
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
630
    action("forwardAlbum")->setEnabled(false);
631

632 633
    // setup sliders

634
    m_sliderAction = action<SliderAction>("trackPositionAction");
635

636 637 638 639
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
640
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
641 642
            this, SLOT(slotSetVolume(int)));

643
    // Call this method manually to avoid warnings.
644

645
    KAction *outputAction = actions()->action("outputSelect");
646 647

    if(outputAction) {
648
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
649
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
650 651
    }
    else
652
        m_player = createPlayer();
653

654
    float volume;
655

656
    if(m_sliderAction->volumeSlider()) {
657
        volume =
658 659 660 661 662
            float(m_sliderAction->volumeSlider()->volume()) /
            float(m_sliderAction->volumeSlider()->maxValue());
    }
    else
        volume = 1; // Assume user wants full volume
663 664 665

    m_player->setVolume(volume);

666 667
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
668 669
}

Michael Pyne's avatar
Michael Pyne committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
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);
}

689
#include "playermanager.moc"
690

691
// vim: set et sw=4 tw=0 sta: