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>
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
#ifdef HAVE_AKODE
56 57 58
    case AkodeBackend:
        p = new aKodePlayer;
        break;
59 60
#endif HAVE_AKODE
#ifdef HAVE_ARTS
61 62 63
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
64
#endif
65
#ifdef HAVE_GSTREAMER
66 67 68
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
69
#endif
70
    default:
71
#ifdef HAVE_ARTS
72
        p = new ArtsPlayer;
İsmail Dönmez's avatar
 
İsmail Dönmez committed
73
#elif HAVE_GSTREAMER
74
        p = new GStreamerPlayer;
75
#else
İsmail Dönmez's avatar
 
İsmail Dönmez committed
76
        p = new aKodePlayer;
77
#endif
78 79
        break;
    }
80 81 82
    return p;
}

83 84 85 86 87
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

125
    return player()->playing();
126 127 128 129
}

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

133
    return player()->paused();
134 135 136 137
}

float PlayerManager::volume() const
{
138
    if(!player())
139 140
        return 0;

141
    return player()->volume();
142 143
}

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

    if(player()->paused())
150
        return StatusPaused;
151 152

    if(player()->playing())
153
        return StatusPlaying;
154

155 156 157
    return 0;
}

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

163
    return player()->totalTime();
164 165
}

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

171
    return player()->currentTime();
172 173 174 175
}

int PlayerManager::position() const
{
176
    if(!player())
177 178
        return 0;

179
    return player()->position();
180 181
}

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

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

    return m_file.property(property);
}

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

208 209 210 211 212
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

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

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

    return str;
223 224
}

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

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

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

252 253 254 255
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

282
    // Make sure that the player() actually starts before doing anything.
283

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

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

296 297
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
298 299

    m_timer->start(m_pollInterval);
300 301

    emit signalPlay();
302 303
}

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

318 319
void PlayerManager::pause()
{
320
    if(!player())
321 322
        return;

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

    m_timer->stop();
329
    action("pause")->setEnabled(false);
330 331

    player()->pause();
332 333

    emit signalPause();
334 335 336 337
}

void PlayerManager::stop()
{
338
    if(!player() || !m_playlistInterface)
339 340
        return;

341 342
    m_timer->stop();

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

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

    player()->stop();
355
    m_playlistInterface->stop();
356

357 358
    m_file = FileHandle::null();

359
    emit signalStop();
360 361
}

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

367
    player()->setVolume(volume);
368 369
}

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

375
    player()->seek(seekTime);
376 377
}

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

    if(!player()->playing() || m_noSeek)
384
        return;
385

386 387
    slotUpdateTime(position);
    player()->seekPosition(position);
388 389 390

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
391 392 393 394
}

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

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

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

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

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

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

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

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

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

    slotSetVolume(volume);
439
    m_sliderAction->volumeSlider()->setVolume(volume);
440 441 442 443
}

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

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

    slotSetVolume(volume);
451
    m_sliderAction->volumeSlider()->setVolume(volume);
452 453 454 455
}

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

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

463 464 465
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
466

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

472
    m_noSeek = true;
473

474
    if(!player()->playing()) {
475
        m_timer->stop();
476

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

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

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

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

511 512 513 514
    // 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.
515

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

523
    m_noSeek = false;
524 525
}

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

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

void PlayerManager::slotSetVolume(int volume)
{
546 547
    float scaledVolume;

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

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

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

    m_statusLabel->setItemCurrentTime(seekTime);
}

575 576 577 578
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

    return m_player;
}

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

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

602 603
    // initialize action states

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

610 611
    // setup sliders

612
    m_sliderAction = action<SliderAction>("trackPositionAction");
613

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

621
    // Call this method manually to avoid warnings.
622

623
    KAction *outputAction = actions()->action("outputSelect");
624 625

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

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

    m_player->setVolume(volume);

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

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

667
#include "playermanager.moc"
668 669

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