
      i                     ^    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	e      Z
y)
    N)Tensor)Optional   )BaseWaveformTransform)#convert_decibels_to_amplitude_ratio)
ObjectDictc                        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Z	 	 	 	 dded
ee   dee   dee   fdZ	 	 	 	 dded
ee   dee   dee   def
dZ x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min_gain_in_dbmax_gain_in_dbmodepp_modesample_ratetarget_rateoutput_typec	                     t         	|   ||||||       || _        || _        | j                  | j                  k\  rt	        d      y )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__s
            }/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torch_audiomentations/augmentations/gain.pyr   zGain.__init__   s`     	### 	 	
 -,$"5"55PQQ 6    samplestargetsc                    t         j                  j                  t        j                  | j                  t         j
                  |j                        t        j                  | j                  t         j
                  |j                        d      }|j                  d      }t        |j                  |f            j                  d      j                  d      | j                  d<   y )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_sizes          r   randomize_parameterszGain.randomize_parameters4   s     **22##5== ##5==  3 
 &ll1o/##2E1G#H Yq\Yq\ 	!!.1r   returnc                 @    t        || j                  d   z  |||      S )Nr(   )r   r   r   r   )r   r1   )r   r   r   r   r   s        r   apply_transformzGain.apply_transformM   s-     d77GG##	
 	
r   )g      2g      @r   g      ?NNNN)NNNN)__name__
__module____qualname____doc__supported_modessupports_multichannelrequires_sample_ratesupports_targetrequires_targetfloatstrr   intr   r   r4   r   r7   __classcell__)r   s   @r   r
   r
   
   sB    BO  OO !& #! $%)%)%)RR R 	R
 R R c]R c]R c]R4 %)$(%)

 c]
 &!	

 c]
6 %)$(%)

 c]
 &!	

 c]
 

r   r
   )r)   r   typingr   core.transforms_interfacer   	utils.dspr   utils.object_dictr   r
    r   r   <module>rJ      s&       = ; *O
  O
r   