o
    Met,                     @   sp  d dl Z d dlmZ d dlmZ d dlZd dlmZ 	d-deeef dedeeef fd	d
Z	d-deeef dedeeef fddZ						d.de
dededededefddZd/de
de
dedefddZ						d0de
de
de
dedee dedeeef dedefddZ	 	!	"d1d#ed$ed%ed&ee def
d'd(Z	)	d2d*e
de
dee dedef
d+d,ZdS )3    N)Optional)Union)TensorFfreqhtkreturnc           
      C   s   |rt | trdtd| d   S dtd| d   S d}d}| | | }d}|| | }tdd }t | tr\|t| | d	 |  }| |k| j}	||	 |d
|	   }|S | |krm|t| | d	 |  }|S )a  Convert Hz to Mels.

    Args:
        freq (Union[Tensor, float]): The input tensor with arbitrary shape.
        htk (bool, optional): Use htk scaling. Defaults to False.

    Returns:
        Union[Tensor, float]: Frequency in mels.

    Examples:
        .. code-block:: python

            import paddle

            val = 3.0
            htk_flag = True
            mel_paddle_tensor = paddle.audio.functional.hz_to_mel(
                paddle.to_tensor(val), htk_flag)
         F@      ?     @        竪P@     @@皙@      ;@绽|=   )
isinstancer   paddlelog10mathlogastypedtype)
r   r   f_minf_spmels
min_log_hzmin_log_mellogsteptargetmask r!   RD:\Projects\ConvertPro\env\Lib\site-packages\paddle/audio/functional/functional.py	hz_to_mel   s0   


r#   melc           
      C   s   |rdd| d  d  S d}d}|||   }d}|| | }t dd	 }t| trI|t|| |   }| |k| j}	||	 |d
|	   }|S | |krX|t || |   }|S )a  Convert mel bin numbers to frequencies.

    Args:
        mel (Union[float, Tensor]): The mel frequency represented as a tensor with arbitrary shape.
        htk (bool, optional): Use htk scaling. Defaults to False.

    Returns:
        Union[float, Tensor]: Frequencies in Hz.

    Examples:
        .. code-block:: python

            import paddle

            val = 3.0
            htk_flag = True
            mel_paddle_tensor = paddle.audio.functional.mel_to_hz(
                paddle.to_tensor(val), htk_flag)

    r
         $@r   r	   r   r   r   r   r   r   )r   r   r   r   r   expr   r   )
r$   r   r   r   freqsr   r   r   r   r    r!   r!   r"   	mel_to_hzL   s$   
r(   @   r       @float32n_melsr   f_maxr   c           	      C   s:   t ||d}t ||d}tj||| |d}t||d}|S )a2  Compute mel frequencies.

    Args:
        n_mels (int, optional): Number of mel bins. Defaults to 64.
        f_min (float, optional): Minimum frequency in Hz. Defaults to 0.0.
        fmax (float, optional): Maximum frequency in Hz. Defaults to 11025.0.
        htk (bool, optional): Use htk scaling. Defaults to False.
        dtype (str, optional): The data type of the return frequencies. Defaults to 'float32'.

    Returns:
        Tensor: Tensor of n_mels frequencies in Hz with shape `(n_mels,)`.

    Examples:
        .. code-block:: python

            import paddle

            n_mels = 64
            f_min = 0.5
            f_max = 10000
            htk_flag = True

            paddle_mel_freq = paddle.audio.functional.mel_frequencies(
                n_mels, f_min, f_max, htk_flag, 'float64')
    )r   r   )r#   r   linspacer(   )	r,   r   r-   r   r   Zmin_melZmax_melr   r'   r!   r!   r"   mel_frequenciesw   s
   r0   srn_fftc                 C   s&   t jdt| d td|d  |dS )a  Compute fourier frequencies.

    Args:
        sr (int): Sample rate.
        n_fft (int): Number of fft bins.
        dtype (str, optional): The data type of the return frequencies. Defaults to 'float32'.

    Returns:
        Tensor: FFT frequencies in Hz with shape `(n_fft//2 + 1,)`.

    Examples:
        .. code-block:: python

            import paddle

            sr = 16000
            n_fft = 128
            fft_freq = paddle.audio.functional.fft_frequencies(sr, n_fft)
    r      r   r.   )r   r/   floatintr1   r2   r   r!   r!   r"   fft_frequencies   s   &r7   slaneynormc                 C   s>  |du r
