o
    |ôjµ  ã                   @   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_vhqÚhqÚsoxr_hqÚmqZsoxr_mqÚlqZsoxr_lqÚ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/academy/tools/voices/clone/sv-venv/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_IÚfloat64ZSOXR_FLOAT64_IÚint32ZSOXR_INT32_IÚint16ZSOXR_INT16_Ir   Ú_DTYPE_ERR_STRÚformat)Úntyper   r   r   Ú_to_soxr_datatype0   s   



r%   c                	   @   s„   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dedededdf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`.
        vr : bool, optional
            (Experimental) Enable variable-rate resampling.
            The ratio of the given in_rate and out_rate must equate to the maximum I/O ratio that will be used.
    r   r   FÚ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ÚvrÚstyper   r   r   r   Ú__init__U   s   
zResampleStream.__init__Ú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©Úaxisé   úInput must be 1-D or 2-D array)Útyper   Úndarrayr-   r.   r   Ú_DTYPE_UNMATCH_ERR_STRr#   Úndimr1   ÚnewaxisÚsqueezer   )r2   r7   ÚlastÚyr   r   r   Úresample_chunkf   s   

zResampleStream.resample_chunkc                 C   ó
   | j  ¡ S )zx Clip counter. (for int I/O)

        Returns
        -------
        int
            Count of clipped samples.
        )r/   Ú	num_clips©r2   r   r   r   rG   …   s   
zResampleStream.num_clipsc                 C   rF   )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/   ÚdelayrH   r   r   r   rI   �   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/   ÚclearrH   r   r   r   rJ   ›   s   zResampleStream.clearr   Úslew_lenc                 C   s   | j  || |¡ dS )a‹   (Experimental) Set new sample-rate ratio for next processing.

        `vr=True` must be set at constructor to use this function.
        WARNING: It's an experimental feature and this API may change in future release.

        Parameters
        ----------
        in_rate : float
            New input sample-rate.
        out_rate : float
            New output sample-rate.
        slew_len : int, optional
            Length of smooth transition in input samples. (default: 0)
            If slew_len > 0, the transition will be done smoothly over the given length.
            If slew_len == 0, the transition will be done immediately.
        N)r/   Úset_io_ratio)r2   r'   r(   rK   r   r   r   rL   ¢   s   zResampleStream.set_io_ratio)r   r   F)F)r8   N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚfloatÚintr6   r   r>   rE   rG   rI   rJ   rL   r   r   r   r   r&   =   s    þÿÿ
ÿ

 r&   r   r7   r'   r(   r8   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   Nr9   r;   r<   )r   r=   r   r>   Úasarrayr   ÚstridesÚitemsizer0   r   r-   ÚAttributeErrorr   r"   r#   r   r@   rA   rB   Úshaper+   r,   )r7   r'   r(   r3   Zdivide_procr   rD   r)   r   r   r   Úresample¶   s,   €ÿ


rX   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   Nr9   )r0   r   r-   rV   r   r"   r#   r@   r   rA   r   rB   )r7   r'   r(   r3   ÚoneshotrD   r   r   r   Ú_resample_oneshotê   s   ÿ
 rZ   )r   )Únumpyr   Únumpy.typingr   Ú r   r   r   r   r   r	   Ú_versionr
   Ú__version__Zlibsoxr_versionZ__libsoxr_version__r+   r?   r,   r"   r   r   r   r%   r&   rQ   r>   rX   rZ   r   r   r   r   Ú<module>   s,   	û	
y$4