
    tKgY              	          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d Zd Z G d d      Zddedededej2                  fdZddej2                  dededej2                  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]c                 "   | t         t        t        t        t        fv r| S t        |       t        u rt        t              | j                         } | dv rt         S | dv rt        S | dv rt        S | dv rt        S | dv rt        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)qs    Q/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/soxr/__init__.py_quality_to_enumr      s    S"b"b!!Aw#~)**		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                  |             )N)npfloat32r   SOXR_FLOAT32_Ifloat64SOXR_FLOAT64_Iint32SOXR_INT32_Iint16SOXR_INT16_I	TypeError_DTYPE_ERR_STRformat)ntypes    r   _to_soxr_datatyper.   4   sx    

&&&	"**	&&&	"((	$$$	"((	$$$--e455r   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_typer.   r   r   CSoxr_csoxrgetattr_process)selfr1   r2   r3   r9   qualitystyper   s           r   __init__zResampleStream.__init__V   s     a<8q=;<<!y<7.55lCDDXXe_
!$**-W%nnWheQOx

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

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

        This can be used to save initialization time.
        N)r<   clearrU   s    r   rY   zResampleStream.clear   s    
 	r   )r"   r	   )F)rD   N)__name__
__module____qualname____doc__floatr   rB   r!   rJ   rR   rT   rW   rY    r   r   r0   r0   A   sf    , +/FF+0F@CF"?

 ?2:: ?B'3 '
#u 
#r   r0   rC   r1   r2   rD   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   r5   )r9   csoxr_split_ch_csoxr_divide_proc_r   NrF   rH   rI   )r   r   r!   rJ   asarrayr"   stridesitemsizer=   r   r9   AttributeErrorr*   r+   r,   r   rM   rN   rO   shaper7   r8   )rC   r1   r2   r@   divide_procr   rQ   r3   s           r   resampleri      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                    	 t        t        d| j                         }t        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   NrF   )r=   r   r9   rf   r*   r+   r,   r!   rL   rM   rN   r   rO   )rC   r1   r2   r@   oneshotrQ   s         r   _resample_oneshotrm      s    8(nQWWI$>? 	QAvv{GXqBJJ'79I'9RSzz!!$$7Ha)9')BCC  8--agg6778s   B 2C	)r	   )numpyr!   numpy.typingr    r   r   r   r   r	   r
   _versionr   __version__libsoxr_version__libsoxr_version__r7   rK   r8   r+   r   r   r.   r0   r^   rJ   ri   rm   r_   r   r   <module>ru      s     "  ) ) , /h..0  	e H9T TA ',
6b bJ1;	 1;E 1;U 1;RZZ 1;hD De Du DWYWaWa Dr   