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