
    mi                         d Z ddlmZmZ ddlZddlmZ ddlmZ ddl	m
Z
  G d d	ej                  j                        Z	 	 	 dd
ej                  dedee   deee      dededee   fdZy)zH
Decomposition of a signal over frequency bands in the waveform domain.
    )OptionalSequenceN   )mel_frequencies)LowPassFilters)simple_reprc                   x     e Zd ZdZ	 	 	 ddedee   deee      dededee   f fdZ	d	 Z
ed
        Zd Z xZS )
SplitBandsa  
    Decomposes a signal over the given frequency bands in the waveform domain using
    a cascade of low pass filters as implemented by `julius.lowpass.LowPassFilters`.
    You can either specify explicitely the frequency cutoffs, or just the number of bands,
    in which case the frequency cutoffs will be spread out evenly in mel scale.

    Args:
        sample_rate (float): Sample rate of the input signal in Hz.
        n_bands (int or None): number of bands, when not giving them explictely with `cutoffs`.
            In that case, the cutoff frequencies will be evenly spaced in mel-space.
        cutoffs (list[float] or None): list of frequency cutoffs in Hz.
        pad (bool): if True, appropriately pad the input with zero over the edge. If `stride=1`,
            the output will have the same length as the input.
        zeros (float): Number of zero crossings to keep. See `LowPassFilters` for more informations.
        fft (bool or None): See `LowPassFilters` for more info.

    ..note::
        The sum of all the bands will always be the input signal.

    ..warning::
        Unlike `julius.lowpass.LowPassFilters`, the cutoffs frequencies must be provided in Hz along
        with the sample rate.

    Shape:

        - Input: `[*, T]`
        - Output: `[B, *, T']`, with `T'=T` if `pad` is True.
            If `n_bands` was provided, `B = n_bands` otherwise `B = len(cutoffs) + 1`

    >>> bands = SplitBands(sample_rate=128, n_bands=10)
    >>> x = torch.randn(6, 4, 1024)
    >>> list(bands(x).shape)
    [10, 6, 4, 1024]
    sample_raten_bandscutoffspadzerosfftc                    t         |           |d u |d u z   dk7  rt        d      || _        || _        |t        |      nd | _        || _        || _        || _	        |8|t        d      |dk\  st        d| d      t        |dz   d|dz        dd }nt        |      d	|z  kD  rt        d
      t        |      dkD  r&t        |D cg c]  }||z  	 c}|||      | _        y d | _        y c c}w )Nr   z;You must provide either n_bands, or cutoffs, but not boths.z+You must provide one of n_bands or cutoffs.z&n_bands must be greater than one (got )r      g      ?z1A cutoff above sample_rate/2 does not make sense.)r   r   r   )super__init__
ValueErrorr   r   list_cutoffsr   r   r   r   maxlenr   lowpass)	selfr   r   r   r   r   r   c	__class__s	           F/var/www/stems/demucs_env/lib/python3.12/site-packages/julius/bands.pyr   zSplitBands.__init__2   s    	tO40A5Z[[&)0)<W$
? !NOOa< #I'RS!TUU%gk1kAoFqLG7|cK// !TUUw<!)*12Q[25cSDL
  DL	 3s   C0c                     | j                   |d    S | j                  |      }|d   }|g}|dd  D ]  }||z
  }|j                  |       |} |j                  ||z
         t        j                  |      S )Nr   r   )r   appendtorchstack)r   inputlowslowbandslow_and_bandbands          r    forwardzSplitBands.forwardQ   s    <<;||E"1g H 	L#%DLLC		 	US[!{{5!!    c                     | j                   | j                   S | j                  1| j                  j                  D cg c]  }|| j                  z   c}S g S c c}w )N)r   r   r   r   )r   r   s     r    r   zSplitBands.cutoffs`   sQ    ==$== \\%26,,2F2FGQA(((GGI Hs   Ac                 4    t        | d| j                  i      S )Nr   )	overrides)r   r   )r   s    r    __repr__zSplitBands.__repr__i   s    4It}}+EFFr,   NNT   N)__name__
__module____qualname____doc__floatr   intr   boolr   r+   propertyr   r0   __classcell__)r   s   @r    r
   r
      sy    !F EIHL9= E  HSM  "8E?3 AE  (0 >"  Gr,   r
   signalr   r   r   r   r   r   c           	      L     t        ||||||      j                  |       |       S )z
    Functional version of `SplitBands`, refer to this class for more information.

    >>> x = torch.randn(6, 4, 1024)
    >>> list(split_bands(x, sample_rate=64, cutoffs=[12, 24]).shape)
    [3, 6, 4, 1024]
    )r
   to)r<   r   r   r   r   r   r   s          r    split_bandsr?   m   s-     Q:k7GS%EHHPQWXXr,   r1   )r6   typingr   r   r#   corer   r   r   utilsr   nnModuler
   Tensorr7   r8   r9   r?    r,   r    <module>rG      s    &  ! # \G \G~ TXGK8<
Y 
Y5 
Y8C= 
Y!(5/2
Y@D
Y
Y'/~
Yr,   