<s> """<STR_LIT>""" <EOL> import __builtin__ <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> pass <EOL> WAVE_FORMAT_PCM = <NUM_LIT> <EOL> _array_fmts = None , '<STR_LIT:b>' , '<STR_LIT:h>' , None , '<STR_LIT:l>' <EOL> import struct <EOL> if struct . pack ( "<STR_LIT:h>" , <NUM_LIT:1> ) == "<STR_LIT>" : <EOL> big_endian = <NUM_LIT:1> <EOL> else : <EOL> big_endian = <NUM_LIT:0> <EOL> from chunk import Chunk <EOL> class Wave_read : <EOL> """<STR_LIT>""" <EOL> def initfp ( self , file ) : <EOL> self . _convert = None <EOL> self . _soundpos = <NUM_LIT:0> <EOL> self . _file = Chunk ( file , bigendian = <NUM_LIT:0> ) <EOL> if self . _file . getname ( ) != '<STR_LIT>' : <EOL> raise Error , '<STR_LIT>' <EOL> if self . _file . read ( <NUM_LIT:4> ) != '<STR_LIT>' : <EOL> raise Error , '<STR_LIT>' <EOL> self . _fmt_chunk_read = <NUM_LIT:0> <EOL> self . _data_chunk = None <EOL> while <NUM_LIT:1> : <EOL> self . _data_seek_needed = <NUM_LIT:1> <EOL> try : <EOL> chunk = Chunk ( self . _file , bigendian = <NUM_LIT:0> ) <EOL> except EOFError : <EOL> break <EOL> chunkname = chunk . getname ( ) <EOL> if chunkname == '<STR_LIT>' : <EOL> self . _read_fmt_chunk ( chunk ) <EOL> self . _fmt_chunk_read = <NUM_LIT:1> <EOL> elif chunkname == '<STR_LIT:data>' : <EOL> if not self . _fmt_chunk_read : <EOL> raise Error , '<STR_LIT>' <EOL> self . _data_chunk = chunk <EOL> self . _nframes = chunk . chunksize // self . _framesize <EOL> self . _data_seek_needed = <NUM_LIT:0> <EOL> break <EOL> chunk . skip ( ) <EOL> if not self . _fmt_chunk_read or not self . _data_chunk : <EOL> raise Error , '<STR_LIT>' <EOL> def __init__ ( self , f ) : <EOL> self . _i_opened_the_file = None <EOL> if isinstance ( f , basestring ) : <EOL> f = __builtin__ . open ( f , '<STR_LIT:rb>' ) <EOL> self . _i_opened_the_file = f <EOL> try : <EOL> self . initfp ( f ) <EOL> except : <EOL> if self . _i_opened_the_file : <EOL> f . close ( ) <EOL> raise <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def getfp ( self ) : <EOL> return self . _file <EOL> def rewind ( self ) : <EOL> self . _data_seek_needed = <NUM_LIT:1> <EOL> self . _soundpos = <NUM_LIT:0> <EOL> def close ( self ) : <EOL> if self . _i_opened_the_file : <EOL> self . _i_opened_the_file . close ( ) <EOL> self . _i_opened_the_file = None <EOL> self . _file = None <EOL> def tell ( self ) : <EOL> return self . _soundpos <EOL> def getnchannels ( self ) : <EOL> return self . _nchannels <EOL> def getnframes ( self ) : <EOL> return self . _nframes <EOL> def getsampwidth ( self ) : <EOL> return self . _sampwidth <EOL> def getframerate ( self ) : <EOL> return self . _framerate <EOL> def getcomptype ( self ) : <EOL> return self . _comptype <EOL> def getcompname ( self ) : <EOL> return self . _compname <EOL> def getparams ( self ) : <EOL> return self . getnchannels ( ) , self . getsampwidth ( ) , self . getframerate ( ) , self . getnframes ( ) , self . getcomptype ( ) , self . getcompname ( ) <EOL> def getmarkers ( self ) : <EOL> return None <EOL> def getmark ( self , id ) : <EOL> raise Error , '<STR_LIT>' <EOL> def setpos ( self , pos ) : <EOL> if pos < <NUM_LIT:0> or pos > self . _nframes : <EOL> raise Error , '<STR_LIT>' <EOL> self . _soundpos = pos <EOL> self . _data_seek_needed = <NUM_LIT:1> <EOL> def readframes ( self , nframes ) : <EOL> if self . _data_seek_needed : <EOL> self . _data_chunk . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> pos = self . _soundpos * self . _framesize <EOL> if pos : <EOL> self . _data_chunk . seek ( pos , <NUM_LIT:0> ) <EOL> self . _data_seek_needed = <NUM_LIT:0> <EOL> if nframes == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if self . _sampwidth > <NUM_LIT:1> and big_endian : <EOL> import array <EOL> chunk = self . _data_chunk <EOL> data = array . array ( _array_fmts [ self . _sampwidth ] ) <EOL> nitems = nframes * self . _nchannels <EOL> if nitems * self . _sampwidth > chunk . chunksize - chunk . size_read : <EOL> nitems = ( chunk . chunksize - chunk . size_read ) / self . _sampwidth <EOL> data . fromfile ( chunk . file . file , nitems ) <EOL> chunk . size_read = chunk . size_read + nitems * self . _sampwidth <EOL> chunk = chunk . file <EOL> chunk . size_read = chunk . size_read + nitems * self . _sampwidth <EOL> data . byteswap ( ) <EOL> data = data . tostring ( ) <EOL> else : <EOL> data = self . _data_chunk . read ( nframes * self . _framesize ) <EOL> if self . _convert and data : <EOL> data = self . _convert ( data ) <EOL> self . _soundpos = self . _soundpos + len ( data ) // ( self . _nchannels * self . _sampwidth ) <EOL> return data <EOL> def _read_fmt_chunk ( self , chunk ) : <EOL> wFormatTag , self . _nchannels , self . _framerate , dwAvgBytesPerSec , wBlockAlign = struct . unpack ( '<STR_LIT>' , chunk . read ( <NUM_LIT> ) ) <EOL> if wFormatTag == WAVE_FORMAT_PCM : <EOL> sampwidth = struct . unpack ( '<STR_LIT>' , chunk . read ( <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> self . _sampwidth = ( sampwidth + <NUM_LIT:7> ) // <NUM_LIT:8> <EOL> else : <EOL> raise Error , '<STR_LIT>' % ( wFormatTag , ) <EOL> self . _framesize = self . _nchannels * self . _sampwidth <EOL> self . _comptype = '<STR_LIT>' <EOL> self . _compname = '<STR_LIT>' <EOL> class Wave_write : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , f ) : <EOL> self . _i_opened_the_file = None <EOL> if isinstance ( f , basestring ) : <EOL> f = __builtin__ . open ( f , '<STR_LIT:wb>' ) <EOL> self . _i_opened_the_file = f <EOL> try : <EOL> self . initfp ( f ) <EOL> except : <EOL> if self . _i_opened_the_file : <EOL> f . close ( ) <EOL> raise <EOL> def initfp ( self , file ) : <EOL> self . _file = file <EOL> self . _convert = None <EOL> self . _nchannels = <NUM_LIT:0> <EOL> self . _sampwidth = <NUM_LIT:0> <EOL> self . _framerate = <NUM_LIT:0> <EOL> self . _nframes = <NUM_LIT:0> <EOL> self . _nframeswritten = <NUM_LIT:0> <EOL> self . _datawritten = <NUM_LIT:0> <EOL> self . _datalength = <NUM_LIT:0> <EOL> self . _headerwritten = False <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def setnchannels ( self , nchannels ) : <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> if nchannels < <NUM_LIT:1> : <EOL> raise Error , '<STR_LIT>' <EOL> self . _nchannels = nchannels <EOL> def getnchannels ( self ) : <EOL> if not self . _nchannels : <EOL> raise Error , '<STR_LIT>' <EOL> return self . _nchannels <EOL> def setsampwidth ( self , sampwidth ) : <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> if sampwidth < <NUM_LIT:1> or sampwidth > <NUM_LIT:4> : <EOL> raise Error , '<STR_LIT>' <EOL> self . _sampwidth = sampwidth <EOL> def getsampwidth ( self ) : <EOL> if not self . _sampwidth : <EOL> raise Error , '<STR_LIT>' <EOL> return self . _sampwidth <EOL> def setframerate ( self , framerate ) : <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> if framerate <= <NUM_LIT:0> : <EOL> raise Error , '<STR_LIT>' <EOL> self . _framerate = framerate <EOL> def getframerate ( self ) : <EOL> if not self . _framerate : <EOL> raise Error , '<STR_LIT>' <EOL> return self . _framerate <EOL> def setnframes ( self , nframes ) : <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> self . _nframes = nframes <EOL> def getnframes ( self ) : <EOL> return self . _nframeswritten <EOL> def setcomptype ( self , comptype , compname ) : <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> if comptype not in ( '<STR_LIT>' , ) : <EOL> raise Error , '<STR_LIT>' <EOL> self . _comptype = comptype <EOL> self . _compname = compname <EOL> def getcomptype ( self ) : <EOL> return self . _comptype <EOL> def getcompname ( self ) : <EOL> return self . _compname <EOL> def setparams ( self , params ) : <EOL> nchannels , sampwidth , framerate , nframes , comptype , compname = params <EOL> if self . _datawritten : <EOL> raise Error , '<STR_LIT>' <EOL> self . setnchannels ( nchannels ) <EOL> self . setsampwidth ( sampwidth ) <EOL> self . setframerate ( framerate ) <EOL> self . setnframes ( nframes ) <EOL> self . setcomptype ( comptype , compname ) <EOL> def getparams ( self ) : <EOL> if not self . _nchannels or not self . _sampwidth or not self . _framerate : <EOL> raise Error , '<STR_LIT>' <EOL> return self . _nchannels , self . _sampwidth , self . _framerate , self . _nframes , self . _comptype , self . _compname <EOL> def setmark ( self , id , pos , name ) : <EOL> raise Error , '<STR_LIT>' <EOL> def getmark ( self , id ) : <EOL> raise Error , '<STR_LIT>' <EOL> def getmarkers ( self ) : <EOL> return None <EOL> def tell ( self ) : <EOL> return self . _nframeswritten <EOL> def writeframesraw ( self , data ) : <EOL> self . _ensure_header_written ( len ( data ) ) <EOL> nframes = len ( data ) // ( self . _sampwidth * self . _nchannels ) <EOL> if self . _convert : <EOL> data = self . _convert ( data ) <EOL> if self . _sampwidth > <NUM_LIT:1> and big_endian : <EOL> import array <EOL> data = array . array ( _array_fmts [ self . _sampwidth ] , data ) <EOL> data . byteswap ( ) <EOL> data . tofile ( self . _file ) <EOL> self . _datawritten = self . _datawritten + len ( data ) * self . _sampwidth <EOL> else : <EOL> self . _file . write ( data ) <EOL> self . _datawritten = self . _datawritten + len ( data ) <EOL> self . _nframeswritten = self . _nframeswritten + nframes <EOL> def writeframes ( self , data ) : <EOL> self . writeframesraw ( data ) <EOL> if self . _datalength != self . _datawritten : <EOL> self . _patchheader ( ) <EOL> def close ( self ) : <EOL> if self . _file : <EOL> self . _ensure_header_written ( <NUM_LIT:0> ) <EOL> if self . _datalength != self . _datawritten : <EOL> self . _patchheader ( ) <EOL> self . _file . flush ( ) <EOL> self . _file = None <EOL> if self . _i_opened_the_file : <EOL> self . _i_opened_the_file . close ( ) <EOL> self . _i_opened_the_file = None <EOL> def _ensure_header_written ( self , datasize ) : <EOL> if not self . _headerwritten : <EOL> if not self . _nchannels : <EOL> raise Error , '<STR_LIT>' <EOL> if not self . _sampwidth : <EOL> raise Error , '<STR_LIT>' <EOL> if not self . _framerate : <EOL> raise Error , '<STR_LIT>' <EOL> self . _write_header ( datasize ) <EOL> def _write_header ( self , initlength ) : <EOL> assert not self . _headerwritten <EOL> self . _file . write ( '<STR_LIT>' ) <EOL> if not self . _nframes : <EOL> self . _nframes = initlength / ( self . _nchannels * self . _sampwidth ) <EOL> self . _datalength = self . _nframes * self . _nchannels * self . _sampwidth <EOL> self . _form_length_pos = self . _file . tell ( ) <EOL> self . _file . write ( struct . pack ( '<STR_LIT>' , <EOL> <NUM_LIT> + self . _datalength , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:16> , <EOL> WAVE_FORMAT_PCM , self . _nchannels , self . _framerate , <EOL> self . _nchannels * self . _framerate * self . _sampwidth , <EOL> self . _nchannels * self . _sampwidth , <EOL> self . _sampwidth * <NUM_LIT:8> , '<STR_LIT:data>' ) ) <EOL> self . _data_length_pos = self . _file . tell ( ) <EOL> self . _file . write ( struct . pack ( '<STR_LIT>' , self . _datalength ) ) <EOL> self . _headerwritten = True <EOL> def _patchheader ( self ) : <EOL> assert self . _headerwritten <EOL> if self . _datawritten == self . _datalength : <EOL> return <EOL> curpos = self . _file . tell ( ) <EOL> self . _file . seek ( self . _form_length_pos , <NUM_LIT:0> ) <EOL> self . _file . write ( struct . pack ( '<STR_LIT>' , <NUM_LIT> + self . _datawritten ) ) <EOL> self . _file . seek ( self . _data_length_pos , <NUM_LIT:0> ) <EOL> self . _file . write ( struct . pack ( '<STR_LIT>' , self . _datawritten ) ) <EOL> self . _file . seek ( curpos , <NUM_LIT:0> ) <EOL> self . _datalength = self . _datawritten <EOL> def open ( f , mode = None ) : <EOL> if mode is None : <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> mode = f . mode <EOL> else : <EOL> mode = '<STR_LIT:rb>' <EOL> if mode in ( '<STR_LIT:r>' , '<STR_LIT:rb>' ) : <EOL> return Wave_read ( f ) <EOL> elif mode in ( '<STR_LIT:w>' , '<STR_LIT:wb>' ) : <EOL> return Wave_write ( f ) <EOL> else : <EOL> raise Error , "<STR_LIT>" <EOL> openfp = open </s>
