
    mi              	          d Z ddlZddlZddlZddlmZ dej                  fdZ	ddej                  de
ded	efd
Zdej                  fdZdej                  fdZde
dedefdZddej                  fdZddededefdZde
de
fdZy)z1
Signal processing or PyTorch related utilities.
    N)
functionalxc                     t        j                  | dk(  t        j                  d| j                  | j                        t        j
                  |       | z        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   s    E/var/www/stems/demucs_env/lib/python3.12/site-packages/julius/core.pysincr      sC     ;;qAvu||BqxxqwwOQVQZQZ[\Q]`aQabb    r
   target_lengthmodevaluec                 X    t        j                  | d|| j                  d   z
  f||      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   s       r   pad_tor      s-     55!]V\\"-==>TQVWWr   freqsc                 >    dt        j                  d| dz  z         z  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   s    r   	hz_to_melr      s      %++a%#+o...r   melsc                     dd| dz  z  dz
  z  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   s    r   	mel_to_hzr#   )   s     "td{#a'((r   n_melsfminfmaxc                    t        t        j                  t        |                  j	                         }t        t        j                  t        |                  j	                         }t        j
                  |||       }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   s         r   mel_frequenciesr-   4   s`     ELLt-
.
3
3
5CU\\%+./446D>>#tV,DT?r   c                 \    t        j                  || dz  j                  d      z         dz  S )z$
    Return the volume in dBFS.
       r   r!   )r   r   mean)r   floors     r   volumer2   E   s)     ;;u1{{2./"44r   freqsrdurc                     t        j                  t        ||z        |      j                         |z  }t        j                  dt
        j                  z  | z  |z        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)r3   r4   r5   r   times        r   	pure_toner=   L   sK     <<BHf5;;=BD99Q[4'$.//r   kernel_sizestridec                    t        | j                        }|j                  d      }t        j                  t        ||      |z
  |z        dz   }|dz
  |z  |z   }t        j                  | d||z
  f      j                         }g }t        |j                               D ]"  }	|j                  |j                  |	             $ |j                  d      dk(  sJ d       ||dgz   }|j                  |||gz   |      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   popr:   ceilmaxr   r   
contiguousrangedimappendr?   
as_strided)
inputr>   r?   r   lengthn_frames
tgt_lengthpaddedstridesrG   s
             r   unfoldrP   Y   s    . EYYr]Fyy#fk2[@FJKaOHQ,&(;6JUU51j6123>>@FGVZZ\" +v}}S)*+;;r?a<!<<#GUh%<<gFFr   )constantr   )g:0yE>)      N)__doc__r:   typingtpr   torch.nnr   r   Tensorr   r8   strr(   r   r   r#   r-   r2   r=   rP   r"   r   r   <module>rZ      s       $cELL cX5<< X X3 XTY X	/U\\ 	/)ELL )C u E "5ell 5
0E 
0u 
0 
0!Gs !GC !Gr   