o
    Siv                     @   s^   d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ G dd dZdS )    N)LiteralOptionalTupleUnion)CutSet)resolve_seed)Decibelsc                   @   s   e Zd ZdZ						ddedeeeeeef f  d	e	d
e
de
deeed ejf de
ddfddZdedefddZdd ZdS )CutMixz
    A transform for batches of cuts (CutSet's) that stochastically performs
    noise augmentation with a constant or varying SNR.
    
            ?TF*   cutssnrppad_to_longestpreserve_idseed)trng
randomizedrandom_mix_offsetreturnNc                 C   sL   || _ t| j dkrtd || _|| _|| _|| _|| _d| _	|| _
dS )a  
        CutMix's constructor.

        :param cuts: a ``CutSet`` containing augmentation data, e.g. noise, music, babble.
        :param snr: either a float, a pair (range) of floats, or ``None``.
            It determines the SNR of the speech signal vs the noise signal that's mixed into it.
            When a range is specified, we will uniformly sample SNR in that range.
            When it's ``None``, the noise will be mixed as-is -- i.e. without any level adjustment.
            Note that it's different from ``snr=0``, which will adjust the noise level so that the SNR is 0.
        :param pad_to_longest: when `True`, each processed :class:`CutSet` will be padded with noise
            to match the duration of the longest Cut in a batch.
        :param preserve_id: When ``True``, preserves the IDs the cuts had before augmentation.
            Otherwise, new random IDs are generated for the augmented cuts (default).
        :param seed: an optional int or "trng". Random seed for choosing the cuts to mix and the SNR.
            If "trng" is provided, we'll use the ``secrets`` module for non-deterministic results
            on each iteration. You can also directly pass a ``random.Random`` instance here.
        :param random_mix_offset: an optional bool.
            When ``True`` and the duration of the to be mixed in cut in longer than the original cut,
            select a random sub-region from the to be mixed in cut.
        r   zEEmpty CutSet in CutMix transform: it'll act as an identity transform.N)r   lenwarningswarnr   r   r   r   r   rngr   )selfr   r   r   r   r   r   r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/dataset/cut_transforms/mix.py__init__   s   
zCutMix.__init__c              	   C   sf   t | jdkr	|S |   | jrtdd |D nd }|j| j|| j| j| jr)dnd | j	| j
d S )Nr   c                 s   s    | ]}|j V  qd S N)duration).0cr   r   r   	<genexpr>D   s    z"CutMix.__call__.<locals>.<genexpr>left)r   r"   r   mix_probr   r   r   )r   r   _lazy_rng_initr   maxmixr   r   r   r   r   to_eager)r   r   maybe_max_durationr   r   r   __call__;   s    zCutMix.__call__c                 C   s>   | j d urd S t| jtjr| j| _ d S tt| j| _ d S r!   )r   
isinstancer   randomRandomr   )r   r   r   r   r(   P   s
   
zCutMix._lazy_rng_init)r
   r   TFr   F)__name__
__module____qualname____doc__r   r   r   r   r   floatboolintr   r/   r0   r    r-   r(   r   r   r   r   r	   
   s6    	
+r	   )r/   r   typingr   r   r   r   lhotser   lhotse.dataset.dataloadingr   lhotse.utilsr   r	   r   r   r   r   <module>   s    