playermanager.cpp 15.6 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 "playermanager.h"
36
#include "playlistinterface.h"
37
#include "slideraction.h"
38
#include "statuslabel.h"
39
#include "actioncollection.h"
40
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
41
#include "coverinfo.h"
42
#include "tag.h"
43

44 45
#include "config.h"

46
using namespace ActionCollection;
47

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

50 51 52 53 54
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
55
    Player(),
56
    m_sliderAction(0),
57
    m_playlistInterface(0),
58
    m_statusLabel(0),
59 60
    m_player(0),
    m_timer(0),
61
    m_noSeek(false),
62 63
    m_muted(false),
    m_setup(false)
64
{
Michael Pyne's avatar
Michael Pyne committed
65 66 67 68 69
// 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();
70 71 72 73
}

PlayerManager::~PlayerManager()
{
74
    delete m_player;
75 76 77 78 79 80 81 82
}

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

PlayerManager *PlayerManager::instance() // static
{
83 84
    static PlayerManager manager;
    return &manager;
85 86
}

87 88
bool PlayerManager::playing() const
{
89
    if(!player())
90 91
        return false;

92
    return player()->playing();
93 94 95 96
}

bool PlayerManager::paused() const
{
97
    if(!player())
98 99
        return false;

100
    return player()->paused();
101 102 103 104
}

float PlayerManager::volume() const
{
105
    if(!player())
106 107
        return 0;

108
    return player()->volume();
109 110
}

111 112 113
int PlayerManager::status() const
{
    if(!player())
114
        return StatusStopped;
115 116

    if(player()->paused())
117
        return StatusPaused;
118 119

    if(player()->playing())
120
        return StatusPlaying;
121

122 123 124
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
125
int PlayerManager::totalTime() const
126
{
127
    if(!player())
128 129
        return 0;

130
    return player()->totalTime();
131 132
}

Scott Wheeler's avatar
Scott Wheeler committed
133
int PlayerManager::currentTime() const
134
{
135
    if(!player())
136 137
        return 0;

138
    return player()->currentTime();
139 140 141 142
}

int PlayerManager::position() const
{
143
    if(!player())
144 145
        return 0;

146
    return player()->position();
147 148
}

149 150 151 152 153 154 155
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
156
    if(!playing() && !paused())
157 158 159 160 161
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
162 163 164 165 166 167 168 169 170 171 172 173 174
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();
}

175 176 177 178 179
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

180 181 182 183 184
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

185 186 187 188 189
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
190 191
}

192
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
193 194 195 196
{
    m_playlistInterface = interface;
}

197 198 199 200 201
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

202 203 204 205
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

206
void PlayerManager::play(const FileHandle &file)
207
{
208
    if(!player() || !m_playlistInterface)
209 210
        return;

211 212
    if(file.isNull()) {
        if(player()->paused())
213
            player()->play();
214
        else if(player()->playing()) {
215 216
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
217
            player()->seekPosition(0);
218
        }
219
        else {
220
            m_playlistInterface->playNext();
221
            m_file = m_playlistInterface->currentFile();
222

223
            if(!m_file.isNull())
224
                player()->play(m_file);
225
        }
226
    }
227
    else {
228
        m_file = file;
229
        player()->play(file);
230
    }
231

232
    // Make sure that the player() actually starts before doing anything.
233

234
    if(!player()->playing()) {
Scott Wheeler's avatar
Scott Wheeler committed
235
        kWarning(65432) << "Unable to play " << file.absFilePath() << endl;
236 237 238
        stop();
        return;
    }
239

240 241 242
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
243 244
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
245
    action("back")->setEnabled(true);
246

247 248
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
249 250

    m_timer->start(m_pollInterval);
251 252

    emit signalPlay();
253 254
}

255 256 257 258 259 260 261 262 263
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
264 265 266 267 268
void PlayerManager::play()
{
    play(FileHandle::null());
}

269 270
void PlayerManager::pause()
{
271
    if(!player())
272 273
        return;

274 275 276 277 278 279
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
280
    action("pause")->setEnabled(false);
281 282

    player()->pause();
283 284

    emit signalPause();
285 286 287 288
}

void PlayerManager::stop()
{
289
    if(!player() || !m_playlistInterface)
290 291
        return;

292 293
    m_timer->stop();

294 295 296 297
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
298
    action("forwardAlbum")->setEnabled(false);
299

300 301 302 303
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
304 305

    player()->stop();
306
    m_playlistInterface->stop();
307

308 309
    m_file = FileHandle::null();

310
    emit signalStop();
311 312
}

Laurent Montel's avatar
Laurent Montel committed
313
void PlayerManager::setVolume(float volume)
314
{
315
    if(!player())
316 317
        return;

318
    player()->setVolume(volume);
319 320
}

Scott Wheeler's avatar
Scott Wheeler committed
321
void PlayerManager::seek(int seekTime)
322
{
323
    if(!player())
324
        return;
325

326
    player()->seek(seekTime);
327 328
}

