o
    pi                     @   sT   d dl Z d dlZd dlmZ d dl mZ ddlmZ ddlmZ G dd deZdS )	    N)Optional)Tensor   )BaseWaveformTransform)
ObjectDictc                       s   e Zd ZdZh dZdZdZdZdZ							dd	e	d
e
deje	 deje deje dee	 f fddZ				ddedee dee dee fddZ				ddedee dee dee def
ddZ  ZS )PeakNormalizationa  
    Apply a constant amount of gain, so that highest signal level present in each audio snippet
    in the batch becomes 0 dBFS, i.e. the loudest level allowed if all samples must be between
    -1 and 1.

    This transform has an alternative mode (apply_to="only_too_loud_sounds") where it only
    applies to audio snippets that have extreme values outside the [-1, 1] range. This is useful
    for avoiding digital clipping in audio that is too loud, while leaving other audio
    untouched.
    >   	per_batchper_channelper_exampleTFallr
         ?Nmodepp_modesample_ratetarget_rateoutput_typec                    s.   t  j||||||d |dv sJ || _d S )N)r   r   r   r   r   r   )r   only_too_loud_sounds)super__init__apply_to)selfr   r   r   r   r   r   r   	__class__ j/home/ubuntu/.local/lib/python3.10/site-packages/torch_audiomentations/augmentations/peak_normalization.pyr      s   

zPeakNormalization.__init__samplestargetsc                 C   s   t jt |dd\}}t j|dd\}}| jdkr"|dk| jd< n| jdkr/|dk| jd< ntd| jd  rJt || jd  d	| jd
< d S d S )N)dimr   g        selectorr   g      ?z8Unknown value of apply_to in PeakNormalization instance!)r      r!   divisors)torchmaxabsr   transform_parameters	Exceptionanyreshape)r   r   r   r   r   most_extreme_values_r   r   r   randomize_parameters3   s   



z&PeakNormalization.randomize_parametersreturnc                 C   s6   d| j v r|| j d   | j d   < t||||dS )Nr"   r    )r   r   r   r   )r&   r   )r   r   r   r   r   r   r   r   apply_transformP   s   
z!PeakNormalization.apply_transform)r   r
   r   NNNN)NNNN)__name__
__module____qualname____doc__supported_modessupports_multichannelrequires_sample_ratesupports_targetrequires_targetstrfloattypingr   intr   r   r,   r   r.   __classcell__r   r   r   r   r   
   sn    
r   )	r#   r:   r   r   core.transforms_interfacer   utils.object_dictr   r   r   r   r   r   <module>   s    