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);
70 void Randomizer::removeId(int id) {
74 Player::Player(QObject *parent) :
77 _player = new Phonon::MediaObject(this);
78 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
79 _player->setTickInterval(1000);
81 _equalizer_enabled = false;
82 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
83 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
84 connect(_player, SIGNAL(finished()), this, SLOT(next()));
85 _path = Phonon::createPath(_player, _output);
86 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
87 foreach (Phonon::EffectDescription desc, effects) {
88 if (desc.name() == "equalizer-10bands") {
89 _equalizer = new Phonon::Effect(desc, this);
91 if (config.getValue("equalizer/equalizer").toString() == "enabled") {
92 for (int i = 0; i < 10; i++) {
93 QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
94 setEqualizerValue(i, var.toDouble());
97 } else if (config.getValue("equalizer/equalizer") == "") {
98 for (int i = 0; i < 10; i++) {
99 config.setValue(QString("equalizer/band%1").arg(i), 0);
104 int seed = QTime::currentTime().msec();
106 _random = _config.getValue("playback/random").toBool();
107 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
111 void Player::setTrackId(int id) {
113 _randomizer.removeId(id);
116 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
117 _history.push(_current);
119 _track = _playlist.tracks().at(_current);
121 _state = PLAYER_LOADING;
122 emit stateChanged(_state);
125 void Player::toggle() {
126 if (_state == PLAYER_PLAYING) { // pause
128 _state = PLAYER_PAUSED;
129 emit stateChanged(_state);
135 void Player::stop() {
137 _state = PLAYER_STOPPED;
138 emit stateChanged(_state);
141 void Player::next() {
142 int count = _playlist.tracks().count();
144 stop(); // empty playlist
147 if (_repeat == REPEAT_ONE) {
152 _history.push(_current % count);
153 if (!_queue.isEmpty()) {
154 _current = _queue.dequeue();
155 } else if (!_prev_history.isEmpty()) {
156 _current = _prev_history.pop();
159 _current = _randomizer.next();
161 _current = _current + 1;
164 if (_random && _history.count() >= count && _repeat == REPEAT_NO||
165 _repeat == REPEAT_NO && _current >= count) {
170 _track = _playlist.tracks().at(_current);
176 void Player::_set_source() {
177 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
178 emit trackChanged(_track);
181 void Player::prev() {
182 if (_history.count() > 0) {
183 _prev_history.push(_current);
184 _current = _history.pop();
185 _track = _playlist.tracks().at(_current);
191 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
193 case Phonon::PlayingState:
194 _state = PLAYER_PLAYING;
195 emit stateChanged(_state);
197 case Phonon::ErrorState:
205 void Player::_tick(qint64 ticks) {
206 int done = ticks/1000;
207 int all = _track.metadata().length();
208 emit tick(done, all);
210 _track.setCount(_track.count()+1);
211 emit trackDone(_track);
215 void Player::setPlaylist(Playlist playlist) {
216 _playlist = playlist;
218 _prev_history.clear();
221 int count = playlist.tracks().count();
222 for (int i = 0; i < count; i++) {
225 _randomizer.setPlaylist(ids);
228 void Player::seek(int s) {
229 _player->seek(s*1000);
230 if (s >= _track.metadata().length()) {
235 void Player::play() {
236 qWarning() << _current;
237 if (_playlist.tracks().isEmpty())
239 _state = PLAYER_PLAYING;
240 emit stateChanged(_state);
241 if (_current == -1) {
243 _track = _playlist.tracks().at(0);
249 void Player::enqueue(int id) {
253 void Player::toggleRandom() {
255 _config.setValue("playback/random", _random);
258 void Player::toggleRepeat() {
259 if (_repeat == REPEAT_NO) {
260 _repeat = REPEAT_ALL;
261 } else if (_repeat == REPEAT_ALL) {
262 _repeat = REPEAT_ONE;
263 } else if (_repeat == REPEAT_ONE) {
266 _config.setValue("playback/repeat", _repeat);
269 void Player::equalizerValue(int band, double *val) {
270 if (_equalizer == NULL) {
274 if (band < 0 || band > 9) {
278 if (_equalizer_enabled) {
279 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
280 QVariant var = _equalizer->parameterValue(plist[band]);
281 *val = var.toDouble();
285 void Player::enableEqualizer() {
286 if (_equalizer == NULL)
288 _equalizer_enabled = true;
289 _path.insertEffect(_equalizer);
291 config.setValue("equalizer/equalizer", "enabled");
294 void Player::disableEqualizer() {
295 if (_equalizer == NULL)
297 _equalizer_enabled = false;
298 _path.removeEffect(_equalizer);
300 config.setValue("equalizer/equalizer", "disabled");
303 void Player::setEqualizerValue(int band, double value) {
304 if (_equalizer == NULL)
306 if (band < 0 || band > 9 || value < -24 || value > 12) {
309 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
310 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
312 config.setValue(QString("equalizer/band%1").arg(band), value);
315 QString Player::artist() {
318 return _playlist.tracks().at(_current).metadata().artist();
321 QString Player::album() {
324 return _playlist.tracks().at(_current).metadata().album();
327 QString Player::title() {
330 return _playlist.tracks().at(_current).metadata().title();