
    J3j                     X   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j                         ZdZdZd	ez  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 Zd Z G d d      Zd dedededej4                  fdZd dej4                  dededej4                  fdZy)!    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]vhqsoxr_vhqhqsoxr_hqmqsoxr_mqlqsoxr_lqqqsoxr_qqc                     t        | t              r| j                         } 	 t        |    S # t        t
        f$ r t        t              w xY wN)
isinstancestrlower_QUALITY_ENUM_DICTKeyError	TypeError
ValueError_QUALITY_ERR_STR)qs    :/DATA/.local/lib/python3.12/site-packages/soxr/__init__.py_quality_to_enumr"   &   sG    !SGGI+!!$$i  +)**+s	   + A
c                 V   | 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        j                  |             r   )npfloat32r   SOXR_FLOAT32_Ifloat64SOXR_FLOAT64_Iint32SOXR_INT32_Iint16SOXR_INT16_Ir   _DTYPE_ERR_STRformat)ntypes    r!   _to_soxr_datatyper0   0   sx    

&&&	"**	&&&	"((	$$$	"((	$$$--e455    c            	           e Zd ZdZ	 ddededefdZddej                  dej                  fdZ	defd	Z
defd
ZddZddedededdfdZy)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.
    in_rateout_ratenum_channelsc                    |dk  s|dk  rt        d      |dk  s	t        |k  rt        t        j                  |            t	        j
                  |      | _        t        | j                        }t        |      }t        j                  ||||||      | _        t        | j                  d| j                         | _        y )Nr   Sample rate should be over 0r   process_)r   	_CH_LIMIT_CH_EXEED_ERR_STRr.   r$   dtype_typer0   r"   r   CSoxr_csoxrgetattr_process)	selfr4   r5   r6   r<   qualityvrstyper    s	            r!   __init__zResampleStream.__init__U   s     a<8q=;<<!y<7.55lCDDXXe_
!$**-W%nnWheQPRSx

|-DEr1   xreturnc                    t        |      t        j                  k7  s|j                  | j                  k7  r(t        t        j                  | j                              |j                  dk(  r>| j                  |ddt        j                  f   |      }t        j                  |d      S |j                  dk(  r| j                  ||      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.   ndimrA   newaxissqueezer   )rB   rG   lastys       r!   resample_chunkzResampleStream.resample_chunkf   s    ( 7bjj AGGtzz$9299$**EFF66Q;a2::.5A::aa((VVq[==D))=>>r1   c                 6    | j                   j                         S )zx Clip counter. (for int I/O)

        Returns
        -------
        int
            Count of clipped samples.
        )r?   	num_clipsrB   s    r!   rX   zResampleStream.num_clips   s     {{$$&&r1   c                 6    | j                   j                         S )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?   delayrY   s    r!   r[   zResampleStream.delay   s     {{  ""r1   Nc                 8    | j                   j                          y)zv Reset resampler. Ready for fresh signal, same config.

        This can be used to save initialization time.
        N)r?   clearrY   s    r!   r]   zResampleStream.clear   s    
 	r1   slew_lenc                 B    | j                   j                  ||z  |       y)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)rB   r4   r5   r^   s       r!   r`   zResampleStream.set_io_ratio   s    " 	  8!3X>r1   )r%   r	   F)F)rH   N)r   )__name__
__module____qualname____doc__floatintrF   r$   rO   rV   rX   r[   r]   r`    r1   r!   r3   r3   =   s    2 49FF+0F@CF"?

 ?2:: ?>'3 '
#u 
#?E ?U ?c ?RV ?r1   r3   rG   r4   r5   rH   c                 *   |dk  s|dk  rt        d      t        |       t        j                  k7  r%t        j                  | t        j
                        } 	 | j                  d   | j                  k(  rt        t        d| j                         }nt        t        d| j                         }t        |      }| j                   dk(  r7 |||| ddt        j"                  f   |      }t        j$                  |d      S | j                   d	k(  rF| j&                  d   }|dk  s	t(        |k  rt        t*        j                  |             |||| |      S t        d
      # t        $ r) t        t        j                  | j                              w xY w)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   r8   )r<   csoxr_split_ch_csoxr_divide_proc_r   NrJ   rL   rM   )r   rN   r$   rO   asarrayr%   stridesitemsizer@   r   r<   AttributeErrorr   r-   r.   r"   rQ   rR   rS   shaper:   r;   )rG   r4   r5   rC   divide_procr    rU   r6   s           r!   resamplerq      sT   . !|x1}788Aw"**JJq

+899Q<1::%!(oaggY,GHK!(.@	,JKK 	!Avv{1Q

]+;Q?zz!!$$	
1wwqz!y<7.55lCDD7Ha339::  8--agg6778s   AE   2Fc                 n   	 t        t        d| j                         }| j                  dk(  r@ |||| ddt        j                  f   t        |            }t        j                  |d      S  |||| t        |            S # t        $ r) t	        t
        j                  | j                              w xY w)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   NrJ   )r@   r   r<   rn   r   r-   r.   rQ   r$   rR   r"   rS   )rG   r4   r5   rC   oneshotrU   s         r!   _resample_oneshotru      s    8(nQWWI$>? 	vv{GXqBJJ'79I'9RSzz!!$$7Ha)9')BCC  8--agg6778s   B 2B4)r	   )numpyr$   numpy.typingr    r   r   r   r   r	   r
   _versionr   __version__libsoxr_version__libsoxr_version__r:   rP   r;   r-   r   r   r"   r0   r3   re   rO   rq   ru   rg   r1   r!   <module>r}      s    "  ) ) , /h..0  	e H9T TA  eS*cD"iD"iD"iD"i +
6v? v?r1;	 1;E 1;U 1;RZZ 1;hD De Du DWYWaWa Dr1   