o
    "Øh`ý  ã                   @   s¦  d Z dZddlZddlZddlmZmZmZ ddl	m
Z ddlmZ zeZW n ey1   eZY nw ddd	d
dddddddœ
Zi dd“dd“dd“dd“dd“dd“dd“d d!“d"d#“d$d%“d&d'“d(d)“d*d+“d,d-“d.d/“d0d1“d2d3“d4d5d6d7d8d9d:d;d<d=œ	¥Zi d>d“d?d“d@d	“dAd
“dBd“dCd“dDd“dEd“dFdG“dHdI“dJdK“dLdM“dNdO“dPdQ“dRdS“dTdU“dVdW“i dXdY“dZd[“d\d]“d^d_“d`da“dbdc“ddde“dfdg“dhdi“djdk“dldm“dndo“dpdq“drds“dtdu“dvdw“dxdy“¥Zddzd{d|d}œZi dd?“dd?“dd?“dd?“dd?“dd?“d d?“d"d?“d$d?“d&dD“d(dD“d*d?“d,df“d.d?“d0d?“d2d?“d~d?“d?d?d?dFdhd?d?dxdœ¥Zd€dd‚dƒd„œZdddd…œZz€ejd†krSdd‡lmZ dˆeƒ  d‰ ZnSejdŠkr…dd‹lmZ dd‡lmZ eƒ d dŒkrpdZn6eƒ d dŽkr{dZn+e d !eƒ eƒ ¡ƒ‚ejd‘kr¢dd‡lmZ eƒ d’v ršd“Zndˆeƒ  d” Zne d•ƒ‚ddl"Z"ej# $e"j%¡Z&ej# 'e&e¡Z(e )e(¡Z*W nu e e+e,fy3   zed–ƒZ-e-du r×e d—ƒ‚e )e-¡Z*W nS e y0   ejd†krîd˜Z.nejdŠkr÷d™Z.n
ejd‘kr dšZ.n‚ dd‡lmZ ejd†kr)eƒ d›kr)ej# /dœ¡rdœndZ0e )ej# 'e0e.¡¡Z*ne )e.¡Z*Y nw Y nw e 1e* 2¡ ¡ 3dždŸ¡Z4e4 5d ¡rMe4e6d ƒd… Z4	£		¤dÔd¥d¦„Z7		dÕd§d¨„Z8				¤dÖd©dª„Z9G d«d¬„ d¬e:ƒZ;d×d­d®„Z<d¯d°„ Z=dØd±d²„Z>dÙd³d´„Z?dµd¶„ Z@G d·d¸„ d¸e:ƒZAdÚdºd»„ZBd¼d½„ ZCd¾d¿„ ZDdÀdÁ„ ZEdÂdÃ„ ZFdÄdÅ„ ZGe*jHfdÆdÇ„ZIdÈdÉ„ ZJdÊdË„ ZKdÌdÍ„ ZLG dÎdÏ„ dÏeMƒZNG dÐdÑ„ dÑeNeOƒZPG dÒdÓ„ dÓePƒZQdS )Ûak  python-soundfile is an audio library based on libsndfile, CFFI and NumPy.

Sound files can be read or written directly using the functions
`read()` and `write()`.
To read a sound file in a block-wise fashion, use `blocks()`.
Alternatively, sound files can be opened as `SoundFile` objects.

For further information, see https://python-soundfile.readthedocs.io/.

z0.13.1é    N)ÚSEEK_SETÚSEEK_CURÚSEEK_END)Úfind_library)Úffié   é   é   é   é   é   é   é   é	   é   )
ÚtitleÚ	copyrightÚsoftwareÚartistÚcommentÚdateÚalbumÚlicenseÚtracknumberÚgenreÚWAVi   ÚAIFFi   ÚAUi   ÚRAWi   ÚPAFi   ÚSVXi   ÚNISTi   ÚVOCi   ÚIRCAMi  
 ÚW64i   ÚMAT4i   ÚMAT5i   ÚPVFi   ÚXIi   ÚHTKi   ÚSDSi   ÚAVRi   i   i   i   i   i   i    i  ! i  " i  # )	ÚWAVEXÚSD2ÚFLACÚCAFÚWVEÚOGGÚMPC2KÚRF64ÚMP3ÚPCM_S8ÚPCM_16ÚPCM_24ÚPCM_32ÚPCM_U8ÚFLOATÚDOUBLEÚULAWÚALAWé   Ú	IMA_ADPCMé   ÚMS_ADPCMé   ÚGSM610é    Ú	VOX_ADPCMé!   ÚNMS_ADPCM_16é"   ÚNMS_ADPCM_24é#   ÚNMS_ADPCM_32é$   ÚG721_32é0   ÚG723_24é1   ÚG723_40é2   ÚDWVW_12é@   ÚDWVW_16éA   ÚDWVW_24éB   ÚDWVW_NéC   ÚDPCM_8éP   ÚDPCM_16éQ   ÚVORBISé`   ÚOPUSéd   ÚALAC_16ép   ÚALAC_20éq   ÚALAC_24ér   ÚALAC_32és   ÚMPEG_LAYER_Ié€   ÚMPEG_LAYER_IIé   ÚMPEG_LAYER_IIIé‚   i   i    i   0)ÚFILEÚLITTLEÚBIGÚCPUr,   )r-   r.   r/   r0   r1   r2   r3   r4   ÚdoubleÚfloatÚintÚshort)Úfloat64Úfloat32Úint32Úint16)ÚCONSTANTÚAVERAGEÚVARIABLEÚdarwin)ÚmachineÚlibsndfile_z.dylibÚwin32)ÚarchitectureÚ64bitzlibsndfile_x64.dllÚ32bitzlibsndfile_x86.dllz%no packaged library for Windows {} {}Úlinux)Úaarch64Ú
