playermanager.cpp 16.5 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 216 217 218
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

219
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
220 221 222 223
{
    m_playlistInterface = interface;
}

224 225 226 227 228
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

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

246 247 248 249
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

250
void PlayerManager::play(const FileHandle &file)
251
{
252
    if(!player() || !m_playlistInterface)
253 254
        return;

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

267
            if(!m_file.isNull())
268
                player()->play(m_file);
269
        }
270
    }
271
    else {
272
        m_file = file;
273
        player()->play(file);
274
    }
275

276
    // Make sure that the player() actually starts before doing anything.
277

278
    if(!player()->playing()) {
279 280 281
        stop();
        return;
    }
282

283 284 285
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
286 287
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
288
    action("back")->setEnabled(true);
289

290 291
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
292 293

    m_timer->start(m_pollInterval);
294 295

    emit signalPlay();
296 297
}

298 299 300 301 302 303 304 305 306
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
307 308 309 310 311
void PlayerManager::play()
{
    play(FileHandle::null());
}

312 313
void PlayerManager::pause()
{
314
    if(!player())
315 316
        return;

317 318 319 320 321 322
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
323
    action("pause")->setEnabled(false);
324 325

    player()->pause();
326 327

    emit signalPause();
328 329 330 331
}

void PlayerManager::stop()
{
332
    if(!player() || !m_playlistInterface)
333 334
        return;

335 336
    m_timer->stop();

337 338 339 340
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
341
    action("forwardAlbum")->setEnabled(false);
342

343 344 345 346
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
347 348

    player()->stop();
349
    m_playlistInterface->stop();
350

351 352
    m_file = FileHandle::null();

353
    emit signalStop();
354 355
}

Laurent Montel's avatar
Laurent Montel committed
356
void PlayerManager::setVolume(float volume)
357
{
358
    if(!player())
359 360
        return;

361
    player()->setVolume(volume);
362 363
}

Scott Wheeler's avatar
Scott Wheeler committed
364
void PlayerManager::seek(int seekTime)
365
{
366
    if(!player())
367
        return;
368

369
    player()->seek(seekTime);
370 371
}

372
void PlayerManager::seekPosition(int position)
373
{
374 375 376 377
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
378
        return;
379

380 381
    slotUpdateTime(position);
    player()->seekPosition(position);
382 383 384

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
385 386 387 388
}

void PlayerManager::seekForward()
{
389
    seekPosition(kMin(SliderAction::maxPosition, position() + 10));
390 391 392 393
}

void PlayerManager::seekBack()
{
394
    seekPosition(kMax(SliderAction::minPosition, position() - 10));
395 396
}

397 398
void PlayerManager::playPause()
{
399
    playing() ? action("pause")->activate() : action("play")->activate();
400 401
}

402 403
void PlayerManager::forward()
{
404 405
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
406

407 408 409 410 411 412 413 414
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
415 416
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
417

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

424 425
void PlayerManager::volumeUp()
{
426
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
427
        return;
428

429
    int volume = m_sliderAction->volumeSlider()->volume() +
430
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
431 432

    slotSetVolume(volume);
433
    m_sliderAction->volumeSlider()->setVolume(volume);
434 435 436 437
}

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

    int volume = m_sliderAction->volumeSlider()->value() -
442
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
443 444

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

void PlayerManager::mute()
{
450
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
451 452 453 454 455 456
        return;

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

457 458 459
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
460

461
void PlayerManager::slotPollPlay()
462
{
463
    if(!player() || !m_playlistInterface)
464
        return;
465

466
    m_noSeek = true;
467

468
    if(!player()->playing()) {
469
        m_timer->stop();
470

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

482 483 484 485
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
486
    }
487

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

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

505 506 507 508
    // 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.
509

510 511 512
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
513 514 515
    {
        m_timer->changeInterval(50);
    }
516

517
    m_noSeek = false;
518 519
}

520
void PlayerManager::slotSetOutput(const QString &system)
521 522
{
    stop();
523 524 525 526 527 528
    setOutput(system);
    setup();
}

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

void PlayerManager::slotSetVolume(int volume)
{
540 541
    float scaledVolume;

542
    if(m_sliderAction->volumeSlider())
543 544 545 546 547 548 549 550 551 552 553 554
        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
555 556 557 558 559 560 561
}

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

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

    m_statusLabel->setItemCurrentTime(seekTime);
}

569 570 571 572
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

573 574 575 576 577 578 579 580
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

581 582 583 584
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

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

596 597
    // initialize action states

598 599 600 601
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
602
    action("forwardAlbum")->setEnabled(false);
603

604 605
    // setup sliders

606
    m_sliderAction = action<SliderAction>("trackPositionAction");
607

608 609 610 611
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
612
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
613 614
            this, SLOT(slotSetVolume(int)));

615
    // Call this method manually to avoid warnings.
616

617
    KAction *outputAction = actions()->action("outputSelect");
618 619

    if(outputAction) {
620
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
621
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
622 623
    }
    else
624
        m_player = createPlayer();
625

626 627 628 629 630 631 632 633 634
    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
635 636 637

    m_player->setVolume(volume);

638 639
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
640 641
}

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

661
#include "playermanager.moc"
662 663

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