playermanager.cpp 15.2 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 29 30 31
#include <phonon/mediaobject.h>
#include <phonon/audiopath.h>
#include <phonon/audiooutput.h>

32
#include <qslider.h>
Laurent Montel's avatar
Laurent Montel committed
33 34
//Added by qt3to4:
#include <QPixmap>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
35 36 37
#include <ktoggleaction.h>
#include <kactioncollection.h>
#include <kselectaction.h>
38 39
#include <math.h>

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

49 50
#include "config.h"

Laurent Montel's avatar
Laurent Montel committed
51
#include <QtDBus>
Laurent Montel's avatar
Laurent Montel committed
52 53
#include "playeradaptor.h"

54
using namespace ActionCollection;
55

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

58 59 60 61 62
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
63
    QObject(),
64
    m_sliderAction(0),
65
    m_playlistInterface(0),
66
    m_statusLabel(0),
67
    m_noSeek(false),
68
    m_muted(false),
69 70 71 72
    m_setup(false),
    m_output(0),
    m_audioPath(0),
    m_media(0)
73
{
Michael Pyne's avatar
Michael Pyne committed
74 75 76 77 78
// 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();
Laurent Montel's avatar
Laurent Montel committed
79 80 81
    new PlayerAdaptor( this );
    QDBus::sessionBus().registerObject("/Player", this);

82 83 84 85 86 87 88 89 90 91 92 93
}

PlayerManager::~PlayerManager()
{
}

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

PlayerManager *PlayerManager::instance() // static
{
94 95
    static PlayerManager manager;
    return &manager;
96 97
}

98 99
bool PlayerManager::playing() const
{
100
    if(!m_media)
101 102
        return false;

103
    return m_media->state() == Phonon::PlayingState;
104 105 106 107
}

bool PlayerManager::paused() const
{
108
    if(!m_media)
109 110
        return false;

111
    return m_media->state() == Phonon::PausedState;
112 113 114 115
}

float PlayerManager::volume() const
{
116
    if(!m_output)
117 118
        return 0;

119
    return m_output->volume();
120 121
}

122 123
int PlayerManager::status() const
{
124
    if(!m_media)
125
        return StatusStopped;
126

127
    if(paused())
128
        return StatusPaused;
129

130
    if(playing())
131
        return StatusPlaying;
132

133 134 135
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
136
int PlayerManager::totalTime() const
137
{
138
    if(!m_media)
139 140
        return 0;

141
    return m_media->totalTime() / 1000;
142 143
}

Scott Wheeler's avatar
Scott Wheeler committed
144
int PlayerManager::currentTime() const
145
{
146
    if(!m_media)
147 148
        return 0;

149
    return m_media->currentTime() / 1000;
150 151 152 153
}

int PlayerManager::position() const
{
154
    if(!m_media)
155 156
        return 0;

157 158 159 160
    long curr = m_media->currentTime();
    if( curr > 0 )
        return static_cast<int>( curr / 1000.0f / m_media->totalTime() + 0.5f );
    return -1;
161 162
}

163 164 165 166 167 168 169
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
170
    if(!playing() && !paused())
171 172 173 174 175
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
176 177 178 179 180
QPixmap PlayerManager::trackCover(const QString &size) const
{
    if(!playing() && !paused())
        return QPixmap();

181
    if(size.toLower() == "small")
Michael Pyne's avatar
Michael Pyne committed
182
        return m_file.coverInfo()->pixmap(CoverInfo::Thumbnail);
183
    if(size.toLower() == "large")
Michael Pyne's avatar
Michael Pyne committed
184 185 186 187 188
        return m_file.coverInfo()->pixmap(CoverInfo::FullSize);

    return QPixmap();
}

189 190 191 192 193
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

194 195 196 197 198
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

199 200 201 202 203
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
204 205
}

206
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
207 208 209 210
{
    m_playlistInterface = interface;
}

211 212 213 214 215
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

216 217 218 219
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

