Reworked player engine
[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         _player = new Phonon::MediaObject(this);
47         _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
48         _player->setTickInterval(1000);
49         _equalizer = NULL;
50         _equalizer_enabled = false;
51         connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
52         connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
53         connect(_player, SIGNAL(finished()), this, SLOT(next()));
54         _path = Phonon::createPath(_player, _output);
55         QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
56         foreach (Phonon::EffectDescription desc, effects) {
57                 if (desc.name() == "equalizer-10bands") {
58                         _equalizer = new Phonon::Effect(desc, this);
59                         Config config;
60                         if (config.equalizerEnabled()) {
61                                 for (int i = 0; i < 10; i++) {
62                                         QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
63                                         setEqualizerValue(i, var.toDouble());
64                                 }
65                                 enableEqualizer();
66                         }
67                 }
68         }
69         int seed = QTime::currentTime().msec();
70         qsrand(seed);
71         _random = _config.getValue("playback/random").toBool();
72         _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
73 }
74
75 void Player::setTrackId(int id) {
76         _to_history(_track);
77         _track = _playlist.tracks().at(id);
78         _set_source();
79         play();
80 }
81
82 void Player::toggle() {
83         if (_state == PLAYER_PLAYING) { // pause
84                 _player->pause();
85                 _state = PLAYER_PAUSED;
86                 emit stateChanged(_state);
87         } else { //play
88                 play();
89         }
90 }
91
92 void Player::stop() {
93         _player->stop();
94         _state = PLAYER_STOPPED;
95         emit stateChanged(_state);
96 }
97
98 void Player::next() {
99         int count = _playlist.tracks().count();
100         if (count == 0) {
101                 stop(); // empty playlist
102                 return;
103         }
104         if (_repeat == REPEAT_ONE) {
105                 _set_source();
106                 play();
107                 return;
108         }
109         Track _new;
110         while (!_queue.isEmpty()) {
111                 _new = _queue.takeFirst();
112                 if (_playlist.tracks().contains(_new)) {
113                         _to_history(_track);
114                         _track = _new;
115                         _set_source();
116                         play();
117                         return;
118                 }
119         }
120         if (!_random) {
121                 int pos = _playlist.tracks().indexOf(_track) + 1;
122                 if (pos >= _playlist.tracks().count()) {
123                         if (_repeat == REPEAT_NO) {
124                                 stop();
125                         }
126                         pos %= _playlist.tracks().count();
127                 }
128                 _to_history(_track);
129                 _track = _playlist.tracks().at(pos);
130                 _set_source();
131                 play();
132                 return;
133         }
134         // random
135         QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
136         int size = sub.count();
137         if (size == 0) {
138                 _to_history(_track);
139                 _set_source();
140                 play();
141                 return;
142         }
143         int pos = qrand() % size;
144         _to_history(_track);
145         _track = sub.at(pos);
146         _set_source();
147         play();
148 }
149
150 void Player::_set_source() {
151         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
152         emit trackChanged(_track);
153 }
154
155 void Player::prev() {
156         if (_history.isEmpty()) {
157                 _set_source();
158                 play();
159                 return;
160         }
161         _queue.push_front(_track);
162         _track = _history.takeFirst();
163         _set_source();
164         play();
165 }
166
167 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
168         switch (newState) {
169         case Phonon::PlayingState:
170                 _state = PLAYER_PLAYING;
171                 emit stateChanged(_state);
172                 break;
173         case Phonon::ErrorState:
174                 play(); // force
175                 break;
176         default:
177                 break;
178         }
179 }
180
181 void Player::_tick(qint64 ticks) {
182         int done = ticks/1000;
183         int all = _track.metadata().length();
184         emit tick(done, all);
185         if (done+2 == all) {
186                 _track.setCount(_track.count()+1);
187                 emit trackDone(_track);
188         }
189 }
190
191 void Player::setPlaylist(Playlist playlist) {
192         _playlist = playlist;
193         _truncate_history();
194 }
195
196 void Player::seek(int s) {
197         _player->seek(s*1000);
198         if (s >= _track.metadata().length()) {
199                 next();
200         }
201 }
202
203 void Player::play() {
204         if (_playlist.tracks().isEmpty())
205                 return;
206         if (_track.source().isEmpty()) {
207                 next();
208                 return;
209         }
210         _state = PLAYER_PLAYING;
211         emit stateChanged(_state);
212         _player->play();
213 }
214
215 void Player::enqueue(int id) {
216         _queue.push_back(_playlist.tracks().at(id));
217 }
218
219 void Player::toggleRandom() {
220         _random = !_random;
221         _config.setValue("playback/random", _random);
222 }
223
224 void Player::toggleRepeat() {
225         if (_repeat == REPEAT_NO) {
226                 _repeat = REPEAT_ALL;
227         } else if (_repeat == REPEAT_ALL) {
228                 _repeat = REPEAT_ONE;
229         } else if (_repeat == REPEAT_ONE) {
230                 _repeat = REPEAT_NO;
231         }
232         _config.setValue("playback/repeat", _repeat);
233 }
234
235 void Player::equalizerValue(int band, double *val) {
236         if (_equalizer == NULL) {
237                 *val = 0;
238                 return;
239         }
240         if (band < 0 || band > 9) {
241                 *val = -24;
242                 return;
243         }
244         if (_equalizer_enabled) {
245                 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
246                 QVariant var = _equalizer->parameterValue(plist[band]);
247                 *val = var.toDouble();
248         }
249 }
250
251 void Player::enableEqualizer() {
252         if (_equalizer == NULL)
253                 return;
254         _equalizer_enabled = true;
255         _path.insertEffect(_equalizer);
256         Config config;
257         config.setEqualizerEnabled(true);
258 }
259
260 void Player::disableEqualizer() {
261         if (_equalizer == NULL)
262                 return;
263         _equalizer_enabled = false;
264         _path.removeEffect(_equalizer);
265         Config config;
266         config.setEqualizerEnabled(false);
267 }
268
269 void Player::setEqualizerValue(int band, double value) {
270         if (_equalizer == NULL)
271                 return;
272         if (band < 0 || band > 9 || value < -24 || value > 12) {
273                 return;
274         }
275         QList<Phonon::EffectParameter> plist = _equalizer->parameters();
276         _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
277         Config config;
278         config.setEqualizerValue(QString("band%1").arg(band), value);
279 }
280
281 QString Player::artist() {
282         return _track.metadata().artist();
283 }
284
285 QString Player::album() {
286         return _track.metadata().album();
287 }
288
289 QString Player::title() {
290         return _track.metadata().title();
291 }
292
293 Track Player::current() {
294         return _track;
295 }
296
297 void Player::pause() {
298         if (_state == PLAYER_PLAYING) {
299                 _player->pause();
300                 _state = PLAYER_PAUSED;
301                 emit stateChanged(_state);
302         }
303 }
304
305 void Player::playIfPaused() {
306         if (_state == PLAYER_PAUSED) {
307                 play();
308         }
309 }
310
311 void Player::_to_history(Track t) {
312         if (!t.source().isEmpty()) {
313                 _history.push_front(t);
314         }
315         _truncate_history();
316         foreach (Track t, _history) {
317         }
318 }
319
320 void Player::_truncate_history() {
321         while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
322                 _history.removeLast();
323         }
324 }