o
    i                     @   s  d dl Zd dlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ e ZdZdZd	e Zd
ZdZe
e
de
de
e	e	de	de	eededeeededeeededeiZdd Zdd ZG dd dZd%dededed ejfd!d"Zd%dejdeded ejfd#d$ZdS )&    N)	ArrayLike   )soxr_ext)QQLQMQHQVHQ)versioni   zJInput should be a `np.ndarray` with matching dtype for ResampleStream({}).z2Channel num({}) out of limit. Should be in [1, %d]zAData type must be one of [float32, float64, int16, int32], not {}z,Quality must be one of [QQ, LQ, MQ, HQ, VHQ]ZvhqZsoxr_vhqZhqZsoxr_hqZmqZsoxr_mqZlqZsoxr_lqZqqZsoxr_qqc              	   C   s8   t | tr	|  } zt|  W S  ttfy   ttw N)
isinstancestrlower_QUALITY_ENUM_DICTKeyError	TypeError
ValueError_QUALITY_ERR_STR)q r   \/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/soxr/__init__.py_quality_to_enum&   s   

r   c                 C   sN   | t jkrtjS | t jkrtjS | t jkrtjS | t jkr tj	S t
t| r   )npfloat32r   ZSOXR_FLOAT32_IZfloat64ZSOXR_FLOAT64_IZint32ZSOXR_INT32_IZint16ZSOXR_INT16_Ir   _DTYPE_ERR_STRformat)Zntyper   r   r   _to_soxr_datatype0   s   



r   c                   @   sh   e Zd ZdZ	ddededefddZdd
ejdejfddZ	defddZ
defddZdddZdS )ResampleStreama?   Streaming resampler

        Use `ResampleStream` for real-time processing or very long signal.

        Parameters
        ----------
        in_rate : float
            Input sample-rate.
        out_rate : float
            Output sample-rate.
        num_channels : int
            Number of channels.
        dtype : type or str, optional
            Internal data type processed with.
            Should be one of float32, float64, int16, int32.
        quality : int or str, optional
            Quality setting.
            One of `QQ`, `LQ`, `MQ`, `HQ`, `VHQ`.
    r   r   in_rateout_ratenum_channelsc                 C   s   |dks|dkrt d|dk st|k rt t|t|| _t| j}t|}t	
|||||| _t| jd| j | _d S )Nr   Sample rate should be over 0r   Zprocess_)r   	_CH_LIMIT_CH_EXEED_ERR_STRr   r   dtype_typer   r   r   ZCSoxr_csoxrgetattr_process)selfr   r   r    r$   qualitystyper   r   r   r   __init__R   s   
zResampleStream.__init__Fxreturnc                 C   sz   t |tjks|j| jkrtt| j|jdkr.| 	|ddtj
f |}tj|ddS |jdkr9| 	||S td)a/   Resample chunk with streaming resampler

        Parameters
        ----------
        x : np.ndarray
            Input array. Input can be mono(1D) or multi-channel(2D of [frame, channel]).
            dtype should match with constructor.

        last : bool, optional
            Set True at final chunk to flush last outputs.
            It should be `True` only once at the end of a continuous sequence.

        Returns
        -------
        np.ndarray
            Resampled data.
            Output is np.ndarray with same ndim with input.

        r   NZaxis   Input must be 1-D or 2-D array)typer   ndarrayr$   r%   r   _DTYPE_UNMATCH_ERR_STRr   ndimr(   newaxissqueezer   )r)   r-   lastyr   r   r   resample_chunkc   s   

zResampleStream.resample_chunkc                 C   
   | j  S )zx Clip counter. (for int I/O)

        Returns
        -------
        int
            Count of clipped samples.
        )r&   	num_clipsr)   r   r   r   r<      s   
zResampleStream.num_clipsc                 C   r;   )z Get current delay.

        SoXR output has an algorithmic delay. This function returns the length of current pending output.

        Returns
        -------
        float
            Current delay in output samples.
        )r&   delayr=   r   r   r   r>      s   

zResampleStream.delayNc                 C   s   | j   dS )zv Reset resampler. Ready for fresh signal, same config.

        This can be used to save initialization time.
        N)r&   clearr=   r   r   r   r?      s   zResampleStream.clear)r   r   )F)r.   N)__name__
__module____qualname____doc__floatintr,   r   r3   r:   r<   r>   r?   r   r   r   r   r   =   s    

r   r   r-   r   r   r.   c                 C   s  |dks|dkrt dt| tjkrtj| tjd} z| jd | jkr.tt	d| j
 }n	tt	d| j
 }W n tyG   tt| j
w t|}| jdkrf|||| ddtjf |}tj|ddS | jd	kr| jd }|dk sxt|k rt t||||| |S t d
)a   Resample signal

    Parameters
    ----------
    x : array_like
        Input array. Input can be mono(1D) or multi-channel(2D of [frame, channel]).
        If input is not `np.ndarray`, it will be converted to `np.ndarray(dtype='float32')`.
        Its dtype should be one of float32, float64, int16, int32.
    in_rate : float
        Input sample-rate.
    out_rate : float
        Output sample-rate.
    quality : int or str, optional
        Quality setting.
        One of `QQ`, `LQ`, `MQ`, `HQ`, `VHQ`.

    Returns
    -------
    np.ndarray
        Resampled data.
        Output is `np.ndarray` with same ndim and dtype with input.
    r   r!   )r$   Zcsoxr_split_ch_Zcsoxr_divide_proc_r   Nr/   r0   r1   )r   r2   r   r3   Zasarrayr   stridesitemsizer'   r   r$   AttributeErrorr   r   r   r   r5   r6   r7   shaper"   r#   )r-   r   r   r*   Zdivide_procr   r9   r    r   r   r   resample   s,   


rJ   c                 C   s   zt td| j }W n ty   tt| jw | jdkr7|||| ddtj	f t
|}tj|ddS |||| t
|S )z
    Resample using libsoxr's `soxr_oneshot()`. Use `resample()` for general use.
    `soxr_oneshot()` becomes slow with long input.
    This function exists for test purpose.
    Zcsoxr_oneshot_r   Nr/   )r'   r   r$   rH   r   r   r   r5   r   r6   r   r7   )r-   r   r   r*   Zoneshotr9   r   r   r   _resample_oneshot   s   
 rK   )r   )numpyr   Znumpy.typingr    r   r   r   r   r   r	   _versionr
   __version__Zlibsoxr_versionZ__libsoxr_version__r"   r4   r#   r   r   r   r   r   r   rD   r3   rJ   rK   r   r   r   r   <module>   s,   		
c$4