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