o
    i                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ eddG dd dejZeddG dd dejZd
S )zSpecAugment module.    )Optional)Sequence)Union)MaskAlongAxis)MaskAlongAxisVariableMaxWidth)MaskAlongAxisLFR)TimeWarp)tablesNspecaug_classesSpecAugc                       s   e Zd ZdZ										dded	ed
ededeeee f dedede	eeee f  de	ee
ee
 f  def fddZdddZ  ZS )r   a:  Implementation of SpecAug.

    Reference:
        Daniel S. Park et al.
        "SpecAugment: A Simple Data
         Augmentation Method for Automatic Speech Recognition"

    .. warning::
        When using cuda mode, time_warp doesn't have reproducibility
        due to `torch.nn.functional.interpolate`.

    T   bicubicr         Napply_time_warptime_warp_windowtime_warp_modeapply_freq_maskfreq_mask_width_rangenum_freq_maskapply_time_masktime_mask_width_rangetime_mask_width_ratio_rangenum_time_maskc                    s   |s
|s
|s
t d|r|d ur|	d urt dt   || _|| _|| _|r0t||d| _nd | _|r>td||d| _	nd | _	|rc|d urQtd||
d| _
d S |	d ur_td|	|
d| _
d S t dd | _
d S )	NBEither one of time_warp, time_mask, or freq_mask should be appliedREither one of "time_mask_width_range" or "time_mask_width_ratio_range" can be usedwindowmodefreq)dimmask_width_rangenum_masktimer!   mask_width_ratio_ranger#   VEither one of "time_mask_width_range" or "time_mask_width_ratio_range" should be used.)
ValueErrorsuper__init__r   r   r   r   	time_warpr   	freq_mask	time_maskr   )selfr   r   r   r   r   r   r   r   r   r   	__class__ Q/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/specaug/specaug.pyr*      sP   


zSpecAug.__init__c                 C   V   | j d ur|  ||\}}| jd ur| ||\}}| jd ur'| ||\}}||fS Nr+   r,   r-   r.   x	x_lengthsr1   r1   r2   forward_      


zSpecAug.forward)
Tr   r   Tr   r   TNNr   r4   __name__
__module____qualname____doc__boolintstrr   r   r   floatr*   r9   __classcell__r1   r1   r/   r2   r      sD    	
@
SpecAugLFRc                       s   e Zd ZdZ											dd	ed
edededeeee f dededede	eeee f  de	ee
ee
 f  def fddZdddZ  ZS )rE   u=   Implementation of SpecAug.
    lfr_rate：low frame rate
    Tr   r   r   r   r   Nr   r   r   r   r   r   lfr_rater   r   r   r   c                    s   |s
|s
|s
t d|r|	d ur|
d urt dt   || _|| _|| _|r0t||d| _nd | _|rAtd|||d d| _	nd | _	|ri|	d urWtd|	||d d| _
d S |
d uretd|
|d| _
d S t d	d | _
d S )
Nr   r   r   r       )r!   r"   r#   rF   r$   r%   r'   )r(   r)   r*   r   r   r   r   r+   r   r,   r-   r   )r.   r   r   r   r   r   r   rF   r   r   r   r   r/   r1   r2   r*   o   sT   


zSpecAugLFR.__init__c                 C   r3   r4   r5   r6   r1   r1   r2   r9      r:   zSpecAugLFR.forward)Tr   r   Tr   r   r   TNNr   r4   r;   r1   r1   r/   r2   rE   i   sJ    	
D)r?   typingr   r   r   %funasr.models.specaug.mask_along_axisr   r   r   funasr.models.specaug.time_warpr   funasr.registerr	   torch.nnnnregisterModuler   rE   r1   r1   r1   r2   <module>   s    

X