U
    9%e                     @   s   d Z ddlZ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mZ dd	lmZm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 ddlmZ ddlmZmZmZm Z  ddl!m"Z"m#Z#m$Z$m%Z% ddddddgZ&eddddddddddd d!d"d#d$dd%ej'e(e)ee$ e)e)ee( e(ee( e(e"e*e#ee+ ee ej'd&d'dZ,eddddddddddd d!d"d#d$dd%ej'e(e)ee$ e)e)ee( e(ee( e(e"e*e#e+ee ej'd&d(dZ-eddddddddddd d!d"d#dd)ej'e(e)ee$ e)e)ee( e(ee( e(e"e*e#ee ej'd*d+dZ.ed,ddddddddd d!d"dd$dd-ej'e(e)ee$ e)e(e(ee( e(e"e*ee) e+ee ej'd.d/dZ/eddddddd0dddddd d!d"d#d$dd1ej'e(e)ee$ e)ee+ee( f ee( e)ee( e(ee( e(e"e*e#ee+ ee ej'd2d3dZ0ed4ddd!dej1dfd5d6Z2e ej' e)eej'd7d8d9Z3dMd;d<Z4d=d> Z5d?d@ Z6ed"d"dAdBdC Z7dDdddddddd d!d"d#d$dddEdFddGej'e)e(e)ee$ e)e(e(ee( e(e"e*e#e+ee ee) e(ee+ eee)ej8j9ej8j:f  ej'dHdIdZ;e)ej'dJdKdLZ<dS )NzConstant-Q transforms    N)jit   )audio)interval_frequencies)
get_fftlib)cqt_frequencies
note_to_hz)stftistft)estimate_tuning   )cache)filters)util)ParameterError)	DTypeLike)OptionalUnion
CollectionList)_WindowSpec_PadMode_FloatLike_co_ensure_not_reachablecqt
hybrid_cqt
pseudo_cqticqtgriffinlim_cqtvqt   )leveli"V  i   T              g{Gz?hannTZconstantsoxr_hq)sr
hop_lengthfminn_binsbins_per_octavetuningfilter_scalenormsparsitywindowscalepad_moderes_typedtype)yr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   returnc                C   s*   t | ||||dd|||||	|
||||dS )a  Compute the constant-Q transform of an audio signal.

    This implementation is based on the recursive sub-sampling method
    described by [#]_.

    .. [#] Schoerkhuber, Christian, and Anssi Klapuri.
        "Constant-Q transform toolbox for music processing."
        7th Sound and Music Computing Conference, Barcelona, Spain. 2010.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)]
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    hop_length : int > 0 [scalar]
        number of samples between successive CQT columns.

    fmin : float > 0 [scalar]
        Minimum frequency. Defaults to `C1 ~= 32.70 Hz`

    n_bins : int > 0 [scalar]
        Number of frequency bins, starting at ``fmin``

    bins_per_octave : int > 0 [scalar]
        Number of bins per octave

    tuning : None or float
        Tuning offset in fractions of a bin.

        If ``None``, tuning will be automatically estimated from the signal.

        The minimum frequency of the resulting CQT will be modified to
        ``fmin * 2**(tuning / bins_per_octave)``.

    filter_scale : float > 0
        Filter scale factor. Small values (<1) use shorter windows
        for improved time resolution.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the CQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, number, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the CQT response by square-root the length of
        each channel's filter.  This is analogous to ``norm='ortho'`` in FFT.

        If ``False``, do not scale the CQT. This is analogous to
        ``norm=None`` in FFT.

    pad_mode : string
        Padding mode for centered frame analysis.

        See also: `librosa.stft` and `numpy.pad`.

    res_type : string
        The resampling mode for recursive downsampling.

    dtype : np.dtype
        The (complex) data type of the output array.  By default, this is inferred to match
        the numerical precision of the input signal.

    Returns
    -------
    CQT : np.ndarray [shape=(..., n_bins, t)]
        Constant-Q value each frequency at each time.

    See Also
    --------
    vqt
    librosa.resample
    librosa.util.normalize

    Notes
    -----
    This function caches at level 20.

    Examples
    --------
    Generate and plot a constant-Q power spectrum

    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> C = np.abs(librosa.cqt(y, sr=sr))
    >>> fig, ax = plt.subplots()
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max),
    ...                                sr=sr, x_axis='time', y_axis='cqt_note', ax=ax)
    >>> ax.set_title('Constant-Q power spectrum')
    >>> fig.colorbar(img, ax=ax, format="%+2.0f dB")

    Limit the frequency range

    >>> C = np.abs(librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('C2'),
    ...                 n_bins=60))
    >>> C
    array([[6.830e-04, 6.361e-04, ..., 7.362e-09, 9.102e-09],
           [5.366e-04, 4.818e-04, ..., 8.953e-09, 1.067e-08],
           ...,
           [4.288e-02, 4.580e-01, ..., 1.529e-05, 5.572e-06],
           [2.965e-03, 1.508e-01, ..., 8.965e-06, 1.455e-05]])

    Using a higher frequency resolution

    >>> C = np.abs(librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('C2'),
    ...                 n_bins=60 * 2, bins_per_octave=12 * 2))
    >>> C
    array([[5.468e-04, 5.382e-04, ..., 5.911e-09, 6.105e-09],
           [4.118e-04, 4.014e-04, ..., 7.788e-09, 8.160e-09],
           ...,
           [2.780e-03, 1.424e-01, ..., 4.225e-06, 2.388e-05],
           [5.147e-02, 6.959e-02, ..., 1.694e-05, 5.811e-06]])
    equalr   )r5   r'   r(   r)   r*   	intervalsgammar+   r,   r-   r.   r/   r0   r1   r2   r3   r4   )r   )r5   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4    r:   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/librosa/core/constantq.pyr      s(     c                C   s:  |dkrt d}|dkr&t| ||d}|d||   }t|||d}|dkrVt|}ntj|d}tj||||
