o
    Si                     @   sZ   d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ eG dd dZdS )    N)	dataclass)LiteralOptionalTupleUnion)CutSet)resolve_seedc                   @   s   e Zd ZU dZeeeeef f ed< dZe	ed< dZ
eed< dZeed< dZeeed	 f ed
< dZeej ed< dZee ed< dZe	ed< dddZdedefddZdS )ClippingTransforma/  
    Applies clipping to each Cut in a CutSet with a given probability.

    The clipping is applied with a probability of ``p``. The gain_db is
    randomly sampled from the provided range if an interval is given,
    or the fixed value is used if a single float is provided.

    :param gain_db: A single value or an interval (tuple/list with two values).
        The amount of gain in decibels to apply before clipping.
        If an interval is provided, the value is sampled uniformly.
    :param hard: If True, apply hard clipping (sharp cutoff); otherwise, apply soft clipping (saturation).
    :param normalize: If True, normalize the input signal to 0 dBFS before applying clipping.
    :param p: The probability of applying clipping (default: 0.5).
    :param seed: Random seed for reproducibility (default: 42).
    :param rng: Optional random number generator (overrides seed if provided).
    :param oversampling: Optional integer factor for oversampling before clipping.
    :param preserve_id: Whether to preserve the original cut ID (default: False).
    gain_dbT	normalizeg      ?pp_hard*   )trng
randomizedseedNrng   oversamplingFpreserve_idreturnc                 C   s   t | jttfr*t| jdksJ d| j | j\}}||k s*J d| d| d| j  kr5dks>n J d| j | jd urL| jd urLtd| jd u r\t	
t| j| _d S d S )	Nr   z>Expected gain_db to be a tuple or a list with two values, got z"Expected min_gain < max_gain, got z >= r      z+Probability p must be between 0 and 1, got z-Either rng or seed must be provided, not both)
isinstancer
   tuplelistlenr   r   r   
ValueErrorrandomRandomr   )selfmin_gainmax_gain r"   Z/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/dataset/cut_transforms/clipping.py__post_init__'   s   


(
zClippingTransform.__post_init__cutsc           	      C   s   g }|D ]H}| j  | jkrG| j  | jkrd}nd}t| jttfr0| j\}}| j ||}n| j}|j	||| j
| j | jd}|| q|| qt|S )NTF)hardr
   r   affix_idr   )r   r   r   r   r   r
   r   r   uniformclip_amplituder   r   r   appendr   )	r   r%   saturated_cutscutr&   r    r!   r
   new_cutr"   r"   r#   __call__8   s(   
zClippingTransform.__call__)r   N)__name__
__module____qualname____doc__r   floatr   __annotations__r   boolr   r   r   intr   r   r   r   r   r   r   r$   r   r.   r"   r"   r"   r#   r	   	   s   
 
r	   )r   dataclassesr   typingr   r   r   r   lhotser   lhotse.dataset.dataloadingr   r	   r"   r"   r"   r#   <module>   s    