473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
6
by: M. Clift | last post by:
Hi All, I have tried to come up with a way to do this myself and all I end up with is very long code. What I have is a say item1, item4, item2, item1 etc... What I want to do is append to...
0
by: Chuck Anderson | last post by:
I am writing a Php script to run on my home PC (Windows) that downloads an Apache access log file and inserts new entries into a database.. The only way I can access these log files is through a...
1
by: discussions | last post by:
Hello all, I would like some advice how best to approach the following problem. I have "sort of" solved it but in a very horrible and complex way, I'm sure there's a better, simple and more...
1
by: Jorge | last post by:
Hi Noor If your process IS a service then yes you suspend or resume it. Kind Regards Jorge
0
MMcCarthy
by: MMcCarthy | last post by:
Organizing a Resume Pertinent information included on your resume may be organized in a variety of ways. Regardless of the type of resume used, you should include the following: Contact...
9
by: beginner | last post by:
Hello, We are writing a performance critical application. As a result, we use Array instead of List or any other more sophisticated container types to store temporary data. However, the array...
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.