o
    2wi                  	   @   s   d Z ddlZddlZddlZddlmZ dejfddZ	d'dejd	e
d
edefddZdejfddZdejfddZde
dedefddZd(dejfddZd)deded efd!d"Zd#e
d$e
fd%d&ZdS )*z1
Signal processing or PyTorch related utilities.
    N)
functionalxc                 C   s,   t | dkt jd| j| jdt | |  S )zh
    Implementation of sinc, i.e. sin(x) / x

    __Warning__: the input is not multiplied by `pi`!
    r   g      ?)devicedtype)torchwheretensorr   r   sin)r    r
   H/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/julius/core.pysinc   s   ,r   constantr   target_lengthmodevaluec                 C   s    t j| d|| jd  f||dS )zI
    Pad the given tensor to the given length, with 0s on the right.
    r   )r   r   )Fpadshape)r   r   r   r   r
   r
   r   pad_to   s    r   freqsc                 C   s   dt d| d   S )z
    Converts a Tensor of frequencies in hertz to the mel scale.
    Uses the simple formula by O'Shaughnessy (1987).

    Args:
        freqs (torch.Tensor): frequencies to convert.

    #
       )r   log10)r   r
   r
   r   	hz_to_mel   s   	r   melsc                 C   s   dd| d  d  S )z
    Converts a Tensor of mel scaled frequencies to Hertz.
    Uses the simple formula by O'Shaughnessy (1987).

    Args:
        mels (torch.Tensor): mel frequencies to convert.
    r   
   r   r   r
   )r   r
   r
   r   	mel_to_hz)   s   r   n_melsfminfmaxc                 C   sB   t tt| }t tt| }t||| }t|S )z
    Return frequencies that are evenly spaced in mel scale.

    Args:
        n_mels (int): number of frequencies to return.
        fmin (float): start from this frequency (in Hz).
        fmax (float): finish at this frequency (in Hz).


    )r   r   r   floatitemlinspacer   )r   r    r!   lowhighr   r
   r
   r   mel_frequencies4   s   r'   :0yE>c                 C   s   t || d d d S )z$
    Return the volume in dBFS.
       r   r   )r   r   mean)r   floorr
   r
   r   volumeE   s   r,         freqsrdurc                 C   s6   t jt|| |d | }t dtj |  | S )z
    Return a pure tone, i.e. cosine.

    Args:
        freq (float): frequency (in Hz)
        sr (float): sample rate (in Hz)
        dur (float): duration (in seconds)
    )r   r)   )r   arangeintr"   cosmathpi)r/   r0   r1   r   timer
   r
   r   	pure_toneL   s   	r8   kernel_sizestridec           
      C   s   t | j}|d}tt||| | d }|d | | }t| d|| f }g }t	|
 D ]
}	|||	 q4|ddksJJ d||dg }||||g |S )aV  1D only unfolding similar to the one from PyTorch.
    However PyTorch unfold is extremely slow.

    Given an input tensor of size `[*, T]` this will return
    a tensor `[*, F, K]` with `K` the kernel size, and `F` the number
    of frames. The i-th frame is a view onto `i * stride: i * stride + kernel_size`.
    This will automatically pad the input to cover at least once all entries in `input`.

    Args:
        input (Tensor): tensor for which to return the frames.
        kernel_size (int): size of each frame.
        stride (int): stride between each frame.

    Shape:

        - Inputs: `input` is `[*, T]`
        - Output: `[*, F, kernel_size]` with `F = 1 + ceil((T - kernel_size) / stride)`


    ..Warning:: unlike PyTorch unfold, this will pad the input
        so that any position in `input` is covered by at least one frame.
    r   r   r   zdata should be contiguous)listr   popr5   ceilmaxr   r   
contiguousrangedimappendr:   
as_strided)
inputr9   r:   r   lengthn_frames
tgt_lengthpaddedstridesrA   r
   r
   r   unfoldY   s   

rJ   )r   r   )r(   )r-   r.   N)__doc__r5   typingtpr   torch.nnr   r   Tensorr   r3   strr"   r   r   r   r'   r,   r8   rJ   r
   r
   r
   r   <module>   s   	