<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import threading <EOL> import time <EOL> import mt_media <EOL> import pyglet <EOL> _debug = pyglet . options [ '<STR_LIT>' ] <EOL> class SilentAudioPlayer ( mt_media . AbstractAudioPlayer ) : <EOL> _buffer_time = <NUM_LIT> <EOL> _min_update_bytes = <NUM_LIT> <EOL> def __init__ ( self , source_group , player ) : <EOL> super ( SilentAudioPlayer , self ) . __init__ ( source_group , player ) <EOL> self . _timestamp = <NUM_LIT:0.> <EOL> self . _timestamp_time = time . time ( ) <EOL> self . _worker_timestamp = <NUM_LIT:0.> <EOL> self . _events = [ ] <EOL> self . _lock = threading . Lock ( ) <EOL> self . _playing = False <EOL> self . _worker_thread = threading . Thread ( target = self . _worker_func ) <EOL> self . _worker_thread . setDaemon ( True ) <EOL> self . _worker_thread . start ( ) <EOL> def delete ( self ) : <EOL> pass <EOL> def play ( self ) : <EOL> if self . _playing : <EOL> return <EOL> self . _playing = True <EOL> self . _timestamp_time = time . time ( ) <EOL> def stop ( self ) : <EOL> if not self . _playing : <EOL> return <EOL> self . _timestamp = self . get_time ( ) <EOL> self . _playing = False <EOL> def seek ( self , timestamp ) : <EOL> self . _lock . acquire ( ) <EOL> self . _timestamp = timestamp <EOL> self . _worker_timestamp = timestamp <EOL> self . _timestamp_time = time . time ( ) <EOL> self . _lock . release ( ) <EOL> def clear ( self ) : <EOL> self . _lock . acquire ( ) <EOL> self . _events = [ ] <EOL> self . _lock . release ( ) <EOL> def get_time ( self ) : <EOL> if self . _playing : <EOL> return self . _timestamp + ( time . time ( ) - self . _timestamp_time ) <EOL> else : <EOL> return self . _timestamp <EOL> def _worker_func ( self ) : <EOL> buffered_time = <NUM_LIT:0.> <EOL> self . _lock . acquire ( ) <EOL> self . _worker_timestamp = <NUM_LIT:0.0> <EOL> self . _lock . release ( ) <EOL> while True : <EOL> self . _lock . acquire ( ) <EOL> timestamp = self . get_time ( ) <EOL> buffered_time -= timestamp - self . _worker_timestamp <EOL> self . _worker_timestamp = timestamp <EOL> if _debug : <EOL> print '<STR_LIT>' % timestamp <EOL> events = self . _events <EOL> while events and events [ <NUM_LIT:0> ] . timestamp <= timestamp : <EOL> events [ <NUM_LIT:0> ] . _sync_dispatch_to_player ( self . player ) <EOL> del events [ <NUM_LIT:0> ] <EOL> if events : <EOL> next_event_timestamp = events [ <NUM_LIT:0> ] . timestamp <EOL> else : <EOL> next_event_timestamp = None <EOL> self . _lock . release ( ) <EOL> secs = self . _buffer_time - buffered_time <EOL> bytes = secs * self . source_group . audio_format . bytes_per_second <EOL> if _debug : <EOL> print '<STR_LIT>' % ( bytes , secs ) <EOL> if bytes < self . _min_update_bytes : <EOL> sleep_time = buffered_time / <NUM_LIT:2> <EOL> if next_event_timestamp is not None : <EOL> sleep_time = min ( sleep_time , <EOL> next_event_timestamp - timestamp ) <EOL> if _debug : <EOL> print '<STR_LIT>' % sleep_time <EOL> time . sleep ( sleep_time ) <EOL> continue <EOL> audio_data = self . source_group . get_audio_data ( int ( bytes ) ) <EOL> if not audio_data : <EOL> mt_media . MediaEvent ( timestamp , <EOL> '<STR_LIT>' ) . _sync_dispatch_to_player ( self . player ) <EOL> break <EOL> buffered_time += audio_data . duration <EOL> self . _lock . acquire ( ) <EOL> self . _events . extend ( audio_data . events ) <EOL> self . _lock . release ( ) <EOL> if _debug : <EOL> print '<STR_LIT>' % audio_data . duration <EOL> print '<STR_LIT>' % buffered_time <EOL> print '<STR_LIT>' % events <EOL> class SilentAudioDriver ( mt_media . AbstractAudioDriver ) : <EOL> def create_audio_player ( self , source_group , player ) : <EOL> return SilentAudioPlayer ( source_group , player ) <EOL> def create_audio_driver ( ) : <EOL> return SilentAudioDriver ( ) </s>
