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) :
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();
76 void Player::setTrackId(int id) {
78 _track = _playlist.tracks().at(id);
83 void Player::toggle() {
84 if (_state == PLAYER_PLAYING) { // pause
86 _state = PLAYER_PAUSED;
87 emit stateChanged(_state);
94 if (_state == PLAYER_STOPPED) {
98 lp.position = _player->currentTime() / 1000 - 2;
99 lp.position = lp.position < 0 ? 0 : lp.position;
100 lp.trackId = _playlist.tracks().indexOf(_track);
101 lp.trackId = lp.trackId < 0 ? 0 : lp.trackId;
102 emit saveLastPlayed(lp);
104 _state = PLAYER_STOPPED;
105 emit stateChanged(_state);
108 void Player::next() {
109 int count = _playlist.tracks().count();
111 stop(); // empty playlist
114 if (_repeat == REPEAT_ONE) {
120 while (!_queue.isEmpty()) {
121 _new = _queue.takeFirst();
122 if (_playlist.tracks().contains(_new)) {
131 int pos = _playlist.tracks().indexOf(_track) + 1;
132 if (pos >= _playlist.tracks().count()) {
133 if (_repeat == REPEAT_NO) {
136 pos %= _playlist.tracks().count();
139 _track = _playlist.tracks().at(pos);
145 QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
146 int size = sub.count();
153 int pos = qrand() % size;
155 _track = sub.at(pos);
160 void Player::_set_source() {
161 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
162 emit trackChanged(_track);
165 void Player::prev() {
166 if (_history.isEmpty()) {
171 _queue.push_front(_track);
172 _track = _history.takeFirst();
177 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
179 case Phonon::PlayingState:
180 _state = PLAYER_PLAYING;
181 emit stateChanged(_state);
182 if (_awaiting_seek) {
183 _awaiting_seek = false;
184 seek(_awaiting_seek_pos);
187 case Phonon::ErrorState:
195 void Player::_tick(qint64 ticks) {
196 int done = ticks/1000;
197 int all = _track.metadata().length();
198 emit tick(done, all);
200 _track.setCount(_track.count()+1);
201 emit trackDone(_track);
205 void Player::setPlaylist(Playlist playlist) {
206 _playlist = playlist;
210 void Player::seek(int s) {
211 _player->seek(s*1000);
212 if (s >= _track.metadata().length()) {
217 void Player::play() {
218 if (_playlist.tracks().isEmpty())
220 if (_track.source().isEmpty()) {
221 emit startPlaylist();
224 _state = PLAYER_PLAYING;
225 emit stateChanged(_state);
229 void Player::enqueue(int id) {
230 _queue.push_back(_playlist.tracks().at(id));
233 void Player::toggleRandom() {
235 _config.setValue("playback/random", _random);
238 void Player::toggleRepeat() {
239 if (_repeat == REPEAT_NO) {
240 _repeat = REPEAT_ALL;
241 } else if (_repeat == REPEAT_ALL) {
242 _repeat = REPEAT_ONE;
243 } else if (_repeat == REPEAT_ONE) {
246 _config.setValue("playback/repeat", _repeat);
249 void Player::equalizerValue(int band, double *val) {
250 if (_equalizer == NULL) {
254 if (band < 0 || band > 9) {
258 if (_equalizer_enabled) {
259 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
260 QVariant var = _equalizer->parameterValue(plist[band]);
261 *val = var.toDouble();
265 void Player::enableEqualizer() {
266 if (_equalizer == NULL)
268 _equalizer_enabled = true;
269 _path.insertEffect(_equalizer);
271 config.setEqualizerEnabled(true);
274 void Player::disableEqualizer() {
275 if (_equalizer == NULL)
277 _equalizer_enabled = false;
278 _path.removeEffect(_equalizer);
280 config.setEqualizerEnabled(false);
283 void Player::setEqualizerValue(int band, double value) {
284 if (_equalizer == NULL)
286 if (band < 0 || band > 9 || value < -24 || value > 12) {
289 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
290 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
292 config.setEqualizerValue(QString("band%1").arg(band), value);
295 QString Player::artist() {
296 return _track.metadata().artist();
299 QString Player::album() {
300 return _track.metadata().album();
303 QString Player::title() {
304 return _track.metadata().title();
307 Track Player::current() {
311 void Player::pause() {
312 if (_state == PLAYER_PLAYING) {
314 _state = PLAYER_PAUSED;
315 emit stateChanged(_state);
319 void Player::playIfPaused() {
320 if (_state == PLAYER_PAUSED) {
325 void Player::_to_history(Track t) {
326 if (!t.source().isEmpty()) {
327 _history.push_front(t);
330 foreach (Track t, _history) {
334 void Player::_truncate_history() {
335 while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
336 _history.removeLast();