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.getValue("equalizer/equalizer").toString() == "enabled") {
94 for (int i = 0; i < 10; i++) {
95 QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
96 setEqualizerValue(i, var.toDouble());
99 } else if (config.getValue("equalizer/equalizer") == "") {
100 for (int i = 0; i < 10; i++) {
101 config.setValue(QString("equalizer/band%1").arg(i), 0);
106 int seed = QTime::currentTime().msec();
108 _random = _config.getValue("playback/random").toBool();
109 _repeat = (RepeatRule) _config.getValue("playback/repeat").toInt();
113 void Player::setTrackId(int id) {
115 _randomizer.removeId(id);
118 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
119 _history.push(_current);
121 _track = _playlist.tracks().at(_current);
123 _state = PLAYER_LOADING;
124 emit stateChanged(_state);
127 void Player::toggle() {
128 if (_state == PLAYER_PLAYING) { // pause
130 _state = PLAYER_PAUSED;
131 emit stateChanged(_state);
137 void Player::stop() {
139 _state = PLAYER_STOPPED;
140 emit stateChanged(_state);
143 void Player::next() {
144 int count = _playlist.tracks().count();
146 stop(); // empty playlist
149 if (_repeat == REPEAT_ONE) {
154 _history.push(_current % count);
155 if (!_queue.isEmpty()) {
156 _current = _queue.dequeue();
157 } else if (!_prev_history.isEmpty()) {
158 _current = _prev_history.pop();
161 _current = _randomizer.next();
163 _current = _current + 1;
166 if (_random && _history.count() >= count && _repeat == REPEAT_NO||
167 _repeat == REPEAT_NO && _current >= count) {
172 _track = _playlist.tracks().at(_current);
178 void Player::_set_source() {
179 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
180 emit trackChanged(_track);
183 void Player::prev() {
184 if (_history.count() > 0) {
185 _prev_history.push(_current);
186 _current = _history.pop();
187 _track = _playlist.tracks().at(_current);
193 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
195 case Phonon::PlayingState:
196 _state = PLAYER_PLAYING;
197 emit stateChanged(_state);
199 case Phonon::ErrorState:
207 void Player::_tick(qint64 ticks) {
208 int done = ticks/1000;
209 int all = _track.metadata().length();
210 emit tick(done, all);
212 _track.setCount(_track.count()+1);
213 emit trackDone(_track);
217 void Player::setPlaylist(Playlist playlist) {
218 _playlist = playlist;
220 _prev_history.clear();
223 int count = playlist.tracks().count();
224 for (int i = 0; i < count; i++) {
227 _randomizer.setPlaylist(ids);
230 void Player::seek(int s) {
231 _player->seek(s*1000);
232 if (s >= _track.metadata().length()) {
237 void Player::play() {
238 if (_playlist.tracks().isEmpty())
240 _state = PLAYER_PLAYING;
241 emit stateChanged(_state);
242 if (_current == -1) {
244 _track = _playlist.tracks().at(0);
250 void Player::enqueue(int id) {
254 void Player::toggleRandom() {
256 _config.setValue("playback/random", _random);
259 void Player::toggleRepeat() {
260 if (_repeat == REPEAT_NO) {
261 _repeat = REPEAT_ALL;
262 } else if (_repeat == REPEAT_ALL) {
263 _repeat = REPEAT_ONE;
264 } else if (_repeat == REPEAT_ONE) {
267 _config.setValue("playback/repeat", _repeat);
270 void Player::equalizerValue(int band, double *val) {
271 if (_equalizer == NULL) {
275 if (band < 0 || band > 9) {
279 if (_equalizer_enabled) {
280 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
281 QVariant var = _equalizer->parameterValue(plist[band]);
282 *val = var.toDouble();
286 void Player::enableEqualizer() {
287 if (_equalizer == NULL)
289 _equalizer_enabled = true;
290 _path.insertEffect(_equalizer);
292 config.setValue("equalizer/equalizer", "enabled");
295 void Player::disableEqualizer() {
296 if (_equalizer == NULL)
298 _equalizer_enabled = false;
299 _path.removeEffect(_equalizer);
301 config.setValue("equalizer/equalizer", "disabled");
304 void Player::setEqualizerValue(int band, double value) {
305 if (_equalizer == NULL)
307 if (band < 0 || band > 9 || value < -24 || value > 12) {
310 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
311 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
313 config.setValue(QString("equalizer/band%1").arg(band), value);
316 QString Player::artist() {
319 return _playlist.tracks().at(_current).metadata().artist();
322 QString Player::album() {
325 return _playlist.tracks().at(_current).metadata().album();
328 QString Player::title() {
331 return _playlist.tracks().at(_current).metadata().title();