From c35f2103691596ee35f2e53094df9076782a7e61 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Mon, 25 Oct 2010 20:33:22 -0400 Subject: [PATCH] Playlist random improvements I now keep track of the last 15 songs played. If a song with the same (artist, title) as one of the songs in the list attempts to play, we skip it. Songs with a negative score have a (20 * score) + 100 per cent chance of playing. When score is -5, the song should never play. I used to try to find a song 5 times before giving up and playing the song with the current id. I have upped the threshold to 15. --- libsaria/collection/lens.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/libsaria/collection/lens.py b/libsaria/collection/lens.py index 03d5fe7e..27a89c40 100644 --- a/libsaria/collection/lens.py +++ b/libsaria/collection/lens.py @@ -73,6 +73,7 @@ class Library(collection.Collection): class Playlist(collection.Collection): def __init__(self): collection.Collection.__init__(self, "playlist.dl_tree") + self.last_tracks = [] def next_id(self, last_id): return_next = False @@ -94,13 +95,26 @@ class Playlist(collection.Collection): if self.size == 0: return getattr = libsaria.collection.lib_get_attr - for i in xrange(5): + last = self.last_tracks + for i in xrange(15): id = self.get_rand_candidate() - if getattr(id, "score") >= 0: - return id - play_anyway = rand.randint(0, 1) - if play_anyway == 1: - return id + artist = getattr(id, "artist") + album = getattr(id, "album") + title = getattr(id, "title") + if (artist, title) in last: + print "Skipping %s by %s because it has played recently." % (title, artist) + continue + score = getattr(id, "score") + if score < 0: + play_anyway = rand.randint(0, 100) + if play_anyway < ((20 * score) + 100): + print "Skipping %s by %s because I don't think you want to hear it." % (title, artist) + continue + last.append((artist, title)) + if len(last) > 15: + last.pop(0) + print "Picking a song took %s iterations" % i + return id return id def get_rand_candidate(self):