o
    ]ôjÂ  ã                   @   s˜   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	„ 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d„ZdS )zH
Decomposition of a signal over frequency bands in the waveform domain.
é    )ÚOptionalÚSequenceNé   )Úmel_frequencies)ÚLowPassFilters)Úsimple_reprc                       sn   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d„Z	dd„ Z
edd„ ƒZdd„ Z‡  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]
    NTé   Úsample_rateÚn_bandsÚcutoffsÚpadÚzerosÚfftc                    sú   t ƒ  ¡  |d u |d u  dkrtdƒ‚ˆ | _|| _|d ur!t|ƒnd | _|| _|| _|| _	|d u rT|d u r9tdƒ‚|dksEtd|› d�ƒ‚t
|d dˆ d ƒdd… }nt|ƒd	ˆ  kr`td
ƒ‚t|ƒdkrxt‡ fdd„|D ƒ|||d�| _d S d | _d S )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.c                    s   g | ]}|ˆ  ‘qS © r   ©Ú.0Úc©r
   r   ú]/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/julius/bands.pyÚ
<listcomp>K   s    z'SplitBands.__init__.<locals>.<listcomp>)r   r   r   )ÚsuperÚ__init__Ú
ValueErrorr
   r   ÚlistÚ_cutoffsr   r   r   r   ÚmaxÚlenr   Úlowpass)Úselfr
   r   r   r   r   r   ©Ú	__class__r   r   r   2   s,   
ÿ
zSplitBands.__init__c                 C   sj   | j d u r	|d  S |   |¡}|d }|g}|dd … D ]}|| }| |¡ |}q| || ¡ t |¡S )Nr   r   )r!   ÚappendÚtorchÚstack)r"   ÚinputZlowsÚlowÚbandsZlow_and_bandZbandr   r   r   ÚforwardQ   s   



zSplitBands.forwardc                    s4   ˆ j d urˆ j S ˆ jd ur‡ fdd„ˆ jjD ƒS g S )Nc                    s   g | ]}|ˆ j  ‘qS r   r   r   ©r"   r   r   r   e   s    z&SplitBands.cutoffs.<locals>.<listcomp>)r   r!   r   r,   r   r,   r   r   `   s
   

zSplitBands.cutoffsc                 C   s   t | d| jid�S )Nr   )Ú	overrides)r   r   r,   r   r   r   Ú__repr__i   s   zSplitBands.__repr__©NNTr	   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úfloatr   Úintr   Úboolr   r+   Úpropertyr   r.   Ú__classcell__r   r   r#   r   r      s$    #þ
ÿÿþþ
r   Tr	   Úsignalr
   r   r   r   r   r   c                 C   s   t ||||||ƒ | ¡| ƒ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)r9   r
   r   r   r   r   r   r   r   r   Úsplit_bandsm   s   
r;   r/   )r3   Útypingr   r   r&   Úcorer   r!   r   Úutilsr   ÚnnÚModuler   ÚTensorr4   r5   r6   r;   r   r   r   r   Ú<module>   s&   _þ
ÿÿþþ