o
    si6                     @   s   d dl mZ ddlmZ d dlmZ d dlmZmZ ddl	m
Z
mZ ddlmZ d d	lZG d
d deZ					dddZd	S )    )nn   )BaseEncoderMaskerDecoder)make_enc_dec)magmagreim   )normsactivations)
pad_x_to_yNc                       sb   e Zd ZdZ													
d fdd	Zdd Zdd Zdd Zdd Zdd Z	  Z
S )DeMaska  
    Simple MLP model for surgical mask speech enhancement A transformed-domain masking approach is used.

    Args:
        input_type (str, optional): whether the magnitude spectrogram "mag" or both real imaginary parts "reim" are
                    passed as features to the masker network.
                    Concatenation of "mag" and "reim" also can be used by using "cat".
        output_type (str, optional): whether the masker ouputs a mask
                    for magnitude spectrogram "mag" or both real imaginary parts "reim".

        hidden_dims (list, optional): list of MLP hidden layer sizes.
        dropout (float, optional): dropout probability.
        activation (str, optional): type of activation used in hidden MLP layers.
        mask_act (str, optional): Which non-linear function to generate mask.
        norm_type (str, optional): To choose from ``'BN'``, ``'gLN'``,
            ``'cLN'``.

        fb_name (str): type of analysis and synthesis filterbanks used,
                            choose between ["stft", "free", "analytic_free"].
        n_filters (int): number of filters in the analysis and synthesis filterbanks.
        stride (int): filterbank filters stride.
        kernel_size (int): length of filters in the filterbank.
        encoder_activation (str)
        sample_rate (float): Sampling rate of the model.
        **fb_kwargs (dict): Additional kwards to pass to the filterbank
            creation.
    r   i           relugLNstft      >  c              	      s   t |f||	|
|d|\}}| ||j}| ||j}t|||||||d}t ||| || _|| _|| _	|| _
|| _|| _|| _d S )N)kernel_size	n_filtersstridesample_rate)	norm_type
activationhidden_dimsdropoutmask_act)r   _get_n_feats_inputn_feats_out_get_n_feats_outputbuild_demask_maskersuper__init__
input_typeoutput_typer   r   r   r   r   )selfr$   r%   r   r   r   r   r   fb_namer   r   r   r   	fb_kwargsencoderdecodern_masker_inn_masker_outmasker	__class__ J/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/models/demask.pyr#   '   s:   
		
zDeMask.__init__c                 C   s8   |dkr|S |dvrt d|d }|dkr||7 }|S )Nreim>   catr   z,Input type should be either mag, reim or catr   r3   NotImplementedError)r&   r$   encoder_n_outn_feats_inputr0   r0   r1   r   U   s   zDeMask._get_n_feats_inputc                 C   s$   |dkr|d S |dkr|S t d)Nr   r   r2   z(Output type should be either mag or reimr4   )r&   r%   r6   r0   r0   r1   r    a   s
   zDeMask._get_n_feats_outputc                 C   sP   |}| j dkrt|}n	| j dkrt|}| |}| jdkr&|ddd}|S )a  Estimates masks based on time-frequency representations.

        Args:
            tf_rep (torch.Tensor): Time-frequency representation in
                (batch, freq, seq).

        Returns:
            torch.Tensor: Estimated masks in (batch, freq, seq).
        r   r3   r   r   )r$   r   r   r-   r%   repeat)r&   tf_repmasker_input	est_masksr0   r0   r1   forward_maskerh   s   





zDeMask.forward_maskerc                 C   s   | j dkr
|d}|| S )aQ  Applies masks to time-frequency representations.

        Args:
            tf_rep (torch.Tensor): Time-frequency representations in
                (batch, freq, seq).
            est_masks (torch.Tensor): Estimated masks in (batch, freq, seq).

        Returns:
            torch.Tensor: Masked time-frequency representations.
        r2   r   )r%   	unsqueeze)r&   r9   r;   r0   r0   r1   apply_masks|   s   

zDeMask.apply_masksc                 C   s8   | j | j| j| j| j| j| jd}|| jj	
  |S )z-Arguments needed to re-instantiate the model.)r$   r%   r   r   r   r   r   )r$   r%   r   r   r   r   r   updater)   
filterbank
get_config)r&   
model_argsr0   r0   r1   get_model_args   s   	zDeMask.get_model_args)r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r#   r   r    r<   r>   rC   __classcell__r0   r0   r.   r1   r   
   s(    .r   r   r   r   r   c              	   C   s   t |}|| g}t| }	| }
|D ]}|t|
|d|||	t|g |}
q|t|
|dt| g tj| S )Nr   )r	   getr
   extendr   Conv1dDropout
Sequential)n_inn_outr   r   r   r   r   make_layer_normnetlayer_activationin_chan
hidden_dimr0   r0   r1   r!      s   
	
 
r!   )r   r   r   r   r   )torchr   base_modelsr   asteroid_filterbanksr   asteroid_filterbanks.transformsr   r   masknnr	   r
   utils.torch_utilsr   warningsr   r!   r0   r0   r0   r1   <module>   s     