aarch64_beÚarmv8bÚarmv8lzlibsndfile_arm64.soz.soz%no packaged library for this platformÚsndfilez8sndfile library not found using ctypes.util.find_libraryzlibsndfile.dylibzlibsndfile.dllzlibsndfile.soÚarm64z/opt/homebrew/lib/z/usr/local/lib/úutf-8Úreplacezlibsndfile-éÿÿÿÿry   FTc              	   C   s`   t | d||	|||
|ƒ}| |||¡}| |||||¡}W d  ƒ n1 s&w   Y  ||jfS )a  Provide audio data from a sound file as NumPy array.

    By default, the whole file is read from the beginning, but the
    position to start reading can be specified with *start* and the
    number of frames to read can be specified with *frames*.
    Alternatively, a range can be specified with *start* and *stop*.

    If there is less data left in the file than requested, the rest of
    the frames are filled with *fill_value*.
    If no *fill_value* is specified, a smaller array is returned.

    Parameters
    ----------
    file : str or int or file-like object
        The file to read from.  See `SoundFile` for details.
    frames : int, optional
        The number of frames to read. If *frames* is negative, the whole
        rest of the file is read.  Not allowed if *stop* is given.
    start : int, optional
        Where to start reading.  A negative value counts from the end.
    stop : int, optional
        The index after the last frame to be read.  A negative value
        counts from the end.  Not allowed if *frames* is given.
    dtype : {'float64', 'float32', 'int32', 'int16'}, optional
        Data type of the returned array, by default ``'float64'``.
        Floating point audio data is typically in the range from
        ``-1.0`` to ``1.0``.  Integer data is in the range from
        ``-2**15`` to ``2**15-1`` for ``'int16'`` and from ``-2**31`` to
        ``2**31-1`` for ``'int32'``.

        .. note:: Reading int values from a float file will *not*
            scale the data to [-1.0, 1.0). If the file contains
            ``np.array([42.6], dtype='float32')``, you will read
            ``np.array([43], dtype='int32')`` for ``dtype='int32'``.

    Returns
    -------
    audiodata : `numpy.ndarray` or type(out)
        A two-dimensional (frames x channels) NumPy array is returned.
        If the sound file has only one channel, a one-dimensional array
        is returned.  Use ``always_2d=True`` to return a two-dimensional
        array anyway.

        If *out* was specified, it is returned.  If *out* has more
        frames than available in the file (or if *frames* is smaller
        than the length of *out*) and no *fill_value* is given, then
        only a part of *out* is overwritten and a view containing all
        valid frames is returned.
    samplerate : int
        The sample rate of the audio file.

    Other Parameters
    ----------------
    always_2d : bool, optional
        By default, reading a mono sound file will return a
        one-dimensional array.  With ``always_2d=True``, audio data is
        always returned as a two-dimensional array, even if the audio
        file has only one channel.
    fill_value : float, optional
        If more frames are requested than available in the file, the
        rest of the output is be filled with *fill_value*.  If
        *fill_value* is not specified, a smaller array is returned.
    out : `numpy.ndarray` or subclass, optional
        If *out* is specified, the data is written into the given array
        instead of creating a new array.  In this case, the arguments
        *dtype* and *always_2d* are silently ignored!  If *frames* is
        not given, it is obtained from the length of *out*.
    samplerate, channels, format, subtype, endian, closefd
        See `SoundFile`.

    Examples
    --------
    >>> import soundfile as sf
    >>> data, samplerate = sf.read('stereo_file.wav')
    >>> data
    array([[ 0.71329652,  0.06294799],
           [-0.26450912, -0.38874483],
           ...
           [ 0.67398441, -0.11516333]])
    >>> samplerate
    44100

    ÚrN)Ú	SoundFileÚ_prepare_readÚreadÚ
samplerate)ÚfileÚframesÚstartÚstopÚdtypeÚ	always_2dÚ
fill_valueÚoutr•   ÚchannelsÚformatÚsubtypeÚendianÚclosefdÚfÚdata© r¥   úg/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/tts/venv/lib/python3.10/site-packages/soundfile.pyr”   Û   s   
Vÿý
r”   c	                 C   sv   ddl }	|	 |¡}|jdkrd}
n|jd }
t| d||
||||||ƒ
}| |¡ W d  ƒ dS 1 s4w   Y  dS )a  Write data to a sound file.

    .. note:: If *file* exists, it will be truncated and overwritten!

    Parameters
    ----------
    file : str or int or file-like object
        The file to write to.  See `SoundFile` for details.
    data : array_like
        The data to write.  Usually two-dimensional (frames x channels),
        but one-dimensional *data* can be used for mono files.
        Only the data types ``'float64'``, ``'float32'``, ``'int32'``
        and ``'int16'`` are supported.

        .. note:: The data type of *data* does **not** select the data
                  type of the written file. Audio data will be
                  converted to the given *subtype*. Writing int values
                  to a float file will *not* scale the values to
                  [-1.0, 1.0). If you write the value ``np.array([42],
                  dtype='int32')``, to a ``subtype='FLOAT'`` file, the
                  file will then contain ``np.array([42.],
                  dtype='float32')``.

    samplerate : int
        The sample rate of the audio data.
    subtype : str, optional
        See `default_subtype()` for the default value and
        `available_subtypes()` for all possible values.

    Other Parameters
    ----------------
    format, endian, closefd, compression_level, bitrate_mode
        See `SoundFile`.

    Examples
    --------
    Write 10 frames of random data to a new file:

    >>> import numpy as np
    >>> import soundfile as sf
    >>> sf.write('stereo_file.wav', np.random.randn(10, 2), 44100, 'PCM_24')

    r   Nr   Úw)ÚnumpyÚasarrayÚndimÚshaper’   Úwrite)r–   r¤   r•   r    r¡   rŸ   r¢   Úcompression_levelÚbitrate_modeÚnprž   r£   r¥   r¥   r¦   r¬   8  s   -



þ"ýr¬   c              
   c   sn    t | d|
|||||ƒ!}| |||¡}| |||||||	¡D ]}|V  qW d  ƒ dS 1 s0w   Y  dS )a8  Return a generator for block-wise reading.

    By default, iteration starts at the beginning and stops at the end
    of the file.  Use *start* to start at a later position and *frames*
    or *stop* to stop earlier.

    If you stop iterating over the generator before it's exhausted,
    the sound file is not closed. This is normally not a problem
    because the file is opened in read-only mode. To close the file
    properly, the generator's ``close()`` method can be called.

    Parameters
    ----------
    file : str or int or file-like object
        The file to read from.  See `SoundFile` for details.
    blocksize : int
        The number of frames to read per block.
        Either this or *out* must be given.
    overlap : int, optional
        The number of frames to rewind between each block.

    Yields
    ------
    `numpy.ndarray` or type(out)
        Blocks of audio data.
        If *out* was given, and the requested frames are not an integer
        multiple of the length of *out*, and no *fill_value* was given,
        the last block will be a smaller view into *out*.

    Other Parameters
    ----------------
    frames, start, stop
        See `read()`.
    dtype : {'float64', 'float32', 'int32', 'int16'}, optional
        See `read()`.
    always_2d, fill_value, out
        See `read()`.
    samplerate, channels, format, subtype, endian, closefd
        See `SoundFile`.

    Examples
    --------
    >>> import soundfile as sf
    >>> for block in sf.blocks('stereo_file.wav', blocksize=1024):
    >>>     pass  # do something with 'block'

    r‘   N)r’   r“   Úblocks)r–   Ú	blocksizeÚoverlapr—   r˜   r™   rš   r›   rœ   r   r•   rž   rŸ   r    r¡   r¢   r£   Úblockr¥   r¥   r¦   r°   q  s   €
3ÿ
ÿþ"ýr°   c                   @   s,   e Zd ZdZdd„ Zedd„ ƒZdd„ ZdS )	Ú_SoundFileInfozInformation about a SoundFilec                 C   sž   || _ t|ƒ>}|j| _|j| _|j| _|j| _t| jƒ|j | _|j| _|j	| _	|j
| _
|j| _|j| _|j| _|j| _W d   ƒ d S 1 sHw   Y  d S ©N)Úverboser’   Únamer•   rž   r—   rv   ÚdurationrŸ   r    r¡   Úformat_infoÚsubtype_infoÚsectionsÚ
extra_info)Úselfr–   r¶   r£   r¥   r¥   r¦   Ú__init__¯  s   

"ôz_SoundFileInfo.__init__c                 C   sv   t | jdƒ\}}t |dƒ\}}|dkrd |||¡}|S |dkr(d ||¡}|S |dkr4d | j¡}|S d |¡}|S )Ni  é<   r   z{0:.0g}:{1:02.0g}:{2:05.3f} hz{0:02.0g}:{1:05.3f} minz{0:d} samplesz	{0:.3f} s)Údivmodr¸   rŸ   r—   )r½   ÚhoursÚrestÚminutesÚsecondsr¸   r¥   r¥   r¦   Ú_duration_str¿  s   úü
ÿz_SoundFileInfo._duration_strc                 C   sD   d  g d¢¡}| jr|d  g d¢¡7 }d  | j d¡¡}| | |¡S )NÚ
)z{0.name}zsamplerate: {0.samplerate} Hzzchannels: {0.channels}zduration: {0._duration_str}z$format: {0.format_info} [{0.format}]z'subtype: {0.subtype_info} [{0.subtype}])z
endian: {0.endian}zsections: {0.sections}zframes: {0.frames}zextra_info: """z
    {1}"""z
    )Újoinr¶   r¼   ÚsplitrŸ   )r½   ÚinfoÚindented_extra_infor¥   r¥   r¦   Ú__repr__Í  s   ÿÿz_SoundFileInfo.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r¾   ÚpropertyrÅ   rË   r¥   r¥   r¥   r¦   r´   ¬  s    
r´   c                 C   s
   t | |ƒS )zŸReturns an object with information about a `SoundFile`.

    Parameters
    ----------
    verbose : bool
        Whether to print additional information.
    )r´   )r–   r¶   r¥   r¥   r¦   rÉ   à  s   
rÉ   c                   C   s   t ttjtjƒƒS )a©  Return a dictionary of available major formats.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.available_formats()
    {'FLAC': 'FLAC (FLAC Lossless Audio Codec)',
     'OGG': 'OGG (OGG Container format)',
     'WAV': 'WAV (Microsoft)',
     'AIFF': 'AIFF (Apple/SGI)',
     ...
     'WAVEX': 'WAVEX (Microsoft)',
     'RAW': 'RAW (header-less)',
     'MAT5': 'MAT5 (GNU Octave 2.1 / Matlab 5.0)'}

    )ÚdictÚ_available_formats_helperÚ_sndÚSFC_GET_FORMAT_MAJOR_COUNTÚSFC_GET_FORMAT_MAJORr¥   r¥   r¥   r¦   Úavailable_formatsë  s   ÿrÖ   c                    s$   t tjtjƒ}t‡ fdd„|D ƒƒS )ad  Return a dictionary of available subtypes.

    Parameters
    ----------
    format : str
        If given, only compatible subtypes are returned.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.available_subtypes('FLAC')
    {'PCM_24': 'Signed 24 bit PCM',
     'PCM_16': 'Signed 16 bit PCM',
     'PCM_S8': 'Signed 8 bit PCM'}

    c                 3   s.    | ]\}}ˆ d u st ˆ |ƒr||fV  qd S rµ   )Úcheck_format)Ú.0r    r·   ©rŸ   r¥   r¦   Ú	<genexpr>  s   €
 ÿÿz%available_subtypes.<locals>.<genexpr>)rÒ   rÓ   ÚSFC_GET_FORMAT_SUBTYPE_COUNTÚSFC_GET_FORMAT_SUBTYPErÑ   )rŸ   Úsubtypesr¥   rÙ   r¦   Úavailable_subtypes   s   ÿrÞ   c              	   C   s,   z	t t| ||ƒƒW S  ttfy   Y dS w )záCheck if the combination of format/subtype/endian is valid.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.check_format('WAV', 'PCM_24')
    True
    >>> sf.check_format('FLAC', 'VORBIS')
    False

    F)ÚboolÚ_format_intÚ
ValueErrorÚ	TypeError)rŸ   r    r¡   r¥   r¥   r¦   r×     s
   ÿr×   c                 C   s   t | ƒ t |  ¡ ¡S )zÍReturn the default subtype for a given format.

    Examples
    --------
    >>> import soundfile as sf
    >>> sf.default_subtype('WAV')
    'PCM_16'
    >>> sf.default_subtype('MAT5')
    'DOUBLE'

    )Ú_check_formatÚ_default_subtypesÚgetÚupperrÙ   r¥   r¥   r¦   Údefault_subtype)  s   rç   c                   @   s6  e Zd ZdZ			dedd„Zedd„ ƒZ	 ed	d„ ƒZ	 ed
d„ ƒZ	 edd„ ƒZ		 edd„ ƒZ
	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZ	 edd„ ƒZdZdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Z e!fd-d.„Z"d/d0„ Z#	3	dfd4d5„Z$dgd6d7„Z%d8d9„ Z&d:d;„ Z'd<d=„ Z(	2	dhd?d@„Z)didAdB„Z*dCdD„ Z+dEdF„ Z,dGdH„ Z-dIdJ„ Z.dKdL„ Z/dMdN„ Z0dOdP„ Z1dQdR„ Z2dSdT„ Z3dUdV„ Z4dWdX„ Z5dYdZ„ Z6d[d\„ Z7d]d^„ Z8d_d`„ Z9dadb„ Z:dcdd„ Z;dS )jr’   z­A sound file.

    For more documentation see the __init__() docstring (which is also
    used for the online documentation (https://python-soundfile.readthedocs.io/).

    r‘   NTc                 C   sà   t |dƒr	| ¡ n|}|| _|du rt|ddƒ}t|ƒ}|| _|	| _|
