o
    æS™i‘  ã                   @   sD   d dl mZ d dlmZ d dlZd dlmZ eG dd„ deƒƒZdS )é    )Ú	dataclass)ÚOptionalN)ÚAudioTransformc                   @   sX   e Zd ZU dZdZeed< dZeed< dZ	eed< de
jd	ed
e
jfdd„Zdd„ ZdS )ÚClippingaO  
    Apply clipping to audio signal.

    This augmentation simulates the effect of clipping that occurs when audio levels exceed
    the dynamic range of recording equipment or when audio is amplified beyond its limits.

    Clips input signal to [-1, 1] range.

    :param gain_db: The amount of gain in decibels to apply before clipping (and to revert back to original level after).
    :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.
    FÚhardg        Úgain_dbTÚ	normalizeÚsamplesÚsampling_rateÚreturnc                 C   s°   t  t  |¡¡}|dksdt  |¡ dk r| ¡ S | jr || }t| jƒdkr2d| jd  }|| }| jr=t  |dd¡}nt  	|¡}t| jƒdkrM|| }| jrT|| }| ¡ S )	Nr   é   i ÿÿÿgš™™™™™¹?é
   g      4@g      ð¿g      ð?)
ÚnpÚmaxÚabsÚlog10Úcopyr   r   r   ÚclipÚtanh)Úselfr	   r
   Úmax_peak_amplitudeÚgain_linearÚsaturated_samples© r   úP/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/augmentation/clipping.pyÚ__call__   s    
zClipping.__call__c                 C   s   ||fS )zb
        Clipping doesn't change the timing of the audio, so timestamps remain unchanged.
        r   )r   ÚoffsetÚdurationr
   r   r   r   Úreverse_timestamps?   s   zClipping.reverse_timestampsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   Úfloatr   r   ÚndarrayÚintr   r   r   r   r   r   r   	   s   
 þý
ü#r   )	Údataclassesr   Útypingr   Únumpyr   Úlhotse.augmentation.transformr   r   r   r   r   r   Ú<module>   s    