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 if (_state == PLAYER_LOADING) {
184 _state = PLAYER_PLAYING;
185 emit stateChanged(_state);
188 case Phonon::StoppedState:
190 case Phonon::LoadingState:
192 case Phonon::PausedState:
193 if (_state == PLAYER_PLAYING) {
195 } else if (_state == PLAYER_ERROR) {
199 case Phonon::BufferingState:
201 case Phonon::ErrorState:
203 // _state = PLAYER_ERROR;
208 void Player::_tick(qint64 ticks) {
209 int done = ticks/1000;
210 int all = _track.metadata().length();
211 emit tick(done, all);
213 _track.setCount(_track.count()+1);
214 emit trackDone(_track);
218 void Player::setPlaylist(Playlist playlist) {
219 _playlist = playlist;
221 _prev_history.clear();
224 int count = playlist.tracks().count();
225 for (int i = 0; i < count; i++) {
228 _randomizer.setPlaylist(ids);
231 void Player::seek(int s) {
232 _player->seek(s*1000);
235 void Player::play() {
236 if (_playlist.tracks().isEmpty())
238 _state = PLAYER_PLAYING;
239 emit stateChanged(_state);
240 if (_current == -1) {
242 _track = _playlist.tracks().at(0);
248 void Player::enqueue(int id) {
252 void Player::toggleRandom() {
254 _config.setValue("playback/random", _random);
257 void Player::toggleRepeat() {
258 if (_repeat == REPEAT_NO) {
259 _repeat = REPEAT_ALL;
260 } else if (_repeat == REPEAT_ALL) {
261 _repeat = REPEAT_ONE;
262 } else if (_repeat == REPEAT_ONE) {
265 _config.setValue("playback/repeat", _repeat);
268 void Player::equalizerValue(int band, double *val) {
269 if (_equalizer == NULL) {
273 if (band < 0 || band > 9) {
277 if (_equalizer_enabled) {
278 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
279 QVariant var = _equalizer->parameterValue(plist[band]);
280 *val = var.toDouble();
284 void Player::enableEqualizer() {
285 if (_equalizer == NULL)
287 _equalizer_enabled = true;
288 _path.insertEffect(_equalizer);
290 config.setValue("equalizer/equalizer", "enabled");
293 void Player::disableEqualizer() {
294 if (_equalizer == NULL)
296 _equalizer_enabled = false;
297 _path.removeEffect(_equalizer);
299 config.setValue("equalizer/equalizer", "disabled");
302 void Player::setEqualizerValue(int band, double value) {
303 if (_equalizer == NULL)
305 if (band < 0 || band > 9 || value < -24 || value > 12) {
308 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
309 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
311 config.setValue(QString("equalizer/band%1").arg(band), value);
314 QString Player::artist() {
317 return _playlist.tracks().at(_current).metadata().artist();
320 QString Player::album() {
323 return _playlist.tracks().at(_current).metadata().album();
326 QString Player::title() {
329 return _playlist.tracks().at(_current).metadata().title();