| _t|||||||ƒ| _	|  
|||¡| _t|ƒ d¡rH|  ¡ rH|  d¡ t | jtjtjtj¡ | jdurl|  | j¡ | jdurn|  | j¡ dS dS dS )añ  Open a sound file.

        If a file is opened with `mode` ``'r'`` (the default) or
        ``'r+'``, no sample rate, channels or file format need to be
        given because the information is obtained from the file. An
        exception is the ``'RAW'`` data format, which always requires
        these data points.

        File formats consist of three case-insensitive strings:

        * a *major format* which is by default obtained from the
          extension of the file name (if known) and which can be
          forced with the format argument (e.g. ``format='WAVEX'``).
        * a *subtype*, e.g. ``'PCM_24'``. Most major formats have a
          default subtype which is used if no subtype is specified.
        * an *endian-ness*, which doesn't have to be specified at all in
          most cases.

        A `SoundFile` object is a *context manager*, which means
        if used in a "with" statement, `close()` is automatically
        called when reaching the end of the code block inside the "with"
        statement.

        Parameters
        ----------
        file : str or int or file-like object
            The file to open.  This can be a file name, a file
            descriptor or a Python file object (or a similar object with
            the methods ``read()``/``readinto()``, ``write()``,
            ``seek()`` and ``tell()``).
        mode : {'r', 'r+', 'w', 'w+', 'x', 'x+'}, optional
            Open mode.  Has to begin with one of these three characters:
            ``'r'`` for reading, ``'w'`` for writing (truncates *file*)
            or ``'x'`` for writing (raises an error if *file* already
            exists).  Additionally, it may contain ``'+'`` to open
            *file* for both reading and writing.
            The character ``'b'`` for *binary mode* is implied because
            all sound files have to be opened in this mode.
            If *file* is a file descriptor or a file-like object,
            ``'w'`` doesn't truncate and ``'x'`` doesn't raise an error.
        samplerate : int
            The sample rate of the file.  If `mode` contains ``'r'``,
            this is obtained from the file (except for ``'RAW'`` files).
        channels : int
            The number of channels of the file.
            If `mode` contains ``'r'``, this is obtained from the file
            (except for ``'RAW'`` files).
        subtype : str, sometimes optional
            The subtype of the sound file.  If `mode` contains ``'r'``,
            this is obtained from the file (except for ``'RAW'``
            files), if not, the default value depends on the selected
            `format` (see `default_subtype()`).
            See `available_subtypes()` for all possible subtypes for
            a given `format`.
        endian : {'FILE', 'LITTLE', 'BIG', 'CPU'}, sometimes optional
            The endian-ness of the sound file.  If `mode` contains
            ``'r'``, this is obtained from the file (except for
            ``'RAW'`` files), if not, the default value is ``'FILE'``,
            which is correct in most cases.
        format : str, sometimes optional
            The major format of the sound file.  If `mode` contains
            ``'r'``, this is obtained from the file (except for
            ``'RAW'`` files), if not, the default value is determined
            from the file extension.  See `available_formats()` for
            all possible values.
        closefd : bool, optional
            Whether to close the file descriptor on `close()`. Only
            applicable if the *file* argument is a file descriptor.
        compression_level : float, optional
            The compression level on 'write()'. The compression level
            should be between 0.0 (minimum compression level) and 1.0
            (highest compression level).
            See `libsndfile document <https://github.com/libsndfile/libsndfile/blob/c81375f070f3c6764969a738eacded64f53a076e/docs/command.md>`__.
        bitrate_mode : {'CONSTANT', 'AVERAGE', 'VARIABLE'}, optional
            The bitrate mode on 'write()'. 
            See `libsndfile document <https://github.com/libsndfile/libsndfile/blob/c81375f070f3c6764969a738eacded64f53a076e/docs/command.md>`__.

        Examples
        --------
        >>> from soundfile import SoundFile

        Open an existing file for reading:

        >>> myfile = SoundFile('existing_file.wav')
        >>> # do something with myfile
        >>> myfile.close()

        Create a new sound file for reading and writing using a with
        statement:

        >>> with SoundFile('new_file.wav', 'x+', 44100, 2) as myfile:
        >>>     # do something with myfile
        >>>     # ...
        >>>     assert not myfile.closed
        >>>     # myfile.close() is called automatically at the end
        >>> assert myfile.closed

        Ú
