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 Player::Player(QObject *parent) :
35 _player = new Phonon::MediaObject(this);
36 _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
37 _player->setTickInterval(1000);
39 _equalizer_enabled == false;
40 QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
41 foreach (Phonon::EffectDescription desc, effects) {
42 if (desc.name() == "equalizer-10bands") {
43 _equalizer = new Phonon::Effect(desc, this);
45 if (config.getValue("equalizer/equalizer").toString() == "enabled") {
46 for (int i = 0; i < 10; i++) {
47 QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
48 setEqualizerValue(i, var.toDouble());
50 _equalizer_enabled = true;
51 } else if (config.getValue("equalizer/equalizer") == "") {
52 for (int i = 0; i < 10; i++) {
53 config.setValue(QString("equalizer/band%1").arg(i), 0);
58 connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
59 connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
60 _path = Phonon::createPath(_player, _output);
61 int seed = QTime::currentTime().msec();
63 _random = _config.getValue("playback/random").toBool();
64 _repeat = _config.getValue("playback/repeat").toBool();
68 void Player::setTrackId(int id) {
70 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
71 _history.push(_current);
73 _track = _playlist.tracks().at(_current);
75 _state = PLAYER_LOADING;
76 emit stateChanged(_state);
79 void Player::toggle() {
80 if (_state == PLAYER_PLAYING) { // pause
82 _state = PLAYER_PAUSED;
83 emit stateChanged(_state);
91 _state = PLAYER_STOPPED;
92 emit stateChanged(_state);
96 int count = _playlist.tracks().count();
98 stop(); // empty playlist
101 _history.push(_current % count);
102 if (!_queue.isEmpty()) {
103 _current = _queue.dequeue();
104 } else if (!_prev_history.isEmpty()) {
105 _current = _prev_history.pop();
108 _current = (count + (qrand() + qrand() + qrand()) % count) % count;
110 _current = _current + 1;
113 if (_random && _history.count() >= count && !_repeat ||
114 !_repeat && _current >= count) {
119 _track = _playlist.tracks().at(_current);
125 void Player::_set_source() {
126 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
127 emit trackChanged(_track);
130 void Player::prev() {
131 if (_history.count() > 0) {
132 _prev_history.push(_current);
133 _current = _history.pop();
134 _track = _playlist.tracks().at(_current);
140 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
142 case Phonon::PlayingState:
143 if (_state == PLAYER_LOADING) {
144 _state = PLAYER_PLAYING;
145 emit stateChanged(_state);
148 case Phonon::StoppedState:
150 case Phonon::LoadingState:
152 case Phonon::PausedState:
153 if (_state == PLAYER_PLAYING) {
155 } else if (_state == PLAYER_ERROR) {
159 case Phonon::BufferingState:
161 case Phonon::ErrorState:
162 _state = PLAYER_ERROR;
167 void Player::_tick(qint64 ticks) {
168 int done = ticks/1000;
169 int all = _track.metadata().length();
170 emit tick(done, all);
172 _track.setCount(_track.count()+1);
173 emit trackDone(_track);
177 void Player::setPlaylist(Playlist playlist) {
178 _playlist = playlist;
180 _prev_history.clear();
184 void Player::seek(int s) {
185 _player->seek(s*1000);
188 void Player::play() {
189 if (_playlist.tracks().isEmpty())
191 _state = PLAYER_PLAYING;
192 emit stateChanged(_state);
193 if (_current == -1) {
195 _track = _playlist.tracks().at(0);
201 void Player::enqueue(int id) {
205 void Player::toggleRandom() {
207 _config.setValue("playback/random", _random);
210 void Player::toggleRepeat() {
212 _config.setValue("playback/repeat", _repeat);
215 void Player::setVolume(int v) {
216 _output->setVolume(v*0.01);
219 void Player::equalizerValue(int band, double *val) {
220 if (band < 0 || band > 9) {
224 if (_equalizer_enabled) {
225 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
226 QVariant var = _equalizer->parameterValue(plist[band]);
227 *val = var.toDouble();
231 void Player::enableEqualizer() {
232 _equalizer_enabled = true;
233 _path.insertEffect(_equalizer);
235 config.setValue("equalizer/equalizer", "enabled");
238 void Player::disableEqualizer() {
239 _equalizer_enabled = false;
240 _path.removeEffect(_equalizer);
242 config.setValue("equalizer/equalizer", "disabled");
245 void Player::setEqualizerValue(int band, double value) {
246 if (band < 0 || band > 9 || value < -24 || value > 12) {
249 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
250 _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
252 config.setValue(QString("equalizer/band%1").arg(band), value);