220
void PlayerManager::play(const FileHandle &file)
221
{
222 223 224 225
    if(!m_media)
        setup();

    if(!m_media || !m_playlistInterface)
226 227
        return;

228
    if(file.isNull()) {
229 230 231
        if(paused())
            m_media->play();
        else if(playing()) {
232 233
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
234
            m_media->seek(0);
235
        }
236
        else {
237
            m_playlistInterface->playNext();
238
            m_file = m_playlistInterface->currentFile();
239

240
            if(!m_file.isNull())
241 242 243 244
            {
                m_media->setUrl(KUrl::fromPath(m_file.absFilePath()));
                m_media->play();
            }
245
        }
246
    }
247
    else {
248
        m_file = file;
249 250
        m_media->setUrl(KUrl::fromPath(m_file.absFilePath()));
        m_media->play();
251
    }
252

253
    // Make sure that the player() actually starts before doing anything.
254

255
    if(!playing()) {
Scott Wheeler's avatar
Scott Wheeler committed
256
        kWarning(65432) << "Unable to play " << file.absFilePath() << endl;
257 258 259
        stop();
        return;
    }
260

261 262 263
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
264 265
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
266
    action("back")->setEnabled(true);
267

268 269
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
270

271
    emit signalPlay();
272 273
}

274 275 276 277 278 279 280 281 282
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
283 284 285 286 287
void PlayerManager::play()
{
    play(FileHandle::null());
}

288 289
void PlayerManager::pause()
{
290
    if(!m_media)
291 292
        return;

293
    if(paused()) {
294 295 296 297
        play();
        return;
    }

298
    action("pause")->setEnabled(false);
299

300
    m_media->pause();
301 302

    emit signalPause();
303 304 305 306
}

void PlayerManager::stop()
{
307
    if(!m_media || !m_playlistInterface)
308 309
        return;

310 311 312 313
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
314
    action("forwardAlbum")->setEnabled(false);
315

316 317 318 319
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
320

321
    m_media->stop();
322
    m_playlistInterface->stop();
323

324 325
    m_file = FileHandle::null();

326
    emit signalStop();
327 328
}

Laurent Montel's avatar
Laurent Montel committed
329
void PlayerManager::setVolume(float volume)
330
{
331 332 333
    if(!m_output)
        setup();
    m_output->setVolume(volume);
334 335
}

Scott Wheeler's avatar
Scott Wheeler committed
336
void PlayerManager::seek(int seekTime)
337
{
338
    if(!m_media)
339
        return;
340

341
    m_media->seek(seekTime);
342 343
}

344
void PlayerManager::seekPosition(int position)
345
{
346
    if(!m_media)
347 348
        return;

349
    if(!playing() || m_noSeek)
350
        return;
351

352
    slotUpdateTime(position);
353
    m_media->seek( static_cast<long>( m_media->totalTime() / 1000.0f * position + 0.5f ) );
354 355 356

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
357 358 359 360
}

void PlayerManager::seekForward()
{
Laurent Montel's avatar
Laurent Montel committed
361
    seekPosition(qMin(SliderAction::maxPosition, position() + 10));
362 363 364 365
}

void PlayerManager::seekBack()
{
Laurent Montel's avatar
Laurent Montel committed
366
    seekPosition(qMax(SliderAction::minPosition, position() - 10));
367 368
}

369 370
void PlayerManager::playPause()
{
David Faure's avatar
David Faure committed
371
    playing() ? action("pause")->trigger() : action("play")->trigger();
372 373
}

374 375
void PlayerManager::forward()
{
376 377
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
378

379 380 381 382 383 384 385 386
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
387 388
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
389

390 391 392 393 394 395
    if(!file.isNull())
        play(file);
    else
        stop();
}

