o
    piY                     @   sB  d Z ddlZddlZddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ eeZG dd dejjZG d	d
 d
ejjZ	d#dededefddZG dd dejjZG dd dejjZG dd dejjZG dd dejjZe	G dd dejjZG dd dejjZG dd  d ejjZG d!d" d"ejjZdS )$a&  Low-level feature pipeline components

This library gathers functions that compute popular speech  features over
batches of data. All the classes are of type nn.Module. This gives the
possibility to have end-to-end  differentiability and to backpropagate the
gradient through them. Our functions are a modified version the ones
in torch audio toolkit (https://github.com/pytorch/audio).

Example
-------
>>> import torch
>>> from speechbrain.dataio.dataio import read_audio
>>> signal =read_audio('tests/samples/single-mic/example1.wav')
>>> signal = signal.unsqueeze(0)
>>> compute_STFT = STFT(
...     sample_rate=16000, win_length=25, hop_length=10, n_fft=400
... )
>>> features = compute_STFT(signal)
>>> features = spectral_magnitude(features)
>>> compute_fbanks = Filterbank(n_mels=40)
>>> features = compute_fbanks(features)
>>> compute_mfccs = DCT(input_size=40, n_out=20)
>>> features = compute_mfccs(features)
>>> compute_deltas = Deltas(input_size=20)
>>> delta1 = compute_deltas(features)
>>> delta2 = compute_deltas(delta1)
>>> features = torch.cat([features, delta1, delta2], dim=2)
>>> compute_cw = ContextWindow(left_frames=5, right_frames=5)
>>> features  = compute_cw(features)
>>> norm = InputNormalization()
>>> features = norm(features, torch.tensor([1]).float())

Authors
 * Mirco Ravanelli 2020
    N)length_to_mask)mark_as_loadermark_as_savermark_as_transferregister_checkpoint_hooks)FilterProperties)
get_loggerc                       sJ   e Zd ZdZdddejddddf fdd		Zd
d ZdefddZ	  Z
S )STFTu  computes the Short-Term Fourier Transform (STFT).

    This class computes the Short-Term Fourier Transform of an audio signal.
    It supports multi-channel audio inputs (batch, time, channels).

    Arguments
    ---------
    sample_rate : int
        Sample rate of the input audio signal (e.g 16000).
    win_length : float
        Length (in ms) of the sliding window used to compute the STFT.
    hop_length : float
        Length (in ms) of the hope of the sliding window used to compute
        the STFT.
    n_fft : int
        Number of fft point of the STFT. It defines the frequency resolution
        (n_fft should be <= than win_len).
    window_fn : function
        A function that takes an integer (number of samples) and outputs a
        tensor to be multiplied with each window before fft.
    normalized_stft : bool
        If True, the function returns the  normalized STFT results,
        i.e., multiplied by win_length^-0.5 (default is False).
    center : bool
        If True (default), the input will be padded on both sides so that the
        t-th frame is centered at time t×hop_length. Otherwise, the t-th frame
        begins at time t×hop_length.
    pad_mode : str
        It can be 'constant','reflect','replicate', 'circular', 'reflect'
        (default). 'constant' pads the input tensor boundaries with a
        constant value. 'reflect' pads the input tensor using the reflection
        of the input boundary. 'replicate' pads the input tensor using
        replication of the input boundary. 'circular' pads using  circular
        replication.
    onesided : True
        If True (default) only returns nfft/2 values. Note that the other
        samples are redundant due to the Fourier transform conjugate symmetry.

    Example
    -------
    >>> import torch
    >>> compute_STFT = STFT(
    ...     sample_rate=16000, win_length=25, hop_length=10, n_fft=400
    ... )
    >>> inputs = torch.randn([10, 16000])
    >>> features = compute_STFT(inputs)
    >>> features.shape
    torch.Size([10, 101, 201, 2])
       
     FTconstantc
           
         s~   t    || _|| _|| _|| _|| _|| _|| _|	| _	t
t| jd | j | _t
t| jd | j | _|| j| _d S Ng     @@)super__init__sample_rate
win_length
hop_lengthn_fftnormalized_stftcenterpad_modeonesidedintroundwindow)
selfr   r   r   r   	window_fnr   r   r   r   	__class__ ]/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/speechbrain/processing/features.pyr   i   s    
zSTFT.__init__c                 C   s   |j }t|dkr|dd}||d |d  |d }tj|| j| j| j| j	
|j| j| j| j| jdd
}t|}t|dkrd||d |d |j d |j d |j d }|ddddd}|S |dd}|S )zReturns the STFT generated from the input waveforms.

        Arguments
        ---------
        x : torch.Tensor
            A batch of audio signals to transform.

        Returns
        -------
        stft : torch.Tensor
                 r   T)return_complex   )shapelen	transposereshapetorchstftr   r   r   r   todevicer   r   r   r   view_as_realpermute)r   xor_shaper,   r    r    r!   forward   s:   
