using namespace SomePlayer::DataObjects;
using namespace SomePlayer::Storage;
+int Randomizer::next() {
+ int res = 0;
+ if (_current == _rand.count()) {
+ _shuffle();
+ _current = 0;
+ res = next();
+ } else {
+ res = _rand.at(_current);
+ }
+ ++_current;
+ return res;
+}
+
+void Randomizer::setPlaylist(QList<int> pl) {
+ _playlist = pl;
+ _current = 0;
+ _shuffle();
+}
+
+void Randomizer::_shuffle() {
+ _rand.clear();
+ // Fisher-Yates algorithm:
+ _rand = _playlist;
+ int cnt = _playlist.count();
+ int j = 0;
+ int tmp = 0;
+ for (int i = cnt-1; i > 0; i--) {
+ j = qrand() % (i+1);
+ tmp = _rand[i];
+ _rand[i] = _rand[j];
+ _rand[j] = tmp;
+ }
+}
+
Player::Player(QObject *parent) :
QObject(parent)
{
_player = new Phonon::MediaObject(this);
_output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
_player->setTickInterval(1000);
- _equalizer == NULL;
- _equalizer_enabled == false;
+ _equalizer = NULL;
+ _equalizer_enabled = false;
+ connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
+ connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
+ _path = Phonon::createPath(_player, _output);
QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
foreach (Phonon::EffectDescription desc, effects) {
if (desc.name() == "equalizer-10bands") {
QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
setEqualizerValue(i, var.toDouble());
}
- _equalizer_enabled = true;
+ enableEqualizer();
} else if (config.getValue("equalizer/equalizer") == "") {
for (int i = 0; i < 10; i++) {
config.setValue(QString("equalizer/band%1").arg(i), 0);
}
}
}
- connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
- connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
- _path = Phonon::createPath(_player, _output);
int seed = QTime::currentTime().msec();
qsrand(seed);
_random = _config.getValue("playback/random").toBool();
_current = _prev_history.pop();
} else {
if (_random) {
- _current = (count + (qrand() + qrand() + qrand()) % count) % count;
+ _current = _randomizer.next();
} else {
_current = _current + 1;
}
case Phonon::BufferingState:
break;
case Phonon::ErrorState:
- _state = PLAYER_ERROR;
+ play(); // force
+// _state = PLAYER_ERROR;
break;
}
}
_history.clear();
_prev_history.clear();
_queue.clear();
+ QList<int> ids;
+ int count = playlist.tracks().count();
+ for (int i = 0; i < count; i++) {
+ ids.append(i);
+ }
+ _randomizer.setPlaylist(ids);
}
void Player::seek(int s) {
}
void Player::equalizerValue(int band, double *val) {
+ if (_equalizer == NULL) {
+ *val = 0;
+ return;
+ }
if (band < 0 || band > 9) {
*val = -24;
return;
}
void Player::enableEqualizer() {
+ if (_equalizer == NULL)
+ return;
_equalizer_enabled = true;
_path.insertEffect(_equalizer);
Config config;
}
void Player::disableEqualizer() {
+ if (_equalizer == NULL)
+ return;
_equalizer_enabled = false;
_path.removeEffect(_equalizer);
Config config;
}
void Player::setEqualizerValue(int band, double value) {
+ if (_equalizer == NULL)
+ return;
if (band < 0 || band > 9 || value < -24 || value > 12) {
return;
}
Config config;
config.setValue(QString("equalizer/band%1").arg(band), value);
}
+
+QString Player::artist() {
+ if (_current < 0)
+ return "";
+ return _playlist.tracks().at(_current).metadata().artist();
+}
+
+QString Player::album() {
+ if (_current < 0)
+ return "";
+ return _playlist.tracks().at(_current).metadata().album();
+}
+
+QString Player::title() {
+ if (_current < 0)
+ return "";
+ return _playlist.tracks().at(_current).metadata().title();
+}