__fspath__NÚmodezr+r   )Úhasattrrè   Ú_nameÚgetattrÚ_check_modeÚ_modeÚ_compression_levelÚ_bitrate_modeÚ_create_info_structÚ_infoÚ_openÚ_fileÚsetÚ
issupersetÚseekableÚseekrÓ   Ú
sf_commandÚSFC_SET_CLIPPINGÚ_ffiÚNULLÚSF_TRUEÚ_set_compression_levelÚ_set_bitrate_mode)r½   r–   ré   r•   rž   r    r¡   rŸ   r¢   r­   r®   Úmode_intr¥   r¥   r¦   r¾   A  s.   g
ÿ
ÿ

üzSoundFile.__init__c                 C   ó   | j S rµ   )rë   ©r½   r¥   r¥   r¦   Ú<lambda>À  ó    zSoundFile.<lambda>c                 C   r  rµ   )rî   r  r¥   r¥   r¦   r  Â  r  c                 C   ó   | j jS rµ   )rò   r•   r  r¥   r¥   r¦   r  Ä  ó    c                 C   r  rµ   ©rò   r—   r  r¥   r¥   r¦   r  Æ  r  c                 C   r  rµ   )rò   rž   r  r¥   r¥   r¦   r  È  r  c                 C   ó   t | jjtj@ ƒS rµ   )Ú_format_strrò   rŸ   rÓ   ÚSF_FORMAT_TYPEMASKr  r¥   r¥   r¦   r  Ë  ó    c                 C   r  rµ   )r	  rò   rŸ   rÓ   ÚSF_FORMAT_SUBMASKr  r¥   r¥   r¦   r  Î  r  c                 C   r  rµ   )r	  rò   rŸ   rÓ   ÚSF_FORMAT_ENDMASKr  r¥   r¥   r¦   r  Ñ  r  c                 C   ó   t | jjtj@ ƒd S ©Nr   )Ú_format_inforò   rŸ   rÓ   r
  r  r¥   r¥   r¦   r  Ô  ó
    ÿÿc                 C   r  r  )r  rò   rŸ   rÓ   r  r  r¥   r¥   r¦   r  Ø  r  c                 C   r  rµ   )rò   r»   r  r¥   r¥   r¦   r  Û  r  c                 C   s
   | j d u S rµ   )rô   r  r¥   r¥   r¦   r  Ý  s   
 c                 C   s   t  | j¡S rµ   )rÓ   Úsf_errorrô   r  r¥   r¥   r¦   r  ß  s    c                 C   r  rµ   )rï   r  r¥   r¥   r¦   r  á  r  c                 C   r  rµ   )rð   r  r¥   r¥   r¦   r  ã  r  c                 C   s8   t  dd¡}t | jtj|t  |¡¡ t  |¡ dd¡S )z8Retrieve the log string generated when opening the file.zchar[]i @  rŽ   r   )	rû   ÚnewrÓ   rù   rô   ÚSFC_GET_LOG_INFOÚsizeofÚstringÚdecode)r½   rÉ   r¥   r¥   r¦   r¼   æ  s
   
ÿzSoundFile.extra_infoc                 C   sD   | j d urd | j ¡nd}|| jd urd | j¡nd7 }d | |¡S )Nz, compression_level={0}Ú z, bitrate_mode='{0}'zœSoundFile({0.name!r}, mode={0.mode!r}, samplerate={0.samplerate}, channels={0.channels}, format={0.format!r}, subtype={0.subtype!r}, endian={0.endian!r}{1}))r­   rŸ   r®   )r½   Úcompression_settingr¥   r¥   r¦   rË   ñ  s   
ÿÿ
ÿÿýzSoundFile.__repr__c                 C   ó   |   ¡  d S rµ   ©Úcloser  r¥   r¥   r¦   Ú__del__û  ó   zSoundFile.__del__c                 C   s   | S rµ   r¥   r  r¥   r¥   r¦   Ú	__enter__þ  s   zSoundFile.__enter__c                 G   r  rµ   r  )r½   Úargsr¥   r¥   r¦   Ú__exit__  r  zSoundFile.__exit__c                 C   sF   |t v r|  ¡  t | jt | | ¡ ¡}t|ƒ dS t | ||¡ dS )z:Write text meta-data in the sound file through properties.N)	Ú
_str_typesÚ_check_if_closedrÓ   Úsf_set_stringrô   ÚencodeÚ_error_checkÚobjectÚ__setattr__)r½   r·   ÚvalueÚerrr¥   r¥   r¦   r(    s   ÿzSoundFile.__setattr__c                 C   sJ   |t v r|  ¡  t | jt | ¡}|rt |¡ dd¡S dS td 	|¡ƒ‚)z9Read text meta-data in the sound file through properties.rŽ   r   r  z)'SoundFile' object has no attribute {0!r})
r"  r#  rÓ   Úsf_get_stringrô   rû   r  r  ÚAttributeErrorrŸ   )r½   r·   r¤   r¥   r¥   r¦   Ú__getattr__  s   ÿzSoundFile.__getattr__c                 C   r  rµ   r  r  r¥   r¥   r¦   Ú__len__  ó   zSoundFile.__len__c                 C   s   dS )NTr¥   r  r¥   r¥   r¦   Ú__bool__  s   zSoundFile.__bool__c                 C   s   |   ¡ S rµ   )r0  r  r¥   r¥   r¦   Ú__nonzero__"  r/  zSoundFile.__nonzero__c                 C   s   | j jtjkS )z)Return True if the file supports seeking.)rò   r÷   rÓ   rý   r  r¥   r¥   r¦   r÷   '  s   zSoundFile.seekablec                 C   s&   |   ¡  t | j||¡}t| jƒ |S )aÜ  Set the read/write position.

        Parameters
        ----------
        frames : int
            The frame index or offset to seek.
        whence : {SEEK_SET, SEEK_CUR, SEEK_END}, optional
            By default (``whence=SEEK_SET``), *frames* are counted from
            the beginning of the file.
            ``whence=SEEK_CUR`` seeks from the current position
            (positive and negative values are allowed for *frames*).
            ``whence=SEEK_END`` seeks from the end (use negative value
            for *frames*).

        Returns
        -------
        int
            The new absolute read/write position in frames.

        Examples
        --------
        >>> from soundfile import SoundFile, SEEK_END
        >>> myfile = SoundFile('stereo_file.wav')

        Seek to the beginning of the file:

        >>> myfile.seek(0)
        0

        Seek to the end of the file:

        >>> myfile.seek(0, SEEK_END)
        44100  # this is the file length

        )r#  rÓ   Úsf_seekrô   r&  Ú
