Source

pygame / examples / sound.py

Diff from to

File examples/sound.py

 and waiting for it to finish. you'll need the pygame.mixer
 module for this to work. Note how in this simple example we
 don't even bother loading all of the pygame package. Just
-pick the mixer for sound and time for the delay function."""
+pick the mixer for sound and time for the delay function.
+
+Optional command line argument:
+  the name of an audio file.
+  
+
+"""
 
 import os.path, sys
 import pygame.mixer, pygame.time
 mixer = pygame.mixer
 time = pygame.time
 
-#choose a desired audio format
-mixer.init(11025) #raises exception on fail
+main_dir = os.path.split(os.path.abspath(__file__))[0]
 
+def main(file_path=None):
+    """Play an audio file as a buffered sound sample
 
-#load the sound    
+    Option argument:
+        the name of an audio file (default data/secosmic_low.wav
 
-if len(sys.argv) > 1 and "wav" in sys.argv[1]:
-    file = sys.argv[1]
-else:
-    file = os.path.join('data', 'secosmic_lo.wav')
-sound = mixer.Sound(file)
+    """
+    if file_path is None:
+        file_path = os.path.join(main_dir,
+                                 'data',
+                                 'secosmic_lo.wav')
 
+    #choose a desired audio format
+    mixer.init(11025) #raises exception on fail
 
-#start playing
-print 'Playing Sound...'
-channel = sound.play()
 
+    #load the sound    
+    sound = mixer.Sound(file_path)
 
-#poll until finished
-while channel.get_busy(): #still playing
-    print '  ...still going...'
-    time.wait(1000)
-print '...Finished'
 
+    #start playing
+    print 'Playing Sound...'
+    channel = sound.play()
 
 
+    #poll until finished
+    while channel.get_busy(): #still playing
+        print '  ...still going...'
+        time.wait(1000)
+    print '...Finished'
+
+if __name__ == '__main__':
+    if len(sys.argv) > 1:
+        main(sys.argv[1])
+    else:
+        main()