Update position when updating the shuffler

custom
ailurux 8 months ago
parent 40c754a72a
commit 5ab4c2f0d6
  1. 23
      src/tangara/audio/track_queue.cpp
  2. 4
      src/tangara/audio/track_queue.hpp

@ -121,9 +121,12 @@ auto TrackQueue::totalSize() const -> size_t {
return sum; return sum;
} }
auto TrackQueue::updateShuffler() -> void { auto TrackQueue::updateShuffler(bool andUpdatePosition) -> void {
if (shuffle_) { if (shuffle_) {
shuffle_->resize(totalSize()); shuffle_->resize(totalSize());
if (andUpdatePosition) {
goTo(shuffle_->current());
}
} }
} }
@ -140,7 +143,7 @@ auto TrackQueue::openPlaylist(const std::string& playlist_file) -> bool {
if (!res) { if (!res) {
return false; return false;
} }
updateShuffler(); updateShuffler(true);
notifyChanged(true, Reason::kExplicitUpdate); notifyChanged(true, Reason::kExplicitUpdate);
return true; return true;
} }
@ -169,16 +172,6 @@ auto TrackQueue::append(Item i) -> void {
current_changed = was_queue_empty; // Dont support inserts yet current_changed = was_queue_empty; // Dont support inserts yet
} }
// If there wasn't anything already playing, then we should make sure we
// begin playback at a random point, instead of always starting with
// whatever was inserted first and *then* shuffling.
// We don't base this purely off of current_changed because we would like
// 'play this track now' (by inserting at the current pos) to work even
// when shuffling is enabled.
if (was_queue_empty && shuffle_) {
playlist_.skipTo(shuffle_->current());
}
if (std::holds_alternative<database::TrackId>(i)) { if (std::holds_alternative<database::TrackId>(i)) {
{ {
const std::unique_lock<std::shared_mutex> lock(mutex_); const std::unique_lock<std::shared_mutex> lock(mutex_);
@ -186,7 +179,7 @@ auto TrackQueue::append(Item i) -> void {
if (!filename.empty()) { if (!filename.empty()) {
playlist_.append(filename); playlist_.append(filename);
} }
updateShuffler(); updateShuffler(was_queue_empty);
} }
notifyChanged(current_changed, Reason::kExplicitUpdate); notifyChanged(current_changed, Reason::kExplicitUpdate);
} else if (std::holds_alternative<database::TrackIterator>(i)) { } else if (std::holds_alternative<database::TrackIterator>(i)) {
@ -213,7 +206,7 @@ auto TrackQueue::append(Item i) -> void {
} }
{ {
const std::unique_lock<std::shared_mutex> lock(mutex_); const std::unique_lock<std::shared_mutex> lock(mutex_);
updateShuffler(); updateShuffler(was_queue_empty);
} }
notifyChanged(current_changed, Reason::kExplicitUpdate); notifyChanged(current_changed, Reason::kExplicitUpdate);
}); });
@ -224,7 +217,7 @@ auto TrackQueue::next() -> void {
next(Reason::kExplicitUpdate); next(Reason::kExplicitUpdate);
} }
auto TrackQueue::goTo(size_t position) { auto TrackQueue::goTo(size_t position) -> void {
position_ = position; position_ = position;
if (opened_playlist_) { if (opened_playlist_) {
if (position_ < opened_playlist_->size()) { if (position_ < opened_playlist_->size()) {

@ -80,7 +80,7 @@ class TrackQueue {
auto insert(Item, size_t index = 0) -> void; auto insert(Item, size_t index = 0) -> void;
auto append(Item i) -> void; auto append(Item i) -> void;
auto updateShuffler() -> void; auto updateShuffler(bool andUpdatePosition) -> void;
/* /*
* Advances to the next track in the queue, placing the current track at the * Advances to the next track in the queue, placing the current track at the
@ -117,7 +117,7 @@ class TrackQueue {
private: private:
auto next(QueueUpdate::Reason r) -> void; auto next(QueueUpdate::Reason r) -> void;
auto goTo(size_t position); auto goTo(size_t position) -> void;
auto getFilepath(database::TrackId id) -> std::optional<std::string>; auto getFilepath(database::TrackId id) -> std::optional<std::string>;
mutable std::shared_mutex mutex_; mutable std::shared_mutex mutex_;

Loading…
Cancel
Save