_errorcode)r½   r—   ÚwhenceÚpositionr¥   r¥   r¦   rø   +  s   $
zSoundFile.seekc                 C   s   |   dt¡S )z'Return the current read/write position.r   )rø   r   r  r¥   r¥   r¦   ÚtellT  s   zSoundFile.tellr   ry   Fc                 C   s‚   |du r|   ||¡}|  |||¡}n|dk s|t|ƒkr t|ƒ}|  d||¡}t|ƒ|kr?|du r9|d|… }|S |||d…< |S )a  Read from the file and return data as NumPy array.

        Reads the given number of frames in the given data format
        starting at the current read/write position.  This advances the
        read/write position by the same number of frames.
        By default, all frames from the current read/write position to
        the end of the file are returned.
        Use `seek()` to move the current read/write position.

        Parameters
        ----------
        frames : int, optional
            The number of frames to read. If ``frames < 0``, the whole
            rest of the file is read.
        dtype : {'float64', 'float32', 'int32', 'int16'}, optional
            Data type of the returned array, by default ``'float64'``.
            Floating point audio data is typically in the range from
            ``-1.0`` to ``1.0``. Integer data is in the range from
            ``-2**15`` to ``2**15-1`` for ``'int16'`` and from
            ``-2**31`` to ``2**31-1`` for ``'int32'``.

            .. note:: Reading int values from a float file will *not*
                scale the data to [-1.0, 1.0). If the file contains
                ``np.array([42.6], dtype='float32')``, you will read
                ``np.array([43], dtype='int32')`` for
                ``dtype='int32'``.

        Returns
        -------
        audiodata : `numpy.ndarray` or type(out)
            A two-dimensional NumPy (frames x channels) array is
            returned. If the sound file has only one channel, a
            one-dimensional array is returned. Use ``always_2d=True``
            to return a two-dimensional array anyway.

            If *out* was specified, it is returned. If *out* has more
            frames than available in the file (or if *frames* is
            smaller than the length of *out*) and no *fill_value* is
            given, then only a part of *out* is overwritten and a view
            containing all valid frames is returned.

        Other Parameters
        ----------------
        always_2d : bool, optional
            By default, reading a mono sound file will return a
            one-dimensional array. With ``always_2d=True``, audio data
            is always returned as a two-dimensional array, even if the
            audio file has only one channel.
        fill_value : float, optional
            If more frames are requested than available in the file,
            the rest of the output is be filled with *fill_value*. If
            *fill_value* is not specified, a smaller array is
            returned.
        out : `numpy.ndarray` or subclass, optional
            If *out* is specified, the data is written into the given
            array instead of creating a new array. In this case, the
            arguments *dtype* and *always_2d* are silently ignored! If
            *frames* is not given, it is obtained from the length of
            *out*.

        Examples
        --------
        >>> from soundfile import SoundFile
        >>> myfile = SoundFile('stereo_file.wav')

        Reading 3 frames from a stereo file:

        >>> myfile.read(3)
        array([[ 0.71329652,  0.06294799],
               [-0.26450912, -0.38874483],
               [ 0.67398441, -0.11516333]])
        >>> myfile.close()

        See Also
        --------
        buffer_read, .write

        Nr   r”   )Ú_check_framesÚ_create_empty_arrayÚlenÚ	_array_io)r½   r—   rš   r›   rœ   r   r¥   r¥   r¦   r”   X  s   PÿzSoundFile.readc                 C   sT   | j |dd}|  |¡}t |d || j ¡}|  d|||¡}||ks%J ‚t |¡S )a‘  Read from the file and return data as buffer object.

        Reads the given number of *frames* in the given data format
        starting at the current read/write position.  This advances the
        read/write position by the same number of frames.
        By default, all frames from the current read/write position to
        the end of the file are returned.
        Use `seek()` to move the current read/write position.

        Parameters
        ----------
        frames : int, optional
            The number of frames to read. If ``frames < 0``, the whole
            rest of the file is read.
        dtype : {'float64', 'float32', 'int32', 'int16'}
            Audio data will be converted to the given data type.

        Returns
        -------
        buffer
            A buffer containing the read data.

        See Also
        --------
        buffer_read_into, .read, buffer_write

        N)rœ   z[]r”   )r7  Ú_check_dtyperû   r  rž   Ú	_cdata_ioÚbuffer)r½   r—   rš   ÚctypeÚcdataÚread_framesr¥   r¥   r¦   Úbuffer_read¶  s   

zSoundFile.buffer_readc                 C   s.   |   |¡}|  ||¡\}}|  d|||¡}|S )a  Read from the file into a given buffer object.

        Fills the given *buffer* with frames in the given data format
        starting at the current read/write position (which can be
        changed with `seek()`) until the buffer is full or the end
        of the file is reached.  This advances the read/write position
        by the number of frames that were read.

        Parameters
        ----------
        buffer : writable buffer
            Audio frames from the file are written to this buffer.
        dtype : {'float64', 'float32', 'int32', 'int16'}
            The data type of *buffer*.

        Returns
        -------
        int
            The number of frames that were read from the file.
            This can be less than the size of *buffer*.
            The rest of the buffer is not filled with meaningful data.

        See Also
        --------
        buffer_read, .read

        r”   )r;  Ú_check_bufferr<  )r½   r=  rš   r>  r?  r—   r¥   r¥   r¦   Úbuffer_read_intoÙ  s   
zSoundFile.buffer_read_intoc                 C   sB   ddl }| |¡}|  d|t|ƒ¡}|t|ƒksJ ‚|  |¡ dS )aø  Write audio data from a NumPy array to the file.

        Writes a number of frames at the read/write position to the
        file. This also advances the read/write position by the same
        number of frames and enlarges the file if necessary.

        Note that writing int values to a float file will *not* scale
        the values to [-1.0, 1.0). If you write the value
        ``np.array([42], dtype='int32')``, to a ``subtype='FLOAT'``
        file, the file will then contain ``np.array([42.],
        dtype='float32')``.

        Parameters
        ----------
        data : array_like
            The data to write. Usually two-dimensional (frames x
            channels), but one-dimensional *data* can be used for mono
            files. Only the data types ``'float64'``, ``'float32'``,
            ``'int32'`` and ``'int16'`` are supported.

            .. note:: The data type of *data* does **not** select the
                  data type of the written file. Audio data will be
                  converted to the given *subtype*. Writing int values
                  to a float file will *not* scale the values to
                  [-1.0, 1.0). If you write the value ``np.array([42],
                  dtype='int32')``, to a ``subtype='FLOAT'`` file, the
                  file will then contain ``np.array([42.],
                  dtype='float32')``.

        Examples
        --------
        >>> import numpy as np
        >>> from soundfile import SoundFile
        >>> myfile = SoundFile('stereo_file.wav')

        Write 10 frames of random data to a new file:

        >>> with SoundFile('stereo_file.wav', 'w', 44100, 2, 'PCM_24') as f:
        >>>     f.write(np.random.randn(10, 2))

        See Also
        --------
        buffer_write, .read

        r   Nr¬   )r¨   Úascontiguousarrayr:  r9  Ú_update_frames)r½   r¤   r¯   Úwrittenr¥   r¥   r¦   r¬   ú  s
   .
zSoundFile.writec                 C   sD   |   |¡}|  ||¡\}}|  d|||¡}||ksJ ‚|  |¡ dS )a€  Write audio data from a buffer/bytes object to the file.

        Writes the contents of *data* to the file at the current
        read/write position.
        This also advances the read/write position by the number of
        frames that were written and enlarges the file if necessary.

        Parameters
        ----------
        data : buffer or bytes
            A buffer or bytes object containing the audio data to be
            written.
        dtype : {'float64', 'float32', 'int32', 'int16'}
            The data type of the audio data stored in *data*.

        See Also
        --------
        .write, buffer_read

        r¬   N)r;  rB  r<  rE  )r½   r¤   rš   r>  r?  r—   rF  r¥   r¥   r¦   Úbuffer_write0  s
   