|d\}}dtt	| d	| k }t
t|}|| }g }|d
krt|| }|t| ||||||||	|
|||d |d
kr(|tt| ||||||||	|
||||d t|||d jS )a	  Compute the hybrid constant-Q transform of an audio signal.

    Here, the hybrid CQT uses the pseudo CQT for higher frequencies where
    the hop_length is longer than half the filter length and the full CQT
    for lower frequencies.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)]
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    hop_length : int > 0 [scalar]
        number of samples between successive CQT columns.

    fmin : float > 0 [scalar]
        Minimum frequency. Defaults to `C1 ~= 32.70 Hz`

    n_bins : int > 0 [scalar]
        Number of frequency bins, starting at ``fmin``

    bins_per_octave : int > 0 [scalar]
        Number of bins per octave

    tuning : None or float
        Tuning offset in fractions of a bin.

        If ``None``, tuning will be automatically estimated from the signal.

        The minimum frequency of the resulting CQT will be modified to
        ``fmin * 2**(tuning / bins_per_octave)``.

    filter_scale : float > 0
        Filter filter_scale factor. Larger values use longer windows.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the CQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, number, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the CQT response by square-root the length of
        each channel's filter.  This is analogous to ``norm='ortho'`` in FFT.

        If ``False``, do not scale the CQT. This is analogous to
        ``norm=None`` in FFT.

    pad_mode : string
        Padding mode for centered frame analysis.

        See also: `librosa.stft` and `numpy.pad`.

    res_type : string
        Resampling mode.  See `librosa.cqt` for details.

    dtype : np.dtype, optional
        The complex dtype to use for computing the CQT.
        By default, this is inferred to match the precision of
        the input signal.

    Returns
    -------
    CQT : np.ndarray [shape=(..., n_bins, t), dtype=np.float]
        Constant-Q energy for each frequency at each time.

    See Also
    --------
    cqt
    pseudo_cqt

    Notes
    -----
    This function caches at level 20.
    NC1r5   r'   r+          @)r)   r+   r   freqs)r@   r'   r-   r0   alphar   r   )r'   r(   r)   r*   r+   r-   r.   r/   r0   r1   r2   r4   )r'   r(   r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   r4   )r   r   r   __et_relative_bwr   _relative_bandwidthwavelet_lengthsnpceillog2intsumminappendr   absr   __trim_stackr4   )r5   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r@   rA   lengths_Zpseudo_filtersZn_bins_pseudoZn_bins_fullcqt_respZfmin_pseudor:   r:   r;   r      sz    h
    

)r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   )r5   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   r6   c                C   s  |dkrt d}|dkr&t| ||d}|dkr:t| j}|d||   }t|||d}|dkrjt|}ntj|d}tj	|||
||d\}}t
|||||	||
||d		\}}}t|}t| ||||d
|dd}|r|t| }n$tj||jdd}|t|| 9 }|S )aO	  Compute the pseudo constant-Q transform of an audio signal.

    This uses a single fft size that is the smallest power of 2 that is greater
    than or equal to the max of:

        1. The longest CQT filter
        2. 2x the hop_length

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)]
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    hop_length : int > 0 [scalar]
        number of samples between successive CQT columns.

    fmin : float > 0 [scalar]
        Minimum frequency. Defaults to `C1 ~= 32.70 Hz`

    n_bins : int > 0 [scalar]
        Number of frequency bins, starting at ``fmin``

    bins_per_octave : int > 0 [scalar]
        Number of bins per octave

    tuning : None or float
        Tuning offset in fractions of a bin.

        If ``None``, tuning will be automatically estimated from the signal.

        The minimum frequency of the resulting CQT will be modified to
        ``fmin * 2**(tuning / bins_per_octave)``.

    filter_scale : float > 0
        Filter filter_scale factor. Larger values use longer windows.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the CQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, number, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the CQT response by square-root the length of
        each channel's filter.  This is analogous to ``norm='ortho'`` in FFT.

        If ``False``, do not scale the CQT. This is analogous to
        ``norm=None`` in FFT.

    pad_mode : string
        Padding mode for centered frame analysis.

        See also: `librosa.stft` and `numpy.pad`.

    dtype : np.dtype, optional
        The complex data type for CQT calculations.
        By default, this is inferred to match the precision of the input signal.

    Returns
    -------
    CQT : np.ndarray [shape=(..., n_bins, t), dtype=np.float]
        Pseudo Constant-Q energy for each frequency at each time.

    Notes
    -----
    This function caches at level 20.
    Nr<   r=   r>   r)   r*   r+   r   r?   r@   r'   r0   r-   rA   )r(   r0   r4   rA   r%   F)r0   r4   phasendimZaxes)r   r   r   	dtype_r2cr4   r   rC   r   rD   rE   __vqt_filter_fftrF   rM   __cqt_responsesqrt	expand_torW   )r5   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r4   r@   rA   rO   rP   	fft_basisn_fftCr:   r:   r;   r   x  sZ    `
    

