Implemented equalizer
[someplayer] / src / player / player.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "player.h"
21 #include <phonon/MediaSource>
22 #include <phonon/Effect>
23 #include <phonon/BackendCapabilities>
24 #include <phonon/EffectParameter>
25 #include "../config.h"
26 #include <QTime>
27
28 using namespace SomePlayer::Playback;
29 using namespace SomePlayer::DataObjects;
30 using namespace SomePlayer::Storage;
31
32 Player::Player(QObject *parent) :
33     QObject(parent)
34 {
35         _player = new Phonon::MediaObject(this);
36         _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
37         _player->setTickInterval(1000);
38         _equalizer == NULL;
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);
44                         Config config;
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());
49                                 }
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);
54                                 }
55                         }
56                 }
57         }
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();
62         qsrand(seed);
63         _random = _config.getValue("playback/random").toBool();
64         _repeat = _config.getValue("playback/repeat").toBool();
65         _current = -1;
66 }
67
68 void Player::setTrackId(int id) {
69         _current = id;
70         if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
71                 _history.push(_current);
72         }
73         _track = _playlist.tracks().at(_current);
74         _set_source();
75         _state = PLAYER_LOADING;
76         emit stateChanged(_state);
77 }
78
79 void Player::toggle() {
80         if (_state == PLAYER_PLAYING) { // pause
81                 _player->pause();
82                 _state = PLAYER_PAUSED;
83                 emit stateChanged(_state);
84         } else { //play
85                 play();
86         }
87 }
88
89 void Player::stop() {
90         _player->stop();
91         _state = PLAYER_STOPPED;
92         emit stateChanged(_state);
93 }
94
95 void Player::next() {
96         int count = _playlist.tracks().count();
97         if (count == 0) {
98                 stop(); // empty playlist
99                 return;
100         }
101         _history.push(_current % count);
102         if (!_queue.isEmpty()) {
103                 _current = _queue.dequeue();
104         } else if (!_prev_history.isEmpty()) {
105                 _current = _prev_history.pop();
106         } else {
107                 if (_random) {
108                         _current = (count + (qrand()  + qrand() + qrand()) % count) % count;
109                 } else {
110                         _current = _current + 1;
111                 }
112         }
113         if (_random && _history.count() >= count && !_repeat ||
114                 !_repeat && _current >= count) {
115                 _history.clear();
116                 stop();
117         } else {
118                 _current %= count;
119                 _track = _playlist.tracks().at(_current);
120                 _set_source();
121                 play();
122         }
123 }
124
125 void Player::_set_source() {
126         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
127         emit trackChanged(_track);
128 }
129
130 void Player::prev() {
131         if (_history.count() > 0) {
132                 _prev_history.push(_current);
133                 _current = _history.pop();
134                 _track = _playlist.tracks().at(_current);
135         }
136         _set_source();
137         play();
138 }
139
140 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
141         switch (newState) {
142         case Phonon::PlayingState:
143                 if (_state == PLAYER_LOADING) {
144                         _state = PLAYER_PLAYING;
145                         emit stateChanged(_state);
146                 }
147                 break;
148         case Phonon::StoppedState:
149                 break;
150         case Phonon::LoadingState:
151                 break;
152         case Phonon::PausedState:
153                 if (_state == PLAYER_PLAYING) {
154                         next();
155                 } else if (_state == PLAYER_ERROR) {
156                         play();
157                 }
158                 break;
159         case Phonon::BufferingState:
160                 break;
161         case Phonon::ErrorState:
162                 _state = PLAYER_ERROR;
163                 break;
164         }
165 }
166
167 void Player::_tick(qint64 ticks) {
168         int done = ticks/1000;
169         int all = _track.metadata().length();
170         emit tick(done, all);
171         if (done+2 == all) {
172                 _track.setCount(_track.count()+1);
173                 emit trackDone(_track);
174         }
175 }
176
177 void Player::setPlaylist(Playlist playlist) {
178         _playlist = playlist;
179         _history.clear();
180         _prev_history.clear();
181         _queue.clear();
182 }
183
184 void Player::seek(int s) {
185         _player->seek(s*1000);
186 }
187
188 void Player::play() {
189         if (_playlist.tracks().isEmpty())
190                 return;
191         _state = PLAYER_PLAYING;
192         emit stateChanged(_state);
193         if (_current == -1) {
194                 _current = 0;
195                 _track = _playlist.tracks().at(0);
196                 _set_source();
197         }
198         _player->play();
199 }
200
201 void Player::enqueue(int id) {
202         _queue.enqueue(id);
203 }
204
205 void Player::toggleRandom() {
206         _random = !_random;
207         _config.setValue("playback/random", _random);
208 }
209
210 void Player::toggleRepeat() {
211         _repeat = !_repeat;
212         _config.setValue("playback/repeat", _repeat);
213 }
214
215 void Player::setVolume(int v) {
216         _output->setVolume(v*0.01);
217 }
218
219 void Player::equalizerValue(int band, double *val) {
220         if (band < 0 || band > 9) {
221                 *val = -24;
222                 return;
223         }
224         if (_equalizer_enabled) {
225                 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
226                 QVariant var = _equalizer->parameterValue(plist[band]);
227                 *val = var.toDouble();
228         }
229 }
230
231 void Player::enableEqualizer() {
232         _equalizer_enabled = true;
233         _path.insertEffect(_equalizer);
234         Config config;
235         config.setValue("equalizer/equalizer", "enabled");
236 }
237
238 void Player::disableEqualizer() {
239         _equalizer_enabled = false;
240         _path.removeEffect(_equalizer);
241         Config config;
242         config.setValue("equalizer/equalizer", "disabled");
243 }
244
245 void Player::setEqualizerValue(int band, double value) {
246         if (band < 0 || band > 9 || value < -24 || value > 12) {
247                 return;
248         }
249         QList<Phonon::EffectParameter> plist = _equalizer->parameters();
250         _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
251         Config config;
252         config.setValue(QString("equalizer/band%1").arg(band), value);
253 }