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 (_rand.count() == 0) {
39 res = _rand.takeFirst();
44 void Randomizer::setPlaylist(QList<int> pl) {
50 void Randomizer::_shuffle() {
52 // Fisher-Yates algorithm:
54 int cnt = _playlist.count();
57 for (int i = cnt-1; i > 0; i--) {
63 if (cnt > 1 && _last == _rand[0]) {
65 _rand.insert(qrand() % (cnt-1) + 1, _last);
72 void Randomizer::removeId(int id) {
76 Player::Player(QObject *parent) :
79 _player = new Phonon::MediaObject(this);
80 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
81 _player->setTickInterval(1000);
83 _equalizer_enabled = false;
84 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
85 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
86 connect(_player, SIGNAL(finished()), this, SLOT(next()));
87 _path = Phonon::createPath(_player, _output);
88 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
89 foreach (Phonon::EffectDescription desc, effects) {
90 if (desc.name() == "equalizer-10bands") {
91 _equalizer = new Phonon::Effect(desc, this);
93 if (config.equalizerEnabled()) {
94 for (int i = 0; i < 10; i++) {
95 QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
96 setEqualizerValue(i, var.toDouble());
102 int seed = QTime::currentTime().msec();
104 _random = _config.getValue("playback/random").toBool();
105 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
109 void Player::setTrackId(int id) {
111 _randomizer.removeId(id);
114 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
115 _history.push(_current);
117 _track = _playlist.tracks().at(_current);
119 _state = PLAYER_LOADING;
120 emit stateChanged(_state);
123 void Player::toggle() {
124 if (_state == PLAYER_PLAYING) { // pause
126 _state = PLAYER_PAUSED;
127 emit stateChanged(_state);
133 void Player::stop() {
135 _state = PLAYER_STOPPED;
136 emit stateChanged(_state);
139 void Player::next() {
140 int count = _playlist.tracks().count();
142 stop(); // empty playlist
145 if (_repeat == REPEAT_ONE) {
150 _history.push(_current % count);
151 if (!_queue.isEmpty()) {
152 _current = _queue.dequeue();
153 } else if (!_prev_history.isEmpty()) {
154 _current = _prev_history.pop();
157 _current = _randomizer.next();
159 _current = _current + 1;
162 if (_random && _history.count() >= count && _repeat == REPEAT_NO||
163 _repeat == REPEAT_NO && _current >= count) {
168 _track = _playlist.tracks().at(_current);
174 void Player::_set_source() {
175 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
176 emit trackChanged(_track);
179 void Player::prev() {
180 if (_history.count() > 0) {
181 _prev_history.push(_current);
182 _current = _history.pop();
183 _track = _playlist.tracks().at(_current);
189 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
191 case Phonon::PlayingState:
192 _state = PLAYER_PLAYING;
193 emit stateChanged(_state);
195 case Phonon::ErrorState:
203 void Player::_tick(qint64 ticks) {
204 int done = ticks/1000;
205 int all = _track.metadata().length();
206 emit tick(done, all);
208 _track.setCount(_track.count()+1);
209 emit trackDone(_track);
213 void Player::setPlaylist(Playlist playlist) {
214 _playlist = playlist;
216 _prev_history.clear();
219 int count = playlist.tracks().count();
220 for (int i = 0; i < count; i++) {
223 _randomizer.setPlaylist(ids);
226 void Player::seek(int s) {
227 _player->seek(s*1000);
228 if (s >= _track.metadata().length()) {
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.setEqualizerEnabled(true);
291 void Player::disableEqualizer() {
292 if (_equalizer == NULL)
294 _equalizer_enabled = false;
295 _path.removeEffect(_equalizer);
297 config.setEqualizerEnabled(false);
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.setEqualizerValue(QString("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();
330 Track Player::current() {
332 return _playlist.tracks().at(_current);