2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <phonon/MediaSource>
22 #include <phonon/Effect>
23 #include <phonon/BackendCapabilities>
24 #include <phonon/EffectParameter>
25 #include "../config.h"
29 using namespace SomePlayer::Playback;
30 using namespace SomePlayer::DataObjects;
31 using namespace SomePlayer::Storage;
33 inline QList<Track> __sub(QList<Track> one, QList<Track> two, Track three) {
35 foreach (Track t, one) {
36 if (!two.contains(t) && !(t == three)) {
43 Player::Player(QObject *parent) :
46 _awaiting_seek = false;
47 _player = new Phonon::MediaObject(this);
48 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
49 _player->setTickInterval(1000);
51 _equalizer_enabled = false;
52 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
53 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
54 connect(_player, SIGNAL(finished()), this, SLOT(next()));
55 _path = Phonon::createPath(_player, _output);
56 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
57 foreach (Phonon::EffectDescription desc, effects) {
58 if (desc.name() == "equalizer-10bands") {
59 _equalizer = new Phonon::Effect(desc, this);
61 if (config.equalizerEnabled()) {
62 for (int i = 0; i < 10; i++) {
63 QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
64 setEqualizerValue(i, var.toDouble());
70 int seed = QTime::currentTime().msec();
72 _random = _config.getValue("playback/random").toBool();
73 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
76 void Player::setTrackId(int id) {
78 id = id >= _playlist.tracks().size() ? _playlist.tracks().size()-1 : id;
80 _track = _playlist.tracks().at(id);
85 void Player::toggle() {
86 if (_state == PLAYER_PLAYING) { // pause
88 _state = PLAYER_PAUSED;
89 emit stateChanged(_state);
96 if (_state == PLAYER_STOPPED) {
100 lp.position = _player->currentTime() / 1000 - 2;
101 lp.position = lp.position < 0 ? 0 : lp.position;
102 lp.trackId = _playlist.tracks().indexOf(_track);
103 lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
104 emit saveLastPlayed(lp);
106 _state = PLAYER_STOPPED;
107 emit stateChanged(_state);
110 void Player::next() {
111 int count = _playlist.tracks().count();
113 stop(); // empty playlist
116 if (_repeat == REPEAT_ONE) {
122 while (!_queue.isEmpty()) {
123 _new = _queue.takeFirst();
124 if (_playlist.tracks().contains(_new)) {
133 int pos = _playlist.tracks().indexOf(_track) + 1;
134 if (pos >= _playlist.tracks().count()) {
135 if (_repeat == REPEAT_NO) {
139 pos %= _playlist.tracks().count();
142 _track = _playlist.tracks().at(pos);
148 QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
149 int size = sub.count();
156 int pos = qrand() % size;
158 _track = sub.at(pos);
163 void Player::_set_source() {
164 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
165 emit trackChanged(_track);
168 void Player::prev() {
169 if (_history.isEmpty()) {
172 _queue.push_front(_track);
173 _track = _history.takeFirst();
178 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
180 case Phonon::PlayingState:
181 _state = PLAYER_PLAYING;
182 emit stateChanged(_state);
183 if (_awaiting_seek) {
184 _awaiting_seek = false;
185 seek(_awaiting_seek_pos);
188 case Phonon::ErrorState:
196 void Player::_tick(qint64 ticks) {
197 int done = ticks/1000;
198 int all = _track.metadata().length();
199 emit tick(done, all);
201 _track.setCount(_track.count()+1);
202 emit trackDone(_track);
206 void Player::setPlaylist(Playlist playlist) {
207 _playlist = playlist;
211 void Player::seek(int s) {
212 _player->seek(s*1000);
213 if (s >= _track.metadata().length()) {
218 void Player::play() {
219 if (_playlist.tracks().isEmpty())
221 if (_track.source().isEmpty()) {
222 emit startPlaylist();
225 _state = PLAYER_PLAYING;
226 emit stateChanged(_state);
230 void Player::enqueue(int id) {
231 _queue.push_back(_playlist.tracks().at(id));
234 void Player::toggleRandom() {
236 _config.setValue("playback/random", _random);
239 void Player::toggleRepeat() {
240 if (_repeat == REPEAT_NO) {
241 _repeat = REPEAT_ALL;
242 } else if (_repeat == REPEAT_ALL) {
243 _repeat = REPEAT_ONE;
244 } else if (_repeat == REPEAT_ONE) {
247 _config.setValue("playback/repeat", _repeat);
250 void Player::equalizerValue(int band, double *val) {
251 if (_equalizer == NULL) {
255 if (band < 0 || band > 9) {
259 if (_equalizer_enabled) {
260 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
261 QVariant var = _equalizer->parameterValue(plist[band]);
262 *val = var.toDouble();
266 void Player::enableEqualizer() {
267 if (_equalizer == NULL)
269 _equalizer_enabled = true;
270 _path.insertEffect(_equalizer);
272 config.setEqualizerEnabled(true);
275 void Player::disableEqualizer() {
276 if (_equalizer == NULL)
278 _equalizer_enabled = false;
279 _path.removeEffect(_equalizer);
281 config.setEqualizerEnabled(false);
284 void Player::setEqualizerValue(int band, double value) {
285 if (_equalizer == NULL)
287 if (band < 0 || band > 9 || value < -24 || value > 12) {
290 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
291 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
293 config.setEqualizerValue(QString("band%1").arg(band), value);
296 QString Player::artist() {
297 return _track.metadata().artist();
300 QString Player::album() {
301 return _track.metadata().album();
304 QString Player::title() {
305 return _track.metadata().title();
308 Track Player::current() {
312 void Player::pause() {
313 if (_state == PLAYER_PLAYING) {
315 _state = PLAYER_PAUSED;
316 emit stateChanged(_state);
320 void Player::playIfPaused() {
321 if (_state == PLAYER_PAUSED) {
326 void Player::_to_history(Track t) {
327 if (!t.source().isEmpty()) {
328 _history.push_front(t);
331 foreach (Track t, _history) {
335 void Player::_truncate_history() {
336 while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
337 _history.removeLast();