resume picking items from a previous list

James Stroud jstroud at ucla.edu
Sat Apr 29 21:56:22 EDT 2006


James Stroud wrote:
> kpp9c wrote:
> 
>> I have a several list of songs that i pick from, lets, say that there
>> are 10 songs in each list and there are 2 lists.
>>
>> For a time i pick from my songs, but i only play a few of the songs in
>> that list... now my wife, Jessica Alba, comes home, and i start playing
>> from Jessica's list of songs. After playing a few songs, Jessica, who
>> needs her beauty sleep, goes to bed, and i start my play loop which
>> starts picking from my songs again...
>>
>> The wrinkle:
>> only now i want it to pick first from among the 6 songs yet not played
>> from the first time around, and *then* when the list is exhausted,
>> shuffle the whole original list of songs and start again.
>>
>> Here is some working but hilariously bad code that does most of this
>> funny biz... I've gotten this far, but can't figure out how to get the
>> loops to keep track of what was played and what wasn't and how to
>> pick-up the list where it left off.
>>
>> I know this is a dumb thing to want to do, but you know, being married
>> to bona-fide star is not easy.
>>
>> # -----------------------------------------------------------
>> #!/usr/bin/env python
>>
>> import random
>> import os
>>
>> def shuffleloop(iterable):
>>     """An iterator like itertools cycle, which returns elements from the
>>     iterable and saves a copy of each. When the iterable is
>>     exhausted, it return elements from the saved copy.
>>
>>     The added wrinkle here is that the saved copy is randomly shuffled.
>>     Repeats indefinitely."""
>>     saved = []
>>     for element in iterable:
>>         yield element
>>         saved.append(element)
>>     while saved:
>>         random.shuffle(saved)
>>         for element in saved:
>>             yield element
>>
>> def playall_reload(startime, playdur, smpl_lst):
>>     '''a loop that shuffles and plays all sounds in a list. If the
>>     sequence is exhausted the list is reloaded, re-shuffled, and plyed
>> through
>>     again. It does this as many times as needed to fill the time
>> specified.
>>
>>     Also returns the end of the last duration so that the begining of the
>> next
>>     section can be fed to the next function or loop.
>>     '''
>>     event = 0; incr = 0; lst_len = len(smpl_lst)
>>     random.shuffle(smpl_lst)
>>     smpl_loop = shuffleloop(smpl_lst)
>>     endpoint = startime + playdur
>>     while startime < endpoint:
>>         sample = smpl_loop.next()
>>         splt = os.path.split(sample)
>>         # get the duration of the current soundfile
>>         # (hard wire it for now)
>>         #incr = DUR()
>>         dur = 10
>>         #load the sample & play it
>>         #
>>         # input(sample)
>>         # PLAY(startime, dur)
>>         #
>>         print "event %d @ %.4f  --> [%s] dur: %.4f" % (event+1, startime,
>> splt[1], dur)
>>         incr = dur
>>         startime = startime + incr
>>         event = event + 1
>>     if (event < lst_len):
>>         print "\n\n*** Heads-up yo: <the input sequence was not 
>> exhausted>
>> ***\n\n"
>>     return startime
>>
>>
>> def test():
>>     kevins = ['/Users/kevin/snd/songs/loveisintheair.aif',
>>         '/Users/kevin/snd/songs/boymeetsgirl.aif',
>>         '/Users/kevin/snd/songs/yourcheatingheart.aif',
>>         '/Users/kevin/snd/songs/kindletheflame.aif',
>>         '/Users/kevin/snd/songs/mywifeissohot.aif',
>>         '/Users/kevin/snd/songs/haha.aif',
>>         '/Users/kevin/snd/songs/blueberryorstrawberry.aif',
>>         '/Users/kevin/snd/songs/didyoupaytheelectricbill.aif',
>>         '/Users/kevin/snd/songs/whereistheremote.aif',
>>         '/Users/kevin/snd/songs/youspenthowmuchforthoseshoes.aif']
>>
>>     jessicas = ['/Users/kevin/snd/quiet_songs/iloveu.aif',
>>         '/Users/kevin/snd/quiet_songs/uloveme.aif',
>>         '/Users/kevin/snd/quiet_songs/wearehappy.aif',
>>         '/Users/kevin/snd/quiet_songs/wearesad.aif',
>>         '/Users/kevin/snd/quiet_songs/letsbreakup.aif',
>>         '/Users/kevin/snd/quiet_songs/letsgetbacktogether.aif',
>>         '/Users/kevin/snd/quiet_songs/walkinthesunshine.aif',
>>         '/Users/kevin/snd/quiet_songs/iloveutruly.aif',
>>         '/Users/kevin/snd/quiet_songs/whosefootisthat.aif',
>>         '/Users/kevin/snd/quiet_songs/ohbaby.aif']
>>
>>     print
>>     one = playall_reload(1.00, 20.00, kevins)
>>     print
>>     two = playall_reload(one, 180, jessicas)
>>     print
>>     three = playall_reload(two, 40.00, kevins)
>>
>> if __name__ == '__main__':
>>     test()
>>
> 
> I didn't read your code, but a class might make it simpler:
> 
> class Playah(object):
>   def __init__(self, playlist):
>     self.playlist = playlist
>     self.reset()
>   def reset(self):
>     self._order = randrange(len(self.playlist))
>     self._i = 0
>   def next():
>     song = self.playlist(self._i)
>     self._i += 1
>     if self._i > len(self.playlist):
>       self.reset()
>     return song
> 
> You could probably make it an iterable if you tried.
> 
> James
> 
> 

Oops, I meant that

self._order = randrange(len(self.playlist))

should be

self._order = range(len(self.playlist))
random.shuffle(self._order)

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list