
    ͑iwH                    D   % S SK Jr  S SKJr  S SKJrJr  S SKJr  S SK	r	S SK	J
r
  SSKJrJrJr  SS	KJr  \(       a  S S
K	Jr  \S   rS\S'    " S S\
R(                  5      r " S S\
R(                  5      r " S S\
R(                  5      r " S S\
R(                  5      rg)    )annotations)partial)TYPE_CHECKINGLiteral)	TypeAliasN)nn   )compute_fbank_matrix
create_dctpower_to_db)
get_window)Tensor)hamminghannkaiserbartlettnuttallgaussianexponentialtriangbohmanblackmancosinetukeytaylorr   _WindowLiteralc                     ^  \ rS rSr% SrS\S'   S\S'           S
                 SU 4S jjjrSS jrS	rU =r	$ )Spectrogram/   aB  Compute spectrogram of given signals, typically audio waveforms.
The spectrogram is defined as the complex norm of the short-time Fourier transformation.

Args:
    n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
    hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
    win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
    window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor', 'bartlett', 'kaiser', 'nuttall'. Defaults to 'hann'.
    power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
    center (bool, optional): Whether to pad `x` to make that the :math:`t   imes hop\_length` at the center of `t`-th frame. Defaults to True.
    pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
    dtype (str, optional): Data type of input and window. Defaults to 'float32'.

Returns:
    :ref:`api_paddle_nn_Layer`. An instance of Spectrogram.



Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.audio.features import Spectrogram

        >>> sample_rate = 16000
        >>> wav_duration = 0.5
        >>> num_channels = 1
        >>> num_frames = sample_rate * wav_duration
        >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
        >>> waveform = wav_data.tile([num_channels, 1])

        >>> feature_extractor = Spectrogram(n_fft=512, window = 'hann', power = 1.0)
        >>> feats = feature_extractor(waveform)
floatpowerr   
fft_windowc	           
       > [         T	U ]  5         US:  d   S5       eXPl        Uc  Un[        XCSUS9U l        [        [        R                  R                  UUUU R                  UUS9U l	        U R                  SU R                  5        g )Nr   z!Power of spectrogram must be > 0.T)fftbinsdtype)n_fft
hop_length
win_lengthwindowcenterpad_moder"   )super__init__r!   r   r"   r   paddlesignalstft_stftregister_buffer)
selfr&   r'   r(   r)   r!   r*   r+   r%   	__class__s
            \/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/audio/features/layers.pyr-   Spectrogram.__init__V   s     	qy===y
J$E
 MM!!??

 	\4??;    c                    U R                  U5      n[        R                  " [        R                  " U5      U R                  5      nU$ )z
Args:
    x (Tensor): Tensor of waveforms with shape `(N, T)`

Returns:
    Tensor: Spectrograms with shape `(N, n_fft//2 + 1, num_frames)`.
)r1   r.   powabsr!   )r3   xr0   spectrograms       r5   forwardSpectrogram.forwardw   s4     zz!}jjD!14::>r7   )r1   r"   r!   )   r?   Nr         ?Treflectfloat32)r&   intr'   
int | Noner(   rD   r)   r   r!   r    r*   boolr+   Literal['reflect']r%   strreturnNoner;   r   rH   r   
__name__
__module____qualname____firstlineno____doc____annotations__r-   r=   __static_attributes____classcell__r4   s   @r5   r   r   /   s    !F L !$!%!''0<< < 	<
 < < < %< < 
< <B
 
r7   r   c                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'                 S                             SU 4S jjjrSS jrSrU =r	$ )MelSpectrogram   a	  Compute the melspectrogram of given signals, typically audio waveforms. It is computed by multiplying spectrogram with Mel filter bank matrix.

Args:
    sr (int, optional): Sample rate. Defaults to 22050.
    n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
    hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
    win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
    window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor', 'bartlett', 'kaiser', 'nuttall'. Defaults to 'hann'.
    power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
    center (bool, optional): Whether to pad `x` to make that the :math:`t   imes hop\_length` at the center of `t`-th frame. Defaults to True.
    pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
    n_mels (int, optional): Number of mel bins. Defaults to 64.
    f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
    f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
    htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
    norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
    dtype (str, optional): Data type of input and window. Defaults to 'float32'.

Returns:
    :ref:`api_paddle_nn_Layer`. An instance of MelSpectrogram.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.audio.features import MelSpectrogram

        >>> sample_rate = 16000
        >>> wav_duration = 0.5
        >>> num_channels = 1
        >>> num_frames = sample_rate * wav_duration
        >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
        >>> waveform = wav_data.tile([num_channels, 1])

        >>> feature_extractor = MelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
        >>> feats = feature_extractor(waveform)
