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 int Randomizer::next() {
35 if (_current == _rand.count()) {
40 res = _rand.at(_current);
46 void Randomizer::setPlaylist(QList<int> pl) {
52 void Randomizer::_shuffle() {
54 // Fisher-Yates algorithm:
56 int cnt = _playlist.count();
59 for (int i = cnt-1; i > 0; i--) {
67 Player::Player(QObject *parent) :
70 _player = new Phonon::MediaObject(this);
71 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
72 _player->setTickInterval(1000);
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);
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());
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);
96 int seed = QTime::currentTime().msec();
98 _random = _config.getValue("playback/random").toBool();
99 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
103 void Player::setTrackId(int id) {
105 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
106 _history.push(_current);
108 _track = _playlist.tracks().at(_current);
110 _state = PLAYER_LOADING;
111 emit stateChanged(_state);
114 void Player::toggle() {
115 if (_state == PLAYER_PLAYING) { // pause
117 _state = PLAYER_PAUSED;
118 emit stateChanged(_state);
124 void Player::stop() {
126 _state = PLAYER_STOPPED;
127 emit stateChanged(_state);
130 void Player::next() {
131 int count = _playlist.tracks().count();
133 stop(); // empty playlist
136 if (_repeat == REPEAT_ONE) {
141 _history.push(_current % count);
142 if (!_queue.isEmpty()) {
143 _current = _queue.dequeue();
144 } else if (!_prev_history.isEmpty()) {
145 _current = _prev_history.pop();
148 _current = _randomizer.next();
150 _current = _current + 1;
153 if (_random && _history.count() >= count && _repeat == REPEAT_NO||
154 _repeat == REPEAT_NO && _current >= count) {
159 _track = _playlist.tracks().at(_current);
165 void Player::_set_source() {
166 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
167 emit trackChanged(_track);
170 void Player::prev() {
171 if (_history.count() > 0) {
172 _prev_history.push(_current);
173 _current = _history.pop();
174 _track = _playlist.tracks().at(_current);
180 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
182 case Phonon::PlayingState:
183 _state = PLAYER_PLAYING;
184 emit stateChanged(_state);
186 case Phonon::StoppedState:
188 case Phonon::LoadingState:
190 case Phonon::PausedState:
191 if (_state == PLAYER_PLAYING) {
193 } else if (_state == PLAYER_ERROR) {
197 case Phonon::BufferingState:
199 case Phonon::ErrorState:
201 // _state = PLAYER_ERROR;
206 void Player::_tick(qint64 ticks) {
207 int done = ticks/1000;
208 int all = _track.metadata().length();
209 emit tick(done, all);
211 _track.setCount(_track.count()+1);
212 emit trackDone(_track);
216 void Player::setPlaylist(Playlist playlist) {
217 _playlist = playlist;
219 _prev_history.clear();
222 int count = playlist.tracks().count();
223 for (int i = 0; i < count; i++) {
226 _randomizer.setPlaylist(ids);
229 void Player::seek(int s) {
230 _player->seek(s*1000);
233 void Player::play() {
234 if (_playlist.tracks().isEmpty())
236 _state = PLAYER_PLAYING;
237 emit stateChanged(_state);
238 if (_current == -1) {
240 _track = _playlist.tracks().at(0);
246 void Player::enqueue(int id) {
250 void Player::toggleRandom() {
252 _config.setValue("playback/random", _random);
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) {
263 _config.setValue("playback/repeat", _repeat);
266 void Player::equalizerValue(int band, double *val) {
267 if (_equalizer == NULL) {
271 if (band < 0 || band > 9) {
275 if (_equalizer_enabled) {
276 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
277 QVariant var = _equalizer->parameterValue(plist[band]);
278 *val = var.toDouble();
282 void Player::enableEqualizer() {
283 if (_equalizer == NULL)
285 _equalizer_enabled = true;
286 _path.insertEffect(_equalizer);
288 config.setValue("equalizer/equalizer", "enabled");
291 void Player::disableEqualizer() {
292 if (_equalizer == NULL)
294 _equalizer_enabled = false;
295 _path.removeEffect(_equalizer);
297 config.setValue("equalizer/equalizer", "disabled");
300 void Player::setEqualizerValue(int band, double value) {
301 if (_equalizer == NULL)
303 if (band < 0 || band > 9 || value < -24 || value > 12) {
306 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
307 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
309 config.setValue(QString("equalizer/band%1").arg(band), value);
312 QString Player::artist() {
315 return _playlist.tracks().at(_current).metadata().artist();
318 QString Player::album() {
321 return _playlist.tracks().at(_current).metadata().album();
324 QString Player::title() {
327 return _playlist.tracks().at(_current).metadata().title();