469,286 Members | 2,407 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

resume picking items from a previous list

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()

Apr 29 '06 #1
5 1236
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
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Apr 30 '06 #2
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/
Apr 30 '06 #3
"kyo guan" wrote:
for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
items[i+1] = items[i];


have you benchmarked this on a wide variety of platforms, or are
you just guessing?

</F>

Apr 30 '06 #4

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...


Not sure I follow. Jessica goes to bed, and you... _listen to music_??

Apr 30 '06 #5
On 30 Apr 2006 05:33:39 -0700, rumours say that "wi*********@gmail.com"
<wi*********@gmail.com> might have written:

kpp9c wrote:
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...

Not sure I follow. Jessica goes to bed, and you... _listen to music_??


He said his *wife*, Jessica Alba. There's *no* way you marry *anyone* you
won't get bored of eventually.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
May 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Guarnieri | last post: by
6 posts views Thread by M. Clift | last post: by
reply views Thread by Chuck Anderson | last post: by
1 post views Thread by discussions | last post: by
1 post views Thread by Jorge | last post: by
13 posts views Thread by PetterL | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.