Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Asynchronous audio player class

  • Loading branch information...
commit f35199a26546d8dc47016ddd6974f35a5d8f3f64 1 parent cf1bc67
@akkana authored
Showing with 66 additions and 0 deletions.
  1. +66 −0 pyplay.py
View
66 pyplay.py
@@ -0,0 +1,66 @@
+#! /usr/bin/env python
+
+# A class to play sounds asynchronously, but not overlapping.
+#
+# Copyright 2012 by Akkana Peck, http://shallowsky.com.
+# Share and enjoy under the GPLv2 or (at your option) any later version.
+
+# Rules:
+#
+
+import sys
+import subprocess
+
+class SoundPlayer :
+ """
+ Play sounds that don't overlap in time.
+ """
+
+ PLAYER = "/usr/bin/aplay"
+
+ def __init__(self) :
+ self.curpath = None
+ self.current = None
+
+ def __del__(self) :
+ print "__del__ : Waiting for last play"
+ self.wait()
+
+ def play(self, path) :
+ if self.current :
+ if self.current.poll() == None :
+ # Current process hasn't finished yet. Is this the same sound?
+ if path == self.curpath :
+ # A repeat of the currently playing sound.
+ # Don't play it more than once.
+ print path, "is still playing. Not playing again"
+ return
+ else :
+ # Trying to play a different sound.
+ # Wait on the current sound then play the new one.
+ print "Different sound; first waiting for", self.curpath
+ self.wait()
+ self.current = None
+ self.curpath = None
+
+ print "Trying to play", path
+
+ self.curpath = path
+ self.current = subprocess.Popen([ SoundPlayer.PLAYER, '-q', path ] )
+
+ def wait(self) :
+ if self.current and self.current.poll() == None :
+ self.current.wait()
+
+# To test this class, run this file with a list of paths to sound files,
+# and make sure to repeat some of them.
+# For instance,
+# pyplay.py pop.wav pop.wav pop.wav meow.wav meow.wav pop.wav pop.wav
+if __name__ == "__main__" :
+ if len(sys.argv) < 1 :
+ print "This test doesn't make much sense without some sound arguments"
+ sys.exit(1)
+ player = SoundPlayer()
+ for arg in sys.argv[1:] :
+ player.play(arg)
+ player.wait()
Please sign in to comment.
Something went wrong with that request. Please try again.