o
    ziB                     @   sH   d dl Z d dl mZ ddlmZ G dd dejZG dd dejZdS )	    N)nn   )AudioSignalc                       sB   e Zd ZdZddedef fddZded	ef fd
dZ  Z	S )L1LossaM  L1 Loss between AudioSignals. Defaults
    to comparing ``audio_data``, but any
    attribute of an AudioSignal can be used.

    Parameters
    ----------
    attribute : str, optional
        Attribute of signal to compare, defaults to ``audio_data``.
    weight : float, optional
        Weight of this loss, defaults to 1.0.
    
audio_data      ?	attributeweightc                    s"   || _ || _t jdi | d S )N )r   r	   super__init__)selfr   r	   kwargs	__class__r
   O/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/metrics/distance.pyr      s   zL1Loss.__init__xyc                    s0   t |trt|| j}t|| j}t ||S )a  
        Parameters
        ----------
        x : AudioSignal
            Estimate AudioSignal
        y : AudioSignal
            Reference AudioSignal

        Returns
        -------
        torch.Tensor
            L1 loss between AudioSignal attributes.
        )
isinstancer   getattrr   r   forward)r   r   r   r   r
   r   r      s   
zL1Loss.forward)r   r   )
__name__
__module____qualname____doc__strfloatr   r   r   __classcell__r
   r
   r   r   r      s    r   c                       sT   e Zd ZdZ					ddededed	ed
ef
 fddZdedefddZ	  Z
S )	SISDRLossa%  
    Computes the Scale-Invariant Source-to-Distortion Ratio between a batch
    of estimated and reference audio signals or aligned features.

    Parameters
    ----------
    scaling : int, optional
        Whether to use scale-invariant (True) or
        signal-to-noise ratio (False), by default True
    reduction : str, optional
        How to reduce across the batch (either 'mean',
        'sum', or none).], by default ' mean'
    zero_mean : int, optional
        Zero mean the references and estimates before
        computing the loss, by default True
    clip_min : int, optional
        The minimum possible loss value. Helps network
        to not focus on making already good examples better, by default None
    weight : float, optional
        Weight of this loss, defaults to 1.0.
    TmeanNr   scaling	reduction	zero_meanclip_minr	   c                    s,   || _ || _|| _|| _|| _t   d S )N)r    r!   r"   r#   r	   r   r   )r   r    r!   r"   r#   r	   r   r
   r   r   D   s   zSISDRLoss.__init__r   r   c                 C   sf  d}t |tr|j}|j}n|}|}|jd }||ddddd}||ddddd}| jrA|jddd}|jddd}nd}d}|| }	|| }
|	d jdd	| }|
|	 jdd	| }| j	rk|| 
dnd}||	 }|
| }|d jdd	}|d jdd	}d
t|| |  }| jd urtj|| jd}| jdkr| }|S | jdkr| }|S )Ng:0yE>r      r   T)dimkeepdim)r&   i)minr   sum)r   r   r   shapereshapepermuter"   r   r*   r    	unsqueezetorchlog10r#   clampr!   )r   r   r   eps
references	estimatesnbmean_referencemean_estimate_references
_estimatesreferences_projectionreferences_on_estimatesscalee_truee_ressignalnoisesdrr
   r
   r   r   S   sF   




zSISDRLoss.forward)Tr   TNr   )r   r   r   r   intr   r   r   r   r   r   r
   r
   r   r   r   -   s&    r   )r/   r    r   r   Moduler   r
   r
   r
   r   <module>   s
    &