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)
{
_current = _prev_history.pop();
} else {
if (_random) {
- _current = (count + (qrand() + qrand() + qrand()) % count) % count;
+ _current = _randomizer.next();
} else {
_current = _current + 1;
}
_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) {
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();
+}