<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import os as _os <EOL> import sys as _sys <EOL> from cffi import FFI as _FFI <EOL> from os import SEEK_SET , SEEK_CUR , SEEK_END <EOL> try : <EOL> _unicode = unicode <EOL> except NameError : <EOL> _unicode = str <EOL> _ffi = _FFI ( ) <EOL> _ffi . cdef ( """<STR_LIT>""" ) <EOL> if _sys . platform == '<STR_LIT:win32>' : <EOL> _ffi . cdef ( """<STR_LIT>""" ) <EOL> _str_types = { <EOL> '<STR_LIT:title>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:date>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> _formats = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> _subtypes = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> _endians = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> _default_subtypes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _ffi_types = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:float>' , <EOL> '<STR_LIT>' : '<STR_LIT:int>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> try : <EOL> _snd = _ffi . dlopen ( '<STR_LIT>' ) <EOL> except OSError as err : <EOL> if _sys . platform == '<STR_LIT>' : <EOL> _libname = '<STR_LIT>' <EOL> elif _sys . platform == '<STR_LIT:win32>' : <EOL> from platform import architecture as _architecture <EOL> _libname = '<STR_LIT>' + _architecture ( ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> else : <EOL> raise <EOL> _snd = _ffi . dlopen ( _os . path . join ( <EOL> _os . path . dirname ( _os . path . abspath ( __file__ ) ) , <EOL> '<STR_LIT>' , _libname ) ) <EOL> __libsndfile_version__ = _ffi . string ( _snd . sf_version_string ( ) ) . decode ( ) <EOL> if __libsndfile_version__ . startswith ( '<STR_LIT>' ) : <EOL> __libsndfile_version__ = __libsndfile_version__ [ len ( '<STR_LIT>' ) : ] <EOL> def read ( file , frames = - <NUM_LIT:1> , start = <NUM_LIT:0> , stop = None , dtype = '<STR_LIT>' , always_2d = False , <EOL> fill_value = None , out = None , samplerate = None , channels = None , <EOL> format = None , subtype = None , endian = None , closefd = True ) : <EOL> """<STR_LIT>""" <EOL> with SoundFile ( file , '<STR_LIT:r>' , samplerate , channels , <EOL> subtype , endian , format , closefd ) as f : <EOL> frames = f . _prepare_read ( start , stop , frames ) <EOL> data = f . read ( frames , dtype , always_2d , fill_value , out ) <EOL> return data , f . samplerate <EOL> def write ( file , data , samplerate , subtype = None , endian = None , format = None , <EOL> closefd = True ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> data = np . asarray ( data ) <EOL> if data . ndim == <NUM_LIT:1> : <EOL> channels = <NUM_LIT:1> <EOL> else : <EOL> channels = data . shape [ <NUM_LIT:1> ] <EOL> with SoundFile ( file , '<STR_LIT:w>' , samplerate , channels , <EOL> subtype , endian , format , closefd ) as f : <EOL> f . write ( data ) <EOL> def blocks ( file , blocksize = None , overlap = <NUM_LIT:0> , frames = - <NUM_LIT:1> , start = <NUM_LIT:0> , stop = None , <EOL> dtype = '<STR_LIT>' , always_2d = False , fill_value = None , out = None , <EOL> samplerate = None , channels = None , <EOL> format = None , subtype = None , endian = None , closefd = True ) : <EOL> """<STR_LIT>""" <EOL> with SoundFile ( file , '<STR_LIT:r>' , samplerate , channels , <EOL> subtype , endian , format , closefd ) as f : <EOL> frames = f . _prepare_read ( start , stop , frames ) <EOL> for block in f . blocks ( blocksize , overlap , frames , <EOL> dtype , always_2d , fill_value , out ) : <EOL> yield block <EOL> class _SoundFileInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file , verbose ) : <EOL> self . verbose = verbose <EOL> with SoundFile ( file ) as f : <EOL> self . name = f . name <EOL> self . samplerate = f . samplerate <EOL> self . channels = f . channels <EOL> self . duration = len ( f ) / f . samplerate <EOL> self . format = f . format <EOL> self . subtype = f . subtype <EOL> self . endian = f . endian <EOL> self . format_info = f . format_info <EOL> self . subtype_info = f . subtype_info <EOL> self . sections = f . sections <EOL> self . extra_info = f . extra_info <EOL> @ property <EOL> def _duration_str ( self ) : <EOL> hours , rest = divmod ( self . duration , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( rest , <NUM_LIT> ) <EOL> if hours >= <NUM_LIT:1> : <EOL> duration = "<STR_LIT>" . format ( hours , minutes , seconds ) <EOL> elif minutes >= <NUM_LIT:1> : <EOL> duration = "<STR_LIT>" . format ( minutes , seconds ) <EOL> else : <EOL> duration = "<STR_LIT>" . format ( seconds ) <EOL> return duration <EOL> def __repr__ ( self ) : <EOL> info = "<STR_LIT:\n>" . join ( <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> if self . verbose : <EOL> info += "<STR_LIT:\n>" . join ( <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> indented_extra_info = ( "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * <NUM_LIT:4> ) . join ( self . extra_info . split ( "<STR_LIT:\n>" ) ) <EOL> return info . format ( self , indented_extra_info ) <EOL> def info ( file , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> return _SoundFileInfo ( file , verbose ) <EOL> def available_formats ( ) : <EOL> """<STR_LIT>""" <EOL> return dict ( _available_formats_helper ( _snd . SFC_GET_FORMAT_MAJOR_COUNT , <EOL> _snd . SFC_GET_FORMAT_MAJOR ) ) <EOL> def available_subtypes ( format = None ) : <EOL> """<STR_LIT>""" <EOL> subtypes = _available_formats_helper ( _snd . SFC_GET_FORMAT_SUBTYPE_COUNT , <EOL> _snd . SFC_GET_FORMAT_SUBTYPE ) <EOL> return dict ( ( subtype , name ) for subtype , name in subtypes <EOL> if format is None or check_format ( format , subtype ) ) <EOL> def check_format ( format , subtype = None , endian = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return bool ( _format_int ( format , subtype , endian ) ) <EOL> except ( ValueError , TypeError ) : <EOL> return False <EOL> def default_subtype ( format ) : <EOL> """<STR_LIT>""" <EOL> _check_format ( format ) <EOL> return _default_subtypes . get ( format . upper ( ) ) <EOL> class SoundFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file , mode = '<STR_LIT:r>' , samplerate = None , channels = None , <EOL> subtype = None , endian = None , format = None , closefd = True ) : <EOL> """<STR_LIT>""" <EOL> self . _name = file <EOL> if mode is None : <EOL> mode = getattr ( file , '<STR_LIT>' , None ) <EOL> mode_int = _check_mode ( mode ) <EOL> self . _mode = mode <EOL> self . _info = _create_info_struct ( file , mode , samplerate , channels , <EOL> format , subtype , endian ) <EOL> self . _file = self . _open ( file , mode_int , closefd ) <EOL> if set ( mode ) . issuperset ( '<STR_LIT>' ) and self . seekable ( ) : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> _snd . sf_command ( self . _file , _snd . SFC_SET_CLIPPING , _ffi . NULL , <EOL> _snd . SF_TRUE ) <EOL> name = property ( lambda self : self . _name ) <EOL> """<STR_LIT>""" <EOL> mode = property ( lambda self : self . _mode ) <EOL> """<STR_LIT>""" <EOL> samplerate = property ( lambda self : self . _info . samplerate ) <EOL> """<STR_LIT>""" <EOL> channels = property ( lambda self : self . _info . channels ) <EOL> """<STR_LIT>""" <EOL> format = property ( <EOL> lambda self : _format_str ( self . _info . format & _snd . SF_FORMAT_TYPEMASK ) ) <EOL> """<STR_LIT>""" <EOL> subtype = property ( <EOL> lambda self : _format_str ( self . _info . format & _snd . SF_FORMAT_SUBMASK ) ) <EOL> """<STR_LIT>""" <EOL> endian = property ( <EOL> lambda self : _format_str ( self . _info . format & _snd . SF_FORMAT_ENDMASK ) ) <EOL> """<STR_LIT>""" <EOL> format_info = property ( <EOL> lambda self : _format_info ( self . _info . format & <EOL> _snd . SF_FORMAT_TYPEMASK ) [ <NUM_LIT:1> ] ) <EOL> """<STR_LIT>""" <EOL> subtype_info = property ( <EOL> lambda self : _format_info ( self . _info . format & <EOL> _snd . SF_FORMAT_SUBMASK ) [ <NUM_LIT:1> ] ) <EOL> """<STR_LIT>""" <EOL> sections = property ( lambda self : self . _info . sections ) <EOL> """<STR_LIT>""" <EOL> closed = property ( lambda self : self . _file is None ) <EOL> """<STR_LIT>""" <EOL> _errorcode = property ( lambda self : _snd . sf_error ( self . _file ) ) <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def extra_info ( self ) : <EOL> """<STR_LIT>""" <EOL> info = _ffi . new ( "<STR_LIT>" , <NUM_LIT:2> ** <NUM_LIT> ) <EOL> _snd . sf_command ( self . _file , _snd . SFC_GET_LOG_INFO , <EOL> info , _ffi . sizeof ( info ) ) <EOL> return _ffi . string ( info ) . decode ( ) <EOL> _file = None <EOL> def __repr__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self ) ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> self . close ( ) <EOL> def __setattr__ ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if name in _str_types : <EOL> self . _check_if_closed ( ) <EOL> err = _snd . sf_set_string ( self . _file , _str_types [ name ] , <EOL> value . encode ( ) ) <EOL> _error_check ( err ) <EOL> else : <EOL> object . __setattr__ ( self , name , value ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in _str_types : <EOL> self . _check_if_closed ( ) <EOL> data = _snd . sf_get_string ( self . _file , _str_types [ name ] ) <EOL> return _ffi . string ( data ) . decode ( ) if data else "<STR_LIT>" <EOL> else : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" . format ( name ) ) <EOL> def __len__ ( self ) : <EOL> return self . _info . frames <EOL> def seekable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _info . seekable == _snd . SF_TRUE <EOL> def seek ( self , frames , whence = SEEK_SET ) : <EOL> """<STR_LIT>""" <EOL> self . _check_if_closed ( ) <EOL> position = _snd . sf_seek ( self . _file , frames , whence ) <EOL> _error_check ( self . _errorcode ) <EOL> return position <EOL> def tell ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . seek ( <NUM_LIT:0> , SEEK_CUR ) <EOL> def read ( self , frames = - <NUM_LIT:1> , dtype = '<STR_LIT>' , always_2d = False , <EOL> fill_value = None , out = None ) : <EOL> """<STR_LIT>""" <EOL> if out is None : <EOL> frames = self . _check_frames ( frames , fill_value ) <EOL> out = self . _create_empty_array ( frames , always_2d , dtype ) <EOL> else : <EOL> if frames < <NUM_LIT:0> or frames > len ( out ) : <EOL> frames = len ( out ) <EOL> frames = self . _array_io ( '<STR_LIT>' , out , frames ) <EOL> if len ( out ) > frames : <EOL> if fill_value is None : <EOL> out = out [ : frames ] <EOL> else : <EOL> out [ frames : ] = fill_value <EOL> return out <EOL> def buffer_read ( self , frames = - <NUM_LIT:1> , ctype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> frames = self . _check_frames ( frames , fill_value = None ) <EOL> cdata = _ffi . new ( ctype + '<STR_LIT>' , frames * self . channels ) <EOL> read_frames = self . _cdata_io ( '<STR_LIT>' , cdata , ctype , frames ) <EOL> assert read_frames == frames <EOL> return _ffi . buffer ( cdata ) <EOL> def buffer_read_into ( self , buffer , ctype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cdata , frames = self . _check_buffer ( buffer , ctype ) <EOL> frames = self . _cdata_io ( '<STR_LIT>' , cdata , ctype , frames ) <EOL> return frames <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> data = np . ascontiguousarray ( data ) <EOL> written = self . _array_io ( '<STR_LIT>' , data , len ( data ) ) <EOL> assert written == len ( data ) <EOL> self . _update_len ( written ) <EOL> def buffer_write ( self , data , ctype ) : <EOL> """<STR_LIT>""" <EOL> cdata , frames = self . _check_buffer ( data , ctype ) <EOL> written = self . _cdata_io ( '<STR_LIT>' , cdata , ctype , frames ) <EOL> assert written == frames <EOL> self . _update_len ( written ) <EOL> def blocks ( self , blocksize = None , overlap = <NUM_LIT:0> , frames = - <NUM_LIT:1> , dtype = '<STR_LIT>' , <EOL> always_2d = False , fill_value = None , out = None ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:r>' not in self . mode and '<STR_LIT:+>' not in self . mode : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if overlap != <NUM_LIT:0> and not self . seekable ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if out is None : <EOL> if blocksize is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> if blocksize is not None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> blocksize = len ( out ) <EOL> frames = self . _check_frames ( frames , fill_value ) <EOL> while frames > <NUM_LIT:0> : <EOL> if frames < blocksize : <EOL> if fill_value is not None and out is None : <EOL> out = self . _create_empty_array ( blocksize , always_2d , dtype ) <EOL> blocksize = frames <EOL> block = self . read ( blocksize , dtype , always_2d , fill_value , out ) <EOL> frames -= blocksize <EOL> if frames > <NUM_LIT:0> and self . seekable ( ) : <EOL> self . seek ( - overlap , SEEK_CUR ) <EOL> frames += overlap <EOL> yield block <EOL> def truncate ( self , frames = None ) : <EOL> """<STR_LIT>""" <EOL> if frames is None : <EOL> frames = self . tell ( ) <EOL> err = _snd . sf_command ( self . _file , _snd . SFC_FILE_TRUNCATE , <EOL> _ffi . new ( "<STR_LIT>" , frames ) , <EOL> _ffi . sizeof ( "<STR_LIT>" ) ) <EOL> if err : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . _info . frames = frames <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_if_closed ( ) <EOL> _snd . sf_write_sync ( self . _file ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . closed : <EOL> self . flush ( ) <EOL> err = _snd . sf_close ( self . _file ) <EOL> self . _file = None <EOL> _error_check ( err ) <EOL> def _open ( self , file , mode_int , closefd ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( file , ( _unicode , bytes ) ) : <EOL> if _os . path . isfile ( file ) : <EOL> if '<STR_LIT:x>' in self . mode : <EOL> raise OSError ( "<STR_LIT>" . format ( self . name ) ) <EOL> elif set ( self . mode ) . issuperset ( '<STR_LIT>' ) : <EOL> _os . close ( _os . open ( file , _os . O_WRONLY | _os . O_TRUNC ) ) <EOL> openfunction = _snd . sf_open <EOL> if isinstance ( file , _unicode ) : <EOL> if _sys . platform == '<STR_LIT:win32>' : <EOL> openfunction = _snd . sf_wchar_open <EOL> else : <EOL> file = file . encode ( _sys . getfilesystemencoding ( ) ) <EOL> file_ptr = openfunction ( file , mode_int , self . _info ) <EOL> elif isinstance ( file , int ) : <EOL> file_ptr = _snd . sf_open_fd ( file , mode_int , self . _info , closefd ) <EOL> elif _has_virtual_io_attrs ( file , mode_int ) : <EOL> file_ptr = _snd . sf_open_virtual ( self . _init_virtual_io ( file ) , <EOL> mode_int , self . _info , _ffi . NULL ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" . format ( self . name ) ) <EOL> _error_check ( _snd . sf_error ( file_ptr ) , <EOL> "<STR_LIT>" . format ( self . name ) ) <EOL> if mode_int == _snd . SFM_WRITE : <EOL> self . _info . frames = <NUM_LIT:0> <EOL> return file_ptr <EOL> def _init_virtual_io ( self , file ) : <EOL> """<STR_LIT>""" <EOL> @ _ffi . callback ( "<STR_LIT>" ) <EOL> def vio_get_filelen ( user_data ) : <EOL> curr = file . tell ( ) <EOL> file . seek ( <NUM_LIT:0> , SEEK_END ) <EOL> size = file . tell ( ) <EOL> file . seek ( curr , SEEK_SET ) <EOL> return size <EOL> @ _ffi . callback ( "<STR_LIT>" ) <EOL> def vio_seek ( offset , whence , user_data ) : <EOL> file . seek ( offset , whence ) <EOL> return file . tell ( ) <EOL> @ _ffi . callback ( "<STR_LIT>" ) <EOL> def vio_read ( ptr , count , user_data ) : <EOL> try : <EOL> buf = _ffi . buffer ( ptr , count ) <EOL> data_read = file . readinto ( buf ) <EOL> except AttributeError : <EOL> data = file . read ( count ) <EOL> data_read = len ( data ) <EOL> buf = _ffi . buffer ( ptr , data_read ) <EOL> buf [ <NUM_LIT:0> : data_read ] = data <EOL> return data_read <EOL> @ _ffi . callback ( "<STR_LIT>" ) <EOL> def vio_write ( ptr , count , user_data ) : <EOL> buf = _ffi . buffer ( ptr , count ) <EOL> data = buf [ : ] <EOL> written = file . write ( data ) <EOL> if written is None : <EOL> written = count <EOL> return written <EOL> @ _ffi . callback ( "<STR_LIT>" ) <EOL> def vio_tell ( user_data ) : <EOL> return file . tell ( ) <EOL> self . _virtual_io = { '<STR_LIT>' : vio_get_filelen , <EOL> '<STR_LIT>' : vio_seek , <EOL> '<STR_LIT>' : vio_read , <EOL> '<STR_LIT>' : vio_write , <EOL> '<STR_LIT>' : vio_tell } <EOL> return _ffi . new ( "<STR_LIT>" , self . _virtual_io ) <EOL> def _getAttributeNames ( self ) : <EOL> """<STR_LIT>""" <EOL> return _str_types <EOL> def _check_if_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . closed : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _check_frames ( self , frames , fill_value ) : <EOL> """<STR_LIT>""" <EOL> if self . seekable ( ) : <EOL> remaining_frames = len ( self ) - self . tell ( ) <EOL> if frames < <NUM_LIT:0> or ( frames > remaining_frames and <EOL> fill_value is None ) : <EOL> frames = remaining_frames <EOL> elif frames < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return frames <EOL> def _check_buffer ( self , data , ctype ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( data , bytes ) : <EOL> data = _ffi . from_buffer ( data ) <EOL> frames , remainder = divmod ( len ( data ) , <EOL> self . channels * _ffi . sizeof ( ctype ) ) <EOL> if remainder : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return data , frames <EOL> def _create_empty_array ( self , frames , always_2d , dtype ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> if always_2d or self . channels > <NUM_LIT:1> : <EOL> shape = frames , self . channels <EOL> else : <EOL> shape = frames , <EOL> return np . empty ( shape , dtype , order = '<STR_LIT:C>' ) <EOL> def _array_io ( self , action , array , frames ) : <EOL> """<STR_LIT>""" <EOL> if ( array . ndim not in ( <NUM_LIT:1> , <NUM_LIT:2> ) or <EOL> array . ndim == <NUM_LIT:1> and self . channels != <NUM_LIT:1> or <EOL> array . ndim == <NUM_LIT:2> and array . shape [ <NUM_LIT:1> ] != self . channels ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( array . shape ) ) <EOL> if not array . flags . c_contiguous : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> ctype = _ffi_types [ array . dtype . name ] <EOL> except KeyError : <EOL> raise TypeError ( "<STR_LIT>" . format ( <EOL> sorted ( _ffi_types . keys ( ) ) ) ) <EOL> assert array . dtype . itemsize == _ffi . sizeof ( ctype ) <EOL> cdata = _ffi . cast ( ctype + '<STR_LIT:*>' , array . __array_interface__ [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] ) <EOL> return self . _cdata_io ( action , cdata , ctype , frames ) <EOL> def _cdata_io ( self , action , data , ctype , frames ) : <EOL> """<STR_LIT>""" <EOL> if ctype not in _ffi_types . values ( ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( ctype ) ) <EOL> self . _check_if_closed ( ) <EOL> if self . seekable ( ) : <EOL> curr = self . tell ( ) <EOL> func = getattr ( _snd , '<STR_LIT>' + action + '<STR_LIT>' + ctype ) <EOL> frames = func ( self . _file , data , frames ) <EOL> _error_check ( self . _errorcode ) <EOL> if self . seekable ( ) : <EOL> self . seek ( curr + frames , SEEK_SET ) <EOL> return frames <EOL> def _update_len ( self , written ) : <EOL> """<STR_LIT>""" <EOL> if self . seekable ( ) : <EOL> curr = self . tell ( ) <EOL> self . _info . frames = self . seek ( <NUM_LIT:0> , SEEK_END ) <EOL> self . seek ( curr , SEEK_SET ) <EOL> else : <EOL> self . _info . frames += written <EOL> def _prepare_read ( self , start , stop , frames ) : <EOL> """<STR_LIT>""" <EOL> if start != <NUM_LIT:0> and not self . seekable ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if frames >= <NUM_LIT:0> and stop is not None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> start , stop , _ = slice ( start , stop ) . indices ( len ( self ) ) <EOL> if stop < start : <EOL> stop = start <EOL> if frames < <NUM_LIT:0> : <EOL> frames = stop - start <EOL> if self . seekable ( ) : <EOL> self . seek ( start , SEEK_SET ) <EOL> return frames <EOL> def _error_check ( err , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if err != <NUM_LIT:0> : <EOL> err_str = _snd . sf_error_number ( err ) <EOL> raise RuntimeError ( prefix + _ffi . string ( err_str ) . decode ( ) ) <EOL> def _format_int ( format , subtype , endian ) : <EOL> """<STR_LIT>""" <EOL> result = _check_format ( format ) <EOL> if subtype is None : <EOL> subtype = default_subtype ( format ) <EOL> if subtype is None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" . format ( format ) ) <EOL> elif not isinstance ( subtype , ( _unicode , str ) ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( subtype ) ) <EOL> try : <EOL> result |= _subtypes [ subtype . upper ( ) ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" . format ( subtype ) ) <EOL> if endian is None : <EOL> endian = '<STR_LIT>' <EOL> elif not isinstance ( endian , ( _unicode , str ) ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( endian ) ) <EOL> try : <EOL> result |= _endians [ endian . upper ( ) ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" . format ( endian ) ) <EOL> info = _ffi . new ( "<STR_LIT>" ) <EOL> info . format = result <EOL> info . channels = <NUM_LIT:1> <EOL> if _snd . sf_format_check ( info ) == _snd . SF_FALSE : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> return result <EOL> def _check_mode ( mode ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( mode , ( _unicode , str ) ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( mode ) ) <EOL> mode_set = set ( mode ) <EOL> if mode_set . difference ( '<STR_LIT>' ) or len ( mode ) > len ( mode_set ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( mode ) ) <EOL> if len ( mode_set . intersection ( '<STR_LIT>' ) ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if '<STR_LIT:+>' in mode_set : <EOL> mode_int = _snd . SFM_RDWR <EOL> elif '<STR_LIT:r>' in mode_set : <EOL> mode_int = _snd . SFM_READ <EOL> else : <EOL> mode_int = _snd . SFM_WRITE <EOL> return mode_int <EOL> def _create_info_struct ( file , mode , samplerate , channels , <EOL> format , subtype , endian ) : <EOL> """<STR_LIT>""" <EOL> original_format = format <EOL> if format is None : <EOL> format = _get_format_from_filename ( file , mode ) <EOL> assert isinstance ( format , ( _unicode , str ) ) <EOL> else : <EOL> _check_format ( format ) <EOL> info = _ffi . new ( "<STR_LIT>" ) <EOL> if '<STR_LIT:r>' not in mode or format . upper ( ) == '<STR_LIT>' : <EOL> if samplerate is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> info . samplerate = samplerate <EOL> if channels is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> info . channels = channels <EOL> info . format = _format_int ( format , subtype , endian ) <EOL> else : <EOL> if any ( arg is not None for arg in ( <EOL> samplerate , channels , original_format , subtype , endian ) ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return info <EOL> def _get_format_from_filename ( file , mode ) : <EOL> """<STR_LIT>""" <EOL> format = '<STR_LIT>' <EOL> file = getattr ( file , '<STR_LIT:name>' , file ) <EOL> try : <EOL> format = _os . path . splitext ( file ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> format = format . decode ( ) <EOL> except Exception : <EOL> pass <EOL> if format . upper ( ) not in _formats and '<STR_LIT:r>' not in mode : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( file ) ) <EOL> return format <EOL> def _format_str ( format_int ) : <EOL> """<STR_LIT>""" <EOL> for dictionary in _formats , _subtypes , _endians : <EOL> for k , v in dictionary . items ( ) : <EOL> if v == format_int : <EOL> return k <EOL> else : <EOL> return '<STR_LIT>' <EOL> def _format_info ( format_int , format_flag = _snd . SFC_GET_FORMAT_INFO ) : <EOL> """<STR_LIT>""" <EOL> format_info = _ffi . new ( "<STR_LIT>" ) <EOL> format_info . format = format_int <EOL> _snd . sf_command ( _ffi . NULL , format_flag , format_info , <EOL> _ffi . sizeof ( "<STR_LIT>" ) ) <EOL> name = format_info . name <EOL> return ( _format_str ( format_info . format ) , <EOL> _ffi . string ( name ) . decode ( ) if name else "<STR_LIT>" ) <EOL> def _available_formats_helper ( count_flag , format_flag ) : <EOL> """<STR_LIT>""" <EOL> count = _ffi . new ( "<STR_LIT>" ) <EOL> _snd . sf_command ( _ffi . NULL , count_flag , count , _ffi . sizeof ( "<STR_LIT:int>" ) ) <EOL> for format_int in range ( count [ <NUM_LIT:0> ] ) : <EOL> yield _format_info ( format_int , format_flag ) <EOL> def _check_format ( format_str ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( format_str , ( _unicode , str ) ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( format_str ) ) <EOL> try : <EOL> format_int = _formats [ format_str . upper ( ) ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" . format ( format_str ) ) <EOL> return format_int <EOL> def _has_virtual_io_attrs ( file , mode_int ) : <EOL> """<STR_LIT>""" <EOL> readonly = mode_int == _snd . SFM_READ <EOL> writeonly = mode_int == _snd . SFM_WRITE <EOL> return all ( [ <EOL> hasattr ( file , '<STR_LIT>' ) , <EOL> hasattr ( file , '<STR_LIT>' ) , <EOL> hasattr ( file , '<STR_LIT>' ) or readonly , <EOL> hasattr ( file , '<STR_LIT>' ) or hasattr ( file , '<STR_LIT>' ) or writeonly , <EOL> ] ) </s>
