o
    wiY                  	   @   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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]c                 C   sv   | t ttttfv r| S t| tu rtt| 	 } | dv rt S | dv r%tS | dv r+tS | dv r1tS | dv r7tS tt)N)vhqsoxr_vhq)hqsoxr_hq)mqsoxr_mq)lqsoxr_lq)qqsoxr_qq)
r	   r   r   r   r   typeint
ValueError_QUALITY_ERR_STRlower)q r   J/home/ubuntu/sommelier/.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| )N)npfloat32r   SOXR_FLOAT32_Ifloat64SOXR_FLOAT64_Iint32SOXR_INT32_Iint16SOXR_INT16_I	TypeError_DTYPE_ERR_STRformat)ntyper   r   r   _to_soxr_datatype4   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   process_)r   	_CH_LIMIT_CH_EXEED_ERR_STRr)   r   dtype_typer+   r   r   CSoxr_csoxrgetattr_process)selfr-   r.   r/   r4   qualitystyper   r   r   r   __init__V   s   
zResampleStream.__init__Fxreturnc                 C   s   t |tjks|j| jkrtt| jt|}|j	dkr3| 
|ddtjf |}tj|ddS |j	dkr>| 
||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)r   r   ndarrayr4   r5   r'   _DTYPE_UNMATCH_ERR_STRr)   ascontiguousarrayndimr9   newaxissqueezer   )r:   r>   lastyr   r   r   resample_chunkg   s   


zResampleStream.resample_chunkc                 C   
   | j  S )zx Clip counter. (for int I/O)

        Returns
        -------
        int
            Count of clipped samples.
        )r7   	num_clipsr:   r   r   r   rN      s   
zResampleStream.num_clipsc                 C   rM   )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.
        )r7   delayrO   r   r   r   rP      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)r7   clearrO   r   r   r   rQ      s   zResampleStream.clear)r   r   )F)r?   N)__name__
__module____qualname____doc__floatr   r=   r   rD   rL   rN   rP   rQ   r   r   r   r   r,   A   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   r0   )r4   csoxr_split_ch_csoxr_divide_proc_r   Nr@   rB   rC   )r   r   r   rD   asarrayr   stridesitemsizer8   r   r4   AttributeErrorr'   r(   r)   r   rG   rH   rI   shaper2   r3   )r>   r-   r.   r;   divide_procr   rK   r/   r   r   r   resample   s,   


r_   c                 C   s   zt td| j }W n ty   tt| jw t| } | j	dkr<|||| 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.
    csoxr_oneshot_r   Nr@   )r8   r   r4   r\   r'   r(   r)   r   rF   rG   rH   r   rI   )r>   r-   r.   r;   oneshotrK   r   r   r   _resample_oneshot   s   

 rb   )r   )numpyr   numpy.typingr    r   r   r   r   r   r	   _versionr
   __version__libsoxr_version__libsoxr_version__r2   rE   r3   r(   r   r   r+   r,   rV   rD   r_   rb   r   r   r   r   <module>   s    	e$4