
    qi                     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    ?/opt/pipecat/venv/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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`.
    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styper    s           r!   __init__zResampleStream.__init__R   s     a<8q=;<<!y<7.55lCDDXXe_
!$**-W%nnWheQO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   rF   lastys       r!   resample_chunkzResampleStream.resample_chunkc   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!   rW   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?   delayrX   s    r!   rZ   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?   clearrX   s    r!   r\   zResampleStream.clear   s    
 	r1   )r%   r	   )F)rG   N)__name__
__module____qualname____doc__floatintrE   r$   rN   rU   rW   rZ   r\    r1   r!   r3   r3   =   se    , +/FF+0F@CF"?

 ?2:: ?>'3 '
#u 
#r1   r3   rF   r4   r5   rG   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   NrI   rK   rL   )r   rM   r$   rN   asarrayr%   stridesitemsizer@   r   r<   AttributeErrorr   r-   r.   r"   rP   rQ   rR   shaper:   r;   )rF   r4   r5   rC   divide_procr    rT   r6   s           r!   resamplerm      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   NrI   )r@   r   r<   rj   r   r-   r.   rP   r$   rQ   r"   rR   )rF   r4   r5   rC   oneshotrT   s         r!   _resample_oneshotrq      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:   rO   r;   r-   r   r   r"   r0   r3   ra   rN   rm   rq   rc   r1   r!   <module>ry      s    "  ) ) , /h..0  	e H9T TA  eS*cD"iD"iD"iD"i +
6` `F1;	 1;E 1;U 1;RZZ 1;hD De Du DWYWaWa Dr1   