playermanager.cpp 15.7 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>
Laurent Montel's avatar
Laurent Montel committed
30 31
//Added by qt3to4:
#include <QPixmap>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
32 33 34
#include <ktoggleaction.h>
#include <kactioncollection.h>
#include <kselectaction.h>
35 36
#include <math.h>

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

46 47
#include "config.h"

48
using namespace ActionCollection;
49

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

52 53 54 55 56
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

94
    return player()->playing();
95 96 97 98
}

bool PlayerManager::paused() const
{
99
    if(!player())
100 101
        return false;

102
    return player()->paused();
103 104 105 106
}

float PlayerManager::volume() const
{
107
    if(!player())
108 109
        return 0;

110
    return player()->volume();
111 112
}

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

    if(player()->paused())
119
        return StatusPaused;
120 121

    if(player()->playing())
122
        return StatusPlaying;
123

124 125 126
    return 0;
}

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

132
    return player()->totalTime();
133 134
}

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

140
    return player()->currentTime();
141 142 143 144
}

int PlayerManager::position() const
{
145
    if(!player())
146 147
        return 0;

148
    return player()->position();
149 150
}

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

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

    return m_file.property(property);
}

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

177 178 179 180 181
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

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

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

    return str;
192 193
}

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

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

204 205 206 207
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

234
    // Make sure that the player() actually starts before doing anything.
235

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

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

249 250
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
251 252

    m_timer->start(m_pollInterval);
253 254

    emit signalPlay();
255 256
}

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

271 272
void PlayerManager::pause()
{
273
    if(!player())
274 275
        return;

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

    m_timer->stop();
282
    action("pause")->setEnabled(false);
283 284

    player()->pause();
285 286

    emit signalPause();
287 288 289 290
}

void PlayerManager::stop()
{
291
    if(!player() || !m_playlistInterface)
292 293
        return;

294 295
    m_timer->stop();

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

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

    player()->stop();
308
    m_playlistInterface->stop();
309

310 311
    m_file = FileHandle::null();

312
    emit signalStop();
313 314
}

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

320
    player()->setVolume(volume);
321 322
}

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

328
    player()->seek(seekTime);
329 330
}

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

    if(!player()->playing() || m_noSeek)
337
        return;
338

339 340
    slotUpdateTime(position);
    player()->seekPosition(position);
341 342 343

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
344 345 346 347
}

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

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

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

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

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

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

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

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

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

    slotSetVolume(volume);
392
    m_sliderAction->volumeSlider()->setVolume(volume);
393 394 395 396
}

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

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

    slotSetVolume(volume);
404
    m_sliderAction->volumeSlider()->setVolume(volume);
405 406 407 408
}

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

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

416 417 418
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
419

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

425
    m_noSeek = true;
426

427
    if(!player()->playing()) {
428
        m_timer->stop();
429

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

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

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

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

464 465 466 467
    // 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.
468

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

476
    m_noSeek = false;
477 478
}

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

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

void PlayerManager::slotSetVolume(int volume)
{
494 495
    float scaledVolume;

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

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

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

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

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

    m_statusLabel->setItemCurrentTime(seekTime);
}

523 524 525 526
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

    return m_player;
}

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

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

550 551 552 553
    if(m_setup)
        return;
    m_setup = true;

554 555
    // initialize action states

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

562 563
    // setup sliders

564
    m_sliderAction = action<SliderAction>("trackPositionAction");
565

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

573
    // Call this method manually to avoid warnings.
574

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

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

584
    float volume;
585

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

    m_player->setVolume(volume);

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

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

619
#include "playermanager.moc"
620

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