RPG Music’N’SFX – The “Previous Song” Function

(Original post from 25.11.2011)

Another little bloglet about my soon-to-be oh so awesome RPG Audioplayer. This time, I have been wondering how to implement a useable previous function.

I decided to create a list of integers, where each integer represents an index of the playlist. Initially, this list will collect the previous indices of the list, e.g. a list of 40 songs. It starts at 0 and goes regularly and playing the first 5 songs, i.e. the list will be [0,1,2,3,4]. Then it is switched to random and songs number 34 and 27 are played, leaving the list at [0,1,2,3,4,33,26]. This will go on until a certain threshold length (say 20 items). Once it reaches length 20, the first item will be removed and the latest one will be appended to the end. That way, this list will not reach ridiculous lengths when used for a long time.

When someone moves backwards, the new index will be the last item in the list, which will then be removed from the list. If someone then truly presses 20 times previous, then the code should just check for an empty list and stay at its current index.

How the audiere code is introduced with first resetting and only if x milliseconds were run before allowing a previous to be called, remains to be seen, but we’ll cross that bridge when we come to it.

So talking in code all the above operations should be standard python fare. Let’s try it:

l = range(0,5)
for i in range(15):
    l.append(random.choice(xrange(41)))

# example l is:
# [0, 1, 2, 3, 4, 14, 17, 33, 12, 20, 16, 7, 28, 
# 38, 24, 18, 29, 5, 36]

# Another index is added
l.pop(0)
l.append(nex_index)

# New list is:
# [1, 2, 3, 4, 14, 17, 33, 12, 20, 16, 7, 28, 
# 38, 24, 18, 29, 5, 36, new_index]

# Someone presses "Previous"
if l = []: # when someone presses previous 20 times
    new_index = index
else:
    new_index = l.pop() # removes last list item

I’m a little surprised myself now, that I only had to use the list.pop() method, but indeed using it, should solve all the problems outlined here.

I shall now implement them and see if it works πŸ˜€

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s