o
    ei                     @   sP   d Z ddlZddlZddlmZ ddlmZ G dd deZG dd deZdS )	aP   Specifies the inference interfaces for speech enhancement modules.

Authors:
 * Aku Rouhe 2021
 * Peter Plantinga 2021
 * Loren Lugosch 2020
 * Mirco Ravanelli 2020
 * Titouan Parcollet 2021
 * Abdel Heba 2021
 * Andreas Nautsch 2022, 2023
 * Pooneh Mousavi 2023
 * Sylvain de Langen 2023
 * Adel Moumen 2023
 * Pradnya Kandarkar 2023
    N)
Pretrained)lengths_arg_existsc                   @   s:   e Zd ZdZg dZdgZdd ZdddZdd	d
ZdS )SpectralMaskEnhancementaY  A ready-to-use model for speech enhancement.

    Arguments
    ---------
    See ``Pretrained``.

    Example
    -------
    >>> import torch
    >>> from speechbrain.inference.enhancement import SpectralMaskEnhancement
    >>> # Model is downloaded from the speechbrain HuggingFace repo
    >>> tmpdir = getfixture("tmpdir")
    >>> enhancer = SpectralMaskEnhancement.from_hparams(
    ...     source="speechbrain/metricgan-plus-voicebank",
    ...     savedir=tmpdir,
    ... )
    >>> enhanced = enhancer.enhance_file(
    ...     "speechbrain/metricgan-plus-voicebank/example.wav"
    ... )
    )compute_stftspectral_magnituderesynthenhance_modelc                 C   s"   | j |}| j |}t|S )a3  Compute the log spectral magnitude features for masking.

        Arguments
        ---------
        wavs : torch.Tensor
            A batch of waveforms to convert to log spectral mags.

        Returns
        -------
        feats : torch.Tensor
            The log spectral magnitude features.
        )hparamsr   r   torchlog1p)selfwavsfeats r   _/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/inference/enhancement.pycompute_features1   s   
z(SpectralMaskEnhancement.compute_featuresNc                 C   s\   | | j}| |}|dur| jj||d}n| j|}t||}| jt	||S )a  Enhance a batch of noisy waveforms.

        Arguments
        ---------
        noisy : torch.Tensor
            A batch of waveforms to perform enhancement on.
        lengths : torch.Tensor
            The lengths of the waveforms if the enhancement model handles them.

        Returns
        -------
        wavs : torch.Tensor
            A batch of enhanced waveforms of the same shape as input.
        Nlengths)
todevicer   modsr   r
   mulr	   r   expm1)r   noisyr   noisy_featuresmaskenhancedr   r   r   enhance_batchB   s   
z%SpectralMaskEnhancement.enhance_batchc                 K   s|   | j |fi |}|| j}|d}t| jr%| j|tdgd}n| |}|dur9tj	||| j
jjd |dS )a  Enhance a wav file.

        Arguments
        ---------
        filename : str
            Location on disk to load file for enhancement.
        output_filename : str
            If provided, writes enhanced data to this file.
        **kwargs : dict
            Arguments forwarded to ``load_audio``.

        Returns
        -------
        wav : torch.Tensor
            The enhanced waveform.
        r   g      ?r   Nurisrcsample_rate)
load_audior   r   	unsqueezer   r   r
   tensor
torchaudiosaver	   r   r!   squeezer   filenameoutput_filenamekwargsr   batchr   r   r   r   enhance_file^   s   



z$SpectralMaskEnhancement.enhance_fileN)	__name__
__module____qualname____doc__HPARAMS_NEEDEDMODULES_NEEDEDr   r   r-   r   r   r   r   r      s    
r   c                   @   s4   e Zd ZdZdgZd
ddZd
ddZd
dd	ZdS )WaveformEnhancementa2  A ready-to-use model for speech enhancement.

    Arguments
    ---------
    See ``Pretrained``.

    Example
    -------
    >>> from speechbrain.inference.enhancement import WaveformEnhancement
    >>> # Model is downloaded from the speechbrain HuggingFace repo
    >>> tmpdir = getfixture("tmpdir")
    >>> enhancer = WaveformEnhancement.from_hparams(
    ...     source="speechbrain/mtl-mimic-voicebank",
    ...     savedir=tmpdir,
    ... )
    >>> enhanced = enhancer.enhance_file(
    ...     "speechbrain/mtl-mimic-voicebank/example.wav"
    ... )
    r   Nc                 C   s    | | j}| j|\}}|S )a  Enhance a batch of noisy waveforms.

        Arguments
        ---------
        noisy : torch.Tensor
            A batch of waveforms to perform enhancement on.
        lengths : torch.Tensor
            The lengths of the waveforms if the enhancement model handles them.

        Returns
        -------
        torch.Tensor
            A batch of enhanced waveforms of the same shape as input.
        )r   r   r   r   )r   r   r   enhanced_wav_r   r   r   r      s   z!WaveformEnhancement.enhance_batchc                 K   sL   | j |fi |}|d}| |}|dur!tj||| jjd |dS )a  Enhance a wav file.

        Arguments
        ---------
        filename : str
            Location on disk to load file for enhancement.
        output_filename : str
            If provided, writes enhanced data to this file.
        **kwargs : dict
            Arguments forwarded to ``load_audio``

        Returns
        -------
        enhanced : torch.Tensor
            The enhanced waveform.
        r   Nr   )r"   r#   r   r%   r&   audio_normalizerr!   r'   r(   r   r   r   r-      s   


z WaveformEnhancement.enhance_filec                 C   s   |  ||S )z#Runs enhancement on the noisy input)r   )r   r   r   r   r   r   forward   s   zWaveformEnhancement.forwardr.   )r/   r0   r1   r2   r4   r   r-   r9   r   r   r   r   r5      s    

 r5   )	r2   r
   r%    speechbrain.inference.interfacesr   speechbrain.utils.callchainsr   r   r5   r   r   r   r   <module>   s    k