rC   n_melsr    f_minf_maxrE   htkLiteral['slaney'] | floatnormr   fbank_matrixc                   > [         TU ]  5         [        UUUUUUUUS9U l        Xl        Xl        Xl        Xl        Xl        Uc  US-  n[        UUU	U
UUUUS9U l
        U R                  SU R                  5        g )N)r&   r'   r(   r)   r!   r*   r+   r%   r	   )srr&   rX   rY   rZ   r[   r]   r%   r^   )r,   r-   r   _spectrogramrX   rY   rZ   r[   r]   r
   r^   r2   )r3   r`   r&   r'   r(   r)   r!   r*   r+   rX   rY   rZ   r[   r]   r%   r4   s                  r5   r-   MelSpectrogram.__init__   s    " 	'!!	
 

	=!GE0	
 	^T->->?r7   c                j    U R                  U5      n[        R                  " U R                  U5      nU$ )z
Args:
    x (Tensor): Tensor of waveforms with shape `(N, T)`

Returns:
    Tensor: Mel spectrograms with shape `(N, n_mels, num_frames)`.
)ra   r.   matmulr^   )r3   r;   spect_featuremel_features       r5   r=   MelSpectrogram.forward   s0     ))!,mmD$5$5}Er7   )ra   rZ   rY   r^   r[   rX   r]   )"V  i   r?   Nr          @TrA   @         I@NFslaneyrB   )r`   rC   r&   rC   r'   rD   r(   rD   r)   r   r!   r    r*   rE   r+   rF   rX   rC   rY   r    rZ   float | Noner[   rE   r]   r\   r%   rG   rH   rI   rJ   rK   rT   s   @r5   rV   rV      s   $L KLL	I
## !$!%!''0"*2.@.@ .@ 	.@
 .@ .@ .@ .@ %.@ .@ .@ .@ .@ (.@ .@  
!.@ .@`
 
r7   rV   c                     ^  \ rS rSr% SrS\S'   S\S'   S\S'                    S                                   SU 4S jjjrSS	 jrS
rU =r	$ )LogMelSpectrogram   a
  Compute log-mel-spectrogram feature of given signals, typically audio waveforms.

Args:
    sr (int, optional): Sample rate. Defaults to 22050.
    n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
    hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
    win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
    window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor', 'bartlett', 'kaiser', 'nuttall'. Defaults to 'hann'.
    power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
    center (bool, optional): Whether to pad `x` to make that the :math:`t   imes hop\_length` at the center of `t`-th frame. Defaults to True.
    pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
    n_mels (int, optional): Number of mel bins. Defaults to 64.
    f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
    f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
    htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
    norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
    ref_value (float, optional): The reference value. If smaller than 1.0, the db level of the signal will be pulled up accordingly. Otherwise, the db level is pushed down. Defaults to 1.0.
    amin (float, optional): The minimum value of input magnitude. Defaults to 1e-10.
    top_db (Optional[float], optional): The maximum db value of spectrogram. Defaults to None.
    dtype (str, optional): Data type of input and window. Defaults to 'float32'.

Returns:
    :ref:`api_paddle_nn_Layer`. An instance of LogMelSpectrogram.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.audio.features import LogMelSpectrogram

        >>> sample_rate = 16000
        >>> wav_duration = 0.5
        >>> num_channels = 1
        >>> num_frames = sample_rate * wav_duration
        >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
        >>> waveform = wav_data.tile([num_channels, 1])

        >>> feature_extractor = LogMelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
        >>> feats = feature_extractor(waveform)
r    	ref_valueaminrm   top_dbc                ~   > [         TU ]  5         [        UUUUUUUUU	U
UUUUS9U l        Xl        Xl        UU l        g )N)r`   r&   r'   r(   r)   r!   r*   r+   rX   rY   rZ   r[   r]   r%   )r,   r-   rV   _melspectrogramrq   rr   rs   )r3   r`   r&   r'   r(   r)   r!   r*   r+   rX   rY   rZ   r[   r]   rq   rr   rs   r%   r4   s                     r5   r-   LogMelSpectrogram.__init__  sZ    ( 	-!! 
" #	r7   c                |    U R                  U5      n[        UU R                  U R                  U R                  S9nU$ )z
Args:
    x (Tensor): Tensor of waveforms with shape `(N, T)`

Returns:
    Tensor: Log mel spectrograms with shape `(N, n_mels, num_frames)`.
)rq   rr   rs   )ru   r   rq   rr   rs   )r3   r;   rf   log_mel_features       r5   r=   LogMelSpectrogram.forwardH  s>     **1-%nn;;	
 r7   )ru   rr   rq   rs   )rh   r?   NNr   ri   TrA   rj   rk   NFrl   r@   绽|=NrB   )$r`   rC   r&   rC   r'   rD   r(   rD   r)   r   r!   r    r*   rE   r+   rF   rX   rC   rY   r    rZ   rm   r[   rE   r]   r\   rq   r    rr   r    rs   rm   r%   rG   rH   rI   rJ   rK   rT   s   @r5   ro   ro      s   'R 
K !%!%!''0"*2#%)) ) 	)
 ) ) ) ) %) ) ) ) ) () )  !)" #)$ %)& 
') )V r7   ro   c                     ^  \ rS rSr% SrS\S'                     S                                     S	U 4S jjjrS
S jrSrU =r	$ )MFCCiZ  a
  Compute mel frequency cepstral coefficients(MFCCs) feature of given waveforms.

Args:
    sr (int, optional): Sample rate. Defaults to 22050.
    n_mfcc (int, optional): [description]. Defaults to 40.
    n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
    hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
    win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
    window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor', 'bartlett', 'kaiser', 'nuttall'. Defaults to 'hann'.
    power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
    center (bool, optional): Whether to pad `x` to make that the :math:`t   imes hop\_length` at the center of `t`-th frame. Defaults to True.
    pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
    n_mels (int, optional): Number of mel bins. Defaults to 64.
    f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
    f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
    htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
    norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
    ref_value (float, optional): The reference value. If smaller than 1.0, the db level of the signal will be pulled up accordingly. Otherwise, the db level is pushed down. Defaults to 1.0.
    amin (float, optional): The minimum value of input magnitude. Defaults to 1e-10.
    top_db (Optional[float], optional): The maximum db value of spectrogram. Defaults to None.
    dtype (str, optional): Data type of input and window. Defaults to 'float32'.

Returns:
    :ref:`api_paddle_nn_Layer`. An instance of MFCC.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.audio.features import MFCC

        >>> sample_rate = 16000
        >>> wav_duration = 0.5
        >>> num_channels = 1
        >>> num_frames = sample_rate * wav_duration
        >>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
        >>> waveform = wav_data.tile([num_channels, 1])

        >>> feature_extractor = MFCC(sr=sample_rate, n_fft=512, window = 'hann')
        >>> feats = feature_extractor(waveform)
r   
dct_matrixc                  > [         TU ]  5         X*::  d   SU SU
 35       e[        S0 SU_SU_SU_SU_SU_SU_S	U_S
U	_SU
_SU_SU_SU_SU_SU_SU_SU_SU_6U l        [	        X*US9U l        U R                  SU R
                  5        g )Nz%n_mfcc cannot be larger than n_mels: z vs r`   r&   r'   r(   r)   r!   r*   r+   rX   rY   rZ   r[   r]   rq   rr   rs   r%   )n_mfccrX   r%   r}    )r,   r-   ro   _log_melspectrogramr   r}   r2   )r3   r`   r   r&   r'   r(   r)   r!   r*   r+   rX   rY   rZ   r[   r]   rq   rr   rs   r%   r4   s                      r5   r-   MFCC.__init__  s   * 	 	
