2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
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.
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.
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.
21 #include <phonon/MediaSource>
22 #include <phonon/Effect>
23 #include <phonon/BackendCapabilities>
24 #include <phonon/EffectParameter>
25 #include "../config.h"
29 using namespace SomePlayer::Playback;
30 using namespace SomePlayer::DataObjects;
31 using namespace SomePlayer::Storage;
33 inline QList<Track> __sub(QList<Track> one, QList<Track> two, Track three) {
35 foreach (Track t, one) {
36 if (!two.contains(t) && !(t == three)) {
43 Player::Player(QObject *parent) :
44 AbstractPlayer(parent)
46 _awaiting_seek = false;
47 _player = new Phonon::MediaObject(this);
48 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
49 _player->setTickInterval(1000);
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);
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());
70 int seed = QTime::currentTime().msec();
72 _random = _config.getValue("playback/random").toBool();
73 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
77 void Player::setTrackId(int id) {
79 id = id >= _playlist.tracks().size() ? _playlist.tracks().size()-1 : id;
81 _track = _playlist.tracks().at(id);
86 void Player::toggle() {
87 if (_state == PLAYER_PLAYING) { // pause
89 _state = PLAYER_PAUSED;
90 emit stateChanged(_state);
97 if (_state == PLAYER_STOPPED) {
101 lp.position = _player->currentTime() / 1000 - 2;
102 lp.position = lp.position < 0 ? 0 : lp.position;
103 lp.trackId = _playlist.tracks().indexOf(_track);
104 lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
105 emit saveLastPlayed(lp);
107 _state = PLAYER_STOPPED;
108 emit stateChanged(_state);
111 void Player::next() {
112 int count = _playlist.tracks().count();
114 stop(); // empty playlist
117 if (_repeat == REPEAT_ONE) {
123 while (!_queue.isEmpty()) {
124 _new = _queue.takeFirst();
125 if (_playlist.tracks().contains(_new)) {
134 int pos = _playlist.tracks().indexOf(_track) + 1;
135 if (pos >= _playlist.tracks().count()) {
136 if (_repeat == REPEAT_NO) {
140 pos %= _playlist.tracks().count();
143 _track = _playlist.tracks().at(pos);
149 QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
150 int size = sub.count();
157 int pos = qrand() % size;
159 _track = sub.at(pos);
164 void Player::_set_source() {
165 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
166 emit trackChanged(_track);
169 void Player::prev() {
170 if (_history.isEmpty()) {
173 _queue.push_front(_track);
174 _track = _history.takeFirst();
179 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
181 case Phonon::PlayingState:
182 _state = PLAYER_PLAYING;
183 emit stateChanged(_state);
184 if (_awaiting_seek) {
185 _awaiting_seek = false;
186 seek(_awaiting_seek_pos);
189 case Phonon::ErrorState:
197 void Player::_tick(qint64 ticks) {
198 int done = ticks/1000;
199 int all = _track.metadata().length();
200 emit tick(done, all);
202 _track.setCount(_track.count()+1);
203 emit trackDone(_track);
207 void Player::setPlaylist(Playlist playlist) {
208 _playlist = playlist;
212 void Player::seek(int s) {
213 _player->seek(s*1000);
214 if (s >= _track.metadata().length()) {
219 void Player::play() {
220 if (_playlist.tracks().isEmpty())
222 if (_track.source().isEmpty()) {
223 emit startPlaylist();
226 _state = PLAYER_PLAYING;
227 emit stateChanged(_state);
231 void Player::enqueue(int id) {
232 _queue.push_back(_playlist.tracks().at(id));
235 void Player::toggleRandom() {
237 _config.setValue("playback/random", _random);
240 void Player::toggleRepeat() {
241 if (_repeat == REPEAT_NO) {
242 _repeat = REPEAT_ALL;
243 } else if (_repeat == REPEAT_ALL) {
244 _repeat = REPEAT_ONE;
245 } else if (_repeat == REPEAT_ONE) {
248 _config.setValue("playback/repeat", _repeat);
251 void Player::equalizerValue(int band, double *val) {
252 if (_equalizer == NULL) {
256 if (band < 0 || band > 9) {
260 if (_equalizer_enabled) {
261 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
262 QVariant var = _equalizer->parameterValue(plist[band]);
263 *val = var.toDouble();
267 void Player::enableEqualizer() {
268 if (_equalizer == NULL)
270 _equalizer_enabled = true;
271 _path.insertEffect(_equalizer);
273 config.setEqualizerEnabled(true);
276 void Player::disableEqualizer() {
277 if (_equalizer == NULL)
279 _equalizer_enabled = false;
280 _path.removeEffect(_equalizer);
282 config.setEqualizerEnabled(false);
285 void Player::setEqualizerValue(int band, double value) {
286 if (_equalizer == NULL)
288 if (band < 0 || band > 9 || value < -24 || value > 12) {
291 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
292 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
294 config.setEqualizerValue(QString("band%1").arg(band), value);
297 QString Player::artist() {
298 return _track.metadata().artist();
301 QString Player::album() {
302 return _track.metadata().album();
305 QString Player::title() {
306 return _track.metadata().title();
309 Track Player::current() {
313 void Player::pause() {
314 if (_state == PLAYER_PLAYING) {
316 _state = PLAYER_PAUSED;
317 emit stateChanged(_state);
321 void Player::playIfPaused() {
322 if (_state == PLAYER_PAUSED) {
327 void Player::_to_history(Track t) {
328 if (!t.source().isEmpty()) {
329 _history.push_front(t);
332 foreach (Track t, _history) {
336 void Player::_truncate_history() {
337 while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
338 _history.removeLast();
346 QString Player::stateText() {
350 return_val = "playing";
353 return_val = "stopped";
356 return_val = "paused";
365 return_val = "error";
368 return_val = "unhandled";