X-Git-Url: http://git.maemo.org/git/?p=someplayer;a=blobdiff_plain;f=src%2Fplayer%2Fplayer.cpp;h=05d37cd65d86cb542974eb9d43938082cc4440f2;hp=ee42116f303bd031b4f05e44330d1f94a08c7041;hb=68e7e55646f7caf47dd678a5f2f51644ddacd751;hpb=dd00ef6be1a5989020c9ae2c1fa6c17910b260d1 diff --git a/src/player/player.cpp b/src/player/player.cpp index ee42116..05d37cd 100644 --- a/src/player/player.cpp +++ b/src/player/player.cpp @@ -1,8 +1,28 @@ +/* + * SomePlayer - An alternate music player for Maemo 5 + * Copyright (C) 2010 Nikolay (somebody) Tischenko + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + #include "player.h" #include using namespace SomePlayer::Playback; using namespace SomePlayer::DataObjects; +using namespace SomePlayer::Storage; Player::Player(QObject *parent) : QObject(parent) @@ -14,23 +34,31 @@ Player::Player(QObject *parent) : connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64))); Phonon::createPath(_player, _output); int seed = reinterpret_cast (_player); - srand(seed); - _random = false; - _repeat = false; + qsrand(seed); + _random = _config.getValue("playback/random").toBool(); + _repeat = _config.getValue("playback/repeat").toBool(); + _current = -1; } void Player::setTrackId(int id) { _current = id; - _history.push(_current); + if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) { + _history.push(_current); + } + _track = _playlist.tracks().at(_current); _set_source(); _state = PLAYER_LOADING; emit stateChanged(_state); } -void Player::play() { - _player->play(); - _state = PLAYER_PLAYING; - emit stateChanged(_state); +void Player::toggle() { + if (_state == PLAYER_PLAYING) { // pause + _player->pause(); + _state = PLAYER_PAUSED; + emit stateChanged(_state); + } else { //play + play(); + } } void Player::stop() { @@ -39,44 +67,52 @@ void Player::stop() { emit stateChanged(_state); } -void Player::pause() { - _player->pause(); - _state = PLAYER_PAUSED; - emit stateChanged(_state); -} - void Player::next() { - _history.push(_current); - if (_random) { - _current = rand() % _playlist.tracks().count(); + int count = _playlist.tracks().count(); + if (count == 0) { + stop(); // empty playlist + return; + } + _history.push(_current % count); + if (!_queue.isEmpty()) { + _current = _queue.dequeue(); + } else if (!_prev_history.isEmpty()) { + _current = _prev_history.pop(); } else { - _current = (_current + 1) % _playlist.tracks().count(); + if (_random) { + _current = (count + (qrand() + qrand() + qrand()) % count) % count; + } else { + _current = _current + 1; + } } - if (_history.count()-1 == _playlist.tracks().count() && !_repeat) { + if (_random && _history.count() >= count && !_repeat || + !_repeat && _current >= count) { _history.clear(); stop(); } else { + _current %= count; + _track = _playlist.tracks().at(_current); _set_source(); play(); } } void Player::_set_source() { - qDebug() << "id: " << _current << " all: " << _playlist.tracks().count(); - Track track = _playlist.tracks().at(_current); - _player->setCurrentSource(Phonon::MediaSource(track.source())); - emit trackChanged(track); + _player->setCurrentSource(Phonon::MediaSource(_track.source())); + emit trackChanged(_track); } void Player::prev() { - if (_history.count() > 0) + if (_history.count() > 0) { + _prev_history.push(_current); _current = _history.pop(); + _track = _playlist.tracks().at(_current); + } _set_source(); play(); } -void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) { - qDebug() << "state changed: " << oldState << "->" << newState; +void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) { switch (newState) { case Phonon::PlayingState: if (_state == PLAYER_LOADING) { @@ -105,15 +141,53 @@ void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) { } void Player::_tick(qint64 ticks) { - emit tick(ticks/1000, _playlist.tracks().at(_current).metadata().length()); + int done = ticks/1000; + int all = _track.metadata().length(); + emit tick(done, all); + if (done+2 == all) { + _track.setCount(_track.count()+1); + emit trackDone(_track); + } } void Player::setPlaylist(Playlist playlist) { _playlist = playlist; _history.clear(); + _prev_history.clear(); + _queue.clear(); } void Player::seek(int s) { - qDebug() << "seeking " << s; _player->seek(s*1000); } + +void Player::play() { + if (_playlist.tracks().isEmpty()) + 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); +} + +void Player::toggleRandom() { + _random = !_random; + _config.setValue("playback/random", _random); +} + +void Player::toggleRepeat() { + _repeat = !_repeat; + _config.setValue("playback/repeat", _repeat); +} + +void Player::setVolume(int v) { + _output->setVolume(v*0.01); +}