3F84xH	
 $5 $
$
$
 "$
 "	$

 $
 $
 $
 $
 $
 $
 $
 $
 $
  $
 $
  !$
" #$
 & %FO\4??;r7   c                    U R                  U5      n[        R                  " UR                  S5      U R                  5      R                  S5      nU$ )z
Args:
    x (Tensor): Tensor of waveforms with shape `(N, T)`

Returns:
    Tensor: Mel frequency cepstral coefficients with shape `(N, n_mfcc, num_frames)`.
)r   r	      )r   r.   rd   	transposer}   )r3   r;   rx   mfccs       r5   r=   MFCC.forward  sJ     2215}}%%i0$//

)I
 	 r7   )r   r}   )rh   (   r?   NNr   ri   TrA   rj   rk   NFrl   r@   rz   NrB   )&r`   rC   r   rC   r&   rC   r'   rD   r(   rD   r)   r   r!   r    r*   rE   r+   rF   rX   rC   rY   r    rZ   rm   r[   rE   r]   r\   rq   r    rr   r    rs   rm   r%   rG   rH   rI   rJ   rK   rT   s   @r5   r|   r|   Z  s   (T  !%!%!''0"*2#'-<-< -< 	-<
 -< -< -< -< -< %-< -< -< -< -< (-<  !-<" #-<$ %-<& '-<( 
)-< -<^ r7   r|   )
__future__r   	functoolsr   typingr   r   typing_extensionsr   r.   r   
functionalr
   r   r   functional.windowr   r   r   rQ   Layerr   rV   ro   r|   r   r7   r5   <module>r      s    #  ) '   F F *#	 "R"(( RjhRXX hVh hVh288 hr7   