a
    âñh)  ã                   @   sä  d dl Zd dlmZmZmZ d dlZd dlmZ d dlm	Z
mZ ddlmZ ddlmZmZ e
 d¡rrd d	lmZ e
 d¡ejd)eee eeeeeeee eeef dœ
dd„ƒƒZe
 d¡e
 dd¡ejdd„ ƒƒƒZe
 d¡ejd*eeeeeddœdd„ƒƒZe
 d¡ejeeeef dœdd„ƒƒZ e
 d¡d+eeeee ee ee ddœdd„ƒZ!e
 d¡edœdd„ƒZ"e
 d¡edœdd „ƒZ#e
 d¡d,eed!œd"d#„ƒZ$e
 d¡d-eed!œd%d&„ƒZ%e
 d¡d.eed!œd'd(„ƒZ&dS )/é    N)ÚAnyÚOptionalÚTuple)ÚTensor)Úmodule_utilsÚmisc_opsé   )Úcommon)Ú
SignalInfoÚEncodingInfoztorchaudio._torchaudio)Ú_torchaudioT)
ÚfilepathÚoutÚnormalizationÚchannels_firstÚ
num_framesÚoffsetÚ
signalinfoÚencodinginfoÚfiletypeÚreturnc	           
   
   C   sŠ   t | ƒ} tj | ¡s"td | ¡ƒ‚|dur6t |¡ nt 	¡ }|dk rNt
