Player factory
[someplayer] / src / player / player.cpp
index 80fc64e..81c2525 100644 (file)
 #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 (_current == _rand.count()) {
-               _shuffle();
-               _current = 0;
-               res = next();
-       } else {
-               res = _rand.at(_current);
-       }
-       ++_current;
-       return res;
-}
-
-void Randomizer::setPlaylist(QList<int> pl) {
-       _playlist = pl;
-       _current = 0;
-       _shuffle();
-}
-
-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;
+inline QList<Track> __sub(QList<Track> one, QList<Track> two, Track three) {
+       QList<Track> result;
+       foreach (Track t, one) {
+               if (!two.contains(t) && !(t == three)) {
+                       result.append(t);
+               }
        }
+       return result;
 }
 
 Player::Player(QObject *parent) :
-    QObject(parent)
+    AbstractPlayer(parent)
 {
+       _awaiting_seek = false;
        _player = new Phonon::MediaObject(this);
        _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        _player->setTickInterval(1000);
@@ -73,41 +51,36 @@ Player::Player(QObject *parent) :
        _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)));
+       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.getValue("equalizer/equalizer").toString() == "enabled") {
+                       if (config.equalizerEnabled()) {
                                for (int i = 0; i < 10; i++) {
-                                       QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
+                                       QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
                                        setEqualizerValue(i, var.toDouble());
                                }
                                enableEqualizer();
-                       } else if (config.getValue("equalizer/equalizer") == "") {
-                               for (int i = 0; i < 10; i++) {
-                                       config.setValue(QString("equalizer/band%1").arg(i), 0);
-                               }
                        }
                }
        }
        int seed = QTime::currentTime().msec();
        qsrand(seed);
        _random = _config.getValue("playback/random").toBool();
-       _repeat = _config.getValue("playback/repeat").toBool();
-       _current = -1;
+       _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
+       _state = PLAYER_DONE;
 }
 
 void Player::setTrackId(int id) {
-       _current = id;
-       if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
-               _history.push(_current);
-       }
-       _track = _playlist.tracks().at(_current);
+       id = id < 0 ? 0 : id;
+       id = id >= _playlist.tracks().size() ? _playlist.tracks().size()-1 : id;
+       _to_history(_track);
+       _track = _playlist.tracks().at(id);
        _set_source();
