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"
28 using namespace SomePlayer::Playback;
29 using namespace SomePlayer::DataObjects;
30 using namespace SomePlayer::Storage;
32 int Randomizer::next() {
34 if (_current == _rand.count()) {
39 res = _rand.at(_current);
45 void Randomizer::setPlaylist(QList<int> pl) {
51 void Randomizer::_shuffle() {
53 // Fisher-Yates algorithm:
55 int cnt = _playlist.count();
58 for (int i = cnt-1; i > 0; i--) {
66 Player::Player(QObject *parent) :
69 _player = new Phonon::MediaObject(this);
70 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
71 _player->setTickInterval(1000);
73 _equalizer_enabled = false;
74 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
75 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
76 _path = Phonon::createPath(_player, _output);
77 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
78 foreach (Phonon::EffectDescription desc, effects) {
79 if (desc.name() == "equalizer-10bands") {
80 _equalizer = new Phonon::Effect(desc, this);
82 if (config.getValue("equalizer/equalizer").toString() == "enabled") {
83 for (int i = 0; i < 10; i++) {
84 QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
85 setEqualizerValue(i, var.toDouble());
88 } else if (config.getValue("equalizer/equalizer") == "") {
89 for (int i = 0; i < 10; i++) {
90 config.setValue(QString("equalizer/band%1").arg(i), 0);
95 int seed = QTime::currentTime().msec();
97 _random = _config.getValue("playback/random").toBool();
98 _repeat = _config.getValue("playback/repeat").toBool();
102 void Player::setTrackId(int id) {
104 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
105 _history.push(_current);
107 _track = _playlist.tracks().at(_current);
109 _state = PLAYER_LOADING;
110 emit stateChanged(_state);
113 void Player::toggle() {
114 if (_state == PLAYER_PLAYING) { // pause
116 _state = PLAYER_PAUSED;
117 emit stateChanged(_state);
123 void Player::stop() {
125 _state = PLAYER_STOPPED;
126 emit stateChanged(_state);
129 void Player::next() {
130 int count = _playlist.tracks().count();
132 stop(); // empty playlist
135 _history.push(_current % count);
136 if (!_queue.isEmpty()) {
137 _current = _queue.dequeue();
138 } else if (!_prev_history.isEmpty()) {
139 _current = _prev_history.pop();
142 _current = _randomizer.next();
144 _current = _current + 1;
147 if (_random && _history.count() >= count && !_repeat ||
148 !_repeat && _current >= count) {
153 _track = _playlist.tracks().at(_current);
159 void Player::_set_source() {
160 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
161 emit trackChanged(_track);
164 void Player::prev() {
165 if (_history.count() > 0) {
166 _prev_history.push(_current);
167 _current = _history.pop();
168 _track = _playlist.tracks().at(_current);
174 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
176 case Phonon::PlayingState:
177 if (_state == PLAYER_LOADING) {
178 _state = PLAYER_PLAYING;
179 emit stateChanged(_state);
182 case Phonon::StoppedState:
184 case Phonon::LoadingState:
186 case Phonon::PausedState:
187 if (_state == PLAYER_PLAYING) {
189 } else if (_state == PLAYER_ERROR) {
193 case Phonon::BufferingState:
195 case Phonon::ErrorState:
197 // _state = PLAYER_ERROR;
202 void Player::_tick(qint64 ticks) {
203 int done = ticks/1000;
204 int all = _track.metadata().length();
205 emit tick(done, all);
207 _track.setCount(_track.count()+1);
208 emit trackDone(_track);
212 void Player::setPlaylist(Playlist playlist) {
213 _playlist = playlist;
215 _prev_history.clear();
218 int count = playlist.tracks().count();
219 for (int i = 0; i < count; i++) {
222 _randomizer.setPlaylist(ids);
225 void Player::seek(int s) {
226 _player->seek(s*1000);
229 void Player::play() {
230 if (_playlist.tracks().isEmpty())
232 _state = PLAYER_PLAYING;
233 emit stateChanged(_state);
234 if (_current == -1) {
236 _track = _playlist.tracks().at(0);
242 void Player::enqueue(int id) {
246 void Player::toggleRandom() {
248 _config.setValue("playback/random", _random);
251 void Player::toggleRepeat() {
253 _config.setValue("playback/repeat", _repeat);
256 void Player::setVolume(int v) {
257 _output->setVolume(v*0.01);
260 void Player::equalizerValue(int band, double *val) {
261 if (_equalizer == NULL) {
265 if (band < 0 || band > 9) {
269 if (_equalizer_enabled) {
270 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
271 QVariant var = _equalizer->parameterValue(plist[band]);
272 *val = var.toDouble();
276 void Player::enableEqualizer() {
277 if (_equalizer == NULL)
279 _equalizer_enabled = true;
280 _path.insertEffect(_equalizer);
281 if (_state == PLAYER_PAUSED)
284 config.setValue("equalizer/equalizer", "enabled");
287 void Player::disableEqualizer() {
288 if (_equalizer == NULL)
290 _equalizer_enabled = false;
291 _path.removeEffect(_equalizer);
293 config.setValue("equalizer/equalizer", "disabled");
296 void Player::setEqualizerValue(int band, double value) {
297 if (_equalizer == NULL)
299 if (band < 0 || band > 9 || value < -24 || value > 12) {
302 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
303 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
305 config.setValue(QString("equalizer/band%1").arg(band), value);
308 QString Player::artist() {
311 return _playlist.tracks().at(_current).metadata().artist();
314 QString Player::album() {
317 return _playlist.tracks().at(_current).metadata().album();
320 QString Player::title() {
323 return _playlist.tracks().at(_current).metadata().title();