zSTFT.forwardreturnc                 C   s   | j stdt| j| jdS )Nz]ValueProperties cannot model a non-centered STFT, as it assumes either centering or causality)window_sizestride)r   
ValueErrorr   r   r   r   r    r    r!   get_filter_properties   s   zSTFT.get_filter_properties)__name__
__module____qualname____doc__r+   hamming_windowr   r3   r   r9   __classcell__r    r    r   r!   r	   6   s    5 1r	   c                       s>   e Zd ZdZdddejddddf fdd		Zdd
dZ  ZS )ISTFTaF  Computes the Inverse Short-Term Fourier Transform (ISTFT)

    This class computes the Inverse Short-Term Fourier Transform of
    an audio signal. It supports multi-channel audio inputs
    (batch, time_step, n_fft, 2, n_channels [optional]).

    Arguments
    ---------
    sample_rate : int
        Sample rate of the input audio signal (e.g. 16000).
    n_fft : int
        Number of points in FFT.
    win_length : float
        Length (in ms) of the sliding window used when computing the STFT.
    hop_length : float
        Length (in ms) of the hope of the sliding window used when computing
        the STFT.
    window_fn : function
        A function that takes an integer (number of samples) and outputs a
        tensor to be used as a window for ifft.
    normalized_stft : bool
        If True, the function assumes that it's working with the normalized
        STFT results. (default is False)
    center : bool
        If True (default), the function assumes that the STFT result was padded
        on both sides.
    onesided : True
        If True (default), the function assumes that there are n_fft/2 values
        for each time frame of the STFT.
    epsilon : float
        A small value to avoid division by 0 when normalizing by the sum of the
        squared window. Playing with it can fix some abnormalities at the
        beginning and at the end of the reconstructed signal. The default value
        of epsilon is 1e-12.

    Example
    -------
    >>> import torch
    >>> compute_STFT = STFT(
    ...     sample_rate=16000, win_length=25, hop_length=10, n_fft=400
    ... )
    >>> compute_ISTFT = ISTFT(
    ...     sample_rate=16000, win_length=25, hop_length=10
    ... )
    >>> inputs = torch.randn([10, 16000])
    >>> outputs = compute_ISTFT(compute_STFT(inputs))
    >>> outputs.shape
    torch.Size([10, 16000])
    Nr
   r   FTg-q=c
           
         s~   t    || _|| _|| _|| _|| _|| _|| _|	| _	t
t| jd | j | _t
t| jd | j | _|| j| _d S r   )r   r   r   r   r   r   r   r   r   epsilonr   r   r   )
r   r   r   r   r   r   r   r   r   rA   r   r    r!   r      s    
zISTFT.__init__c              
   C   s  |j }| jdu r| jr|j d d d }n| jdu r#| js#|j d }n| j}t|dkrG|ddddd}|d|j d |j d |j d }nt|dkrU|dddd}t|d	 |d
 }tj||| j	| j
| j|j| j| j|d}t|dkr||d |d d}|dd}|S )a  Returns the ISTFT generated from the input signal.

        Arguments
        ---------
        x : torch.Tensor
            A batch of audio signals in the frequency domain to transform.
        sig_length : int
            The length of the output signal in number of samples. If not
            specified will be equal to: (time_step - 1) * hop_length + n_fft

        Returns
        -------
        istft : torch.Tensor
        Nr$   r#      r   r&   r"   ).r   ).r#   )inputr   r   r   r   r   r   length)r'   r   r   r(   r0   r*   r+   complexistftr   r   r   r-   r.   r   r)   )r   r1   
sig_lengthr2   r   rG   r    r    r!   r3     s4   $zISTFT.forwardN)	r:   r;   r<   r=   r+   r>   r   r3   r?   r    r    r   r!   r@      s    5!r@   r#   F+=powerlogepsc                 C   s@   |  dd}|dk r|| }| |}|rt|| S |S )ay  Returns the magnitude of a complex spectrogram.

    Arguments
    ---------
    stft : torch.Tensor
        A tensor, output from the stft function.
    power : int
        What power to use in computing the magnitude.
        Use power=1 for the power spectrogram.
        Use power=0.5 for the magnitude spectrogram.
    log : bool
        Whether to apply log to the spectral features.
    eps : float
        A small value to prevent square root of zero.

    Returns
    -------
    spectr : torch.Tensor

    Example
    -------
    >>> a = torch.Tensor([[3, 4]])
    >>> spectral_magnitude(a, power=0.5)
    tensor([5.])
    r$   rC   r#   )powsumr+   rL   )r,   rK   rL   rM   spectrr    r    r!   spectral_magnitudeS  s   
rQ   c                       s   e Zd ZdZ										
					d  fdd	Zdd Zedd Zedd Zdd Z	dd Z
