o
    }oia                     @   sL   d dl mZmZ d dlZd dlmZmZ d dlmZm	Z	 G dd deZ
dS )    )DictOptionalN)NeuralModule	typecheck
NeuralTypeSpectrogramTypec                       s   e Zd ZdZddee def fddZede	ee
f fd	d
Zede	ee
f fddZe dejdejdejfddZ  ZS )MixtureConsistencyProjectiona  Ensure estimated sources are consistent with the input mixture.
    Note that the input mixture is assume to be a single-channel signal.

    Args:
        weighting: Optional weighting mode for the consistency constraint.
            If `None`, use uniform weighting. If `power`, use the power of the
            estimated source as the weight.
        eps: Small positive value for regularization

    Reference:
        Wisdom et al, Differentiable consistency constraints for improved deep speech enhancement, 2018
    N:0yE>	weightingepsc                    s6   t    || _|| _| jdvrtd| j dd S )N)NpowerWeighting mode  not implemented)super__init__r   r   NotImplementedError)selfr   r   	__class__ ^/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/audio/modules/projections.pyr   %   s   

z%MixtureConsistencyProjection.__init__returnc                 C   s   t dt t dt dS )+Returns definitions of module output ports.BCDT)mixtureestimater   r   r   r   r   input_types-   s   

z(MixtureConsistencyProjection.input_typesc                 C   s   dt dt iS )r   outputr   r   r!   r   r   r   output_types5   s   z)MixtureConsistencyProjection.output_typesr   r    c                 C   s   | ddkrtd|j | d}tj|ddd}| jdu r&d| }n"| jdkr?| d}||jddd| j  }n	t	d	| j d
||||   }|S )a)  Enforce mixture consistency on the estimated sources.
        Args:
            mixture: Single-channel mixture, shape (B, 1, F, N)
            estimate: M estimated sources, shape (B, M, F, N)

        Returns:
            Source estimates consistent with the mixture, shape (B, M, F, N)
           z.Mixture must have a single channel, got shape T)dimkeepdimNr      r   r   )
size
ValueErrorshapetorchsumr   abspowr   r   )r   r   r    Mestimated_mixtureweightconsistent_estimater   r   r   forward<   s   




z$MixtureConsistencyProjection.forward)Nr
   )__name__
__module____qualname____doc__r   strfloatr   propertyr   r   r"   r$   r   r-   Tensorr5   __classcell__r   r   r   r   r	      s    &r	   )typingr   r   r-   nemo.core.classesr   r   nemo.core.neural_typesr   r   r	   r   r   r   r   <module>   s
   