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 = _config.getValue("playback/repeat").toBool();
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 _history.push(_current % count);
137 if (!_queue.isEmpty()) {
138 _current = _queue.dequeue();
139 } else if (!_prev_history.isEmpty()) {
140 _current = _prev_history.pop();
143 _current = _randomizer.next();
145 _current = _current + 1;
148 if (_random && _history.count() >= count && !_repeat ||
149 !_repeat && _current >= count) {
154 _track = _playlist.tracks().at(_current);
160 void Player::_set_source() {
161 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
162 emit trackChanged(_track);
165 void Player::prev() {
166 if (_history.count() > 0) {
167 _prev_history.push(_current);
168 _current = _history.pop();
169 _track = _playlist.tracks().at(_current);
175 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
177 case Phonon::PlayingState:
178 if (_state == PLAYER_LOADING) {
179 _state = PLAYER_PLAYING;
180 emit stateChanged(_state);
183 case Phonon::StoppedState:
185 case Phonon::LoadingState:
187 case Phonon::PausedState:
188 if (_state == PLAYER_PLAYING) {
190 } else if (_state == PLAYER_ERROR) {
194 case Phonon::BufferingState:
196 case Phonon::ErrorState:
198 // _state = PLAYER_ERROR;
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);
230 void Player::play() {
231 if (_playlist.tracks().isEmpty())
233 _state = PLAYER_PLAYING;
234 emit stateChanged(_state);
235 if (_current == -1) {
237 _track = _playlist.tracks().at(0);
243 void Player::enqueue(int id) {
247 void Player::toggleRandom() {
249 _config.setValue("playback/random", _random);
252 void Player::toggleRepeat() {
254 _config.setValue("playback/repeat", _repeat);
257 void Player::setVolume(int v) {
258 _output->setVolume(v*0.01);
261 void Player::equalizerValue(int band, double *val) {
262 if (_equalizer == NULL) {
266 if (band < 0 || band > 9) {
270 if (_equalizer_enabled) {
271 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
272 QVariant var = _equalizer->parameterValue(plist[band]);
273 *val = var.toDouble();
277 void Player::enableEqualizer() {
278 if (_equalizer == NULL)
280 _equalizer_enabled = true;
281 _path.insertEffect(_equalizer);
283 config.setValue("equalizer/equalizer", "enabled");
286 void Player::disableEqualizer() {
287 if (_equalizer == NULL)
289 _equalizer_enabled = false;
290 _path.removeEffect(_equalizer);
292 config.setValue("equalizer/equalizer", "disabled");
295 void Player::setEqualizerValue(int band, double value) {
296 if (_equalizer == NULL)
298 if (band < 0 || band > 9 || value < -24 || value > 12) {
301 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
302 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
304 config.setValue(QString("equalizer/band%1").arg(band), value);
307 QString Player::artist() {
310 return _playlist.tracks().at(_current).metadata().artist();
313 QString Player::album() {
316 return _playlist.tracks().at(_current).metadata().album();
319 QString Player::title() {
322 return _playlist.tracks().at(_current).metadata().title();