Ignore:
Timestamp:
05/12/12 15:48:45 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

The background sounds play back properly

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/sound.py

    r133 r170  
    44
    55import os
     6import traceback
    67
    78#------------------------------------------------------------------------------
     
    5051
    5152            self._requestCondition = threading.Condition()
    52             self._requestedPaths = []
    53             self._pendingAliases = []
     53            self._requests = []
     54            self._pending = []
    5455            self._count = 0
    5556
    5657            self.daemon = True
    5758
    58         def requestSound(self, name):
     59        def requestSound(self, name, finishCallback = None, extra = None):
    5960            """Request the playback of the sound with the given name."""
    6061            path = os.path.join(self._soundsDirectory, name)
    6162            with self._requestCondition:
    62                 self._requestedPaths.append(path)
     63                self._requests.append((path, (finishCallback, extra)))
    6364                self._requestCondition.notify()
    6465
     
    7273            while True:
    7374                with self._requestCondition:
    74                     if not self._requestedPaths:
    75                         if self._pendingAliases:
    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)
    7879                        else:
    7980                            timeout = 10.0
     
    8182                        self._requestCondition.wait(timeout)
    8283
    83                     requestedPaths = []
    84                     for path in self._requestedPaths:
    85                         requestedPaths.append((path, self._count))
     84                    requests = []
     85                    for (path, finishData) in self._requests:
     86                        requests.append((path, finishData, self._count))
    8687                        self._count += 1
    87                     self._requestedPaths = []
     88                    self._requests = []
    8889
    8990                    now = time.time()
    90                     aliasesToClose = []
    91                     while self._pendingAliases and \
    92                           self._pendingAliases[0][0]<=now:
    93                         aliasesToClose.append(self._pendingAliases[0][1])
    94                         del self._pendingAliases[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]
    9596
    96                 for alias in aliasesToClose:
     97                for (alias, (finishCallback, extra)) in toClose:
     98                    success = True
    9799                    try:
    98100                        print "Closing", alias
     
    101103                    except Exception, e:
    102104                        print "Failed closing " + alias + ":", str(e)
     105                        success = False
    103106
    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:
    105114                    try:
    106115                        alias = "mlxsound%d" % (counter,)
     
    117126                        timeout = time.time() + length / 1000.0
    118127                        with self._requestCondition:
    119                             self._pendingAliases.append((timeout, alias))
    120                             self._pendingAliases.sort()
     128                            self._pending.append((timeout, (alias, finishData)))
     129                            self._pending.sort()
    121130                        print "Started to play", path
    122131                    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()
    124139
    125140    _thread = None
     
    132147        _thread.start()
    133148
    134     def startSound(name):
     149    def startSound(name, finishCallback = None, extra = None):
    135150        """Start playing back the given sound.
    136151       
    137152        name should be the name of a sound file relative to the sound directory
    138153        given in initializeSound."""
    139         _thread.requestSound(name)
     154        _thread.requestSound(name, finishCallback = finishCallback,
     155                             extra = extra)
    140156       
    141157#------------------------------------------------------------------------------
     
    147163        pass
    148164
    149     def startSound(name):
     165    def startSound(name, finishCallback = None, extra = None):
    150166        """Start playing back the given sound.
    151167
     
    157173
    158174if __name__ == "__main__":
     175    def callback(result, extra):
     176        print "callback", result, extra
     177   
    159178    initializeSound("e:\\home\\vi\\tmp")
    160     startSound("malev.mp3")
     179    startSound("malev.mp3", finishCallback = callback, extra="malev.mp3")
    161180    time.sleep(5)
    162     startSound("ding.wav")
     181    startSound("ding.wav", finishCallback = callback, extra="ding1.wav")
    163182    time.sleep(5)
    164     startSound("ding.wav")
     183    startSound("ding.wav", finishCallback = callback, extra="ding2.wav")
    165184    time.sleep(5)
    166     startSound("ding.wav")
     185    startSound("ding.wav", finishCallback = callback, extra="ding3.wav")
    167186    time.sleep(50)
    168187
Note: See TracChangeset for help on using the changeset viewer.