"""Windows only, plays random wav files at random intervals. Run at command prompt with -h to see options. One function available: playRandomSounds(directory, minSecs, maxSecs, runTime)""" import winsound import glob import os import sys import random import time #----------------------------------------- def getRandomSound(directory): allSounds = glob.glob(os.path.join(directory, "*.wav")) if len(allSounds) == 0: print 'No wav files found in %s' % directory return return random.choice(allSounds) #----------------------------------------- def usage(err = None): if err: print err print print """Plays a random wav file at random intervals. Options: -d Where to find the wav files. Will default to \\wav. -l How long to run. Pass 0 to go forever (use ctrl-c to exit). Defaults to 0. -m This is the shortest time between sounds. Defaults to 60 seconds. -x This is the longest time between sounds. Defaults to 300 seconds (5 minutes). Brought to you by Megabyte Rodeo Software. Visit us online at http://www.megabyterodeo.com and http://www.fetidcascade.com""" sys.exit(0) #----------------------------------------- def main(): import getopt try: opts, args = getopt.getopt(sys.argv[1:], "l:m:x:d:") directory = os.path.join(os.path.dirname(sys.argv[0]), 'wav') runTime = 0 minSecs = 60 maxSecs = 300 for o, a in opts: if o == "-d": directory = a if o == "-l": runTime = int(a) if o == "-m": minSecs = int(a) if o == "-x": maxSecs = int(a) if minSecs > maxSecs: raise "no bueno" except: usage() playRandomSounds(directory, minSecs, maxSecs, runTime) #----------------------------------------- def playRandomSounds(directory, minSecs, maxSecs, runTime): try: startTime = time.time() endTime = startTime + runTime while 1: sound = getRandomSound(directory) if sound: print 'Playing %s' % sound winsound.PlaySound(sound, winsound.SND_FILENAME) if not runTime == 0 and time.time() > endTime: print "Time's up, exiting" sys.exit(0) sleepSeconds = random.randint(minSecs, maxSecs) print '%s: Sleeping for %s seconds' % (time.strftime('%H:%M:%S'), sleepSeconds) time.sleep(sleepSeconds) except (KeyboardInterrupt, SystemExit): print "Bye!" except Exception, ex: print "Oops, something wrong: %s" % ex.__class__.__name__ #----------------------------------------- if __name__ == '__main__': main()