Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/sound.py

    r422 r401  
    166166    def startSound(name, finishCallback = None, extra = None):
    167167        """Start playing back the given sound.
    168 
     168       
    169169        name should be the name of a sound file relative to the sound directory
    170170        given in initializeSound."""
    171171        _thread.requestSound(name, finishCallback = finishCallback,
    172172                             extra = extra)
    173 
     173       
    174174#------------------------------------------------------------------------------
    175175
    176176else: # os.name!="nt"
    177     import threading
    178     try:
    179         import pyglet
    180 
    181         class SoundThread(threading.Thread):
    182             """A thread executing the pyglet event loop that directs the
    183             playback of the sound files."""
    184             class Player(pyglet.media.ManagedSoundPlayer):
    185                 """Player which handles the end-of-stream condition
    186                 properly."""
    187 
    188                 def __init__(self, finishCallback, extra):
    189                     """Construct the player with the given data."""
    190                     super(SoundThread.Player, self).__init__()
    191 
    192                     self._finishCallback = finishCallback
    193                     self._extra = extra
    194 
    195                 def _on_eos(self):
    196                     if self._finishCallback is not None:
    197                         self._finishCallback(True, self._extra)
    198                     return super(SoundThread.Player, self)._on_eos()
    199 
    200             class EventLoop(pyglet.app.EventLoop):
    201                 """Own implementation of the event loop that collects the
    202                 requested sound files and plays them."""
    203 
    204                 def __init__(self, soundsDirectory):
    205                     """Construct the event loop."""
    206                     super(SoundThread.EventLoop, self).__init__()
    207 
    208                     self._soundsDirectory = soundsDirectory
    209 
    210                     self._lock = threading.Lock()
    211                     self._requestedSounds = []
    212 
    213                 def startSound(self, name, finishCallback, extra):
    214                     """Add the sound with the given name"""
    215                     with self._lock:
    216                         path = os.path.join(self._soundsDirectory, name)
    217                         self._requestedSounds.append( (path,
    218                                                        finishCallback, extra) )
    219 
    220                 def idle(self):
    221                     """The idle callback."""
    222                     with self._lock:
    223                         requestedSounds = self._requestedSounds
    224                         self._requestedSounds = []
    225 
    226                     for (path, finishCallback, extra) in requestedSounds:
    227                         try:
    228                             media = pyglet.media.load(path)
    229                             player = SoundThread.Player(finishCallback, extra)
    230                             player.queue(media)
    231                             player.play()
    232                         except Exception, e:
    233                             print "mlx.SoundThread.EventLoop.idle: " + str(e)
    234                             if finishCallback is not None:
    235                                 finishCallback(False, extra)
    236 
    237                     timeout = super(SoundThread.EventLoop, self).idle()
    238                     return 0.1 if timeout is None else min(timeout, 0.1)
    239 
    240             def __init__(self, soundsDirectory):
    241                 """Construct the sound playing thread with the given
    242                 directory."""
    243                 super(SoundThread, self).__init__()
    244 
    245                 self.daemon = True
    246                 self.eventLoop = SoundThread.EventLoop(soundsDirectory)
    247 
    248             def run(self):
    249                 """Run the event loop."""
    250                 self.eventLoop.run()
    251 
    252             def startSound(self, name, finishCallback = None, extra = None):
    253                 """Start the playback of the given sound."""
    254                 self.eventLoop.startSound(name, finishCallback, extra)
    255 
    256         _thread = None
    257 
    258         def initializeSound(soundsDirectory):
    259             """Initialize the sound handling with the given directory containing
    260             the sound files."""
    261             global _thread
    262             _thread = SoundThread(soundsDirectory)
    263             _thread.start()
    264 
    265 
    266         def startSound(name, finishCallback = None, extra = None):
    267             """Start playing back the given sound."""
    268             _thread.startSound(name, finishCallback = finishCallback,
    269                                extra = extra)
    270 
    271     except:
    272         print "The pyglet library is missing from your system. It is needed for sound playback on Linux"
    273         def initializeSound(soundsDirectory):
    274             """Initialize the sound handling with the given directory containing
    275             the sound files."""
    276             pass
    277 
    278         def startSound(name, finishCallback = None, extra = None):
    279             """Start playing back the given sound.
    280 
    281             FIXME: it does not do anything currently, but it should."""
    282             print "sound.startSound:", name
     177    def initializeSound(soundsDirectory):
     178        """Initialize the sound handling with the given directory containing
     179        the sound files."""
     180        pass
     181
     182    def startSound(name, finishCallback = None, extra = None):
     183        """Start playing back the given sound.
     184
     185        FIXME: it does not do anything currently, but it should."""
     186        print "sound.startSound:", name
    283187
    284188#------------------------------------------------------------------------------
     
    288192    def callback(result, extra):
    289193        print "callback", result, extra
    290 
     194   
    291195    initializeSound("e:\\home\\vi\\tmp")
    292196    startSound("malev.mp3", finishCallback = callback, extra="malev.mp3")
Note: See TracChangeset for help on using the changeset viewer.