(   )r'   r(   r)   r+   r,   r-   r.   r/   r0   r1   lengthr3   r4   )r_   r'   r(   r)   r+   r,   r-   r.   r/   r0   r1   ra   r3   r4   r6   c          %      C   s  |dkrt d}|d||   }| jd }ttt|| }t|||d}|dkr`t|}ntj	|d}tj
|||	||d\}}|dk	rtt|t| | }| d	d|f } t|}d}|g}|g}t|d D ]`}|d
 d d
kr|d
|d
 d  |d
|d
 d  q|d
|d
  |d
|d
  qtt||D ]>\}\}}t||||  }t|| || | }t||| ||||	||| d\}}} |j }!dtjtt|!d
d }"|"|||  9 }"|
rtjd|!|| |"| d	|ddf dd}#n"tjd|!|"| d	|ddf dd}#t|#d||d}$tj|$d|| |ddd}$|dkrl|$}n|d	d|$jd f  |$7  < qL|dk	st|rtj||d}|S )af  Compute the inverse constant-Q transform.

    Given a constant-Q transform representation ``C`` of an audio signal ``y``,
    this function produces an approximation ``y_hat``.

    Parameters
    ----------
    C : np.ndarray, [shape=(..., n_bins, n_frames)]
        Constant-Q representation as produced by `cqt`

    sr : number > 0 [scalar]
        sampling rate of the signal

    hop_length : int > 0 [scalar]
        number of samples between successive frames

    fmin : float > 0 [scalar]
        Minimum frequency. Defaults to `C1 ~= 32.70 Hz`

    bins_per_octave : int > 0 [scalar]
        Number of bins per octave

    tuning : float [scalar]
        Tuning offset in fractions of a bin.

        The minimum frequency of the CQT will be modified to
        ``fmin * 2**(tuning / bins_per_octave)``.

    filter_scale : float > 0 [scalar]
        Filter scale factor. Small values (<1) use shorter windows
        for improved time resolution.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the CQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, number, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the CQT response by square-root the length
        of each channel's filter. This is analogous to ``norm='ortho'`` in FFT.

        If ``False``, do not scale the CQT. This is analogous to ``norm=None``
        in FFT.

    length : int > 0, optional
        If provided, the output ``y`` is zero-padded or clipped to exactly
        ``length`` samples.

    res_type : string
        Resampling mode.
        See `librosa.resample` for supported modes.

    dtype : numeric type
        Real numeric type for ``y``.  Default is inferred to match the numerical
        precision of the input CQT.

    Returns
    -------
    y : np.ndarray, [shape=(..., n_samples), dtype=np.float]
        Audio time-series reconstructed from the CQT representation.

    See Also
    --------
    cqt
    librosa.resample

    Notes
    -----
    This function caches at level 40.

    Examples
    --------
    Using default parameters

    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> C = librosa.cqt(y=y, sr=sr)
    >>> y_hat = librosa.icqt(C=C, sr=sr)

    Or with a different hop length and frequency resolution:

    >>> hop_length = 256
    >>> bins_per_octave = 12 * 3
    >>> C = librosa.cqt(y=y, sr=sr, hop_length=256, n_bins=7*bins_per_octave,
    ...                 bins_per_octave=bins_per_octave)
    >>> y_hat = librosa.icqt(C=C, sr=sr, hop_length=hop_length,
    ...                 bins_per_octave=bins_per_octave)
    Nr<   r>   rU   rR   r   r?   rS   .r   r   g      ?)r0   r4   rA   )axiszfc,c,c,...ct->...ftT)optimizezfc,c,...ct->...ftones)r0   r(   r4   F)orig_sr	target_srr3   r1   ZfixrB   size) r   shaperI   rF   rG   floatr   rC   r   rD   rE   maxr[   rangeinsert	enumerateziprK   slicerY   HZtodenserJ   r   Zabs2ZasarrayZeinsumr
   r   resampleAssertionErrorZ
fix_length)%r_   r'   r(   r)   r+   r,   r-   r.   r/   r0   r1   ra   r3   r4   r*   	n_octavesr@   rA   rO   Zf_cutoffZn_framesZC_scaler5   ZsrsZhopsimy_srmy_hop	n_filtersslr]   r^   rP   Z	inv_basisZ
freq_powerZD_octZy_octr:   r:   r;   r     s    r

    


	    	
"r7   )r'   r(   r)   r*   r8   r9   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   )r5   r'   r(   r)   r*   r8   r9   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   c          &      C   s  t |tst|}ttt|| }t||}|dkrBtd}|dkrXt	| ||d}|dkrlt
| j}|d||   }t||||dd}|| d }t|}|dkrt|}ntj|d}tj||||	||d	\}}|d }||krtd
| d| d|dkr$tjdtdd d}t| |||||||\} }}g }| ||  }}}t|D ]}|dkrxt| d}nt| |d  | | }|| } || }!t|| |	|
|||||!d	\}"}#}$|"dd  t|| 9  < |t||#||"||d |d dkr\|d }|d }tj|dd|dd}q\t |||}%|r|tj||||	||d	\}}$t
j!||%j"dd}|%t| }%|%S )u'  Compute the variable-Q transform of an audio signal.

    This implementation is based on the recursive sub-sampling method
    described by [#]_.

    .. [#] Schörkhuber, Christian, Anssi Klapuri, Nicki Holighaus, and Monika Dörfler.
        "A Matlab toolbox for efficient perfect reconstruction time-frequency
        transforms with log-frequency resolution."
        In Audio Engineering Society Conference: 53rd International Conference: Semantic Audio.
        Audio Engineering Society, 2014.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)]
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    hop_length : int > 0 [scalar]
        number of samples between successive VQT columns.

    fmin : float > 0 [scalar]
        Minimum frequency. Defaults to `C1 ~= 32.70 Hz`

    n_bins : int > 0 [scalar]
        Number of frequency bins, starting at ``fmin``

    intervals : str or array of floats in [1, 2)
        Either a string specification for an interval set, e.g.,
        `'equal'`, `'pythagorean'`, `'ji3'`, etc. or an array of
        intervals expressed as numbers between 1 and 2.
        .. see also:: librosa.interval_frequencies

    gamma : number > 0 [scalar]
        Bandwidth offset for determining filter lengths.

        If ``gamma=0``, produces the constant-Q transform.

        If 'gamma=None', gamma will be calculated such that filter bandwidths are equal to a
        constant fraction of the equivalent rectangular bandwidths (ERB). This is accomplished
        by solving for the gamma which gives::

            B_k = alpha * f_k + gamma = C * ERB(f_k),

        where ``B_k`` is the bandwidth of filter ``k`` with center frequency ``f_k``, alpha
        is the inverse of what would be the constant Q-factor, and ``C = alpha / 0.108`` is the
        constant fraction across all filters.

        Here we use ``ERB(f_k) = 24.7 + 0.108 * f_k``, the best-fit curve derived
        from experimental data in [#]_.

        .. [#] Glasberg, Brian R., and Brian CJ Moore.
            "Derivation of auditory filter shapes from notched-noise data."
            Hearing research 47.1-2 (1990): 103-138.

    bins_per_octave : int > 0 [scalar]
        Number of bins per octave

    tuning : None or float
        Tuning offset in fractions of a bin.

        If ``None``, tuning will be automatically estimated from the signal.

        The minimum frequency of the resulting VQT will be modified to
        ``fmin * 2**(tuning / bins_per_octave)``.

    filter_scale : float > 0
        Filter scale factor. Small values (<1) use shorter windows
        for improved time resolution.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the VQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, number, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the VQT response by square-root the length of
        each channel's filter.  This is analogous to ``norm='ortho'`` in FFT.

        If ``False``, do not scale the VQT. This is analogous to
        ``norm=None`` in FFT.

    pad_mode : string
        Padding mode for centered frame analysis.

        See also: `librosa.stft` and `numpy.pad`.

    res_type : string
        The resampling mode for recursive downsampling.

    dtype : np.dtype
        The dtype of the output array.  By default, this is inferred to match the
        numerical precision of the input signal.

    Returns
    -------
    VQT : np.ndarray [shape=(..., n_bins, t), dtype=np.complex]
        Variable-Q value each frequency at each time.

    See Also
    --------
    cqt

    Notes
    -----
    This function caches at level 20.

    Examples
    --------
    Generate and plot a variable-Q power spectrum

    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('choice'), duration=5)
    >>> C = np.abs(librosa.cqt(y, sr=sr))
    >>> V = np.abs(librosa.vqt(y, sr=sr))
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max),
    ...                          sr=sr, x_axis='time', y_axis='cqt_note', ax=ax[0])
    >>> ax[0].set(title='Constant-Q power spectrum', xlabel=None)
    >>> ax[0].label_outer()
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(V, ref=np.max),
    ...                                sr=sr, x_axis='time', y_axis='cqt_note', ax=ax[1])
    >>> ax[1].set_title('Variable-Q power spectrum')
    >>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
    Nr<   r=   r>   T)r*   r)   r8   r+   sortr   r?   )r@   r'   r0   r-   r9   rA   z!Wavelet basis with max frequency=z$ would exceed the Nyquist frequency=z,. Try reducing the number of frequency bins.zdSupport for VQT with res_type=None is deprecated in librosa 0.10