396 397
void PlayerManager::volumeUp()
{
398
    if(!m_media || !m_sliderAction || !m_sliderAction->volumeSlider())
399
        return;
400

401
    int volume = m_sliderAction->volumeSlider()->volume() +
402
        m_sliderAction->volumeSlider()->maximum() / 25; // 4% up
403 404

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

void PlayerManager::volumeDown()
{
410
    if(!m_media || !m_sliderAction || !m_sliderAction->volumeSlider())
411
        return;
412 413

    int volume = m_sliderAction->volumeSlider()->value() -
414
        m_sliderAction->volumeSlider()->maximum() / 25; // 4% down
415 416

    slotSetVolume(volume);
417
    m_sliderAction->volumeSlider()->setVolume(volume);
418 419 420 421
}

void PlayerManager::mute()
{
422
    if(!m_media || !m_sliderAction || !m_sliderAction->volumeSlider())
423 424 425 426 427 428
        return;

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

429 430 431
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
432

433 434 435 436 437 438 439 440 441 442 443 444
void PlayerManager::slotFinished()
{
    m_playlistInterface->playNext();
    FileHandle nextFile = m_playlistInterface->currentFile();
    if(!nextFile.isNull())
        play(nextFile);
    else
        stop();
    m_statusLabel->setItemTotalTime(totalTime());
}

void PlayerManager::slotTick(qint64 msec)
445
{
446
    if(!m_media || !m_playlistInterface)
447
        return;
448

449
    m_noSeek = true;
450

451 452 453 454 455
    if(!m_sliderAction->dragging()) {
        if(m_sliderAction->trackPositionSlider()) {
            int position = static_cast<int>(msec / 1000.0f / m_media->totalTime() + 0.5f);
            m_sliderAction->trackPositionSlider()->setValue(position);
        }
456

457
        if(m_statusLabel) {
458
            m_statusLabel->setItemCurrentTime(msec / 1000);
459
        }
460
    }
461

462 463 464 465 466 467 468 469
    // 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())
470
    {
471
        int maxV = m_sliderAction->volumeSlider()->maximum();
472
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
473 474 475 476 477

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

479
    m_noSeek = false;
480 481
}

482 483
void PlayerManager::slotSetVolume(int volume)
{
484 485
    float scaledVolume;

486
    if(m_sliderAction->volumeSlider())
487
        scaledVolume = float(volume) / m_sliderAction->volumeSlider()->maximum();
488 489
    else {
        scaledVolume = float(volume) / 100.0; // Hopefully this is accurate
Laurent Montel's avatar
Laurent Montel committed
490
        scaledVolume = qMin(1.0f, scaledVolume);
491 492 493 494 495 496 497
    }

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

    scaledVolume *= scaledVolume;
    scaledVolume *= scaledVolume;
498
    m_output->setVolume(scaledVolume); // scaledVolume ^ 4
499 500 501 502 503 504 505
}

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

506
    float positionFraction = float(position) / SliderAction::maxPosition;
507
    float totalTime = float(m_media->totalTime()) / 1000.0f;
Scott Wheeler's avatar
Scott Wheeler committed
508
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
509 510 511 512

    m_statusLabel->setItemCurrentTime(seekTime);
}

513 514 515 516 517 518 519 520
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

521 522 523
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
524
       !action("forwardAlbum") ||
525 526
       !action("forward") ||
       !action("trackPositionAction"))
527
    {
Laurent Montel's avatar
Laurent Montel committed
528
        kWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
529 530 531
        return;
    }

532 533 534 535
    if(m_setup)
        return;
    m_setup = true;

536 537 538 539 540 541
    m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    m_audioPath = new Phonon::AudioPath(this);
    m_media = new Phonon::MediaObject(this);
    m_audioPath->addOutput(m_output);
    m_media->addAudioPath(m_audioPath);

542 543
    // initialize action states

544 545 546 547
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
548
    action("forwardAlbum")->setEnabled(false);
549

550 551
    // setup sliders

552
    m_sliderAction = action<SliderAction>("trackPositionAction");
553

554 555 556 557
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
558
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
559 560
            this, SLOT(slotSetVolume(int)));

561
    float volume;
562

563
    if(m_sliderAction->volumeSlider()) {
564
        volume =
565
            float(m_sliderAction->volumeSlider()->volume()) /
566
            float(m_sliderAction->volumeSlider()->maximum());
567 568
    }
    else
569
        volume = 1.0f; // Assume user wants full volume
570

571
    m_output->setVolume(volume);
572

573 574
    connect(m_media, SIGNAL(tick(qint64)), SLOT(slotTick()));
    connect(m_media, SIGNAL(finished()), SLOT(slotFinished()));
575 576
}

Michael Pyne's avatar
Michael Pyne committed
577 578 579 580 581 582 583 584 585 586 587
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)
{
588
    if(randomMode.toLower() == "random")
Michael Pyne's avatar
Michael Pyne committed
589
        action<KToggleAction>("randomPlay")->setChecked(true);
590
    if(randomMode.toLower() == "albumrandom")
Michael Pyne's avatar
Michael Pyne committed
591
        action<KToggleAction>("albumRandomPlay")->setChecked(true);
592
    if(randomMode.toLower() == "norandom")
Michael Pyne's avatar
Michael Pyne committed
593 594 595
        action<KToggleAction>("disableRandomPlay")->setChecked(true);
}

596
#include "playermanager.moc"
597

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