ed	fddZdd Zdd Z  ZS )!
Filterbanka  computes filter bank (FBANK) features given spectral magnitudes.

    Arguments
    ---------
    n_mels : float
        Number of Mel filters used to average the spectrogram.
    log_mel : bool
        If True, it computes the log of the FBANKs.
    filter_shape : str
        Shape of the filters ('triangular', 'rectangular', 'gaussian').
    f_min : int
        Lowest frequency for the Mel filters.
    f_max : int
        Highest frequency for the Mel filters.
    n_fft : int
        Number of fft points of the STFT. It defines the frequency resolution
        (n_fft should be<= than win_len).
    sample_rate : int
        Sample rate of the input audio signal (e.g, 16000)
    power_spectrogram : float
        Exponent used for spectrogram computation.
    amin : float
        Minimum amplitude (used for numerical stability).
    ref_value : float
        Reference value used for the dB scale.
    top_db : float
        Minimum negative cut-off in decibels.
    param_change_factor : bool
        If freeze=False, this parameter affects the speed at which the filter
        parameters (i.e., central_freqs and bands) can be changed.  When high
        (e.g., param_change_factor=1) the filters change a lot during training.
        When low (e.g. param_change_factor=0.1) the filter parameters are more
        stable during training
    param_rand_factor : float
        This parameter can be used to randomly change the filter parameters
        (i.e, central frequencies and bands) during training.  It is thus a
        sort of regularization. param_rand_factor=0 does not affect, while
        param_rand_factor=0.15 allows random variations within +-15% of the
        standard values of the filter parameters (e.g., if the central freq
        is 100 Hz, we can randomly change it from 85 Hz to 115 Hz).
    freeze : bool
        If False, it the central frequency and the band of each filter are
        added into nn.parameters. If True, the standard frozen features
        are computed.

    Example
    -------
    >>> import torch
    >>> compute_fbanks = Filterbank()
    >>> inputs = torch.randn([10, 101, 201])
    >>> features = compute_fbanks(inputs)
    >>> features.shape
    torch.Size([10, 101, 40])
    (   T
triangularr   @  r   >  r$   绽|=      ?      T@        c                    s  t    || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _| jd d | _tt| j
| j| _td| _|| _|| _| j	dkrQd| _nd| _| j| jkrid| j| jf }tj|dd t| | j| | j| jd }| |}|dd  |d d	  }|d d	 | _|dd	 | _| jstj !| j| j| j  | _tj !| j| j| j  | _td
| jd | j}|"| jj#d
 d| _$d S )Nr$   r#   cpur      zRequire f_min: %f < f_max: %fT)exc_inforC   r   )%r   r   n_melslog_melfilter_shapef_minf_maxr   r   power_spectrogramamin	ref_valuetop_dbfreezen_stftmathlog10maxdb_multiplierr+   r.   
device_inpparam_change_factorparam_rand_factor
multiplierloggererrorlinspace_to_mel_to_hzband	f_centralnn	Parameterrepeatr'   all_freqs_mat)r   r^   r_   r`   ra   rb   r   r   rc   rd   re   rf   rn   ro   rg   err_msgmelhzrv   	all_freqsr   r    r!   r     sV   


zFilterbank.__init__c           	      C   sp  | j | jjd ddd}| j| jjd ddd}| js8|| j| j | j  }|| j| j | j  }n#| j	dkr[| j
r[dtdd | j	  | j	 }||d  }||d  }| |||j}|j}t|dkr|dddd}||d |d  |d |d }t||}| jr| |}t|dkr|j}||d |d |d |d }|dddd}|S )zReturns the FBANks.

        Arguments
        ---------
        spectrogram : torch.Tensor
            A batch of spectrogram tensors.

        Returns
        -------
        fbanks : torch.Tensor
        r#   r   rX   r$   r&   r"   )rw   rz   r{   r'   r)   rv   rg   r   rn   ro   trainingr+   rand_create_fbank_matrixr-   r.   r(   r0   r*   matmulr_   _amplitude_to_DB)	r   spectrogramf_central_matband_matrand_changefbank_matrixsp_shapefbanksfb_shaper    r    r!   r3     sf   

zFilterbank.forwardc                 C   s   dt d| d   S )zReturns mel-frequency value corresponding to the input
        frequency value in Hz.

        Arguments
        ---------
        hz : float
            The frequency point in Hz.

        Returns
        -------
        The mel-frequency value
        #
  r#     )ri   rj   )r~   r    r    r!   rt   J  s   zFilterbank._to_melc                 C   s   dd| d  d  S )a  Returns hz-frequency value corresponding to the input
        mel-frequency value.

        Arguments
        ---------
        mel : float
            The frequency point in the mel-scale.

        Returns
        -------
        The hz-frequency value
        r   r   r   r#   r    )r}   r    r    r!   ru   Z  s   zFilterbank._to_hzc           	      C   sN   || | }|d }| d }t jd| jd}t |t ||dd}|S )a  Returns fbank matrix using triangular filters.

        Arguments
        ---------
        all_freqs : torch.Tensor
            torch.Tensor gathering all the frequency points.
        f_central : torch.Tensor
            torch.Tensor gathering central frequencies of each filter.
        band : torch.Tensor
            torch.Tensor gathering the bands of each filter.

        Returns
        -------
        fbank_matrix : torch.Tensor
        rX   r#   r.   r   )r+   zerosrm   rk   minr)   )	r   r   rw   rv   slope	left_side
