§
    ïóÉjŒ%  ã                   óÐ   — 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         €úr/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib/python3.11/site-packages/julius/resample.pyr   zResampleFrac.__init__   sœ   ø€ õ6 	‰Œ×ÒÑÔÐÝ˜&¥#Ñ&Ô&ð 	E­j¸ÅÑ.EÔ.Eð 	EÝÐCÑDÔDÐDÝŒh�v˜vÑ&Ô&ˆØ ‘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àˆÝ�”˜dœkÑ*Ô*ˆð
 	ˆdŒlÑˆõ& ”i ¤
¨T¬[Ñ 8¸2Ñ =Ñ>Ô>ˆŒõ
 Œl˜DœK˜<¨¬°t´{Ñ)BÑCÔC×IÒIÑKÔKˆÝ�t”{Ñ#Ô#ð 	#ð 	#ˆAØ��D”K‘ # d¤k¡/Ñ1°RÑ7ˆAØ—’˜$œ*˜ d¤jÑ1Ô1ˆAØ•”‰LˆAÝ”Y˜q ¤™|¨A™~Ñ.Ô.°Ñ1ˆFÝ˜!‘W”W˜vÑ%ˆFà�KŠK˜Ÿ
š
™œÑ%Ô%Ð%Ø�NŠN˜6Ñ"Ô"Ð"Ð"à×Ò˜X¥u¤{°7Ñ';Ô';×'@Ò'@ÀÄÈaÐQSÑ'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Ø”ˆØ”˜”ˆØ�IŠI�b˜&Ñ!Ô!ˆÝŒE�!�A�A�A�t�G”*˜tœ{¨D¬K¸$¼+Ñ,EÐFÈ[ÐYÑYÔYˆÝŒX�a˜œ¨T¬[Ð9Ñ9Ô9ˆØ�LŠL˜˜AÑÔ×&Ò&¥t¨E°#°2°#¬JÑ'7Ô'7¸2¸$Ñ'>Ñ?Ô?ˆå#œo¨d¬k¸FÑ.BÀTÄ[Ñ.PÑQÔQÐÝ!œJÐ':Ñ;Ô;×@Ò@ÑBÔBÐÝ %¤Ð,?Ñ @Ô @× EÒ EÑ GÔ GÐàÐ Ø9=Ð$XÐ$5Ð$5ÐCXÐ!Ð!Ø˜QÒÐ -Ð2CÒ"CÐ"CÝÐWÐDUÐ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ð^#.ð #.˜œð #.°h¸s´mð #.Ð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¨™M°EÐ
:Ñ
:Ô
:€CØ���A�ŒY€FÝŒ˜�v ‘| U¨S¡[°!°e±)Ñ<Ô<€AØ�Œ�L€AÝ�1‰gŒg˜Ñ×$Ò$ 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«   € ð ”7�L€UˆDÝ*¨5Ñ1Ô1×4Ò4°QÑ7Ô7€FØ
L�!Œ(�1—6’6˜"˜a Ñ&Ô&¨¸Ð
>Ñ
>Ô
>¸sÀAÀBÀB¸wÔ
GÔ
LÐ
ZÈeÐ
ZÐUYÐ
ZÐ
ZÐ
Z€CÝŒ�Q˜�H "Ð%Ñ%Ô%€AØˆ1Œ6Ð�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  € ð 	„wˆr„{�Q�˜!ÒÐÝŒE�!�VÑÔˆØˆc�3�3�Q�3ˆhŒK€EØˆS�!�$�Q�$ˆYŒ<€DØ”:�L€UˆDÝ*¨5Ñ1Ô1×4Ò4°QÑ7Ô7€FØ
ÐX•!”(˜4Ÿ9š9 R¨¨DÑ1Ô1°6À5ÐIÑIÔIÈ#ÈsÐPRÈsÈ(ÔSÔXð Ø	ðØðð ð ñ €Càˆ3Œ8Ð�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  ðð
ð 
ð €€€Ø Ð Ð Ð Ð Ð à €€€Ø $Ð $Ð $Ð $Ð $Ð $à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ð!ð !ð !ð !ð !�5”8”?ñ !ô !ð !ðF 5:ØDIðVð V�U”\ð V¨3ð V¸ð VØðVØ,1ðVà!)¨#¤ðVà=AðVð Vð Vð Vð"ð ð ðð ð ð ð")ð )ð )ð )ð )ð )r   