o
    }oi<                     @   sL   d dl Zd dlmZ dejdedejdejfddZed	d	d
dd ZdS )    N)jitDratephi_advancescale_bufferc                 C   sz   t jd| jd |t jd}t j| jd t|f| jdd}t | dddf }t j| ddgd	d
} t	| |||||}|S )a  
    Optimized implementation of phase vocoder from Librosa.
    Reference implementation:
        - https://librosa.github.io/librosa/generated/librosa.core.phase_vocoder.html
    Args:
        D: Complex spectograms of shape [d, t, complex=2].
        rate: Speed rate, must be float greater than 0.
        phi_advance: Precomputed phase advance buffer array of length [n_fft + 1]
        scale_buffer: Precomputed numpy buffer array of length [n_fft + 1]
    Returns:
        Complex64 ndarray of shape [d, t / rate, complex=2]
    r      )dtypeF)orderN)r   r   )r      constant)mode)
nparangeshapefloat64zeroslenr   anglepad_phase_vocoder_kernel)r   r   r   r   
time_steps	d_stretch	phase_acc r   `/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/utils/numba_utils.pyphase_vocoder   s    r   T)nopythonnogilc                 C   s   dt j }t|D ]l\}}| ddt|t|d f }	|	dddf }
|	dddf }t |d}d| t |
 |t |  }|t d|  |dd|f< t |t |
 | }|| }t |d| |||  }||| 7 }q	|S )a  
    Numba optimized kernel to compute the phase vocoder step.
    Args:
        D: Complex spectograms of shape [d, t, complex=2].
        rate: Speed rate, must be float greater than 0.
        time_steps: Numpy ndarray of linearly spaced time steps, shape = [t]
        phi_advance: Precomputed phase advance buffer array of length [n_fft + 1]
        d_stretch: Output complex matrix of shape [d, t / rate, complex=2]
        phase_acc: Phase accumulator initialized to first sample of shape [d, complex=2]
        scale_buffer: Precomputed numpy buffer array of length [n_fft + 1]
    Returns:
        Complex64 ndarray of shape [d, t / rate, complex=2]
    g       @Nr   r   r   g      ?y              ?)	r   pi	enumerateintmodabsexpr   round)r   r   r   r   r   r   two_pitstepcolumns	columns_0	columns_1alphamagdphasescaler   r   r   r   /   s   
  r   )numpyr   numbar   ndarrayfloatr   r   r   r   r   r   <module>   s
    
