o
    fôjB  ã                   @   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
   úl/var/www/html/academy/tools/voices/clone/sv-venv/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r�tj|| jd�}| jdkr¨| ¡ }|S | jdkr±| ¡ }|S )Ng:Œ0âŽyE>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Ú
referencesZ	estimatesÚnbZmean_referenceZmean_estimateZ_referencesZ
_estimatesZreferences_projectionZreferences_on_estimatesÚscaleZe_trueZe_resÚsignalÚnoiseZ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
    &