
    i)                     ~    d dl Z d dlmZmZ d dlmZmZ ddlmZm	Z	m
Z
  G d de j                  j                        Zy)    N)conv1dconv2d)UnionOptional   )linspacetemperature_sigmoid	amp_to_dbc                   "    e Zd ZdZ ej
                         	 	 	 	 	 	 	 	 	 	 	 ddedededededed	ed
ededededef fd       Z	 ej
                         de
ej                  df   fd       Z ej
                         	 ddej                  deej                     dej                  fd       Z ej
                         dej                  dej                  fd       Z	 ddej                  deej                     dej                  fdZ xZS )	TorchGateaY  
    A PyTorch module that applies a spectral gate to an input signal.

    Arguments:
        sr {int} -- Sample rate of the input signal.
        nonstationary {bool} -- Whether to use non-stationary or stationary masking (default: {False}).
        n_std_thresh_stationary {float} -- Number of standard deviations above mean to threshold noise for
                                           stationary masking (default: {1.5}).
        n_thresh_nonstationary {float} -- Number of multiplies above smoothed magnitude spectrogram. for
                                        non-stationary masking (default: {1.3}).
        temp_coeff_nonstationary {float} -- Temperature coefficient for non-stationary masking (default: {0.1}).
        n_movemean_nonstationary {int} -- Number of samples for moving average smoothing in non-stationary masking
                                          (default: {20}).
        prop_decrease {float} -- Proportion to decrease signal by where the mask is zero (default: {1.0}).
        n_fft {int} -- Size of FFT for STFT (default: {1024}).
        win_length {[int]} -- Window length for STFT. If None, defaults to `n_fft` (default: {None}).
        hop_length {[int]} -- Hop length for STFT. If None, defaults to `win_length` // 4 (default: {None}).
        freq_mask_smooth_hz {float} -- Frequency smoothing width for mask (in Hz). If None, no smoothing is applied
                                     (default: {500}).
        time_mask_smooth_ms {float} -- Time smoothing width for mask (in ms). If None, no smoothing is applied
                                     (default: {50}).
    Nsrnonstationaryn_std_thresh_stationaryn_thresh_nonstationarytemp_coeff_nonstationaryn_movemean_nonstationaryprop_decreasen_fft
win_length
hop_lengthfreq_mask_smooth_hztime_mask_smooth_msc                 h   t         |           || _        || _        d|cxk  rdk  sJ  J || _        || _        |	| j
                  n|	| _        |
| j                  dz  n|
| _        || _        || _	        || _
        || _        || _        || _        | j                  d| j                                y )Ng              ?   smoothing_filter)super__init__r   r   r   r   r   r   r   r   r   r   r   r   register_buffer_generate_mask_smoothing_filter)selfr   r   r   r   r   r   r   r   r   r   r   r   	__class__s                ]/var/www/html/dev/engine/venv/lib/python3.12/site-packages/noisereduce/torchgate/torchgate.pyr   zTorchGate.__init__   s      	 *m*s****** 
(2(:$**
2<2D$//Q.* (?$ )A%(@%&<# $7 #6 /1U1U1WX    returnc           	         | j                   | j                  y| j                   dn1t        | j                   | j                  | j                  dz  z  z        }|dk  r2t        dt        | j                  | j                  dz  z         d      | j                  dn1t        | j                  | j                  | j                  z  dz  z        }|dk  r2t        dt        | j                  | j                  z  dz         d      |dk(  r|dk(  ryt        j                  t        d	d|dz   d
      t        dd	|dz         g      dd }t        j                  t        d	d|dz   d
      t        dd	|dz         g      dd }t        j                  ||      j                  d	      j                  d	      }||j                         z  S )a  
        A PyTorch module that applies a spectral gate to an input signal using the STFT.

        Returns:
            smoothing_filter (torch.Tensor): a 2D tensor representing the smoothing filter,
            with shape (n_grad_freq, n_grad_time), where n_grad_freq is the number of frequency
            bins to smooth and n_grad_time is the number of time frames to smooth.
            If both self.freq_mask_smooth_hz and self.time_mask_smooth_ms are None, returns None.
        Nr      z)freq_mask_smooth_hz needs to be at least z Hzi  z)time_mask_smooth_ms needs to be at least z msr   F)endpoint)r   r   intr   r   
