o
    i
                     @   s:   d Z ddlZddlmZ ddlmZ G dd dejZdS )z(Sliding Window for raw audio input data.    N)Tuplec                	       sr   e Zd ZdZ					ddededed	ef fd
dZdejdejdeejejf fddZ	defddZ
  ZS )SlidingWindowa  Sliding Window.
    Provides a sliding window over a batched continuous raw audio tensor.
    Optionally, provides padding (Currently not implemented).
    Combine this module with a pre-encoder compatible with raw audio data,
    for example Sinc convolutions.
    Known issues:
    Output length is calculated incorrectly if audio shorter than win_length.
    WARNING: trailing values are discarded - padding not implemented yet.
    There is currently no additional window function applied to input values.
            N
win_length
hop_lengthchannelspaddingc                    s,   t    || _|| _|| _|| _|| _dS )aK  Initialize.
        Args:
            win_length: Length of frame.
            hop_length: Relative starting point of next frame.
            channels: Number of input channels.
            padding: Padding (placeholder, currently not implemented).
            fs:  Sampling rate (placeholder for compatibility, not used).
        N)super__init__fsr   r   r	   r
   )selfr   r   r	   r
   r   	__class__ N/home/ubuntu/.local/lib/python3.10/site-packages/funasr/frontends/windowing.pyr      s   

zSlidingWindow.__init__inputinput_lengthsreturnc                 C   s|   |  }|d }|d }| j}| j}||||ddd}|d|| j}	|	dddd }
|| j | j d }|
|fS )a@  Apply a sliding window on the input.
        Args:
            input: Input (B, T, C*D) or (B, T*C*D), with D=C=1.
            input_lengths: Input lengths within batch.
        Returns:
            Tensor: Output with dimensions (B, T, C, D), with D=win_length.
            Tensor: Output lengths within batch.
        r   r         )sizer	   r   viewpermuteunfoldr   
contiguous)r   r   r   
input_sizeBTCD
continuouswindowedoutputoutput_lengthsr   r   r   forward/   s   zSlidingWindow.forwardc                 C   s   | j S )zDReturn output length of feature dimension D, i.e. the window length.)r   )r   r   r   r   output_sizeH   s   zSlidingWindow.output_size)r   r   r   NN)__name__
__module____qualname____doc__intr   torchTensorr   r&   r'   __classcell__r   r   r   r   r      s0    
r   )r+   r-   torch.nnnntypingr   Moduler   r   r   r   r   <module>   s
   