Autoresuming
[someplayer] / src / player / player.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "player.h"
21 #include <phonon/MediaSource>
22 #include <phonon/Effect>
23 #include <phonon/BackendCapabilities>
24 #include <phonon/EffectParameter>
25 #include "../config.h"
26 #include <QTime>
27 #include <QTimer>
28
29 using namespace SomePlayer::Playback;
30 using namespace SomePlayer::DataObjects;
31 using namespace SomePlayer::Storage;
32
33 inline QList<Track> __sub(QList<Track> one, QList<Track> two, Track three) {
34         QList<Track> result;
35         foreach (Track t, one) {
36                 if (!two.contains(t) && !(t == three)) {
37                         result.append(t);
38                 }
39         }
40         return result;
41 }
42
43 Player::Player(QObject *parent) :
44     QObject(parent)
45 {
46         _awaiting_seek = false;
47         _player = new Phonon::MediaObject(this);
48         _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
49         _player->setTickInterval(1000);
50         _equalizer = NULL;
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);
60                         Config config;
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());
65                                 }
66                                 enableEqualizer();
67                         }
68                 }
69         }
70         int seed = QTime::currentTime().msec();
71         qsrand(seed);
72         _random = _config.getValue("playback/random").toBool();
73         _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
74 }
75
76 void Player::setTrackId(int id) {
77         _to_history(_track);
78         _track = _playlist.tracks().at(id);
79         _set_source();
80         play();
81 }
82
83 void Player::toggle() {
84         if (_state == PLAYER_PLAYING) { // pause
85                 _player->pause();
86                 _state = PLAYER_PAUSED;
87                 emit stateChanged(_state);
88         } else { //play
89                 play();
90         }
91 }
92
93 void Player::stop() {
94         if (_state == PLAYER_STOPPED) {
95                 return;
96         }
97         LastPlayed lp;
98         lp.position = _player->currentTime() / 1000 - 2;
99         lp.position = lp.position < 0 ? 0 : lp.position;
100         lp.trackId = _playlist.tracks().indexOf(_track);
101         lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
102         emit saveLastPlayed(lp);
103         _player->stop();
104         _state = PLAYER_STOPPED;
105         emit stateChanged(_state);
106 }
107
108 void Player::next() {
109         int count = _playlist.tracks().count();
110         if (count == 0) {
111                 stop(); // empty playlist
112                 return;
113         }
114         if (_repeat == REPEAT_ONE) {
115                 _set_source();
116                 play();
117                 return;
118         }
119         Track _new;
120         while (!_queue.isEmpty()) {
121                 _new = _queue.takeFirst();
122                 if (_playlist.tracks().contains(_new)) {
123                         _to_history(_track);
124                         _track = _new;
125                         _set_source();
126                         play();
127                         return;
128                 }
129         }
130         if (!_random) {
131                 int pos = _playlist.tracks().indexOf(_track) + 1;
132                 if (pos >= _playlist.tracks().count()) {
133                         if (_repeat == REPEAT_NO) {
134                                 stop();
135                         }
136                         pos %= _playlist.tracks().count();
137                 }
138                 _to_history(_track);
139                 _track = _playlist.tracks().at(pos);
140                 _set_source();
141                 play();
142                 return;
143         }
144         // random
145         QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
146         int size = sub.count();
147         if (size == 0) {
148                 _to_history(_track);
149                 _set_source();
150                 play();
151                 return;
152         }
153         int pos = qrand() % size;
154         _to_history(_track);
155         _track = sub.at(pos);
156         _set_source();
157         play();
158 }
159
160 void Player::_set_source() {
161         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
162         emit trackChanged(_track);
163 }
164
165 void Player::prev() {
166         if (_history.isEmpty()) {
167                 _set_source();
168                 play();
169                 return;
170         }
171         _queue.push_front(_track);
172         _track = _history.takeFirst();
173         _set_source();
174         play();
175 }
176
177 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
178         switch (newState) {
179         case Phonon::PlayingState:
180                 _state = PLAYER_PLAYING;
181                 emit stateChanged(_state);
182                 if (_awaiting_seek) {
183                         _awaiting_seek = false;
184                         seek(_awaiting_seek_pos);
185                 }
186                 break;
187         case Phonon::ErrorState:
188                 play(); // force
189                 break;
190         default:
191                 break;
192         }
193 }
194
195 void Player::_tick(qint64 ticks) {
196         int done = ticks/1000;
197         int all = _track.metadata().length();
198         emit tick(done, all);
199         if (done+2 == all) {
200                 _track.setCount(_track.count()+1);
201                 emit trackDone(_track);
202         }
203 }
204
205 void Player::setPlaylist(Playlist playlist) {
206         _playlist = playlist;
207         _truncate_history();
208 }
209
210 void Player::seek(int s) {
211         _player->seek(s*1000);
212         if (s >= _track.metadata().length()) {
213                 next();
214         }
215 }
216
217 void Player::play() {
218         if (_playlist.tracks().isEmpty())
219                 return;
220         if (_track.source().isEmpty()) {
221                 emit startPlaylist();
222                 return;
223         }
224         _state = PLAYER_PLAYING;
225         emit stateChanged(_state);
226         _player->play();
227 }
228
229 void Player::enqueue(int id) {
230         _queue.push_back(_playlist.tracks().at(id));
231 }
232
233 void Player::toggleRandom() {
234         _random = !_random;
235         _config.setValue("playback/random", _random);
236 }
237
238 void Player::toggleRepeat() {
239         if (_repeat == REPEAT_NO) {
240                 _repeat = REPEAT_ALL;
241         } else if (_repeat == REPEAT_ALL) {
242                 _repeat = REPEAT_ONE;
243         } else if (_repeat == REPEAT_ONE) {
244                 _repeat = REPEAT_NO;
245         }
246         _config.setValue("playback/repeat", _repeat);
247 }
248
249 void Player::equalizerValue(int band, double *val) {
250         if (_equalizer == NULL) {
251                 *val = 0;
252                 return;
253         }
254         if (band < 0 || band > 9) {
255                 *val = -24;
256                 return;
257         }
258         if (_equalizer_enabled) {
259                 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
260                 QVariant var = _equalizer->parameterValue(plist[band]);
261                 *val = var.toDouble();
262         }
263 }
264
265 void Player::enableEqualizer() {
266         if (_equalizer == NULL)
267                 return;
268         _equalizer_enabled = true;
269         _path.insertEffect(_equalizer);
270         Config config;
271         config.setEqualizerEnabled(true);
272 }
273
274 void Player::disableEqualizer() {
275         if (_equalizer == NULL)
276                 return;
277         _equalizer_enabled = false;
278         _path.removeEffect(_equalizer);
279         Config config;
280         config.setEqualizerEnabled(false);
281 }
282
283 void Player::setEqualizerValue(int band, double value) {
284         if (_equalizer == NULL)
285                 return;
286         if (band < 0 || band > 9 || value < -24 || value > 12) {
287                 return;
288         }
289         QList<Phonon::EffectParameter> plist = _equalizer->parameters();
290         _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
291         Config config;
292         config.setEqualizerValue(QString("band%1").arg(band), value);
293 }
294
295 QString Player::artist() {
296         return _track.metadata().artist();
297 }
298
299 QString Player::album() {
300         return _track.metadata().album();
301 }
302
303 QString Player::title() {
304         return _track.metadata().title();
305 }
306
307 Track Player::current() {
308         return _track;
309 }
310
311 void Player::pause() {
312         if (_state == PLAYER_PLAYING) {
313                 _player->pause();
314                 _state = PLAYER_PAUSED;
315                 emit stateChanged(_state);
316         }
317 }
318
319 void Player::playIfPaused() {
320         if (_state == PLAYER_PAUSED) {
321                 play();
322         }
323 }
324
325 void Player::_to_history(Track t) {
326         if (!t.source().isEmpty()) {
327                 _history.push_front(t);
328         }
329         _truncate_history();
330         foreach (Track t, _history) {
331         }
332 }
333
334 void Player::_truncate_history() {
335         while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
336                 _history.removeLast();
337         }
338 }
339
340 Player::~Player() {
341         stop();
342 }