o
    7wi
                     @   s4   d Z ddlZddlZddlZG dd dejjZdS )a  
Codec Augmentation via torchaudio

This library provides codec augmentation techniques in torchaudio for enhanced
audio data processing.

For detailed guidance and usage examples, refer to the tutorial at:
https://pytorch.org/audio/stable/tutorials/audio_data_augmentation_tutorial.html

Note: This code is compatible with FFmpeg as the torchaudio backend.
When using FFmpeg2, the maximum number of samples for processing is limited to 16.

Authors
 * Mirco Ravanelli 2023
    Nc                       s4   e Zd ZdZd
 fdd	ZdddZdd	 Z  ZS )CodecAugmenta  
    Apply random audio codecs to input waveforms using torchaudio.

    This class provides an interface for applying codec augmentation techniques to audio data.

    Arguments
    ---------
    sample_rate: int
        The sample rate of the input waveform.

    Example
    -------
    >>> waveform = torch.rand(4, 16000)
    >>> if torchaudio.list_audio_backends()[0] == 'ffmpeg':
    ...     augmenter = CodecAugment(16000)
    ...     output_waveform = augmenter(waveform)
    >  c                    s   t    || _g d| _d S )N))wav	pcm_mulaw)mp3N)g722N)super__init__sample_rateavailable_format_encoders)selfr
   	__class__ V/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/speechbrain/augment/codec.pyr	   *   s   
zCodecAugment.__init__Nc                 C   s@   t jj||d}||ddd| j}|dd|jS )a  
        Apply the selected audio codec.

        Arguments
        ----------
        waveform: torch.Tensor
            Input waveform of shape `[batch, time]`.
        format: str
            The audio format to use (e.g., "wav", "mp3"). Default is None.
        encoder: str
            The encoder to use for the format (e.g., "opus", "vorbis"). Default is None.

        Returns
        ---------
        torch.Tensor:
            Coded version of the input waveform of shape `[batch, time]`.
        formatencoderr      cpu)
torchaudioioAudioEffectorapply	transposetor
   device)r   waveformr   r   audio_effectorwaveform_augr   r   r   apply_codec3   s   zCodecAugment.apply_codecc                 C   s    t | j\}}| j|||dS )a>  
        Apply a random audio codec from the available list.

        Arguments
        ---------
        waveform: torch.Tensor
            Input waveform of shape `[batch, time]`.

        Returns
        -------
        torch.Tensor
            Coded version of the input waveform of shape `[batch, time]`.
        r   )randomchoicer   r    )r   r   r   r   r   r   r   forwardM   s   zCodecAugment.forward)r   )NN)__name__
__module____qualname____doc__r	   r    r#   __classcell__r   r   r   r   r      s
    
	r   )r'   r!   torchr   nnModuler   r   r   r   r   <module>   s
    