<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import time <EOL> from pyglet . media import AbstractAudioPlayer , AbstractAudioDriver , MediaThread , MediaEvent <EOL> import pyglet <EOL> _debug = pyglet . options [ '<STR_LIT>' ] <EOL> class SilentAudioPacket ( object ) : <EOL> def __init__ ( self , timestamp , duration ) : <EOL> self . timestamp = timestamp <EOL> self . duration = duration <EOL> def consume ( self , dt ) : <EOL> self . timestamp += dt <EOL> self . duration -= dt <EOL> class SilentAudioPlayerPacketConsumer ( AbstractAudioPlayer ) : <EOL> _buffer_time = <NUM_LIT> <EOL> _min_update_bytes = <NUM_LIT> <EOL> _sleep_time = <NUM_LIT> <EOL> def __init__ ( self , source_group , player ) : <EOL> super ( SilentAudioPlayerPacketConsumer , self ) . __init__ ( source_group , player ) <EOL> self . _timestamp_time = None <EOL> self . _packets = [ ] <EOL> self . _packets_duration = <NUM_LIT:0> <EOL> self . _events = [ ] <EOL> self . _playing = False <EOL> self . _thread = MediaThread ( target = self . _worker_func ) <EOL> if source_group . audio_format : <EOL> self . _thread . start ( ) <EOL> def delete ( self ) : <EOL> if _debug : <EOL> print '<STR_LIT>' <EOL> self . _thread . stop ( ) <EOL> def play ( self ) : <EOL> if _debug : <EOL> print '<STR_LIT>' <EOL> self . _thread . condition . acquire ( ) <EOL> if not self . _playing : <EOL> self . _playing = True <EOL> self . _timestamp_time = time . time ( ) <EOL> self . _thread . condition . notify ( ) <EOL> self . _thread . condition . release ( ) <EOL> def stop ( self ) : <EOL> if _debug : <EOL> print '<STR_LIT>' <EOL> self . _thread . condition . acquire ( ) <EOL> if self . _playing : <EOL> timestamp = self . get_time ( ) <EOL> if self . _packets : <EOL> packet = self . _packets [ <NUM_LIT:0> ] <EOL> self . _packets_duration -= timestamp - packet . timestamp <EOL> packet . consume ( timestamp - packet . timestamp ) <EOL> self . _playing = False <EOL> self . _thread . condition . release ( ) <EOL> def clear ( self ) : <EOL> if _debug : <EOL> print '<STR_LIT>' <EOL> self . _thread . condition . acquire ( ) <EOL> del self . _packets [ : ] <EOL> self . _packets_duration = <NUM_LIT:0> <EOL> del self . _events [ : ] <EOL> self . _thread . condition . release ( ) <EOL> def get_time ( self ) : <EOL> if _debug : <EOL> print '<STR_LIT>' <EOL> self . _thread . condition . acquire ( ) <EOL> packets = self . _packets <EOL> if self . _playing : <EOL> result = None <EOL> offset = time . time ( ) - self . _timestamp_time <EOL> while packets : <EOL> packet = packets [ <NUM_LIT:0> ] <EOL> if offset > packet . duration : <EOL> del packets [ <NUM_LIT:0> ] <EOL> self . _timestamp_time += packet . duration <EOL> offset -= packet . duration <EOL> self . _packets_duration -= packet . duration <EOL> else : <EOL> packet . consume ( offset ) <EOL> self . _packets_duration -= offset <EOL> self . _timestamp_time += offset <EOL> result = packet . timestamp <EOL> break <EOL> else : <EOL> if packets : <EOL> result = packets [ <NUM_LIT:0> ] . timestamp <EOL> else : <EOL> result = None <EOL> self . _thread . condition . release ( ) <EOL> if _debug : <EOL> print '<STR_LIT>' , result <EOL> return result <EOL> def _worker_func ( self ) : <EOL> thread = self . _thread <EOL> eos = False <EOL> events = self . _events <EOL> while True : <EOL> thread . condition . acquire ( ) <EOL> if thread . stopped or ( eos and not events ) : <EOL> thread . condition . release ( ) <EOL> break <EOL> timestamp = self . get_time ( ) <EOL> if _debug : <EOL> print '<STR_LIT>' % timestamp <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> secs = self . _buffer_time - self . _packets_duration <EOL> bytes = secs * self . source_group . audio_format . bytes_per_second <EOL> if _debug : <EOL> print '<STR_LIT>' % ( bytes , secs ) <EOL> while bytes > self . _min_update_bytes and not eos : <EOL> audio_data = self . source_group . get_audio_data ( int ( bytes ) ) <EOL> if not audio_data and not eos : <EOL> events . append ( MediaEvent ( timestamp , '<STR_LIT>' ) ) <EOL> events . append ( MediaEvent ( timestamp , '<STR_LIT>' ) ) <EOL> eos = True <EOL> break <EOL> if self . _playing and not self . _packets : <EOL> self . _timestamp_time = time . time ( ) <EOL> self . _packets . append ( SilentAudioPacket ( audio_data . timestamp , <EOL> audio_data . duration ) ) <EOL> self . _packets_duration += audio_data . duration <EOL> for event in audio_data . events : <EOL> event . timestamp += audio_data . timestamp <EOL> events . append ( event ) <EOL> events . extend ( audio_data . events ) <EOL> bytes -= audio_data . length <EOL> sleep_time = self . _sleep_time <EOL> if not self . _playing : <EOL> sleep_time = None <EOL> elif events and events [ <NUM_LIT:0> ] . timestamp and timestamp : <EOL> sleep_time = min ( sleep_time , events [ <NUM_LIT:0> ] . timestamp - timestamp ) <EOL> if _debug : <EOL> print '<STR_LIT>' , sleep_time <EOL> thread . sleep ( sleep_time ) <EOL> thread . condition . release ( ) <EOL> class SilentTimeAudioPlayer ( AbstractAudioPlayer ) : <EOL> _time = <NUM_LIT:0.0> <EOL> _systime = None <EOL> def play ( self ) : <EOL> self . _systime = time . time ( ) <EOL> def stop ( self ) : <EOL> self . _time = self . get_time ( ) <EOL> self . _systime = None <EOL> def delete ( self ) : <EOL> pass <EOL> def clear ( self ) : <EOL> pass <EOL> def get_time ( self ) : <EOL> if self . _systime is None : <EOL> return self . _time <EOL> else : <EOL> return time . time ( ) - self . _systime + self . _time <EOL> class SilentAudioDriver ( AbstractAudioDriver ) : <EOL> def create_audio_player ( self , source_group , player ) : <EOL> if source_group . audio_format : <EOL> return SilentAudioPlayerPacketConsumer ( source_group , player ) <EOL> else : <EOL> return SilentTimeAudioPlayer ( source_group , player ) <EOL> def create_audio_driver ( ) : <EOL> return SilentAudioDriver ( ) </s>
