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