dƒ‚|dk r^t
dƒ‚t | |||||||¡}	t ||¡ ||	fS )zSee torchaudio.loadz{} not found or is a directoryNéÿÿÿÿz6Expected value for num_samples -1 (entire file) or >=0r   zExpected positive offset value)ÚstrÚosÚpathÚisfileÚOSErrorÚformatÚ	_misc_opsÚcheck_inputÚtorchZFloatTensorÚ
ValueErrorr   Zread_audio_fileZnormalize_audio)
r   r   r   r   r   r   r   r   r   Úsample_rate© r#   ú^/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchaudio/backend/sox_backend.pyÚload   s,    ør%   zPlease use "torchaudio.load".z0.9.0c                 K   s   d|d< t | fi |¤ŽS )Ni   r   )r%   )r   Úkwargsr#   r#   r$   Úload_wavA   s    r'   é   )r   Úsrcr"   Ú	precisionr   r   c                 C   sR   t ƒ }|rdnd}||_| ¡ dkr(dn| |¡|_| ¡ |_||_t| |||ƒS )zSee torchaudio.saver   r   )	Úsox_signalinfo_tÚrateÚdimÚsizeZchannelsZnumelÚlengthr*   Úsave_encinfo)r   r)   r"   r*   r   ÚsiÚch_idxr#   r#   r$   ÚsaveI   s    
r3   )r   r   c                 C   s
   t  | ¡S )zSee torchaudio.info)r   Úget_info)r   r#   r#   r$   ÚinfoW   s    r5   )r   r)   r   r   r   r   r   c           
      C   s�  |rdnd\}}t j t j | ¡¡}t j |¡s>td |¡ƒ‚t |¡ | 	¡ dkr`| 
|¡ n,| 	¡ dksz| |¡dkrŒtd | ¡ ¡ƒ‚|�r|jrÊt|jtƒsÊt|jƒ|jkrÂt|jƒ|_ntdƒ‚|j�rt|jtƒ�st|jƒ|jkrþt|jƒ|_ntd	ƒ‚| ¡ d
k�r2| ¡ dk�r2|d }| ¡ }t j | ¡d }	t|	ƒdk�r\|	dd… n|}|�rr| dd¡}| ¡ }t | ||||¡ dS )a  Saves a tensor of an audio signal to disk as a standard format like mp3, wav, etc.

    Args:
        filepath (str): Path to audio file
        src (Tensor): An input 2D tensor of shape `[C x L]` or `[L x C]` where L is
            the number of audio frames, C is the number of channels
        channels_first (bool, optional): Set channels first or length first in result. (Default: ``True``)
        signalinfo (sox_signalinfo_t, optional): A sox_signalinfo_t type, which could be helpful if the
            audio type cannot be automatically determined (Default: ``None``).
        encodinginfo (sox_encodinginfo_t, optional): A sox_encodinginfo_t type, which could be set if the
            audio type cannot be automatically determined (Default: ``None``).
        filetype (str, optional): A filetype or extension to be set if sox cannot determine it
            automatically. (Default: ``None``)

    Example
        >>> data, sample_rate = torchaudio.load('foo.mp3')
        >>> torchaudio.save('foo.wav', data, sample_rate)

    )r   r   )r   r   zDirectory does not exist: {}r   é   r(   z*Expected format where C < 16, but found {}z$Sample rate should be a float or intz"Bit precision should be an integerg      ð¿g      ð?l        r   N)r   r   ÚdirnameÚabspathÚisdirr   r   r   r   r-   Z
unsqueeze_r.   r!   r,   Ú
isinstanceÚfloatÚ	TypeErrorr*   ÚintÚminÚmaxÚlongÚsplitextÚlenZ	transposeÚ
contiguousr   Zwrite_audio_file)
r   r)   r   r   r   r   r2   Zlen_idxZabs_dirpathÚ	extensionr#   r#   r$   r0   ^   s:    
ÿr0   )r   c                   C   s   t  ¡ S )a  Create a sox_signalinfo_t object. This object can be used to set the sample
    rate, number of channels, length, bit precision and headroom multiplier
    primarily for effects

    Returns: sox_signalinfo_t(object)
        - rate (float), sample rate as a float, practically will likely be an integer float
        - channel (int), number of audio channels
        - precision (int), bit precision
        - length (int), length of audio in samples * channels, 0 for unspecified and -1 for unknown
        - mult (float, optional), headroom multiplier for effects and ``None`` for no multiplier

    Example
        >>> si = torchaudio.sox_signalinfo_t()
        >>> si.channels = 1
        >>> si.rate = 16000.
        >>> si.precision = 16
        >>> si.length = 0
    )r   r+   r#   r#   r#   r$   r+   ¥   s    r+   c                  C   s&   t  ¡ } tdƒ}|| _|| _|| _| S )a  Create a sox_encodinginfo_t object.  This object can be used to set the encoding
    type, bit precision, compression factor, reverse bytes, reverse nibbles,
    reverse bits and endianness.  This can be used in an effects chain to encode the
    final output or to save a file with a specific encoding.  For example, one could
    use the sox ulaw encoding to do 8-bit ulaw encoding.  Note in a tensor output
    the result will be a 32-bit number, but number of unique values will be determined by
    the bit precision.

    Returns: sox_encodinginfo_t(object)
        - encoding (sox_encoding_t), output encoding
        - bits_per_sample (int), bit precision, same as `precision` in sox_signalinfo_t
        - compression (float), compression for lossy formats, 0.0 for default compression
        - reverse_bytes (sox_option_t), reverse bytes, use sox_option_default
        - reverse_nibbles (sox_option_t), reverse nibbles, use sox_option_default
        - reverse_bits (sox_option_t), reverse bytes, use sox_option_default
        - opposite_endian (sox_bool), change endianness, use sox_false

    Example
        >>> ei = torchaudio.sox_encodinginfo_t()
        >>> ei.encoding = torchaudio.get_sox_encoding_t(1)
        >>> ei.bits_per_sample = 16
        >>> ei.compression = 0
        >>> ei.reverse_bytes = torchaudio.get_sox_option_t(2)
        >>> ei.reverse_nibbles = torchaudio.get_sox_option_t(2)
        >>> ei.reverse_bits = torchaudio.get_sox_option_t(2)
        >>> ei.opposite_endian = torchaudio.get_sox_bool(0)

    r6   )r   Úsox_encodinginfo_tÚget_sox_option_tZreverse_bytesZreverse_nibblesZreverse_bits)ÚeiZsdor#   r#   r$   rE   ¼   s    rE   )Úir   c                 C   s   | du rt jS t  | ¡S dS )a7  Get enum of sox_encoding_t for sox encodings.

    Args:
        i (int, optional): Choose type or get a dict with all possible options
            use ``__members__`` to see all options when not specified. (Default: ``None``)

    Returns:
        sox_encoding_t: A sox_encoding_t type for output encoding
    N)r   Zsox_encoding_t©rH   r#   r#   r$   Úget_sox_encoding_tâ   s    rJ   r6   c                 C   s   | du rt jS t  | ¡S dS )aJ  Get enum of sox_option_t for sox encodinginfo options.

    Args:
        i (int, optional): Choose type or get a dict with all possible options
            use ``__members__`` to see all options when not specified.
            (Default: ``sox_option_default`` or ``2``)
    Returns:
        sox_option_t: A sox_option_t type
    N)r   Zsox_option_trI   r#   r#   r$   rF   ô   s    rF   c                 C   s   | du rt jS t  | ¡S dS )a6  Get enum of sox_bool for sox encodinginfo options.

    Args:
        i (int, optional): Choose type or get a dict with all possible options
            use ``__members__`` to see all options when not specified. (Default:
            ``sox_false`` or ``0``)

    Returns:
        sox_bool: A sox_bool type
    N)r   Zsox_boolrI   r#   r#   r$   Úget_sox_bool  s    rK   )NTTr   r   NNN)r(   T)TNNN)N)r6   )r   )'Úos.pathr   Útypingr   r   r   r    r   Ztorchaudio._internalr   Z
_mod_utilsr   r   Ú r	   r
   r   Zis_module_availableZ
torchaudior   Zrequires_moduleZ
_impl_loadr   Úboolr=   r%   Ú
deprecatedZ_impl_load_wavr'   Z
_impl_saver3   Z
_impl_infor5   r0   r+   rE   rJ   rF   rK   r#   r#   r#   r$   Ú<module>   sx   
        øø-
    ûûF%