zSoundFile.buffer_writer   c              	   c   st   ddl }d| jvrd| jvrtdƒ‚|  ||¡}|du r:|du r%tdƒ‚|dur+|nt||ƒ}	|  |	||¡}d}
n|durBtdƒ‚t|ƒ}d	}
d}|dkr¸|du rUd}n
t|ƒ}||d|…< t|| |ƒ}|  ||||||d… ¡ |r|du r„| 	|| d… ¡}n|| d… |dd…< ||| kr¢|du r¢|d|| … }n|}|
r«| 	|¡n|V  ||8 }|dksNdS dS )
aÆ  Return a generator for block-wise reading.

        By default, the generator yields blocks of the given
        *blocksize* (using a given *overlap*) until the end of the file
        is reached; *frames* can be used to stop earlier.

        Parameters
        ----------
        blocksize : int
            The number of frames to read per block. Either this or *out*
            must be given.
        overlap : int, optional
            The number of frames to rewind between each block.
        frames : int, optional
            The number of frames to read.
            If ``frames < 0``, the file is read until the end.
        dtype : {'float64', 'float32', 'int32', 'int16'}, optional
            See `read()`.

        Yields
        ------
        `numpy.ndarray` or type(out)
            Blocks of audio data.
            If *out* was given, and the requested frames are not an
            integer multiple of the length of *out*, and no
            *fill_value* was given, the last block will be a smaller
            view into *out*.


        Other Parameters
        ----------------
        always_2d, fill_value, out
            See `read()`.
        fill_value : float, optional
            See `read()`.
        out : `numpy.ndarray` or subclass, optional
            If *out* is specified, the data is written into the given
            array instead of creating a new array. In this case, the
            arguments *dtype* and *always_2d* are silently ignored!

        Examples
        --------
        >>> from soundfile import SoundFile
        >>> with SoundFile('stereo_file.wav') as f:
        >>>     for block in f.blocks(blocksize=1024):
        >>>         pass  # do something with 'block'

        r   Nr‘   ú+z*blocks() is not allowed in write-only modez)One of {blocksize, out} must be specifiedTz-Only one of {blocksize, out} may be specifiedF)
r¨   ré   ÚSoundFileRuntimeErrorr7  râ   Úminr8  r9  r”   Úcopy)r½   r±   r²   r—   rš   r›   rœ   r   r¯   Úout_sizeÚcopy_outÚoverlap_memoryÚoutput_offsetÚtoreadr³   r¥   r¥   r¦   r°   K  sF   €2ÿëzSoundFile.blocksc                 C   sX   |du r|   ¡ }t | jtjt d|¡t d¡¡}|r&t | j¡}t	|dƒ‚|| j
_dS )an  Truncate the file to a given number of frames.

        After this command, the read/write position will be at the new
        end of the file.

        Parameters
        ----------
        frames : int, optional
            Only the data before *frames* is kept, the rest is deleted.
            If not specified, the current read/write position is used.

        Nzsf_count_t*Ú
sf_count_tzError truncating the file)r6  rÓ   rù   rô   ÚSFC_FILE_TRUNCATErû   r  r  r  ÚLibsndfileErrorrò   r—   )r½   r—   r*  r¥   r¥   r¦   Útruncate¨  s   
þ
zSoundFile.truncatec                 C   s   |   ¡  t | j¡ dS )aj  Write unwritten data to the file system.

        Data written with `write()` is not immediately written to
        the file system but buffered in memory to be written at a later
        time.  Calling `flush()` makes sure that all changes are
        actually written to the file system.

        This has no effect on files opened in read-only mode.

        N)r#  rÓ   Úsf_write_syncrô   r  r¥   r¥   r¦   ÚflushÀ  s   zSoundFile.flushc                 C   s0   | j s|  ¡  t | j¡}d| _t|ƒ dS dS )z.Close the file.  Can be called multiple times.N)ÚclosedrV  rÓ   Úsf_closerô   r&  )r½   r*  r¥   r¥   r¦   r  Î  s   ûzSoundFile.closec                 C   s0  t |ttfƒrOtj |¡r/d| jv rtd | j	¡ƒ‚t
| jƒ d¡r/t t |tjtjB ¡¡ tj}t |tƒrGtjdkr@tj}n| t ¡ ¡}|||| jƒ}n*t |tƒr^t ||| j|¡}nt||ƒrqt |  |¡|| jtj¡}ntd | j	¡ƒ‚|tjkrt  |¡}t!|d | j	¡d‚|tj"kr–d| j_#|S )	z9Call the appropriate sf_open*() function from libsndfile.ÚxzFile exists: {0!r}zw+rƒ   zInvalid file: {0!r}zError opening {0!r}: ©Úprefixr   )$Ú
isinstanceÚ_unicodeÚbytesÚ_osÚpathÚisfileré   ÚOSErrorrŸ   r·   rõ   rö   r  ÚopenÚO_WRONLYÚO_TRUNCrÓ   Úsf_openÚ_sysÚplatformÚsf_wchar_openr%  Úgetfilesystemencodingrò   rw   Ú
sf_open_fdÚ_has_virtual_io_attrsÚsf_open_virtualÚ_init_virtual_iorû   rü   râ   r  rS  Ú	SFM_WRITEr—   )r½   r–   r   r¢   ÚopenfunctionÚfile_ptrr*  r¥   r¥   r¦   ró   ×  s2   





ÿ


zSoundFile._openc                    sŽ   t  d¡‡ fdd„ƒ}t  d¡‡ fdd„ƒ}t  d¡‡ fdd	„ƒ}t  d
¡‡ fdd„ƒ}t  d¡‡ fdd„ƒ}|||||dœ| _t  d| j¡S )z4Initialize callback functions for sf_open_virtual().Úsf_vio_get_filelenc                    s,   ˆ   ¡ }ˆ  dt¡ ˆ   ¡ }ˆ  |t¡ |S ©Nr   )r6  rø   r   r   )Ú	user_dataÚcurrÚsize©r–   r¥   r¦   Úvio_get_filelený  s
   z3SoundFile._init_virtual_io.<locals>.vio_get_filelenÚsf_vio_seekc                    s   ˆ   | |¡ ˆ  ¡ S rµ   )rø   r6  )Úoffsetr4  rt  rw  r¥   r¦   Úvio_seek  s   z,SoundFile._init_virtual_io.<locals>.vio_seekÚsf_vio_readc                    s\   zt  | |¡}ˆ  |¡}W |S  ty-   ˆ  |¡}t|ƒ}t  | |¡}||d|…< Y |S w rs  )rû   r=  Úreadintor,  r”   r9  )ÚptrÚcountrt  ÚbufÚ	data_readr¤   rw  r¥   r¦   Úvio_read
  s   û
ûz,SoundFile._init_virtual_io.<locals>.vio_readÚsf_vio_writec                    s2   t  | |¡}|d d … }ˆ  |¡}|d u r|}|S rµ   )rû   r=  r¬   )r~  r  rt  r€  r¤   rF  rw  r¥   r¦   Ú	vio_write  s   
z-SoundFile._init_virtual_io.<locals>.vio_writeÚsf_vio_tellc                    s   ˆ   ¡ S rµ   )r6  )rt  rw  r¥   r¦   Úvio_tell!  s   z,SoundFile._init_virtual_io.<locals>.vio_tell)Úget_filelenrø   r”   r¬   r6  zSF_VIRTUAL_IO*)rû   ÚcallbackÚ_virtual_ior  )r½   r–   rx  r{  r‚  r„  r†  r¥   rw  r¦   rn  û  s"   	üzSoundFile._init_virtual_ioc                 C   s   t S )z€Return all attributes used in __setattr__ and __getattr__.

        This is useful for auto-completion (e.g. IPython).

        )r"  r  r¥   r¥   r¦   Ú_getAttributeNames.  s   zSoundFile._getAttributeNamesc                 C   s   | j rtdƒ‚dS )z…Check if the file is closed and raise an error if it is.

        This should be used in every method that uses self._file.

        zI/O operation on closed fileN)rW  rI  r  r¥   r¥   r¦   r#  6  s   ÿzSoundFile._check_if_closedc                 C   sJ   |   ¡ r| j|  ¡  }|dk s||kr|du r|}|S |dk r#tdƒ‚|S )z8Reduce frames to no more than are available in the file.r   Nz/frames must be specified for non-seekable files)r÷   r—   r6  rá   )r½   r—   rœ   Úremaining_framesr¥   r¥   r¦   r7  ?  s   þzSoundFile._check_framesc                 C   sV   |t  ¡ v sJ ‚t|tƒst |¡}tt|ƒ| jt 	|¡ ƒ\}}|r't