ValueError_n_fftr   torchcatr   outer	unsqueezesum)r!   n_grad_freqn_grad_timev_fv_tr   s         r#   r    z)TorchGate._generate_mask_smoothing_filterI   s    ##+0H0H0P ''/ T--DJJN1KLM 	
 ?;CDKKZ[OA\<^;__bc  ''/ T--$//DGG2Kt1STU 	
 ?;CSWSZSZAZ^b@b<c;ddgh  !q 0iiA{Q?A{Q/

 B iiA{Q?A{Q/

 B !;;sC0::1=GGJ"2"6"6"888r$   X_dbxnc                    |t        j                  || j                  | j                  | j                  dddt        j
                  | j                        j                  |j                              }t        |      j                  |j                        }n|}t        j                  |d      \  }}||| j                  z  z   }t        j                  ||j                  d            }|S )a  
        Computes a stationary binary mask to filter out noise in a log-magnitude spectrogram.

        Arguments:
            X_db (torch.Tensor): 2D tensor of shape (frames, freq_bins) containing the log-magnitude spectrogram.
            xn (torch.Tensor): 1D tensor containing the audio signal corresponding to X_db.

        Returns:
            sig_mask (torch.Tensor): Binary mask of the same shape as X_db, where values greater than the threshold
            are set to 1, and the rest are set to 0.
        Tconstantr   r   r   return_complexpad_modecenterwindowdtyper)   )dimr'   )r-   stftr   r   r   hann_windowtodevicer
   r@   std_meanr   gtr0   )	r!   r6   r7   XNXN_dbstd_freq_noisemean_freq_noisenoise_threshsig_masks	            r#   _stationary_maskzTorchGate._stationary_mask~   s     >jj????##((9<<RYYG	B bM$$4::$6EE +0..B*G' '$:V:V)VV 88D,"8"8";<r$   X_absc                    t        |j                  dd|j                  d         t        j                  | j
                  |j                  |j                        j                  ddd      d      j                  |j                        | j
                  z  }||z
  |z  }t        || j                  | j                        }|S )a  
        Computes a non-stationary binary mask to filter out noise in a log-magnitude spectrogram.

        Arguments:
            X_abs (torch.Tensor): 2D tensor of shape (frames, freq_bins) containing the magnitude spectrogram.

        Returns:
            sig_mask (torch.Tensor): Binary mask of the same shape as X_abs, where values greater than the threshold
            are set to 1, and the rest are set to 0.
        r)   r   )r@   rE   samepadding)r   reshapeshaper-   onesr   r@   rE   viewr	   r   r   )r!   rO   
X_smoothedslowness_ratiorM   s        r#   _nonstationary_maskzTorchGate._nonstationary_mask   s     b!U[[_5

11++ << $q!R. d5;;++	, 	  *,
:&D779V9V
 r$   xc                    |j                   dk(  sJ |j                  d   | j                  dz  k  rt        d| j                  dz         | |j                   dk(  s|j                   dk(  sJ |:|j                  d   | j                  dz  k  rt        d| j                  dz         t	        j
                  || j                  | j                  | j                  dddt	        j                  | j                        j                  |j                              }| j                  r | j                  |j                               }n| j                  t        |      |      }| j                   |d	z  d	z
  z  d	z   }| j"                  @t%        |j'                  d      | j"                  j                  |j(                        d
      }||j+                  d      z  }t	        j,                  || j                  | j                  | j                  dt	        j                  | j                        j                  |j                              }|j                  |j(                        S )a  
        Apply the proposed algorithm to the input signal.

        Arguments:
            x (torch.Tensor): The input audio signal, with shape (batch_size, signal_length).
            xn (Optional[torch.Tensor]): The noise signal used for stationary noise reduction. If `None`, the input
                                         signal is used as the noise signal. Default: `None`.

        Returns:
            torch.Tensor: The denoised audio signal, with the same shape as the input signal.
        r'   r)   zx must be bigger than r   zxn must be bigger than Tr9   r:   r   rQ   rR   )r   r   r   r=   r>   r?   )ndimrU   r   	Exceptionr-   rB   r   r   rC   rD   rE   r   rZ   absrN   r
   r   r   r   r0   r@   squeezeistft)r!   r[   r7   XrM   Yys          r#   forwardzTorchGate.forward   s    vv{{772;1,,4T__q5H4IJKKzRWW\RWW\99>bhhrlT__q-@@5doo6I5JKLL JJ**$$T__588B	
 //8H,,Yq\2>H %%C#)=>D   ,""1%%%((8H   ## KK**$$T__588B
 tt!''t""r$   )Fg      ?g?g?   r   i   NNi  2   )N)__name__
__module____qualname____doc__r-   no_gradr*   boolfloatr   r   Tensorr    r   rN   rZ   re   __classcell__)r"   s   @r#   r   r      s   . U]]_ $),(+*-(*"%(%''Y'Y 'Y "'	'Y
 !&'Y #('Y #&'Y 'Y 'Y 'Y 'Y #'Y #'Y 'YR U]]_29u||T7I1J 29 29h U]]_?C&LL&&.u||&<&	& &P U]]_ %,,  B =A@#@##+ELL#9@#	@#r$   r   )r-   torch.nn.functionalr   r   typingr   r   utilsr   r	   r
   nnModuler    r$   r#   <module>rw      s*     . " ; ;A# A#r$   