o
    Sik                     @   sr   d dl Z d dlmZ d dlmZ d dlmZmZmZ G dd dZ	de
ded	e
fd
dZdeded	efddZdS )    N)Optional)CutSet)LOG_EPSILONSecondsexactly_one_not_nullc                   @   sh   e Zd ZdZdddedddfdee dee dee ded	e	d
e	de
ddfddZdedefddZdS )ExtraPaddinga0  
    A transform on batch of cuts (``CutSet``) that adds a number of
    extra context frames/samples/seconds on both sides of the cut.
    Exactly one type of duration has to specified in the constructor.

    It is intended mainly for training frame-synchronous ASR models
    with convolutional layers to avoid using padding inside of the
    hidden layers, by giving the model larger context in the input.
    Another useful application is to shift the input by a little,
    so that the data seen after frame subsampling is a bit different,
    which makes this a data augmentation technique.

    This is best used as the first transform in the transform list
    for dataset - it will ensure that each individual cut gets extra
    context before concatenation, or that it will be filled with noise, etc.
    NFbothextra_framesextra_samplesextra_secondspad_feat_value
randomizedpreserve_id	directionreturnc                 C   sR   t |||s
J d|dv sJ d|| _|| _|| _|| _|| _|| _|| _dS )a  
        ExtraPadding's constructor.

        :param extra_frames: The total number of frames to add to each cut.
            We will add half that number on each side of the cut ("both" directions padding).
        :param extra_samples: The total number of samples to add to each cut.
            We will add half that number on each side of the cut ("both" directions padding).
        :param extra_seconds: The total duration in seconds to add to each cut.
            We will add half that number on each side of the cut ("both" directions padding).
        :param pad_feat_value: When padding a cut with precomputed features, what
            value should be used for padding (the default is a very low log-energy).
        :param randomized: When ``True``, we will sample a value from a uniform distribution of
            ``[0, extra_X]`` for each cut (for samples/frames -- sample an int,
            for duration -- sample a float).
        :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 direction: The padding direction.
        zSFor ExtraPadding, you have to specify exactly one of: frames, samples, or duration.)r   leftrightz&Only three padding modes are supportedN)r   r	   r
   r   r   r   r   r   )selfr	   r
   r   r   r   r   r    r   _/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/dataset/cut_transforms/extra_padding.py__init__   s   

zExtraPadding.__init__cutsc                    sn    j d urt fdd|D S  jd ur"t fdd|D S  jd ur3t fdd|D S td)Nc                 3   :    | ]}|j |jt j jd   j j jdV  qdS )valuesample)
num_framesr   r   r   N)padr   maybe_sample_intr	   r   r   r   r   .0cr   r   r   	<genexpr>H   s    
z(ExtraPadding.__call__.<locals>.<genexpr>c                 3   s6    | ]}|j |jt j jd   j jdV  qdS )r   )num_samplesr   r   N)r   r$   r   r
   r   r   r   r   r"   r   r   r#   S   s    	
c                 3   r   )r   )durationr   r   r   N)r   r%   maybe_sample_floatr   r   r   r   r   r   r"   r   r   r#   _   s    
z@Implementation error in ExtraPadding (please report this issue).)r	   r   	from_cutsr
   r   
ValueError)r   r   r   r"   r   __call__F   s   


	
zExtraPadding.__call__)__name__
__module____qualname____doc__r   r   intr   floatboolstrr   r   r)   r   r   r   r   r      s6    	
,r   r   r   r   c                 C      |rt d| } | S Nr   )randomrandintr   r   r   r   r   q      r   c                 C   r2   r3   )r4   uniformr   r   r   r   r&   w   r6   r&   )r4   typingr   lhotser   lhotse.utilsr   r   r   r   r.   r0   r   r/   r&   r   r   r   r   <module>   s    i