o
    %ݫi                     @   sx   d Z ddlZddlmZ ddlmZ ddlmZ G dd dej	j
ZG dd deZG d	d
 d
eZG dd deZdS )zb
Combinations of processing algorithms to implement downsampling methods.

Authors
 * Salah Zaiem
    N)Conv1d)	Pooling1dc                   @   s   e Zd ZdZdd ZdS )Downsamplerz#Wrapper for downsampling techniquesc                 C   s
   |  |S )zDownsampling function

        Arguments
        ---------
        x : tensor
            Speech samples of shape [B,n_samples] with B the batch size

        Returns
        -------
        Downsampled outputs.
        )downsampler)selfx r   R/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/lobes/downsampling.pyforward   s   
zDownsampler.forwardN)__name__
__module____qualname____doc__r
   r   r   r   r	   r      s    r   c                           e Zd ZdZ fddZ  ZS )SignalDownsamplera  Signal downsampling (Decimation)

    Arguments
    ---------
    downsampling_factor : int
        Factor of downsampling (i.e. ratio (length before ds / length after ds))
    initial_sampling_rate : int
        Sampling_rate of the input audios

    Example
    -------
    >>> sd = SignalDownsampler(2,16000)
    >>> a = torch.rand([8,28000])
    >>> a = sd(a)
    >>> print(a.shape)
    torch.Size([8, 14000])
    c                    s8   t    || _t|| | _tj|| jtjd| _	d S )N)dtype)
super__init__downsampling_factorinttarget_ds_rateTResampletorchfloat32r   )r   r   initial_sampling_rate	__class__r   r	   r   5   s   

zSignalDownsampler.__init__r   r   r   r   r   __classcell__r   r   r   r	   r   "   s    r   c                       r   )Conv1DDownsamplera  1D Convolutional downsampling with a learned convolution

    Arguments
    ---------
    downsampling_factor : int
        Factor of downsampling (i.e. ratio (length before ds / length after ds))
    kernel_size : int
        Kernel size of the 1D filter (must be an odd integer)
    Example
    -------
    >>> sd = Conv1DDownsampler(3,161)
    >>> a = torch.rand([8,33000])
    >>> a = sd(a)
    >>> print(a.shape)
    torch.Size([8, 10947])
    c                    s6   t    || _|| _t| jd| jdd d gd| _d S )Nvalid   )stridepaddingkernel_sizeout_channelsinput_shape)r   r   r%   r   r   r   )r   r   r%   r   r   r	   r   P   s   
zConv1DDownsampler.__init__r   r   r   r   r	   r    >   s    r    c                       s$   e Zd ZdZ	d fdd	Z  ZS )PoolingDownsamplera?  1D Pooling downsampling (non-learned)

    Arguments
    ---------
    downsampling_factor : int
        Factor of downsampling (i.e. ratio (length before ds / length after ds))
    kernel_size : int
        Kernel size of the 1D filter (must be an odd integer)
    padding : int
        The number of padding elements to apply.
    pool_type : string
        Pooling approach, must be within ["avg","max"]
    Example
    -------
    >>> sd = PoolingDownsampler(3,41)
    >>> a = torch.rand([8,33000])
    >>> a = sd(a)
    >>> print(a.shape)
    torch.Size([8, 10987])
    r   avgc                    sB   t    || _|| _|| _|| _t| j| j| jd| jd| _d S )N   )r#   r$   r%   
input_dims	pool_type)r   r   r%   r$   r,   r   r   r   )r   r   r%   r$   r,   r   r   r	   r   s   s   
zPoolingDownsampler.__init__)r   r)   r   r   r   r   r	   r(   ]   s    r(   )r   r   torchaudio.transforms
transformsr   speechbrain.nnet.CNNr   speechbrain.nnet.poolingr   nnModuler   r   r    r(   r   r   r   r	   <module>   s    