and will be removed in version 1.0.r   )category
stacklevelr&   r   )r0   r9   r4   rA   r4   re   rf   r3   r1   rU   rV   )#
isinstancestrlenrI   rF   rG   rj   rK   r   r   r   rX   r4   r   rk   rC   r   rD   rE   r   warningswarnFutureWarning__early_downsamplerl   rp   rY   r[   rL   rZ   r   rr   rN   r\   rW   )&r5   r'   r(   r)   r*   r8   r9   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   rt   rx   r@   Z	freqs_topZfmax_trA   rO   filter_cutoffnyquistZvqt_respZmy_yrv   rw   ru   ry   Z	freqs_octZ	alpha_octr]   r^   rP   Vr:   r:   r;   r     s     







       

    



   c
              
   C   s   t j|| ||d|||	d\}
}|
jd }|dk	rh|ddtt|  k rhtddtt|  }|
|ddtjf t| 9 }
t	 }|j
|
|ddddd|d d f }tj|||d}|||fS )	z6Generate the frequency domain variable-Q filter basis.T)r@   r'   r-   r.   Zpad_fftr0   r9   rA   r   Nr>   )nrb   r   )Zquantiler4   )r   Zwaveletri   rF   rG   rH   rI   Znewaxisrj   r   fftr   Zsparsify_rows)r'   r@   r-   r.   r/   r(   r0   r9   r4   rA   ZbasisrO   r^   r   r]   r:   r:   r;   rY     s$    

