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