o
    8wiJ                     @   s2   d dl Z d dlmZmZ i Zdd ZdddZdS )	    N)rfftirfftc                 C   s   zt |  W S  ty   Y nw t| tr| dksJ | }	 |}dD ]}|| dkr3|| }|| dks)q!|dkr>|t | < |S |d7 }q)a7  
    Returns the next largest number ``n >= size`` whose prime factors are all
    2, 3, or 5. These sizes are efficient for fast fourier transforms.
    Equivalent to :func:`scipy.fftpack.next_fast_len`.

    Note: This function was originally copied from the https://github.com/pyro-ppl/pyro
    repository, where the license was Apache 2.0. Any modifications to the original code can be
    found at https://github.com/asteroid-team/torch-audiomentations/commits

    :param int size: A positive number.
    :returns: A possibly larger number.
    :rtype int:
    r   T)            )_NEXT_FAST_LENKeyError
isinstanceint)size	next_size	remainingn r   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torch_audiomentations/utils/convolution.pynext_fast_len   s&   
r   fullc                 C   s   |  d}| d}|dkr|| d }n"|dkr&t||t|| d }n|dkr0t||}ntd||| d }t|}t| |d}t||d}	||	 }
t|
|d}|| d }|d	||| f S )
a  
    Computes the 1-d convolution of signal by kernel using FFTs.
    The two arguments should have the same rightmost dim, but may otherwise be
    arbitrarily broadcastable.

    Note: This function was originally copied from the https://github.com/pyro-ppl/pyro
    repository, where the license was Apache 2.0. Any modifications to the original code can be
    found at https://github.com/asteroid-team/torch-audiomentations/commits

    :param torch.Tensor signal: A signal to convolve.
    :param torch.Tensor kernel: A convolution kernel.
    :param str mode: One of: 'full', 'valid', 'same'.
    :return: A tensor with broadcasted shape. Letting ``m = signal.size(-1)``
        and ``n = kernel.size(-1)``, the rightmost size of the result will be:
        ``m + n - 1`` if mode is 'full';
        ``max(m, n) - min(m, n) + 1`` if mode is 'valid'; or
        ``max(m, n)`` if mode is 'same'.
    :rtype torch.Tensor:
    r   r   validsamezUnknown mode: {})r   r   .)r   maxmin
ValueErrorformatr   r   r   )signalkernelmodemr   truncatepadded_sizefast_ftt_sizef_signalf_kernelf_resultresult	start_idxr   r   r   convolve(   s"   

r'   )r   )torchtorch_audiomentations.utils.fftr   r   r   r   r'   r   r   r   r   <module>   s
     