Version bump
[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     AbstractPlayer(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         _state = PLAYER_DONE;
75 }
76
77 void Player::setTrackId(int id) {
78         id = id < 0 ? 0 : id;
79         id = id >= _playlist.tracks().size() ? _playlist.tracks().size()-1 : id;
80         _to_history(_track);
81         _track = _playlist.tracks().at(id);
82         _set_source();
83         play();
84 }
85
86 void Player::toggle() {
87         if (_state == PLAYER_PLAYING) { // pause
88                 _player->pause();
89                 _state = PLAYER_PAUSED;
90                 emit stateChanged(_state);
91         } else { //play
92                 play();
93         }
94 }
95
96 void Player::stop() {
97         if (_state == PLAYER_STOPPED) {
98                 return;
99         }
100         LastPlayed lp;
101         lp.position = _player->currentTime() / 1000 - 2;
102         lp.position = lp.position < 0 ? 0 : lp.position;
103         lp.trackId = _playlist.tracks().indexOf(_track);
104         lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
105         emit saveLastPlayed(lp);
106         _player->stop();
107         _state = PLAYER_STOPPED;
108         emit stateChanged(_state);
109 }
110
111 void Player::next() {
112         int count = _playlist.tracks().count();
113         if (count == 0) {
114                 stop(); // empty playlist
115                 return;
116         }
117         if (_repeat == REPEAT_ONE) {
118                 _set_source();
119                 play();
120                 return;
121         }
122         Track _new;
123         while (!_queue.isEmpty()) {
124                 _new = _queue.takeFirst();
125                 if (_playlist.tracks().contains(_new)) {
126                         _to_history(_track);
127                         _track = _new;
128                         _set_source();
129                         play();
130                         return;
131                 }
132         }
133         if (!_random) {
134                 int pos = _playlist.tracks().indexOf(_track) + 1;
135                 if (pos >= _playlist.tracks().count()) {
136                         if (_repeat == REPEAT_NO) {
137                                 stop();
138                                 return;
139                         }
140                         pos %= _playlist.tracks().count();
141                 }
142                 _to_history(_track);
143                 _track = _playlist.tracks().at(pos);
144                 _set_source();
145                 play();
146                 return;
147         }
148         // random
149         QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
150         int size = sub.count();
151         if (size == 0) {
152                 _to_history(_track);
153                 _set_source();
154                 play();
155                 return;
156         }
157         int pos = qrand() % size;
158         _to_history(_track);
159         _track = sub.at(pos);
160         _set_source();
161         play();
162 }
163
164 void Player::_set_source() {
165         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
166         emit trackChanged(_track);
167 }
168
169 void Player::prev() {
170         if (_history.isEmpty()) {
171                 return;
172         }
173         _queue.push_front(_track);
174         _track = _history.takeFirst();
175         _set_source();
176         play();
177 }
178
179 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
180         switch (newState) {
181         case Phonon::PlayingState:
182                 _state = PLAYER_PLAYING;
183                 emit stateChanged(_state);
184                 if (_awaiting_seek) {
185                         _awaiting_seek = false;
186                         seek(_awaiting_seek_pos);
187                 }
188                 break;
189         case Phonon::ErrorState:
190                 play(); // force
191                 break;
192         default:
193                 break;
194         }
195 }
196
197 void Player::_tick(qint64 ticks) {
198         int done = ticks/1000;
199         int all = _track.metadata().length();
200         emit tick(done, all);
201         if (done+2 == all) {
202                 _track.setCount(_track.count()+1);
203                 emit trackDone(_track);
204         }
205 }
206
207 void Player::setPlaylist(Playlist playlist) {
208         _playlist = playlist;
209         _truncate_history();
210 }
211
212 void Player::seek(int s) {
213         _player->seek(s*1000);
214         if (s >= _track.metadata().length()) {
215                 next();
216         }
217 }
218
219 void Player::play() {
220         if (_playlist.tracks().isEmpty())
221                 return;
222         if (_track.source().isEmpty()) {
223                 emit startPlaylist();
224                 return;
225         }
226         _state = PLAYER_PLAYING;
227         emit stateChanged(_state);
228         _player->play();
229 }
230
231 void Player::enqueue(int id) {
232         _queue.push_back(_playlist.tracks().at(id));
233 }
234
235 void Player::toggleRandom() {
236         _random = !_random;
237         _config.setValue("playback/random", _random);
238 }
239
240 void Player::toggleRepeat() {
241         if (_repeat == REPEAT_NO) {
242                 _repeat = REPEAT_ALL;
243         } else if (_repeat == REPEAT_ALL) {
244                 _repeat = REPEAT_ONE;
245         } else if (_repeat == REPEAT_ONE) {
246                 _repeat = REPEAT_NO;
247         }
248         _config.setValue("playback/repeat", _repeat);
249 }
250
251 void Player::equalizerValue(int band, double *val) {
252         if (_equalizer == NULL) {
253                 *val = 0;
254                 return;
255         }
256         if (band < 0 || band > 9) {
257                 *val = -24;
258                 return;
259         }
260         if (_equalizer_enabled) {
261                 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
262                 QVariant var = _equalizer->parameterValue(plist[band]);
263                 *val = var.toDouble();
264         }
265 }
266
267 void Player::enableEqualizer() {
268         if (_equalizer == NULL)
269                 return;
270         _equalizer_enabled = true;
271         _path.insertEffect(_equalizer);
272         Config config;
273         config.setEqualizerEnabled(true);
274 }
275
276 void Player::disableEqualizer() {
277         if (_equalizer == NULL)
278                 return;
279         _equalizer_enabled = false;
280         _path.removeEffect(_equalizer);
281         Config config;
282         config.setEqualizerEnabled(false);
283 }
284
285 void Player::setEqualizerValue(int band, double value) {
286         if (_equalizer == NULL)
287                 return;
288         if (band < 0 || band > 9 || value < -24 || value > 12) {
289                 return;
290         }
291         QList<Phonon::EffectParameter> plist = _equalizer->parameters();
292         _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
293         Config config;
294         config.setEqualizerValue(QString("band%1").arg(band), value);
295 }
296
297 QString Player::artist() {
298         return _track.metadata().artist();
299 }
300
301 QString Player::album() {
302         return _track.metadata().album();
303 }
304
305 QString Player::title() {
306         return _track.metadata().title();
307 }
308
309 Track Player::current() {
310         return _track;
311 }
312
313 void Player::pause() {
314         if (_state == PLAYER_PLAYING) {
315                 _player->pause();
316                 _state = PLAYER_PAUSED;
317                 emit stateChanged(_state);
318         }
319 }
320
321 void Player::playIfPaused() {
322         if (_state == PLAYER_PAUSED) {
323                 play();
324         }
325 }
326
327 void Player::_to_history(Track t) {
328         if (!t.source().isEmpty()) {
329                 _history.push_front(t);
330         }
331         _truncate_history();
332         foreach (Track t, _history) {
333         }
334 }
335
336 void Player::_truncate_history() {
337         while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
338                 _history.removeLast();
339         }
340 }
341
342 Player::~Player() {
343         stop();
344 }
345
346 QString Player::stateText() {
347         QString return_val;
348         switch (_state) {
349         case PLAYER_PLAYING:
350                 return_val = "playing";
351                 break;
352         case PLAYER_STOPPED:
353                 return_val = "stopped";
354                 break;
355         case PLAYER_PAUSED:
356                 return_val = "paused";
357                 break;
358         case PLAYER_DONE:
359                 return_val = "idle";
360                 break;
361         case PLAYER_LOADING:
362                 return_val = "idle";
363                 break;
364         case PLAYER_ERROR:
365                 return_val = "error";
366                 break;
367         default:
368                 return_val = "unhandled";
369                 break;
370         }
371         return return_val;
372 }