329
void PlayerManager::seekPosition(int position)
330
{
331 332 333 334
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
335
        return;
336

337 338
    slotUpdateTime(position);
    player()->seekPosition(position);
339 340 341

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
342 343 344 345
}

void PlayerManager::seekForward()
{
Laurent Montel's avatar
Laurent Montel committed
346
    seekPosition(qMin(SliderAction::maxPosition, position() + 10));
347 348 349 350
}

void PlayerManager::seekBack()
{
Laurent Montel's avatar
Laurent Montel committed
351
    seekPosition(qMax(SliderAction::minPosition, position() - 10));
352 353
}

354 355
void PlayerManager::playPause()
{
David Faure's avatar
David Faure committed
356
    playing() ? action("pause")->trigger() : action("play")->trigger();
357 358
}

359 360
void PlayerManager::forward()
{
361 362
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
363

364 365 366 367 368 369 370 371
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
372 373
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
374

375 376 377 378 379 380
    if(!file.isNull())
        play(file);
    else
        stop();
}

381 382
void PlayerManager::volumeUp()
{
383
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
384
        return;
385

386
    int volume = m_sliderAction->volumeSlider()->volume() +
387
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
388 389

    slotSetVolume(volume);
390
    m_sliderAction->volumeSlider()->setVolume(volume);
391 392 393 394
}

void PlayerManager::volumeDown()
{
395
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
396
        return;
397 398

    int volume = m_sliderAction->volumeSlider()->value() -
399
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
400 401

    slotSetVolume(volume);
402
    m_sliderAction->volumeSlider()->setVolume(volume);
403 404 405 406
}

void PlayerManager::mute()
{
407
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
408 409 410 411 412 413
        return;

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

414 415 416
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
417

418
void PlayerManager::slotPollPlay()
419
{
420
    if(!player() || !m_playlistInterface)
421
        return;
422

423
    m_noSeek = true;
424

425
    if(!player()->playing()) {
426
        m_timer->stop();
427

428 429
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
430
        if(!nextFile.isNull())
431
            play(nextFile);
432 433
        else
            stop();
434 435
    }
    else if(!m_sliderAction->dragging()) {
436 437
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
438

439 440 441 442
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
443
    }
444

445 446 447 448 449 450 451 452
    // 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())
453 454
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
455
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
456 457 458 459 460

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

462 463 464 465
    // 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.
466

467 468 469
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
470 471 472
    {
        m_timer->changeInterval(50);
    }
473

474
    m_noSeek = false;
475 476
}

477
void PlayerManager::slotSetOutput(const QString &system)
478 479
{
    stop();
480 481 482 483 484 485
    setOutput(system);
    setup();
}

void PlayerManager::setOutput(const QString &system)
{
486
    delete m_player;
487
    m_player = new Player;
488 489 490 491
}

void PlayerManager::slotSetVolume(int volume)
{
492 493
    float scaledVolume;

494
    if(m_sliderAction->volumeSlider())
495 496 497
        scaledVolume = float(volume) / m_sliderAction->volumeSlider()->maxValue();
    else {
        scaledVolume = float(volume) / 100.0; // Hopefully this is accurate
Laurent Montel's avatar
Laurent Montel committed
498
        scaledVolume = qMin(1.0f, scaledVolume);
499 500 501 502 503 504 505 506
    }

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

    scaledVolume *= scaledVolume;
    scaledVolume *= scaledVolume;
    setVolume(scaledVolume); // scaledVolume ^ 4
507 508 509 510 511 512 513
}

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

514
    float positionFraction = float(position) / SliderAction::maxPosition;
515
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
516
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
517 518 519 520

    m_statusLabel->setItemCurrentTime(seekTime);
}

521 522 523 524
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

525 526 527 528 529 530 531 532
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

533 534 535 536
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

537 538 539
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
540
       !action("forwardAlbum") ||
541 542
       !action("forward") ||
       !action("trackPositionAction"))
543
    {
Laurent Montel's avatar
Laurent Montel committed
544
        kWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
545 546 547
        return;
    }

548 549 550 551
    if(m_setup)
        return;
    m_setup = true;

552 553
    // initialize action states

554 555 556 557
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
558
    action("forwardAlbum")->setEnabled(false);
559

560 561
    // setup sliders

562
    m_sliderAction = action<SliderAction>("trackPositionAction");
563

564 565 566 567
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
568
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
569 570
            this, SLOT(slotSetVolume(int)));

571
    // Call this method manually to avoid warnings.
572

573
    KAction *outputAction = ActionCollection::actions()->action("outputSelect");
574 575

    if(outputAction) {
576
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
577
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
578 579
    }
    else
580
        m_player = new Player;
581

582
    float volume;
583

584
    if(m_sliderAction->volumeSlider()) {
585
        volume =
586 587 588 589 590
            float(m_sliderAction->volumeSlider()->volume()) /
            float(m_sliderAction->volumeSlider()->maxValue());
    }
    else
        volume = 1; // Assume user wants full volume
591 592 593

    m_player->setVolume(volume);

594 595
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
596 597
}

Michael Pyne's avatar
Michael Pyne committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
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);
}

617
#include "playermanager.moc"
618

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