
    Vh%                         d Z ddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ  G d d	ej        j                  Z	 	 ddej        dededededee         defdZd ZddZddZdS )a  
Differentiable, Pytorch based resampling.
Implementation of Julius O. Smith algorithm for resampling.
See https://ccrma.stanford.edu/~jos/resample/ for details.
This implementation is specially optimized for when new_sr / old_sr is a fraction
with a small numerator and denominator when removing the gcd (e.g. new_sr = 700, old_sr = 500).

Very similar to [bmcfee/resampy](https://github.com/bmcfee/resampy) except this implementation
is optimized for the case mentioned before, while resampy is slower but more general.

    N)Optional)
functional   )sincsimple_reprc            	       n     e Zd ZdZddedededef fdZd	 Zddej	        de
e         defdZd Z xZS )ResampleFracz?
    Resampling from the sample rate `old_sr` to `new_sr`.
       =
ףp=?old_srnew_srzerosrolloffc                 P   t                                                       t          |t                    rt          |t                    st	          d          t          j        ||          }||z  | _        ||z  | _        || _	        || _
        |                                  dS )a  
        Args:
            old_sr (int): sample rate of the input signal x.
            new_sr (int): sample rate of the output.
            zeros (int): number of zero crossing to keep in the sinc filter.
            rolloff (float): use a lowpass filter that is `rolloff * new_sr / 2`,
                to ensure sufficient margin due to the imperfection of the FIR filter used.
                Lowering this value will reduce anti-aliasing, but will reduce some of the
                highest frequencies.

        Shape:

            - Input: `[*, T]`
            - Output: `[*, T']` with `T' = int(new_sr * T / old_sr)


        .. caution::
            After dividing `old_sr` and `new_sr` by their GCD, both should be small
            for this implementation to be fast.

        >>> import torch
        >>> resample = ResampleFrac(4, 5)
        >>> x = torch.randn(1000)
        >>> print(len(resample(x)))
        1250
        z$old_sr and new_sr should be integersN)super__init__
isinstanceint
ValueErrormathgcdr   r   r   r   _init_kernels)selfr   r   r   r   r   	__class__s         U/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/julius/resample.pyr   zResampleFrac.__init__   s    6 	&#&& 	Ej.E.E 	ECDDDhvv&&mm
    c                 `   | j         | j        k    rd S g }t          | j        | j                   }|| j        z  }t	          j        | j        | j         z  |z            | _        t          j	        | j         | j        | j         z             
                                }t          | j                  D ]}| | j        z  || j         z  z   |z  }|                    | j         | j                  }|t          j        z  }t          j        || j        z  dz            dz  }t          |          |z  }|                    |                                           |                    |           |                     dt          j        |                              | j        dd                     d S )N   kernelr   )r   r   minr   r   ceilr   _widthtorcharangefloatrangeclamp_picosr   div_sumappendregister_bufferstackview)r   kernelssridxitwindowr    s           r   r   zResampleFrac._init_kernelsC   s{   ;$+%%Fdk**
 	dl& i
T[ 82 =>>
 lDK<t{)BCCIIKKt{## 	# 	#ADK#dk/1R7A$*dj11ALAYq|A~..1F!WWv%FKK

%%%NN6""""Xu{7';';'@'@aQS'T'TUUUUUr   NFxoutput_lengthfullc                 :   | j         | j        k    r|S |j        }|j        d         }|                    d|          }t	          j        |dddf         | j        | j        | j         z   fd          }t	          j        || j        | j                   }|	                    dd                              t          |dd                   dgz             }t          j        | j        |z  | j         z            }t          j        |                                          }	t          j        |                                          }
||r|	n|
}nC|dk     s||	k    rt!          d	|	           t          j        |          }|rt!          d
          |dd|f         S )a  
        Resample x.
        Args:
            x (Tensor): signal to resample, time should be the last dimension
            output_length (None or int): This can be set to the desired output length
                (last dimension). Allowed values are between 0 and
                ceil(length * new_sr / old_sr). When None (default) is specified, the
                floored output length will be used. In order to select the largest possible
                size, use the `full` argument.
            full (bool): return the longest possible output from the input. This can be useful
                if you chain resampling operations, and want to give the `output_length` only
                for the last one, while passing `full=True` to all the other ones.
        r!   N	replicate)mode)strider   r   r   z$output_length must be between 0 and z0You cannot pass both full=True and output_length.)r   r   shapereshapeFpadr$   conv1dr    	transposelistr%   	as_tensorr#   longfloorr   tensor)r   r8   r9   r:   r?   lengthysyfloat_output_lengthmax_output_lengthdefault_output_lengthapplied_output_lengths               r   forwardzResampleFrac.forwardr   s    ;$+%%HIIb&!!E!AAAtG*t{DK$+,EF[YYYXaT[999LLA&&tE#2#J'7'72$'>??#odkF.BT[.PQQ!J':;;@@BB %,? @ @ E E G G 9=$X$5$5CX!!Q-2C"C"CWDUWWXXX$)L$?$?! U !STTT,,,,--r   c                      t          |           S )Nr   )r   s    r   __repr__zResampleFrac.__repr__   s    4   r   )r   r   )NF)__name__
__module____qualname____doc__r   r'   r   r   r%   Tensorr   boolrQ   rS   __classcell__)r   s   @r   r
   r
      s         $ $s $C $ $5 $ $ $ $ $ $L-V -V -V^#. #. #.hsm #.RV #. #. #. #.J! ! ! ! ! ! !r   r
   r   r   Fr8   r   r   r   r   r9   r:   c                 b     t          ||||                              |           | ||          S )aR  
    Functional version of `ResampleFrac`, refer to its documentation for more information.

    ..warning::
        If you call repeatidly this functions with the same sample rates, then the
        resampling kernel will be recomputed everytime. For best performance, you should use
        and cache an instance of `ResampleFrac`.
    )r
   to)r8   r   r   r   r   r9   r:   s          r   resample_fracr]      s5     ><w77::1==aPTUUUr   c                     t          j        d| z  dz   d          }|dd d         }t          j        |  dz   | dz
  d| z            }|t          j        z  }t          |          |z                      ddd          }|S )N   r   F)periodicr         ?r!   )r%   hann_windowlinspacer   r*   r   r1   )r   winwinoddr6   r    s        r   _kernel_upsample2_downsample2rf      s     
AIME
:
:
:CAYFv|US[!e)<<ALA1gg$$Q2..FMr   c                 .   | j         ^ }}t          |                              |           } t          j        |                     dd|          ||          dddf         j        g ||R  }t          j        | |gd          } |j        g |dR  S )a  
    Upsample x by a factor of two. The output will be exactly twice as long as the input.
    Args:
        x (Tensor): signal to upsample, time should be the last dimension
        zeros (int): number of zero crossing to keep in the sinc filter.

    This function is kept only for reference, you should use the more generic `resample_frac`
    one. This function does not perform anti-aliasing filtering.
    r!   r   padding.N)dim)r?   rf   r\   rA   rC   r1   r%   r0   )r8   r   othertimer    outrL   s          r   
_upsample2rn      s     7LUD*51144Q77F
L!(166"a&&
>
>
>sABBw
G
L
Ze
ZUY
Z
Z
ZCQH"%%%A165"r   c           	         | j         d         dz  dk    rt          j        | d          } | ddddf         }| ddddf         }|j         ^ }}t          |                              |           }| t          j        |                    dd|          ||          dddf         j        g ||R  z   } |j        g |dR                      d	          S )
a  
    Downsample x by a factor of two. The output length is half of the input, ceiled.
    Args:
        x (Tensor): signal to downsample, time should be the last dimension
        zeros (int): number of zero crossing to keep in the sinc filter.

    This function is kept only for reference, you should use the more generic `resample_frac`
    one. This function does not perform anti-aliasing filtering.
    r!   r   r   )r   r   .Nr   rh   ra   )r?   rA   rB   rf   r\   rC   r1   mul)r8   r   xevenxoddrk   rl   r    rm   s           r   _downsample2rs      s    	wr{Q!E!Vc33Q3hKES!$Q$Y<D:LUD*51144Q77F
X!(499RD1165III#sPRs(SX 	   C38UB##C(((r   )r   r   NF)r   )rW   r   typingr   r%   torch.nnr   rA   corer   utilsr   nnModuler
   rX   r   r'   rY   r]   rf   rn   rs    r   r   <module>r{      sK  
 
         $ $ $ $ $ $            ! ! ! ! !58? ! ! !F 5:DIV VU\ V3 V VV,1V!)#V=AV V V V"     ") ) ) ) ) )r   