$(rY   )rQ   r*   r4   r6   c           	      C   s   t dd | D }t| d j}||d< ||d< tj||dd}|}| D ]p}|jd }||k r|d| d	d	|f |dd	|d	d	f< n&|dd	|f |d|| |d	d	f< ||8 }qH|S )
z,Trim and stack a collection of CQT responsesc                 s   s   | ]}|j d  V  qdS )rB   N)ri   ).0c_ir:   r:   r;   	<genexpr>I  s     z__trim_stack.<locals>.<genexpr>r   rU   rB   F)r4   order.N)rK   listri   rF   empty)	rQ   r*   r4   Zmax_colri   Zcqt_outendr   Zn_octr:   r:   r;   rN   E  s    
,&
rN   rd   c                 C   s   t | |||||d}|s"t|}|d|jd |jd f}	tj|	jd |jd |	jd f|jd}
t|	jd D ]}||	| |
|< qtt	|j}|jd |d< |
|S )z3Compute the filter response with a target STFT hop.)r^   r(   r0   r2   r4   rB   rU   r   r}   )
r	   rF   rM   Zreshaperi   r   r4   rl   dotr   )r5   r^   r(   r]   moder0   rT   r4   DZDrZoutput_flatru   ri   r:   r:   r;   rZ   a  s(         
 
rZ   c              	   C   sJ   t dttt| | d d }t|}t d|| d }t||S )z3Compute the number of early downsampling operationsr   r   )rk   rI   rF   rG   rH   __num_two_factorsrK   )r   r   r(   rt   Zdownsample_count1num_twosZdownsample_count2r:   r:   r;   __early_downsample_count~  s    &r   c                 C   s   t ||||}|dkrd| }	||	 }| jd |	k rRtdt| dd|dd|t|	 }
tj| |	d|d	d
} |s| t|	9 } |
}| ||fS )z=Perform early downsampling on an audio signal, if it applies.r   r   rB   zInput signal length=dz is too short for z-octave CQTr   Tr~   )	r   ri   r   r   rj   r   rr   rF   r[   )r5   r'   r(   r3   rt   r   r   r1   Zdownsample_countZdownsample_factorZnew_srr:   r:   r;   r     s2           r   )Znopythonr   c                 C   s2   | dkrdS d}| d dkr.|d7 }| d } q|S )zjReturn how many times integer x can be evenly divided by 2.

    Returns 0 for non-positive integers.
    r   r   r   r:   )xr   r:   r:   r;   r     s    