dƒ‚||fS )z1Convert buffer to cdata and check for valid size.z*Data size must be a multiple of frame size)Ú
_ffi_typesÚvaluesr\  r^  rû   Úfrom_bufferrÀ   r9  rž   r  rá   )r½   r¤   r>  r—   Ú	remainderr¥   r¥   r¦   rB  J  s   

ÿzSoundFile._check_bufferc                 C   s8   ddl }|s| jdkr|| jf}n|f}|j||ddS )z-Create an empty array with appropriate shape.r   Nr   ÚC)Úorder)r¨   rž   Úempty)r½   r—   r›   rš   r¯   r«   r¥   r¥   r¦   r8  U  s
   zSoundFile._create_empty_arrayc              	   C   s2   zt | W S  ty   td tt  ¡ ƒ|¡ƒ‚w )z7Check if dtype string is valid and return ctype string.z(dtype must be one of {0!r} and not {1!r})rŒ  ÚKeyErrorrá   rŸ   ÚsortedÚkeys)r½   rš   r¥   r¥   r¦   r;  ^  s   
ÿÿzSoundFile._check_dtypec                 C   sÄ   |j dvrtd |j|j dk rd¡ƒ‚d¡ƒ‚|j dkrdn|jd }|| jkr3td |j| j|¡ƒ‚|jjs;tdƒ‚|  |jj	¡}|jj
t |¡ksMJ ‚t |d |jd	 d
 ¡}|  ||||¡S )z+Check array and call low-level IO function.)r   r   zInvalid shape: {0!r} ({1})r   z0 dimensions not supportedztoo many dimensionsz5Invalid shape: {0!r} (Expected {1} channels, got {2})zData must be C-contiguousÚ*r¤   r   )rª   rá   rŸ   r«   rž   ÚflagsÚc_contiguousr;  rš   r·   Úitemsizerû   r  ÚcastÚ__array_interface__r<  )r½   ÚactionÚarrayr—   Úarray_channelsr>  r?  r¥   r¥   r¦   r:  f  s   
$
zSoundFile._array_ioc                 C   sr   |t  ¡ v sJ ‚|  ¡  |  ¡ r|  ¡ }ttd| d | ƒ}|| j||ƒ}t| j	ƒ |  ¡ r7|  
|| t¡ |S )z.Call one of libsndfile's read/write functions.Úsf_Úf_)rŒ  r  r#  r÷   r6  rì   rÓ   rô   r&  r3  rø   r   )r½   rœ  r¤   r>  r—   ru  Úfuncr¥   r¥   r¦   r<  t  s   
zSoundFile._cdata_ioc                 C   sD   |   ¡ r|  ¡ }|  dt¡| j_|  |t¡ dS | j j|7  _dS )z!Update self.frames after writing.r   N)r÷   r6  rø   r   rò   r—   r   )r½   rF  ru  r¥   r¥   r¦   rE    s
   zSoundFile._update_framesc                 C   s|   |dkr|   ¡ stdƒ‚|dkr|durtdƒ‚t||ƒ | j¡\}}}||k r*|}|dk r2|| }|   ¡ r<|  |t¡ |S )z)Seek to start frame and calculate length.r   z(start is only allowed for seekable filesNz&Only one of {frames, stop} may be used)r÷   rá   râ   ÚsliceÚindicesr—   rø   r   )r½   r˜   r™   r—   Ú_r¥   r¥   r¦   r“   Š  s   zSoundFile._prepare_readc                 C   sB   i }t  ¡ D ]\}}t | j|¡}|rt |¡ dd¡||< q|S )a5  Get all metadata present in this SoundFile

        Returns
        -------

        metadata: dict[str, str]
            A dict with all metadata. Possible keys are: 'title', 'copyright',
            'software', 'artist', 'comment', 'date', 'album', 'license',
            'tracknumber' and 'genre'.
        rŽ   r   )r"  ÚitemsrÓ   r+  rô   rû   r  r  )r½   ÚstrsÚstrtypeÚstridr¤   r¥   r¥   r¦   Úcopy_metadataš  s   €zSoundFile.copy_metadatac                 C   sf   |t v sJ ‚t d¡}t | |d< t | jtj|t |¡¡}|tjkr1t 	| j¡}t
|d|› ƒ‚dS )z,Call libsndfile's set bitrate mode function.zint[1]r   zError set bitrate mode N)Ú_bitrate_modesrû   r  rÓ   rù   rô   ÚSFC_SET_BITRATE_MODEr  rý   r  rS  )r½   r®   Úpointer_bitrate_moder*  r¥   r¥   r¦   rÿ   ¬  s   

þzSoundFile._set_bitrate_modec                 C   sz   d|  krdkst dƒ‚ t dƒ‚t d¡}||d< t | jtj|t |¡¡}|tjkr;t 	| j¡}t
|d|› ƒ‚dS )z1Call libsndfile's set compression level function.r   r   z)Compression level must be in range [0..1]z	double[1]zError set compression level N)rá   rû   r  rÓ   rù   rô   ÚSFC_SET_COMPRESSION_LEVELr  rý   r  rS  )r½   r­   Úpointer_compression_levelr*  r¥   r¥   r¦   rþ   ¸  s   ÿ

