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 _player = new Phonon::MediaObject(this);
47 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
48 _player->setTickInterval(1000);
50 _equalizer_enabled = false;
51 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
52 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
53 connect(_player, SIGNAL(finished()), this, SLOT(next()));
54 _path = Phonon::createPath(_player, _output);
55 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
56 foreach (Phonon::EffectDescription desc, effects) {
57 if (desc.name() == "equalizer-10bands") {
58 _equalizer = new Phonon::Effect(desc, this);
60 if (config.equalizerEnabled()) {
61 for (int i = 0; i < 10; i++) {
62 QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
63 setEqualizerValue(i, var.toDouble());
69 int seed = QTime::currentTime().msec();
71 _random = _config.getValue("playback/random").toBool();
72 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
75 void Player::setTrackId(int id) {
77 _track = _playlist.tracks().at(id);
82 void Player::toggle() {
83 if (_state == PLAYER_PLAYING) { // pause
85 _state = PLAYER_PAUSED;
86 emit stateChanged(_state);
94 _state = PLAYER_STOPPED;
95 emit stateChanged(_state);
99 int count = _playlist.tracks().count();
101 stop(); // empty playlist
104 if (_repeat == REPEAT_ONE) {
110 while (!_queue.isEmpty()) {
111 _new = _queue.takeFirst();
112 if (_playlist.tracks().contains(_new)) {
121 int pos = _playlist.tracks().indexOf(_track) + 1;
122 if (pos >= _playlist.tracks().count()) {
123 if (_repeat == REPEAT_NO) {
126 pos %= _playlist.tracks().count();
129 _track = _playlist.tracks().at(pos);
135 QList<Track> sub = __sub(_playlist.tracks(), _history, _track);
136 int size = sub.count();
143 int pos = qrand() % size;
145 _track = sub.at(pos);
150 void Player::_set_source() {
151 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
152 emit trackChanged(_track);
155 void Player::prev() {
156 if (_history.isEmpty()) {
161 _queue.push_front(_track);
162 _track = _history.takeFirst();
167 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
169 case Phonon::PlayingState:
170 _state = PLAYER_PLAYING;
171 emit stateChanged(_state);
173 case Phonon::ErrorState:
181 void Player::_tick(qint64 ticks) {
182 int done = ticks/1000;
183 int all = _track.metadata().length();
184 emit tick(done, all);
186 _track.setCount(_track.count()+1);
187 emit trackDone(_track);
191 void Player::setPlaylist(Playlist playlist) {
192 _playlist = playlist;
196 void Player::seek(int s) {
197 _player->seek(s*1000);
198 if (s >= _track.metadata().length()) {
203 void Player::play() {
204 if (_playlist.tracks().isEmpty())
206 if (_track.source().isEmpty()) {
210 _state = PLAYER_PLAYING;
211 emit stateChanged(_state);
215 void Player::enqueue(int id) {
216 _queue.push_back(_playlist.tracks().at(id));
219 void Player::toggleRandom() {
221 _config.setValue("playback/random", _random);
224 void Player::toggleRepeat() {
225 if (_repeat == REPEAT_NO) {
226 _repeat = REPEAT_ALL;
227 } else if (_repeat == REPEAT_ALL) {
228 _repeat = REPEAT_ONE;
229 } else if (_repeat == REPEAT_ONE) {
232 _config.setValue("playback/repeat", _repeat);
235 void Player::equalizerValue(int band, double *val) {
236 if (_equalizer == NULL) {
240 if (band < 0 || band > 9) {
244 if (_equalizer_enabled) {
245 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
246 QVariant var = _equalizer->parameterValue(plist[band]);
247 *val = var.toDouble();
251 void Player::enableEqualizer() {
252 if (_equalizer == NULL)
254 _equalizer_enabled = true;
255 _path.insertEffect(_equalizer);
257 config.setEqualizerEnabled(true);
260 void Player::disableEqualizer() {
261 if (_equalizer == NULL)
263 _equalizer_enabled = false;
264 _path.removeEffect(_equalizer);
266 config.setEqualizerEnabled(false);
269 void Player::setEqualizerValue(int band, double value) {
270 if (_equalizer == NULL)
272 if (band < 0 || band > 9 || value < -24 || value > 12) {
275 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
276 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
278 config.setEqualizerValue(QString("band%1").arg(band), value);
281 QString Player::artist() {
282 return _track.metadata().artist();
285 QString Player::album() {
286 return _track.metadata().album();
289 QString Player::title() {
290 return _track.metadata().title();
293 Track Player::current() {
297 void Player::pause() {
298 if (_state == PLAYER_PLAYING) {
300 _state = PLAYER_PAUSED;
301 emit stateChanged(_state);
305 void Player::playIfPaused() {
306 if (_state == PLAYER_PAUSED) {
311 void Player::_to_history(Track t) {
312 if (!t.source().isEmpty()) {
313 _history.push_front(t);
316 foreach (Track t, _history) {
320 void Player::_truncate_history() {
321 while (_history.size() > 50 || _history.size() > _playlist.tracks().size()/2) {
322 _history.removeLast();