U
    9%eX                     @   s   d dl Z d dl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G dd dZdeedddZdeedddZdS )    N   )cysoxr)QQLQMQHQVHQ)versioni   zConverting input to {}. Change ResampleStream/input dtype to avoid implicit conversion. This implicit conversion is deprecated and will be removed in next major update.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   sz   | t ttttfkr| S t| tkr*tt| 	 } | dkr>t S | dkrJtS | dkrVtS | dkrbtS | dkrntS ttd S )N)ZvhqZsoxr_vhq)ZhqZsoxr_hq)ZmqZsoxr_mq)ZlqZsoxr_lq)ZqqZsoxr_qq)
r   r   r   r   r   typeint
ValueError_QUALITY_ERR_STRlower)q r   L/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/soxr/__init__.py_quality_to_enum   s     r   c                   @   s.   e Zd ZdZdeeed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`.
    float32r   )in_rateout_ratenum_channelsc                 C   s   |dks|dkrt d|dk s(t|k r6t t|t|| _| jtjtjtj	tj
fkrlt t| jt|}t|||| jj|| _d S )Nr   Sample rate should be over 0r   )r   	_CH_LIMIT_CH_EXEED_ERR_STRformatnpdtype_typer   float64int16int32_DTYPE_ERR_STRr   r   ZCySoxrr
   _cysoxr)selfr   r   r   r   qualityr   r   r   r   __init__F   s    zResampleStream.__init__Fc                 C   s   t |tjks|j| jkrBtjt| jt	dd tj
|| jd}t|}|jdkr| j|ddtjf |}tj|ddS |jdkr| j||S tddS )a   Resample chunk with streaming resampler

        Parameters
        ----------
        x : array_like
            Input array. Input can be 1D(mono) or 2D(frames, channels).
            If input is not `np.ndarray` or not dtype in constructor,
            it will be converted to `np.ndarray` with dtype setting.

        last : bool, optional
            Set True at end of input sequence.

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

           )
stacklevelr   r   NZaxisInput must be 1-D or 2-D array)r
   r   ndarrayr   r   warningswarn_CONVERT_WARN_STRr   DeprecationWarningasarrayascontiguousarrayndimr#   processnewaxissqueezer   )r$   xlastyr   r   r   resample_chunkW   s    


zResampleStream.resample_chunkN)r   r   )F)__name__
__module____qualname____doc__floatr   r&   r:   r   r   r   r   r   1   s        r   r   )r   r   c                 C   s   |dks|dkrt dt| tjkr6tj| tjd} | jjtjtjtjtj	fkrdt t
| jjt|}t| } | jdkrt||| ddtjf |}tj|ddS | jdkr| jd }|dk st|k rt t|t||| |S t ddS )	ay   Resample signal

    Parameters
    ----------
    x : array_like
        Input array. Input can be 1D(mono) or 2D(frames, channels).
        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)   r   Nr*   r'   r+   )r   r
   r   r,   r1   r   r   r   r    r!   r"   r   r   r2   r3   r   Zcysoxr_divide_procr5   r6   shaper   r   )r7   r   r   r%   r   r9   r   r   r   r   resamplez   s"    



rA   c                 C   s   t ||| 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.
    )r   Zcysoxr_oneshotr   )r7   r   r   r%   r   r   r   _resample_oneshot   s    rB   )r   )r   )r-   numpyr    r   r   r   r   r   r   _versionr	   __version__Zlibsoxr_versionZ__libsoxr_version__r   r/   r   r"   r   r   r   r?   rA   rB   r   r   r   r   <module>   s   I1