Fixed broken image issue in taglib
[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 = (RepeatRule) _config.getValue("playback/repeat").toInt();
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         if (_repeat == REPEAT_ONE) {
137                 _set_source();
138                 play();
139                 return;
140         }
141         _history.push(_current % count);
142         if (!_queue.isEmpty()) {
143                 _current = _queue.dequeue();
144         } else if (!_prev_history.isEmpty()) {
145                 _current = _prev_history.pop();
146         } else {
147                 if (_random) {
148                         _current = _randomizer.next();
149                 } else {
150                         _current = _current + 1;
151                 }
152         }
153         if (_random && _history.count() >= count && _repeat == REPEAT_NO||
154                 _repeat == REPEAT_NO && _current >= count) {
155                 _history.clear();
156                 stop();
157         } else {
158                 _current %= count;
159                 _track = _playlist.tracks().at(_current);
160                 _set_source();
161                 play();
162         }
163 }
164
165 void Player::_set_source() {
166         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
167         emit trackChanged(_track);
168 }
169
170 void Player::prev() {
171         if (_history.count() > 0) {
172                 _prev_history.push(_current);
173                 _current = _history.pop();
174                 _track = _playlist.tracks().at(_current);
175         }
176         _set_source();
177         play();
178 }
179
180 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
181         switch (newState) {
182         case Phonon::PlayingState:
183                 _state = PLAYER_PLAYING;
184                 emit stateChanged(_state);
185                 break;
186         case Phonon::StoppedState:
187                 break;
188         case Phonon::LoadingState:
189                 break;
190         case Phonon::PausedState:
191                 if (_state == PLAYER_PLAYING) {
192                         next();
193                 } else if (_state == PLAYER_ERROR) {
194                         play();
195                 }
196                 break;
197         case Phonon::BufferingState:
198                 break;
199         case Phonon::ErrorState:
200                 play(); // force
201 //              _state = PLAYER_ERROR;
202                 break;
203         }
204 }
205
206 void Player::_tick(qint64 ticks) {
207         int done = ticks/1000;
208         int all = _track.metadata().length();
209         emit tick(done, all);
210         if (done+2 == all) {
211                 _track.setCount(_track.count()+1);
212                 emit trackDone(_track);
213         }
214 }
215
216 void Player::setPlaylist(Playlist playlist) {
217         _playlist = playlist;
218         _history.clear();
219         _prev_history.clear();
220         _queue.clear();
221         QList<int> ids;
222         int count = playlist.tracks().count();
223         for (int i = 0; i < count; i++) {
224                 ids.append(i);
225         }
226         _randomizer.setPlaylist(ids);
227 }
228
229 void Player::seek(int s) {
230         _player->seek(s*1000);
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.setValue("equalizer/equalizer", "enabled");
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.setValue("equalizer/equalizer", "disabled");
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.setValue(QString("equalizer/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 }