Fixed empty label in library after start
[someplayer] / src / player / player.cpp
index 0181fc6..51071b1 100644 (file)
 
 #include "player.h"
 #include <phonon/MediaSource>
+#include <phonon/Effect>
+#include <phonon/BackendCapabilities>
+#include <phonon/EffectParameter>
+#include "../config.h"
+#include <QTime>
+#include <QTimer>
 
 using namespace SomePlayer::Playback;
 using namespace SomePlayer::DataObjects;
 using namespace SomePlayer::Storage;
 
+int Randomizer::next() {
+       int res = 0;
+       if (_rand.count() == 0) {
+               _shuffle();
+               res = next();
+       } else {
+               res = _rand.takeFirst();
+       }
+       return res;
+}
+
+void Randomizer::setPlaylist(QList<int> pl) {
+       _playlist = pl;
+       _shuffle();
+       _last = -1;
+}
+
+void Randomizer::_shuffle() {
+       _rand.clear();
+       // Fisher-Yates algorithm:
+       _rand = _playlist;
+       int cnt = _playlist.count();
+       int j = 0;
+       int tmp = 0;
+       for (int i = cnt-1; i > 0; i--) {
+               j = qrand() % (i+1);
+               tmp = _rand[i];
+               _rand[i] = _rand[j];
+               _rand[j] = tmp;
+       }
+       if (cnt > 1 && _last == _rand[0]) {
+               _rand.removeAt(0);
+               _rand.insert(qrand() % (cnt-1) + 1, _last);
+       }
+       if (!_rand.isEmpty())
+               _last = _rand.last();
+       else _last = -1;
+}
+
+void Randomizer::removeId(int id) {
+       _rand.removeOne(id);
+}
+
 Player::Player(QObject *parent) :
     QObject(parent)
 {
        _player = new Phonon::MediaObject(this);
        _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        _player->setTickInterval(1000);
+       _equalizer = NULL;
+       _equalizer_enabled = false;
        connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
        connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
-       Phonon::createPath(_player, _output);
-       int seed = reinterpret_cast<int> (_player);
+       connect(_player, SIGNAL(finished()), this, SLOT(next()));
+       _path = Phonon::createPath(_player, _output);
+       QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
+       foreach (Phonon::EffectDescription desc, effects) {
+               if (desc.name() == "equalizer-10bands") {
+                       _equalizer = new Phonon::Effect(desc, this);
+                       Config config;
+                       if (config.equalizerEnabled()) {
+                               for (int i = 0; i < 10; i++) {
+                                       QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
+                                       setEqualizerValue(i, var.toDouble());
+                               }
+                               enableEqualizer();
+                       }
+               }
+       }
+       int seed = QTime::currentTime().msec();
        qsrand(seed);
        _random = _config.getValue("playback/random").toBool();
-       _repeat = _config.getValue("playback/repeat").toBool();
+       _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
        _current = -1;
 }
 
 void Player::setTrackId(int id) {
+       if (_random) {
+               _randomizer.removeId(id);
+       }
        _current = id;
        if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
                _history.push(_current);
@@ -73,6 +142,11 @@ void Player::next() {
                stop(); // empty playlist
                return;
        }
+       if (_repeat == REPEAT_ONE) {
+               _set_source();
+               play();
+               return;
+       }
        _history.push(_current % count);
        if (!_queue.isEmpty()) {
                _current = _queue.dequeue();
@@ -80,13 +154,13 @@ void Player::next() {
                _current = _prev_history.pop();
        } else {
                if (_random) {
-                       _current = (count + (qrand()  + qrand() + qrand()) % count) % count;
+                       _current = _randomizer.next();
                } else {
                        _current = _current + 1;
                }
        }
-       if (_random && _history.count() >= count && !_repeat ||
-               !_repeat && _current >= count) {
+       if (_random && _history.count() >= count && _repeat == REPEAT_NO||
+               _repeat == REPEAT_NO && _current >= count) {
                _history.clear();
                stop();
        } else {
@@ -115,27 +189,13 @@ void Player::prev() {
 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
        switch (newState) {
        case Phonon::PlayingState:
-               if (_state == PLAYER_LOADING) {
-                       _state = PLAYER_PLAYING;
-                       emit stateChanged(_state);
-               }
-               break;
-       case Phonon::StoppedState:
-               break;
-       case Phonon::LoadingState:
-               break;
-       case Phonon::PausedState:
-               if (_state == PLAYER_PLAYING) {
-                       next();
-               } else if (_state == PLAYER_ERROR) {
-                       play();
-               }
-               break;
-       case Phonon::BufferingState:
+               _state = PLAYER_PLAYING;
+               emit stateChanged(_state);
                break;
        case Phonon::ErrorState:
-               _state = PLAYER_ERROR;
-               qDebug() << _player->errorString();
+               play(); // force
+               break;
+       default:
                break;
        }
 }
@@ -155,13 +215,24 @@ void Player::setPlaylist(Playlist playlist) {
        _history.clear();
        _prev_history.clear();
        _queue.clear();
+       QList<int> ids;
+       int count = playlist.tracks().count();
+       for (int i = 0; i < count; i++) {
+               ids.append(i);
+       }
+       _randomizer.setPlaylist(ids);
 }
 
 void Player::seek(int s) {
        _player->seek(s*1000);
+       if (s >= _track.metadata().length()) {
+               next();
+       }
 }
 
 void Player::play() {
+       if (_playlist.tracks().isEmpty())
+               return;
        _state = PLAYER_PLAYING;
        emit stateChanged(_state);
        if (_current == -1) {
@@ -182,7 +253,98 @@ void Player::toggleRandom() {
 }
 
 void Player::toggleRepeat() {
-       _repeat = !_repeat;
+       if (_repeat == REPEAT_NO) {
+               _repeat = REPEAT_ALL;
+       } else if (_repeat == REPEAT_ALL) {
+               _repeat = REPEAT_ONE;
+       } else if (_repeat == REPEAT_ONE) {
+               _repeat = REPEAT_NO;
+       }
        _config.setValue("playback/repeat", _repeat);
 }
 
+void Player::equalizerValue(int band, double *val) {
+       if (_equalizer == NULL) {
+               *val = 0;
+               return;
+       }
+       if (band < 0 || band > 9) {
+               *val = -24;
+               return;
+       }
+       if (_equalizer_enabled) {
+               QList<Phonon::EffectParameter> plist = _equalizer->parameters();
+               QVariant var = _equalizer->parameterValue(plist[band]);
+               *val = var.toDouble();
+       }
+}
+
+void Player::enableEqualizer() {
+       if (_equalizer == NULL)
+               return;
+       _equalizer_enabled = true;
+       _path.insertEffect(_equalizer);
+       Config config;
+       config.setEqualizerEnabled(true);
+}
+
+void Player::disableEqualizer() {
+       if (_equalizer == NULL)
+               return;
+       _equalizer_enabled = false;
+       _path.removeEffect(_equalizer);
+       Config config;
+       config.setEqualizerEnabled(false);
+}
+
+void Player::setEqualizerValue(int band, double value) {
+       if (_equalizer == NULL)
+               return;
+       if (band < 0 || band > 9 || value < -24 || value > 12) {
+               return;
+       }
+       QList<Phonon::EffectParameter> plist = _equalizer->parameters();
+       _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
+       Config config;
+       config.setEqualizerValue(QString("band%1").arg(band), value);
+}
+
+QString Player::artist() {
+       if (_current < 0)
+               return "";
+       return _playlist.tracks().at(_current).metadata().artist();
+}
+
+QString Player::album() {
+       if (_current < 0)
+               return "";
+       return _playlist.tracks().at(_current).metadata().album();
+}
+
+QString Player::title() {
+       if (_current < 0)
+               return "";
+       return _playlist.tracks().at(_current).metadata().title();
+}
+
+Track Player::current() {
+       if (_current >= 0 && _current < _playlist.tracks().count()) {
+               return _playlist.tracks().at(_current);
+       } else {
+               return Track();
+       }
+}
+
+void Player::pause() {
+       if (_state == PLAYER_PLAYING) {
+               _player->pause();
+               _state = PLAYER_PAUSED;
+               emit stateChanged(_state);
+       }
+}
+
+void Player::playIfPaused() {
+       if (_state == PLAYER_PAUSED) {
+               play();
+       }
+}