t | d }tj|td|d  f|d}t| ||d}	t|d ||||d}
|
dd |
dd  }|
d|	d }t|D ]&}||  ||  }||d  ||d   }tt	|t
||||< qE|d	krd
|
d|d  |
d|   }||d9 }|S t|tst|t rtjjj||dd}|S )ae  Compute fbank matrix.

    Args:
        sr (int): Sample rate.
        n_fft (int): Number of fft bins.
        n_mels (int, optional): Number of mel bins. Defaults to 64.
        f_min (float, optional): Minimum frequency in Hz. Defaults to 0.0.
        f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
        htk (bool, optional): Use htk scaling. Defaults to False.
        norm (Union[str, float], optional): Type of normalization. Defaults to 'slaney'.
        dtype (str, optional): The data type of the return matrix. Defaults to 'float32'.

    Returns:
        Tensor: Mel transform matrix with shape `(n_mels, n_fft//2 + 1)`.

    Examples:
        .. code-block:: python

            import paddle

            n_mfcc = 23
            n_mels = 51
            paddle_dct = paddle.audio.functional.create_dct(n_mfcc, n_mels)
    Nr3   r   r.   r6   )r   r-   r   r   r   r8          @)pZaxis)r4   r   Zzerosr5   r7   r0   	unsqueezerangemaximumZ
zeros_likeminimumr   nnZ
functional	normalize)r1   r2   r,   r   r-   r   r9   r   weightsZfftfreqsZmel_fZfdiffZrampsilowerupperZenormr!   r!   r"   compute_fbank_matrix   s2   !

 rG   r	   r         T@spect	ref_valueamintop_dbc                 C   s   |dkrt d|dkrt dt| }dtt|| |  }|dtt|| 8 }|durF|dk r:t dt||| |  }|S )aX  Convert a power spectrogram (amplitude squared) to decibel (dB) units. The function computes the scaling `10 * log10(x / ref)` in a numerically stable way.

    Args:
        spect (Tensor): STFT power spectrogram.
        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): Minimum threshold. Defaults to 1e-10.
        top_db (Optional[float], optional): Threshold the output at `top_db` below the peak. Defaults to None.

    Returns:
        Tensor: Power spectrogram in db scale.

    Examples:
        .. code-block:: python

            import paddle

            val = 3.0
            decibel_paddle = paddle.audio.functional.power_to_db(
                paddle.to_tensor(val))
    r   zamin must be strictly positivez#ref_value must be strictly positiver%   Nztop_db must be non-negative)	Exceptionr   Z	ones_liker   r?   r   max)rI   rJ   rK   rL   ZonesZlog_specr!   r!   r"   power_to_db   s   
rO   orthon_mfccc                 C   s   t j||d}t j| |dd}t tjt| |d  | }|du r,|d9 }|jS |dks2J |d  dtd 9  < |tdt| 9 }|jS )	al  Create a discrete cosine transform(DCT) matrix.

    Args:
        n_mfcc (int): Number of mel frequency cepstral coefficients. 
        n_mels (int): Number of mel filterbanks.
        norm (Optional[str], optional): Normalizaiton type. Defaults to 'ortho'.
        dtype (str, optional): The data type of the return matrix. Defaults to 'float32'.

    Returns:
        Tensor: The DCT matrix with shape `(n_mels, n_mfcc)`.

    Examples:
        .. code-block:: python

            import paddle
            n_mfcc = 23
            n_mels = 257
            dct = paddle.audio.functional.create_dct(n_mfcc, n_mels)
    r.   r   g      ?Nr;   rP   r   r	   )	r   Zaranger=   cosr   pir4   sqrtT)rQ   r,   r9   r   nkdctr!   r!   r"   
create_dct&  s   rY   )F)r)   r   r*   Fr+   )r+   )r)   r   NFr8   r+   )r	   r   rH   )rP   r+   )r   typingr   r   r   r   r4   boolr#   r(   r5   strr0   r7   rG   rO   rY   r!   r!   r!   r"   <module>   s   

6

+
&

I
,