right_sidezeror   r    r    r!   _triangular_filtersj  s   
zFilterbank._triangular_filtersc           	      C   s@   || }|| }| | }}||}||  dd}|S )a  Returns fbank matrix using rectangular filters.

        Arguments
        ---------
        all_freqs : torch.Tensor
            torch.Tensor gathering all the frequency points.
        f_central : torch.Tensor
            torch.Tensor gathering central frequencies of each filter.
        band : torch.Tensor
            torch.Tensor gathering the bands of each filter.

        Returns
        -------
        fbank_matrix : torch.Tensor
        r   r#   )gelefloatr)   )	r   r   rw   rv   low_hzhigh_hzr   
right_sizer   r    r    r!   _rectangular_filters  s   
zFilterbank._rectangular_filtersc                 C   s*   t d|| ||  d  dd}|S )aX  Returns fbank matrix using gaussian filters.

        Arguments
        ---------
        all_freqs : torch.Tensor
            torch.Tensor gathering all the frequency points.
        f_central : torch.Tensor
            torch.Tensor gathering central frequencies of each filter.
        band : torch.Tensor
            torch.Tensor gathering the bands of each filter.
        smooth_factor: torch.Tensor
            Smoothing factor of the gaussian filter. It can be used to employ
            sharper or flatter filters.

        Returns
        -------
        fbank_matrix : torch.Tensor
        g      r$   r   r#   )r+   expr)   )r   r   rw   rv   smooth_factorr   r    r    r!   _gaussian_filters  s   zFilterbank._gaussian_filtersc                 C   sP   | j dkr| | j||}|S | j dkr| | j||}|S | | j||}|S )a  Returns fbank matrix to use for averaging the spectrum with
           the set of filter-banks.

        Arguments
        ---------
        f_central_mat : torch.Tensor
            torch.Tensor gathering central frequencies of each filter.
        band_mat : torch.Tensor
            torch.Tensor gathering the bands of each filter.

        Returns
        -------
        fbank_matrix : torch.Tensor
        rT   rectangular)r`   r   r{   r   r   )r   r   r   r   r    r    r!   r     s   

	zFilterbank._create_fbank_matrixc                 C   s^   | j ttj|| jd }|| j | j 8 }|jdd| j }t||	|j
d dd}|S )zConverts  linear-FBANKs to log-FBANKs.

        Arguments
        ---------
        x : torch.Tensor
            A batch of linear FBANK tensors.

        Returns
        -------
        x_db : torch.Tensor
        r   )rC   dimr   r#   )rp   r+   rj   clamprd   rl   amaxrf   rk   viewr'   )r   r1   x_dbnew_x_db_maxr    r    r!   r     s
   zFilterbank._amplitude_to_DB)rS   TrT   r   rU   r   rV   r$   rW   rX   rY   rX   rZ   T)r:   r;   r<   r=   r   r3   staticmethodrt   ru   r   r   r+   tensorr   r   r   r?   r    r    r   r!   rR   {  s8    9KL


 rR   c                       *   e Zd ZdZd fdd	Zdd Z  ZS )	DCTaw  Computes the discrete cosine transform.

    This class is primarily used to compute MFCC features of an audio signal
    given a set of FBANK features as input.

    Arguments
    ---------
    input_size : int
        Expected size of the last dimension in the input.
    n_out : int
        Number of output coefficients.
    ortho_norm : bool
        Whether to use orthogonal norm.

    Example
    -------
    >>> import torch
    >>> inputs = torch.randn([10, 101, 40])
    >>> compute_mfccs = DCT(input_size=inputs.size(-1))
    >>> features = compute_mfccs(inputs)
    >>> features.shape
    torch.Size([10, 101, 20])
    r\   Tc                    s   t    ||krtd||f tt|}tt|d}ttj	t| |d  | }|rM|d  dt
d 9  < |t
dt| 9 }n|d9 }| | _d S )NzCCannot select more DCT coefficients than inputs (n_out=%i, n_in=%i)r#   g      ?r   rX          @)r   r   r7   r+   aranger   	unsqueezecosri   pisqrttdct_mat)r   
input_sizen_out
ortho_normnkdctr   r    r!   r     s   
 zDCT.__init__c                 C   s   |j }t|dkr||j d |j d  |j d |j d }t|| j|j}t|dkrA||d |j d |j d |d }|S )zReturns the DCT of the input tensor.

        Arguments
        ---------
        x : torch.Tensor
            A batch of tensors to transform, usually fbank features.

        Returns
        -------
        dct : torch.Tensor
        r&   r   r"   r#   r$   )r'   r(   r*   r+   r   r   r-   r.   )r   r1   input_shaper   r    r    r!   r3   '  s   *zDCT.forward)r\   Tr:   r;   r<   r=   r   r3   r?   r    r    r   r!   r     s    r   c                       *   e Zd ZdZd fdd	Zdd Z  ZS )Deltasa  Computes delta coefficients (time derivatives).

    Arguments
    ---------
    input_size : int
        The expected size of the inputs for parameter initialization.
    window_length : int
        Length of the window used to compute the time derivatives.

    Example
    -------
    >>> inputs = torch.randn([10, 101, 20])
    >>> compute_deltas = Deltas(input_size=inputs.size(-1))
    >>> features = compute_deltas(inputs)
    >>> features.shape
    torch.Size([10, 101, 20])
    rB   c                    sn   t    |d d | _| j| jd  d| j d  d | _| dtj| j | jd tjd|dd d S )Nr#   r$   r"   kernel)dtype)	r   r   r   denomregister_bufferr+   r   float32rz   )r   r   window_lengthr   r    r!   r   W  s   
$
zDeltas.__init__c                 C   s   | dd dd}|j}t|dkr$||d |d  |d |d }tjjj|| j| jfdd}tjjj	|| j
|j|jd d	| j }t|dkr]||d |d |d |d }| dd dd}|S )
zReturns the delta coefficients.

        Arguments
        ---------
        x : torch.Tensor
            A batch of tensors.

        Returns
        -------
        delta_coeff : torch.Tensor
        r#   r$   rC   r&   r   r"   	replicate)mode)groups)r)   r'   r(   r*   r+   rx   
functionalpadr   conv1dr   r-   r.   r   )r   r1   r2   delta_coeffr    r    r!   r3   e  s"   "zDeltas.forward)rB   r   r    r    r   r!   r   D  s    r   c                       r   )ContextWindowa  Computes the context window.

    This class applies a context window by gathering multiple time steps
    in a single feature vector. The operation is performed with a
    convolutional layer based on a fixed kernel designed for that.

    Arguments
    ---------
    left_frames : int
         Number of left frames (i.e, past frames) to collect.
    right_frames : int
        Number of right frames (i.e, future frames) to collect.

    Example
    -------
    >>> import torch
    >>> compute_cw = ContextWindow(left_frames=5, right_frames=5)
    >>> inputs = torch.randn([10, 101, 20])
    >>> features = compute_cw(inputs)
    >>> features.shape
    torch.Size([10, 101, 220])
    r   c                    s   t    || _|| _| j| j d | _dt| j| j d | _t| j| j| _	| j| jkr>| j| j }t
| j	|d| _	d| _d S )Nr#   r$   T)r   r   left_framesright_framescontext_lenrk   
kernel_lenr+   eyer   roll
first_call)r   r   r   lagr   r    r!   r     s   

zContextWindow.__init__c                 C   s   | dd}| jdu r(d| _| j|jd dd|jd | j | jd| _|j}t	|dkrB|
|d |d  |d |d }tjjj|| j|j|jd t| j| jd}t	|dkrr|
|d |jd |d |jd	 }| dd}|S )
zReturns the tensor with the surrounding context.

        Arguments
        ---------
        x : torch.Tensor
            A batch of tensors.

        Returns
        -------
        cw_x : torch.Tensor
            The context-enriched tensor
        r#   r$   TFr&   r   r"   )r   paddingrC   )r)   r   r   rz   r'   r   r   r   r   r(   r*   r+   rx   r   r   r-   r.   rk   r   r   )r   r1   r2   cw_xr    r    r!   r3     s,   
"zContextWindow.forward)r   r   r   r    r    r   r!   r     s    r   c                       s   e Zd ZU dZddlmZ eeejf e	d< eeejf e	d< eeef e	d< 						
	d fdd	Z
eg dfddZdd Zdd Zdd Z fddZedd ZeedddZ  ZS )InputNormalizationa  Performs mean and variance normalization of the input tensor.

    Arguments
    ---------
    mean_norm : True
         If True, the mean will be normalized.
    std_norm : True
         If True, the standard deviation will be normalized.
    norm_type : str
         It defines how the statistics are computed ('sentence' computes them
         at sentence level, 'batch' at batch level, 'speaker' at speaker
         level, while global computes a single normalization vector for all
         the sentences in the dataset). Speaker and global statistics are
         computed with a moving average approach.
    avg_factor : float
         It can be used to manually set the weighting factor between
         current statistics and accumulated ones.
    requires_grad : bool
        Whether this module should be updated using the gradient during training.
    update_until_epoch : int
        The epoch after which updates to the norm stats should stop.

    Example
    -------
    >>> import torch
    >>> norm = InputNormalization()
    >>> inputs = torch.randn([10, 101, 20])
    >>> inp_len = torch.ones([10])
    >>> features = norm(inputs, inp_len)
    r   )Dictspk_dict_meanspk_dict_stdspk_dict_countTglobalNFr"   c                    sr   t    || _|| _|| _|| _|| _tdg| _	tdg| _
i | _i | _i | _d| _d| _d| _|| _d S )Nr   rX   rW   )r   r   	mean_normstd_norm	norm_type
avg_factorrequires_gradr+   r   	glob_meanglob_stdr   r   r   weightcountrM   update_until_epoch)r   r   r   r   r   r   r   r   r    r!   r   
  s   
	
zInputNormalization.__init__c                 C   sJ  |j d }g }g }| jdks| jdkrt|}t|D ]}	t||	 |j d   }
| ||	d|
df \}}|| || | jdkrU||	 |j	 |j	 ||	< | jdkrt||	 d }| j
r|| jvrz|| j|< || j|< d| j|< nT| j| d | j|< | jdu rd| j|  | _n| j| _d| j | j| | | j|  | j|< d| j | j| | | j|  | j|< | j|   | j|   | j| j	}| j| j	}n|| jv r| j| j	}| j| j	}n|j	}|j	}||	 | | ||	< q| jdks
| jdkrtjt|dd	}tjt|dd	}| jdkr,||j	 |j	 }| jdkr| j
r| jdkrC|| _|| _n@|du sN|| jk r| jdu r]d| jd  | _n| j| _d| j | j| | j|  | _d| j | j| | j|  | _| j  | j  | jd | _|| jj	| | jj	| }|S )
a  Returns the tensor with the surrounding context.

        Arguments
        ---------
        x : torch.Tensor
            A batch of tensors.
        lengths : torch.Tensor
            A batch of tensors containing the relative length of each
            sentence (e.g, [0.7, 0.9, 1.0]). It is used to avoid
            computing stats on zero-padded steps.
        spk_ids : torch.Tensor containing the ids of each speaker (e.g, [0 10 6]).
            It is used to perform per-speaker normalization when
            norm_type='speaker'.
        epoch : int
            The epoch count.

        Returns
        -------
        x : torch.Tensor
            The normalized tensor.
        r   sentencespeakerr#   .Nbatchr   r   )r'   r   r+   
empty_likeranger   r   _compute_current_statsappenddatar   r   r   r   r   r   r-   detachmeanstackr   r   r   r   )r   r1   lengthsspk_idsepoch	N_batchescurrent_meanscurrent_stdsoutsnt_idactual_sizecurrent_meancurrent_stdspk_idspeaker_meanspeaker_stdr    r    r!   r3   #  s   
















zInputNormalization.forwardc                 C   s|   | j rtj|dd j}n	tjdg|jd}| jr%tj|dd j}n	tjdg|jd}t	|| j
t| }||fS )aM  Computes mean and std

        Arguments
        ---------
        x : torch.Tensor
            A batch of tensors.

        Returns
        -------
        current_mean : torch.Tensor
            The average of x along dimension 0
        current_std : torch.Tensor
            The standard deviation of x along dimension 0
        r   r   rZ   r   rX   )r   r+   r   r   r   r   r.   r   stdrk   rM   	ones_like)r   r1   r   r   r    r    r!   r     s   z)InputNormalization._compute_current_statsc                 C   sD   i }| j |d< | j|d< | j|d< | j|d< | j|d< | j|d< |S )z=Fills the dictionary containing the normalization statistics.r   r   r   r   r   r   )r   r   r   r   r   r   )r   stater    r    r!   _statistics_dict  s   





z#InputNormalization._statistics_dictc                 C   s   |d | _ t|d tr|d | _|d | _n
|d | _|d | _i | _|d D ]}|d | | j|< q(i | _|d D ]}|d | | j|< q;|d | _|S )zLoads the dictionary containing the statistics.

        Arguments
        ---------
        state : dict
            A dictionary containing the normalization statistics.

        Returns
        -------
        state : dict
        r   r   r   r   r   r   )r   
isinstancer   r   r   r   r   r   )r   r  spkr    r    r!   _load_statistics_dict  s   




z(InputNormalization._load_statistics_dictc                    sh   t t| |} | j|| _| j|| _| jD ]}| j| || j|< | j| || j|< q| S )z,Puts the needed tensors in the right device.)r   r   r-   r   r   r   r   )r   r.   r  r   r    r!   r-     s   
zInputNormalization.toc                 C   s   |   }t|| dS )zSave statistic dictionary.

        Arguments
        ---------
        path : str
            A path where to save the dictionary.
        N)r  r+   save)r   pathstatsr    r    r!   _save  s   	zInputNormalization._savec                 C   s"   ~d}t j||d}| | dS )a  Load statistic dictionary.

        Arguments
        ---------
        path : str
            The path of the statistic dictionary
        end_of_epoch : bool
            Whether this is the end of an epoch.
            Here for compatibility, but not used.
        r[   )map_locationN)r+   loadr  )r   r
  end_of_epochr.   r  r    r    r!   _load  s   zInputNormalization._load)TTr   NFr"   )F)r:   r;   r<   r=   typingr   r   r+   Tensor__annotations__r   r   r3   r   r  r  r-   r   r  r   r   r  r?   r    r    r   r!   r     s0   
  "&

r   c                       s^   e Zd ZdZ					d fdd	Zdd	d
Zdd Zdd Zdd Zdd Z	dd Z
  ZS )
GlobalNormaN  A global normalization module - computes a single mean and standard deviation
    for the entire batch across unmasked positions and uses it to normalize the
    inputs to the desired mean and standard deviation.

    This normalization is reversible - it is possible to use the .denormalize()
    method to recover the original values.

    Arguments
    ---------
    norm_mean: float
        the desired normalized mean
    norm_std: float
        the desired normalized standard deviation
    update_steps: float
        the number of steps over which statistics will be collected
    length_dim: int
        the dimension used to represent the length
    mask_value: float
        the value with which to fill masked positions
        without a mask_value, the masked positions would be normalized,
        which might not be desired

    Example
    -------
    >>> import torch
    >>> from speechbrain.processing.features import GlobalNorm
    >>> global_norm = GlobalNorm(
    ...     norm_mean=0.5,
    ...     norm_std=0.2,
    ...     update_steps=3,
    ...     length_dim=1
    ... )
    >>> x = torch.tensor([[1., 2., 3.]])
    >>> x_norm = global_norm(x)
    >>> x_norm
    tensor([[0.3000, 0.5000, 0.7000]])
    >>> x = torch.tensor([[5., 10., -4.]])
    >>> x_norm = global_norm(x)
    >>> x_norm
    tensor([[0.6071, 0.8541, 0.1623]])
    >>> x_denorm = global_norm.denormalize(x_norm)
    >>> x_denorm
    tensor([[ 5.0000, 10.0000, -4.0000]])
    >>> x = torch.tensor([[100., -100., -50.]])
    >>> global_norm.freeze()
    >>> global_norm(x)
    tensor([[ 5.3016, -4.5816, -2.1108]])
    >>> global_norm.denormalize(x_norm)
    tensor([[ 5.0000, 10.0000, -4.0000]])
    >>> global_norm.unfreeze()
    >>> global_norm(x)
    tensor([[ 5.3016, -4.5816, -2.1108]])
    >>> global_norm.denormalize(x_norm)
    tensor([[ 5.0000, 10.0000, -4.0000]])
    rZ   rX   Nr$   c           	         sz   t    td}td}td}| d| | d| | d| || _|| _|| _d| _|| _	|| _
d| _d S )NrZ   running_meanrunning_stdr   r   F)r   r   r+   r   r   	norm_meannorm_std
mask_value
step_countupdate_steps
length_dimfrozen)	r   r  r  r  r  r  r  r  r   r   r    r!   r   \  s   




zGlobalNorm.__init__Fc                 C   s
  |du rt t|}|du r| j}| ||}|s^| js^| jdu s(| j| jk r^||}|	 }|
 }| }	| j|	 }
| j| j |	|  |
 | j_| j| j |	|  |
 | j_|
| j_| |}t |spt j||jd}| |}|| |}|  jd7  _|S )a  Normalizes the tensor provided

        Arguments
        ---------
        x: torch.Tensor
            the tensor to normalize
        lengths: torch.Tensor
            a tensor of relative lengths (padding will not
            count towards normalization)
        mask_value: float
            the value to use for masked positions
        skip_update: false
            whether to skip updates to the norm

        Returns
        -------
        result: torch.Tensor
            the normalized tensor
        Nr   r#   )r+   onesr(   r  get_maskr  r  r  masked_selectr   r  rO   r   r  r   r  	normalize	is_tensorr   r.   masked_fill)r   r1   r   r  skip_updatemaskx_maskedr   r  r   
new_weightmask_value_normr    r    r!   r3   t  s:   




zGlobalNorm.forwardc                 C   $   || j  | j }|| j | j }|S )a&  Performs the normalization operation against the running
        mean and standard deviation

        Arguments
        ---------
        x: torch.Tensor
            the tensor to normalize

        Returns
        -------
        result: torch.Tensor
            the normalized tensor
        )r  r  r  r  r   r1   r    r    r!   r!    s   zGlobalNorm.normalizec                 C   sT   | | j}t|| |}td| D ]}|| jkr ||}q|| }|S )aF  Returns the length mask for the specified tensor

        Arguments
        ---------
        x: torch.Tensor
            the tensor for which the mask will be obtained

        lengths: torch.Tensor
            the length tensor

        Returns
        -------
        mask: torch.Tensor
            the mask tensor
        r#   )sizer  r   r   r   r   	expand_asbool)r   r1   r   max_lenr%  r   r    r    r!   r    s   

zGlobalNorm.get_maskc                 C   r)  )zReverses the normalization process

        Arguments
        ---------
        x: torch.Tensor
            a normalized tensor

        Returns
        -------
        result: torch.Tensor
            a denormalized version of x
        )r  r  r  r  r*  r    r    r!   denormalize  s   zGlobalNorm.denormalizec                 C   
   d| _ dS )z%Stops updates to the running mean/stdTNr  r8   r    r    r!   rg        
zGlobalNorm.freezec                 C   r0  )z'Resumes updates to the running mean/stdFNr1  r8   r    r    r!   unfreeze  r2  zGlobalNorm.unfreeze)rZ   rX   Nr$   rZ   )NNF)r:   r;   r<   r=   r   r3   r!  r  r/  rg   r3  r?   r    r    r   r!   r  #  s    :
8r  c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )MinLevelNorma0  A commonly used normalization for the decibel scale

    The scheme is as follows

    x_norm = (x - min_level_db)/-min_level_db * 2 - 1

    The rationale behind the scheme is as follows:

    The top of the scale is assumed to be 0db.
    x_rel = (x - min) / (max - min) gives the relative position on the scale
    between the minimum and the maximum where the minimum is 0. and the
    maximum is 1.

    The subsequent rescaling (x_rel * 2 - 1) puts it on a scale from -1. to 1.
    with the middle of the range centered at zero.

    Arguments
    ---------
    min_level_db: float
        the minimum level

    Example
    -------
    >>> norm = MinLevelNorm(min_level_db=-100.)
    >>> x = torch.tensor([-50., -20., -80.])
    >>> x_norm = norm(x)
    >>> x_norm
    tensor([ 0.0000,  0.6000, -0.6000])
    c                    s   t    || _d S rI   )r   r   min_level_db)r   r5  r   r    r!   r     s   

zMinLevelNorm.__init__c                 C   s4   || j  | j   }|d9 }|d }t|dd}|S )a  Normalizes audio features in decibels (usually spectrograms)

        Arguments
        ---------
        x: torch.Tensor
            input features

        Returns
        -------
        normalized_features: torch.Tensor
            the normalized features
        r   rX   rC   r#   )r5  r+   clipr*  r    r    r!   r3     s
   zMinLevelNorm.forwardc                 C   s4   t |dd}|d d }|| j 9 }|| j7 }|S )zReverses the min level normalization process

        Arguments
        ---------
        x: torch.Tensor
            the normalized tensor

        Returns
        -------
        result: torch.Tensor
            the denormalized tensor
        rC   r#   rX   r   )r+   r6  r5  r*  r    r    r!   r/  &  s
   
zMinLevelNorm.denormalize)r:   r;   r<   r=   r   r3   r/  r?   r    r    r   r!   r4    s
    r4  c                       r   )	DynamicRangeCompressionaq  Dynamic range compression for audio signals - clipped log scale
    with an optional multiplier

    Arguments
    ---------
    multiplier: float
        the multiplier constant
    clip_val: float
        the minimum accepted value (values below this
        minimum will be clipped)

    Example
    -------
    >>> drc = DynamicRangeCompression()
    >>> x = torch.tensor([10., 20., 0., 30.])
    >>> drc(x)
    tensor([  2.3026,   2.9957, -11.5129,   3.4012])
    >>> drc = DynamicRangeCompression(2.)
    >>> x = torch.tensor([10., 20., 0., 30.])
    >>> drc(x)
    tensor([  2.9957,   3.6889, -10.8198,   4.0943])
    r#   h㈵>c                    s   t    || _|| _d S rI   )r   r   rp   clip_val)r   rp   r9  r   r    r!   r   R  s   

z DynamicRangeCompression.__init__c                 C   s   t t j|| jd| j S )zPerforms the forward pass

        Arguments
        ---------
        x: torch.Tensor
            the source signal

        Returns
        -------
        result: torch.Tensor
            the result
        r   )r+   rL   r   r9  rp   r*  r    r    r!   r3   W  s   zDynamicRangeCompression.forward)r#   r8  r   r    r    r   r!   r7  :  s    r7  )r#   FrJ   ) r=   ri   r+   speechbrain.dataio.dataior   speechbrain.utils.checkpointsr   r   r   r   !speechbrain.utils.filter_analysisr   speechbrain.utils.loggerr   r:   rq   rx   Moduler	   r@   r   r-  r   rQ   rR   r   r   r   r   r  r4  r7  r    r    r    r!   <module>   sD    $  
(  LHW  A NJ