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