Playlist track cur_index

We should track the current song index between sessions.  This means we
need to set it when selecting a random song.  This also means we need to
save the playlist on each song selection.
This commit is contained in:
Bryan Schumaker 2010-12-01 22:33:07 -05:00
parent d20693bba7
commit 7453a4e5a1

View File

@ -16,7 +16,7 @@ visible = None
recent = None recent = None
recent_msg = "Skipping %s by %s because it has played recently" recent_msg = "Skipping %s by %s because it has played recently"
skip_msg = "Skipping %s by %s because I don't think you like it" skip_msg = "Skipping %s by %s because I don't think you like it"
cur_index = -1 cur_index = None
def add_id(id): def add_id(id):
global song_list global song_list
@ -33,22 +33,27 @@ def reset():
global song_list global song_list
global song_set global song_set
global recent global recent
global cur_index
recent = [] recent = []
song_list = [] song_list = []
song_set = set() song_set = set()
cur_index = -1
def load(): def load():
global song_list global song_list
global song_set global song_set
global recent global recent
global visible global visible
global cur_index
objects = libsaria.path.files.load("playlist", ".list") objects = libsaria.path.files.load("playlist", ".list")
if objects == None or len(objects) != 2: if objects == None or len(objects) != 3:
reset() reset()
else: else:
song_list = objects[0] song_list = objects[0]
recent = objects[1] recent = objects[1]
cur_index = objects[2]
song_set = set(song_list) song_set = set(song_list)
libsaria.sources.cur_lib_id = song_list[cur_index]
def load_bg2(callback): def load_bg2(callback):
load() load()
@ -61,7 +66,7 @@ def load_bg(callback):
thr.start() thr.start()
def save(): def save():
libsaria.path.files.save( (song_list, recent), "playlist", ".list") libsaria.path.files.save( (song_list, recent, cur_index), "playlist", ".list")
def walk(): def walk():
global song_list global song_list
@ -95,15 +100,21 @@ def seq_next():
global cur_index global cur_index
global song_list global song_list
cur_index += 1 cur_index += 1
max_index = len(song_list)
if cur_index == max_index:
cur_index = 0
while is_visible(song_list[cur_index]) == False: while is_visible(song_list[cur_index]) == False:
cur_index += 1 cur_index += 1
if cur_index == max_index:
cur_index = 0
return song_list[cur_index] return song_list[cur_index]
def rand_candidate(list, max): def rand_candidate(list, max):
index = rand.randint(0, max-1) index = rand.randint(0, max-1)
return list[index] return list[index], index
def rand_next(): def rand_next():
global cur_index
n = num_visible() n = num_visible()
if n == 0: if n == 0:
return return
@ -115,7 +126,7 @@ def rand_next():
selection = list(visible) selection = list(visible)
for i in xrange(15): for i in xrange(15):
id = rand_candidate(selection, n) id, index = rand_candidate(selection, n)
(artist, title, score) = get_attrs(id, "artist", "title", "score") (artist, title, score) = get_attrs(id, "artist", "title", "score")
if (artist, title) in recent: if (artist, title) in recent:
print recent_msg % (artist, title) print recent_msg % (artist, title)
@ -130,12 +141,23 @@ def rand_next():
recent.pop(0) recent.pop(0)
if i > 0: if i > 0:
print "Picking a song took %s iterations" % i print "Picking a song took %s iterations" % i
cur_index = index
return id return id
def play_id(id):
global cur_index
cur_index = song_list.index(id)
library.play_id(id)
save()
def next(): def next():
id = None
if len(song_list) == 0:
return
if libsaria.prefs.get_pref("libsaria.random") == True: if libsaria.prefs.get_pref("libsaria.random") == True:
id = rand_next() id = rand_next()
else: else:
id = seq_next() id = seq_next()
if id != None: if id != None:
save()
return call("NEXT", library.play_id, id) return call("NEXT", library.play_id, id)