o
    pi                     @   sX   d dl Z d dl m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)Tensor)Optional   )BaseWaveformTransform)#convert_decibels_to_amplitude_ratio)
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
de	dee
 dee de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 )Gaina  
    Multiply the audio by a random amplitude factor to reduce or increase the volume. This
    technique can help a model become somewhat invariant to the overall gain of the input audio.

    Warning: This transform can return samples outside the [-1, 1] range, which may lead to
    clipping or wrap distortion, depending on what you do with the audio in a later stage.
    See also https://en.wikipedia.org/wiki/Clipping_(audio)#Digital_clipping
    >   	per_batchper_channelper_exampleTF      2      @r         ?Nmin_gain_in_dbmax_gain_in_dbmodepp_modesample_ratetarget_rateoutput_typec	           	         s<   t  j||||||d || _|| _| j| jkrtdd S )N)r   r   r   r   r   r   z1max_gain_in_db must be higher than min_gain_in_db)super__init__r   r   
ValueError)	selfr   r   r   r   r   r   r   r   	__class__ \/home/ubuntu/.local/lib/python3.10/site-packages/torch_audiomentations/augmentations/gain.pyr      s   zGain.__init__samplestargetsc                 C   sh   t jjt j| jt j|jdt j| jt j|jddd}|d}t	|j
|fddd| jd< d S )N)dtypedeviceT)lowhighvalidate_argsr   )sample_shape   gain_factors)torchdistributionsUniformtensorr   float32r"   r   sizer   sample	unsqueezetransform_parameters)r   r   r   r    r   distributionselected_batch_sizer   r   r   randomize_parameters4   s    
	zGain.randomize_parametersreturnc                 C   s   t || jd  |||dS )Nr(   )r   r   r    r   )r   r1   )r   r   r   r    r   r   r   r   apply_transformM   s   zGain.apply_transform)r   r   r   r   NNNN)NNNN)__name__
__module____qualname____doc__supported_modessupports_multichannelrequires_sample_ratesupports_targetrequires_targetfloatstrr   intr   r   r4   r   r6   __classcell__r   r   r   r   r   
   sx    		
r   )r)   r   typingr   core.transforms_interfacer   	utils.dspr   utils.object_dictr   r   r   r   r   r   <module>   s    