Changeset 170:7cda0cc74e19 for src/mlx/sound.py
- Timestamp:
- 05/12/12 15:48:45 (12 years ago)
- Branch:
- default
- hg-Phase:
- (<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/sound.py
r133 r170 4 4 5 5 import os 6 import traceback 6 7 7 8 #------------------------------------------------------------------------------ … … 50 51 51 52 self._requestCondition = threading.Condition() 52 self._request edPaths = []53 self._pending Aliases= []53 self._requests = [] 54 self._pending = [] 54 55 self._count = 0 55 56 56 57 self.daemon = True 57 58 58 def requestSound(self, name ):59 def requestSound(self, name, finishCallback = None, extra = None): 59 60 """Request the playback of the sound with the given name.""" 60 61 path = os.path.join(self._soundsDirectory, name) 61 62 with self._requestCondition: 62 self._request edPaths.append(path)63 self._requests.append((path, (finishCallback, extra))) 63 64 self._requestCondition.notify() 64 65 … … 72 73 while True: 73 74 with self._requestCondition: 74 if not self._request edPaths:75 if self._pending Aliases:76 timeout = max(time.time() - 77 self._pendingAliases[0][0],0.0)75 if not self._requests: 76 if self._pending: 77 timeout = max(time.time() - self._pending[0][0], 78 0.0) 78 79 else: 79 80 timeout = 10.0 … … 81 82 self._requestCondition.wait(timeout) 82 83 83 request edPaths = []84 for path in self._requestedPaths:85 request edPaths.append((path, self._count))84 requests = [] 85 for (path, finishData) in self._requests: 86 requests.append((path, finishData, self._count)) 86 87 self._count += 1 87 self._request edPaths = []88 self._requests = [] 88 89 89 90 now = time.time() 90 aliasesToClose = []91 while self._pending Aliasesand \92 self._pending Aliases[0][0]<=now:93 aliasesToClose.append(self._pendingAliases[0][1])94 del self._pending Aliases[0]91 toClose = [] 92 while self._pending and \ 93 self._pending[0][0]<=now: 94 toClose.append(self._pending[0][1]) 95 del self._pending[0] 95 96 96 for alias in aliasesToClose: 97 for (alias, (finishCallback, extra)) in toClose: 98 success = True 97 99 try: 98 100 print "Closing", alias … … 101 103 except Exception, e: 102 104 print "Failed closing " + alias + ":", str(e) 105 success = False 103 106 104 for (path, counter) in requestedPaths: 107 if finishCallback is not None: 108 try: 109 finishCallback(success, extra) 110 except: 111 traceback.print_exc() 112 113 for (path, finishData, counter) in requests: 105 114 try: 106 115 alias = "mlxsound%d" % (counter,) … … 117 126 timeout = time.time() + length / 1000.0 118 127 with self._requestCondition: 119 self._pending Aliases.append((timeout, alias))120 self._pending Aliases.sort()128 self._pending.append((timeout, (alias, finishData))) 129 self._pending.sort() 121 130 print "Started to play", path 122 131 except Exception, e: 123 print "Failed to start playing " + path + ":", str(e) 132 print "Failed to start playing " + path + ":", str(e) 133 (finishCallback, extra) = finishData 134 if finishCallback is not None: 135 try: 136 finishCallback(None, extra) 137 except: 138 traceback.print_exc() 124 139 125 140 _thread = None … … 132 147 _thread.start() 133 148 134 def startSound(name ):149 def startSound(name, finishCallback = None, extra = None): 135 150 """Start playing back the given sound. 136 151 137 152 name should be the name of a sound file relative to the sound directory 138 153 given in initializeSound.""" 139 _thread.requestSound(name) 154 _thread.requestSound(name, finishCallback = finishCallback, 155 extra = extra) 140 156 141 157 #------------------------------------------------------------------------------ … … 147 163 pass 148 164 149 def startSound(name ):165 def startSound(name, finishCallback = None, extra = None): 150 166 """Start playing back the given sound. 151 167 … … 157 173 158 174 if __name__ == "__main__": 175 def callback(result, extra): 176 print "callback", result, extra 177 159 178 initializeSound("e:\\home\\vi\\tmp") 160 startSound("malev.mp3" )179 startSound("malev.mp3", finishCallback = callback, extra="malev.mp3") 161 180 time.sleep(5) 162 startSound("ding.wav" )181 startSound("ding.wav", finishCallback = callback, extra="ding1.wav") 163 182 time.sleep(5) 164 startSound("ding.wav" )183 startSound("ding.wav", finishCallback = callback, extra="ding2.wav") 165 184 time.sleep(5) 166 startSound("ding.wav" )185 startSound("ding.wav", finishCallback = callback, extra="ding3.wav") 167 186 time.sleep(50) 168 187
Note:
See TracChangeset
for help on using the changeset viewer.