þz SoundFile._set_compression_level)	r‘   NNNNNTNN)r   ry   FNN)r   N)Nr   r   ry   FNNrµ   )<rÌ   rÍ   rÎ   rÏ   r¾   rÐ   r·   ré   r•   r—   rž   rŸ   r    r¡   r¹   rº   r»   rW  r3  r­   r®   r¼   rô   rË   r  r  r!  r(  r-  r.  r0  r1  r÷   r   rø   r6  r”   rA  rC  r¬   rG  r°   rT  rV  r  ró   rn  rŠ  r#  r7  rB  r8  r;  r:  r<  rE  r“   r©  rÿ   rþ   r¥   r¥   r¥   r¦   r’   9  s²    
þÿÿÿÿÿ



)
ÿ
^#!6
ÿ
]	$3			r’   r  c                 C   s   | dkr
t | |d‚dS )z+Raise LibsndfileError if there is an error.r   rZ  N)rS  )r*  r[  r¥   r¥   r¦   r&  Å  s   ÿr&  c                 C   s  t | ƒ}|du rt| ƒ}|du rtd | ¡ƒ‚nt|ttfƒs&td |¡ƒ‚z
|t| ¡  O }W n t	y>   t
d |¡ƒ‚w |du rFd}nt|ttfƒsTtd |¡ƒ‚z
|t| ¡  O }W n t	yl   t
d |¡ƒ‚w t d¡}||_d	|_t |¡tjkr„t
d
ƒ‚|S )z8Return numeric ID for given format|subtype|endian combo.Nz)No default subtype for major format {0!r}zInvalid subtype: {0!r}zUnknown subtype: {0!r}rq   zInvalid endian-ness: {0!r}zUnknown endian-ness: {0!r}úSF_INFO*r   z1Invalid combination of format, subtype and endian)rã   rç   râ   rŸ   r\  r]  ÚstrÚ	_subtypesræ   r“  rá   Ú_endiansrû   r  rž   rÓ   Úsf_format_checkÚSF_FALSE)rŸ   r    r¡   ÚresultrÉ   r¥   r¥   r¦   rà   Ë  s@   ÿÿÿÿ
ÿrà   c                 C   s”   t | ttfƒstd | ¡ƒ‚t| ƒ}| d¡st| ƒt|ƒkr&td | ¡ƒ‚t| 	d¡ƒdkr3tdƒ‚d|v r<t
j}|S d|v rEt
j}|S t
j}|S )z=Check if mode is valid and return its integer representation.zInvalid mode: {0!r}zxrwb+Úxrwr   z&mode must contain exactly one of 'xrw'rH  r‘   )r\  r]  r°  râ   rŸ   rõ   Ú
differencer9  rá   ÚintersectionrÓ   ÚSFM_RDWRÚSFM_READro  )ré   Úmode_setr   r¥   r¥   r¦   rí   ë  s   üÿrí   c           	      C   s¶   |}|du rt | |ƒ}t|ttfƒsJ ‚nt|ƒ t d¡}d|vs(| ¡ dkrG|du r0tdƒ‚||_	|du r;tdƒ‚||_
t|||ƒ|_|S tdd„ |||||fD ƒƒrYtd	ƒ‚|S )
z*Check arguments and create SF_INFO struct.Nr¯  r‘   r   zsamplerate must be specifiedzchannels must be specifiedc                 s   s    | ]}|d uV  qd S rµ   r¥   )rØ   Úargr¥   r¥   r¦   rÚ     s   € z&_create_info_struct.<locals>.<genexpr>z\Not allowed for existing files (except 'RAW'): samplerate, channels, format, subtype, endian)Ú_get_format_from_filenamer\  r]  r°  rã   rû   r  ræ   râ   r•   rž   rà   rŸ   Úany)	r–   ré   r•   rž   rŸ   r    r¡   Úoriginal_formatrÉ   r¥   r¥   r¦   rñ   þ  s(   

ü

ÿrñ   c                 C   sr   d}t | d| ƒ} ztj | ¡d dd… }| dd¡}W n	 ty%   Y nw | ¡ tvr7d|vr7td	 	| ¡ƒ‚|S )
a  Return a format string obtained from file (or file.name).

    If file already exists (= read mode), an empty string is returned on
    error.  If not, an exception is raised.
    The return type will always be str or unicode (even if
    file/file.name is a bytes object).

    r  r·   r   r   NrŽ   r   r‘   zGNo format specified and unable to get format from file extension: {0!r})
rì   r_  r`  Úsplitextr  Ú	Exceptionræ   Ú_formatsrâ   rŸ   )r–   ré   rŸ   r¥   r¥   r¦   r½    s   	ÿÿr½  c                 C   s:   t ttfD ]}| ¡ D ]\}}|| kr|    S qqdS )z;Return the string representation of a given numeric format.zn/a)rÂ  r±  r²  r¥  )Ú
format_intÚ
dictionaryÚkÚvr¥   r¥   r¦   r	  1  s   ÿÿr	  c                 C   sT   t  d¡}| |_t t j||t  d¡¡ |j}t|jƒ|r't  	|¡ 
dd¡fS dfS )z6Return the ID and short description of a given format.zSF_FORMAT_INFO*ÚSF_FORMAT_INFOrŽ   r   r  )rû   r  rŸ   rÓ   rù   rü   r  r·   r	  r  r  )rÃ  Úformat_flagr¹   r·   r¥   r¥   r¦   r  ;  s   
ÿÿÿr  c                 c   sF    t  d¡}t t j| |t  d¡¡ t|d ƒD ]}t||ƒV  qdS )z8Helper for available_formats() and available_subtypes().zint*rw   r   N)rû   r  rÓ   rù   rü   r  Úranger  )Ú
count_flagrÈ  r  rÃ  r¥   r¥   r¦   rÒ   F  s   €
ÿrÒ   c                 C   sL   t | ttfƒstd | ¡ƒ‚z	t|  ¡  }W |S  ty%   td | ¡ƒ‚w )z4Check if `format_str` is valid and return format ID.zInvalid format: {0!r}zUnknown format: {0!r})	r\  r]  r°  râ   rŸ   rÂ  ræ   r“  rá   )Ú
format_strrÃ  r¥   r¥   r¦   rã   N  s   þÿrã   c                 C   sN   |t jk}|t jk}tt| dƒt| dƒt| dƒp|t| dƒp$t| dƒp$|gƒS )z>Check if file has all the necessary attributes for virtual IO.rø   r6  r¬   r”   r}  )rÓ   rº  ro  Úallrê   )r–   r   ÚreadonlyÚ	writeonlyr¥   r¥   r¦   rl  Y  s   

ürl  c                   @   ó   e Zd ZdZdS )ÚSoundFileErrorz-Base class for all soundfile-specific errors.N©rÌ   rÍ   rÎ   rÏ   r¥   r¥   r¥   r¦   rÐ  e  s    rÐ  c                   @   rÏ  )rI  zKsoundfile module runtime error.

    Errors that used to be `RuntimeError`.NrÑ  r¥   r¥   r¥   r¦   rI  i  s    rI  c                   @   s.   e Zd ZdZd
dd„Zedd„ ƒZdd„ Zd	S )rS  zjlibsndfile errors.


    Attributes
    ----------
    code
        libsndfile internal error number.
    r  c                 C   s   t  | ||¡ || _|| _d S rµ   )rI  r¾   Úcoder[  )r½   rÒ  r[  r¥   r¥   r¦   r¾   x  s   
zLibsndfileError.__init__c                 C   s(   | j rt | j ¡}t |¡ dd¡S dS )zRaw libsndfile error message.rŽ   r   z'(Garbled error message from libsndfile))rÒ  rÓ   Úsf_error_numberrû   r  r  )r½   Úerr_strr¥   r¥   r¦   Úerror_string}  s   zLibsndfileError.error_stringc                 C   s   | j | j S rµ   )r[  rÕ  r  r¥   r¥   r¦   Ú__str__‰  r  zLibsndfileError.__str__N©r  )rÌ   rÍ   rÎ   rÏ   r¾   rÐ   rÕ  rÖ  r¥   r¥   r¥   r¦   rS  o  s    

rS  )r   r   Nry   FNNNNNNNT)NNNTNN)Nr   r   r   Nry   FNNNNNNNT)Frµ   )NNr×  )RrÏ   Ú__version__Úosr_  Úsysrg  r   r   r   Úctypes.utilr   Ú_find_libraryÚ
_soundfiler   rû   Úunicoder]  Ú	NameErrorr°  r"  rÂ  r±  r²  rä   rŒ  rª  rh  r   Ú_machineÚ_packaged_libnamer„   Ú_architecturerb  rŸ   Ú_soundfile_datar`  ÚdirnameÚ__file__Ú_pathrÇ   Ú
_full_pathÚdlopenrÓ   ÚImportErrorrâ   Ú_libnameÚ_explicit_libnameÚisdirÚ_hbrew_pathr  Úsf_version_stringr  Ú__libsndfile_version__Ú
startswithr9  r”   r¬   r°   r'  r´   rÉ   rÖ   rÞ   r×   rç   r’   r&  rà   rí   rñ   r½  r	  ÚSFC_GET_FORMAT_INFOr  rÒ   rã   rl  rÁ  rÐ  ÚRuntimeErrorrI  rS  r¥   r¥   r¥   r¦   Ú<module>   s<   
ÿöÿþýüûúùø	÷
öõôóòñðïæÿþýüûúùø	÷
öõôóòñðïîíìëêéèçæåäãâá à!ß"Þ&üÿþýûúùø	÷
öõôóòñðïîæüýÿ
ÿ
€í€ù
þ]
ÿ9
ý;
4

       
 