r       gGz?random)n_iterr'   r(   r)   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   ra   momentuminitrandom_state)r_   r   r'   r(   r)   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   ra   r   r   r   r6   c                C   s  |dkrt d}|dkr$tj }nLt|tr>tjj|d}n2t|tjjtjjfrZ|}nt| t	d||dkrt
jd| ddd	 n|d
k rt	d| dtj| jtjd}t|}|dkrtdtj |j| jd |dd< n(|dkrd|dd< nt	d| dtd}t|D ]}|}t| | |||||||
|||||	|d}t|||| jd |||||
|||	||d}||d|  |  |dd< |dd  t||   < q.t| | |||||||
|||||	|dS )u  Approximate constant-Q magnitude spectrogram inversion using the "fast" Griffin-Lim
    algorithm.

    Given the magnitude of a constant-Q spectrogram (``C``), the algorithm randomly initializes
    phase estimates, and then alternates forward- and inverse-CQT operations. [#]_

    This implementation is based on the (fast) Griffin-Lim method for Short-time Fourier Transforms, [#]_
    but adapted for use with constant-Q spectrograms.

    .. [#] D. W. Griffin and J. S. Lim,
        "Signal estimation from modified short-time Fourier transform,"
        IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.

    .. [#] Perraudin, N., Balazs, P., & Søndergaard, P. L.
        "A fast Griffin-Lim algorithm,"
        IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (pp. 1-4),
        Oct. 2013.

    Parameters
    ----------
    C : np.ndarray [shape=(..., n_bins, n_frames)]
        The constant-Q magnitude spectrogram

    n_iter : int > 0
        The number of iterations to run

    sr : number > 0
        Audio sampling rate

    hop_length : int > 0
        The hop length of the CQT

    fmin : number > 0
        Minimum frequency for the CQT.

        If not provided, it defaults to `C1`.

    bins_per_octave : int > 0
        Number of bins per octave

    tuning : float
        Tuning deviation from A440, in fractions of a bin

    filter_scale : float > 0
        Filter scale factor. Small values (<1) use shorter windows
        for improved time resolution.

    norm : {inf, -inf, 0, float > 0}
        Type of norm to use for basis function normalization.
        See `librosa.util.normalize`.

    sparsity : float in [0, 1)
        Sparsify the CQT basis by discarding up to ``sparsity``
        fraction of the energy in each basis.

        Set ``sparsity=0`` to disable sparsification.

    window : str, tuple, or function
        Window specification for the basis filters.
        See `filters.get_window` for details.

    scale : bool
        If ``True``, scale the CQT response by square-root the length
        of each channel's filter.  This is analogous to ``norm='ortho'``
        in FFT.

        If ``False``, do not scale the CQT. This is analogous to ``norm=None``
        in FFT.

    pad_mode : string
        Padding mode for centered frame analysis.

        See also: `librosa.stft` and `numpy.pad`.

    res_type : string
        The resampling mode for recursive downsampling.

        See ``librosa.resample`` for a list of available options.

    dtype : numeric type
        Real numeric type for ``y``.  Default is inferred to match the precision
        of the input CQT.

    length : int > 0, optional
        If provided, the output ``y`` is zero-padded or clipped to exactly
        ``length`` samples.

    momentum : float > 0
        The momentum parameter for fast Griffin-Lim.
        Setting this to 0 recovers the original Griffin-Lim method.
        Values near 1 can lead to faster convergence, but above 1 may not converge.

    init : None or 'random' [default]
        If 'random' (the default), then phase values are initialized randomly
        according to ``random_state``.  This is recommended when the input ``C`` is
        a magnitude spectrogram with no initial phase estimates.

        If ``None``, then the phase is initialized from ``C``.  This is useful when
        an initial guess for phase can be provided, or when you want to resume
        Griffin-Lim from a previous output.

    random_state : None, int, np.random.RandomState, or np.random.Generator
        If int, random_state is the seed used by the random number generator
        for phase initialization.

        If `np.random.RandomState` or `np.random.Generator` instance, the random number generator itself.

        If ``None``, defaults to the `np.random.default_rng()` object.

    Returns
    -------
    y : np.ndarray [shape=(..., n)]
        time-domain signal reconstructed from ``C``

    See Also
    --------
    cqt
    icqt
    griffinlim
    filters.get_window
    resample

    Examples
    --------
    A basis CQT inverse example

    >>> y, sr = librosa.load(librosa.ex('trumpet', hq=True), sr=None)
    >>> # Get the CQT magnitude, 7 octaves at 36 bins per octave
    >>> C = np.abs(librosa.cqt(y=y, sr=sr, bins_per_octave=36, n_bins=7*36))
    >>> # Invert using Griffin-Lim
    >>> y_inv = librosa.griffinlim_cqt(C, sr=sr, bins_per_octave=36)
    >>> # And invert without estimating phase
    >>> y_icqt = librosa.icqt(C, sr=sr, bins_per_octave=36)

    Wave-plot the results

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
    >>> librosa.display.waveshow(y, sr=sr, color='b', ax=ax[0])
    >>> ax[0].set(title='Original', xlabel=None)
    >>> ax[0].label_outer()
    >>> librosa.display.waveshow(y_inv, sr=sr, color='g', ax=ax[1])
    >>> ax[1].set(title='Griffin-Lim reconstruction', xlabel=None)
    >>> ax[1].label_outer()
    >>> librosa.display.waveshow(y_icqt, sr=sr, color='r', ax=ax[2])
    >>> ax[2].set(title='Magnitude-only icqt reconstruction')
    Nr<   )seedzUnsupported random_state=r   zGriffin-Lim with momentum=z+ > 1 can be unstable. Proceed with caution!r   )r|   r   z&griffinlim_cqt() called with momentum=z < 0r}   r   rg   g      ?zinit=z must either None or 'random'r$   )r'   r(   r+   r)   r,   r-   r0   ra   r3   r.   r1   r/   r4   rU   )r'   r+   r*   r(   r)   r,   r-   r0   r.   r1   r/   r2   r3   )r'   r(   r+   r,   r-   r)   r0   ra   r3   r.   r1   r/   r4   )r   rF   r   Zdefault_rngr   rI   RandomState	Generatorr   r   r   r   r   ri   	complex64r   ZtinyZphasorpiarrayrl   r   r   rM   )r_   r   r'   r(   r)   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   ra   r   r   r   rngZanglesZepsZrebuiltrP   ZtprevZinverser:   r:   r;   r     s     ,


(

")r+   r6   c                 C   s*   dd|   }t |d d |d d  S )a  Compute the relative bandwidth coefficient for equal
    (geometric) freuqency spacing and a give number of bins
    per octave.

    This is a special case of the more general `relative_bandwidth`
    calculation that can be used when only a single basis frequency
    is used.

    Parameters
    ----------
    bins_per_octave : int

    Returns
    -------
    alpha : np.ndarray > 0
        Value is cast up to a 1d array to allow slicing
    r   r   )rF   Z
atleast_1d)r+   rr:   r:   r;   rC     s    rC   )rd   TN)=__doc__r   numpyrF   Znumbar    r   r8   r   r   r   convertr   r   Zspectrumr	   r
   Zpitchr   _cacher   r   r   Zutil.exceptionsr   Znumpy.typingr   typingr   r   r   r   Z_typingr   r   r   r   __all__Zndarrayrj   rI   boolr   r   r   r   r   r   r   rY   rN   rZ   r   r   r   r   r   r   r   rC   r:   r:   r:   r;   <module>   s   % 8  c  ",       

"

  