-       _state = PLAYER_LOADING;
-       emit stateChanged(_state);
+       play();
 }
 
 void Player::toggle() {
@@ -121,6 +94,15 @@ void Player::toggle() {
 }
 
 void Player::stop() {
+       if (_state == PLAYER_STOPPED) {
+               return;
+       }
+       LastPlayed lp;
+       lp.position = _player->currentTime() / 1000 - 2;
+       lp.position = lp.position < 0 ? 0 : lp.position;
+       lp.trackId = _playlist.tracks().indexOf(_track);
+       lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
+       emit saveLastPlayed(lp);
        _player->stop();
        _state = PLAYER_STOPPED;
        emit stateChanged(_state);
@@ -132,28 +114,51 @@ void Player::next() {
                stop(); // empty playlist
                return;
        }
-       _history.push(_current % count);
-       if (!_queue.isEmpty()) {
-               _current = _queue.dequeue();
-       } else if (!_prev_history.isEmpty()) {
-               _current = _prev_history.pop();
-       } else {
-               if (_random) {
-                       _current = _randomizer.next();
-               } else {
-                       _current = _current + 1;
+       if (_repeat == REPEAT_ONE) {
+               _set_source();
+               play();
+               return;
+       }
+       Track _new;
+       while (!_queue.isEmpty()) {
+               _new = _queue.takeFirst();
+               if (_playlist.tracks().contains(_new)) {
+                       _to_history(_track);
+                       _track = _new;
+                       _set_source();
+                       play();
+                       return;
+               }
+       }
+       if (!_random) {
+               int pos = _playlist.tracks().indexOf(_track) + 1;
+               if (pos >= _playlist.tracks().count()) {
+                       if (_repeat == REPEAT_NO) {
+                               stop();
+                               return;
+                       }
+                       pos %= _playlist.tracks().count();
                }
+               _to_history(_track);
+               _track = _playlist.tracks().at(pos);
+               _set_source();
+               play();
+               return;
        }
-       if (_random && _history.count() >= count && !_repeat ||
-               !_repeat && _current >= count) {
-               _history.clear();
-               stop();
-       } else {
-               _current %= count;
-               _track = _playlist.tracks().at(_current);
+       // random
+       QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
+       int size = sub.count();
+       if (size == 0) {
+               _to_history(_track);
                _set_source();
                play();
+               return;
        }
+       int pos = qrand() % size;
+       _to_history(_track);
+       _track = sub.at(pos);
+       _set_source();
+       play();
 }
 
 void Player::_set_source() {
@@ -162,11 +167,11 @@ void Player::_set_source() {
 }
 
 void Player::prev() {
-       if (_history.count() > 0) {
-               _prev_history.push(_current);
-               _current = _history.pop();
-               _track = _playlist.tracks().at(_current);
+       if (_history.isEmpty()) {
+               return;
        }
+       _queue.push_front(_track);
+       _track = _history.takeFirst();
        _set_source();
        play();
 }
@@ -174,27 +179,17 @@ 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();
+               _state = PLAYER_PLAYING;
+               emit stateChanged(_state);
+               if (_awaiting_seek) {
+                       _awaiting_seek = false;
+                       seek(_awaiting_seek_pos);
                }
                break;
-       case Phonon::BufferingState:
-               break;
        case Phonon::ErrorState:
                play(); // force
-//             _state = PLAYER_ERROR;
+               break;
+       default:
                break;
        }
 }
@@ -211,36 +206,30 @@ void Player::_tick(qint64 ticks) {
 
 void Player::setPlaylist(Playlist playlist) {
        _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);
+       _truncate_history();
 }
 
 void Player::seek(int s) {
        _player->seek(s*1000);
+       if (s >= _track.metadata().length()) {
+               next();
+       }
 }
 
 void Player::play() {
        if (_playlist.tracks().isEmpty())
                return;
+       if (_track.source().isEmpty()) {
+               emit startPlaylist();
+               return;
+       }
        _state = PLAYER_PLAYING;
        emit stateChanged(_state);
-       if (_current == -1) {
-               _current = 0;
-               _track = _playlist.tracks().at(0);
-               _set_source();
-       }
        _player->play();
 }
 
 void Player::enqueue(int id) {
-       _queue.enqueue(id);
+       _queue.push_back(_playlist.tracks().at(id));
 }
 
 void Player::toggleRandom() {
@@ -249,14 +238,16 @@ 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::setVolume(int v) {
-       _output->setVolume(v*0.01);
-}
-
 void Player::equalizerValue(int band, double *val) {
        if (_equalizer == NULL) {
                *val = 0;
@@ -279,7 +270,7 @@ void Player::enableEqualizer() {
        _equalizer_enabled = true;
        _path.insertEffect(_equalizer);
        Config config;
-       config.setValue("equalizer/equalizer", "enabled");
+       config.setEqualizerEnabled(true);
 }
 
 void Player::disableEqualizer() {
@@ -288,7 +279,7 @@ void Player::disableEqualizer() {
        _equalizer_enabled = false;
        _path.removeEffect(_equalizer);
        Config config;
-       config.setValue("equalizer/equalizer", "disabled");
+       config.setEqualizerEnabled(false);
 }
 
 void Player::setEqualizerValue(int band, double value) {
@@ -300,23 +291,82 @@ void Player::setEqualizerValue(int band, double value) {
        QList<Phonon::EffectParameter> plist = _equalizer->parameters();
        _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
        Config config;
-       config.setValue(QString("equalizer/band%1").arg(band), value);
+       config.setEqualizerValue(QString("band%1").arg(band), value);
 }
 
 QString Player::artist() {
-       if (_current < 0)
-               return "";
-       return _playlist.tracks().at(_current).metadata().artist();
+       return _track.metadata().artist();
 }
 
 QString Player::album() {
-       if (_current < 0)
-               return "";
-       return _playlist.tracks().at(_current).metadata().album();
+       return _track.metadata().album();
 }
 
 QString Player::title() {
-       if (_current < 0)
-               return "";
-       return _playlist.tracks().at(_current).metadata().title();
+       return _track.metadata().title();
+}
+
+Track Player::current() {
+       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();
+       }
+}
+
+void Player::_to_history(Track t) {
+       if (!t.source().isEmpty()) {
+               _history.push_front(t);
+       }
+       _truncate_history();
+       foreach (Track t, _history) {
+       }
+}
+
+void Player::_truncate_history() {
+       while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
+               _history.removeLast();
+       }
+}
+
+Player::~Player() {
+       stop();
+}
+
+QString Player::stateText() {
+       QString return_val;
+       switch (_state) {
+       case PLAYER_PLAYING:
+               return_val = "playing";
+               break;
+       case PLAYER_STOPPED:
+               return_val = "stopped";
+               break;
+       case PLAYER_PAUSED:
+               return_val = "paused";
+               break;
+       case PLAYER_DONE:
+               return_val = "idle";
+               break;
+       case PLAYER_LOADING:
+               return_val = "idle";
+               break;
+       case PLAYER_ERROR:
+               return_val = "error";
+               break;
+       default:
+               return_val = "unhandled";
+               break;
+       }
+       return return_val;
 }