o
    Si!                     @   s   d dl mZmZmZ d dlmZmZ d dlZd dl	Z	z
d dl
mZmZ W n ey5   d dl	mZmZ Y nw i Zdd Zde	jde	jd	e	jfd
dZeG dd dZdS )    )asdict	dataclassfield)ListOptionalN)irfftrfftc                 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)a5  
    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   M/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/augmentation/utils.pynext_fast_len   s&   
r   signalkernelreturnc           
      C   s|   | j dkr
|j dksJ d| d}|d}|| d }t|}t| |d}t||d}|| }t||d}	|	d| S )a  
    Computes the 1-d convolution of signal by kernel using FFTs.
    Both signal and kernel must be 1-dimensional.
    :param torch.Tensor signal: A signal to convolve.
    :param torch.Tensor kernel: A convolution kernel.
    :param str mode: One of: 'full', 'valid', 'same'.
    :return: torch.Tensor Convolution of signal with kernel. Returns the full convolution, i.e.,
        the output tensor will have size m + n - 1, where m is the length of the
        signal and n is the length of the kernel.
    r   z'signal and kernel must be 1-dimensional)r   N)ndimr   r   r   r   )
r   r   mr   padded_sizefast_ftt_sizef_signalf_kernelf_resultresultr   r   r   
convolve1d1   s   

r$   c                   @   s   e Zd ZU dZeed< edd dZeed< dZ	e
ed< d	Ze
ed
< dZe
ed< dZe
ed< dZe
ed< dZee ed< dZee ed< dd Zdd ZddedejfddZdS )FastRandomRIRGeneratori>  src                   C   s   ddgS )Ni2   r   r   r   r   r   <lambda>R   s    zFastRandomRIRGenerator.<lambda>)default_factorydirect_rangeg?max_T60g      ?alphag       ag       @b皙?tauN	room_seedsource_seedc                 C   sR   | j d urtj| j ntj | _| jd ur!tj| j| _d S tj | _d S N)r1   nprandomdefault_rngroom_rngr2   
source_rngselfr   r   r   __post_init__[   s   

z$FastRandomRIRGenerator.__post_init__c                 C   s   t | S r3   )r   r9   r   r   r   to_dictg   s   zFastRandomRIRGenerator.to_dictr   nsourcer   c              
      s  ddl m}m} |  ddlm} d}| j| }|||tt| }||tt| | j}t	tj
j}	t| jjd| jddd j t| jjddddd j}
t| jjd	d
|fd| jd dt|   }tt|  }ddtd|
    d  } fddt|D t| jdd}||  }|j| dd|tfddt|D d}dd| }t  t d t||	  }|   d||dd  d }t| jj| j| j |fd|| j! }t"|| t#d}t$%dd|gd}t$t&|d|gd}t&||}t't||  t#d| d  }||| }t|D ]}|| ||   || 7  < q]t&||( }t|D ]'}d||t)|| || j*d  d  dt+|| || j*d  d  |f< q{|| }t||gd|d d}||}|||tt| d}||( |dd}|dddf }|, S )z
        :param nsource: number of sources (RIR filters) to simulate. Default: 1.
        :return: simulated RIR filter for all sources, shape: (nsource, nsample)
        r   )check_for_torchaudioget_or_create_resampler)highpass_biquad@   g?r   )r   g333333?r/   g      (@r	   g     @u@r   g{GzĿc                    s*   g | ]}t d   |  d qS )      ?r   )torchlinspace.0i)T60direct_distimagevelocityr   r   
<listcomp>   s    z3FastRandomRIRGenerator.__call__.<locals>.<listcomp>rC   T)num_samplesreplacementc                    s   g | ]
} | |  qS r   r   rF   )
dist_rangedist_select_idxr   r   rM      s    r   r
   i  g      T@N)-lhotse.augmentation.torchaudior>   r?   torchaudio.functionalr@   r&   r   r4   sqrtfinfofloat16epsrD   
from_numpyr7   uniformr+   datar8   ceillongexppowrangerE   r,   summultinomialviewstacklog10r-   r.   r0   maximumonescatreshapezerosminimumfloatmaxr*   minnumpy)r:   r=   r>   r?   r@   ratio	sample_sr	resample1	resample2rW   R
direct_idx
rir_lengthreflect_coef	dist_prob
dist_ratiodistreflect_maxreflect_ratioreflect_pertubrir	delta_idxdelta_decayrH   direct_mask
rir_directall_rirrir_downsamplerir_hp
rir_filterr   )rI   rJ   rP   rQ   rK   rL   r   __call__j   s   

$
   zFastRandomRIRGenerator.__call__rB   )__name__
__module____qualname__r&   r   __annotations__r   r*   r   r+   rk   r,   r-   r.   r0   r1   r   r2   r;   r<   r4   ndarrayr   r   r   r   r   r%   O   s   
 r%   )dataclassesr   r   r   typingr   r   rn   r4   rD   	torch.fftr   r   ImportErrorr   r   Tensorr$   r%   r   r   r   r   <module>   s    