
    0;jiӰ              +       $   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% g dZ& ed          ddddddddddddddd d!ej'        d"e(d#e)d$ee$         d%e)d&e)d'ee(         d(e(d)ee(         d*e(d+e"d,e*d-e#d.ee+         d/ee         d0ej'        f d1            Z, ed          ddddddddddddddd d!ej'        d"e(d#e)d$ee$         d%e)d&e)d'ee(         d(e(d)ee(         d*e(d+e"d,e*d-e#d.e+d/ee         d0ej'        f d2            Z- ed          dddddddddddddd3d!ej'        d"e(d#e)d$ee$         d%e)d&e)d'ee(         d(e(d)ee(         d*e(d+e"d,e*d-e#d/ee         d0ej'        fd4            Z. ed5          dddddddddddddd6d7ej'        d"e(d#e)d$ee$         d&e)d'e(d(e(d)ee(         d*e(d+e"d,e*d8ee)         d.e+d/ee         d0ej'        fd9            Z/ ed          ddddd:dddddddddddd;d!ej'        d"e(d#e)d$ee$         d%e)d<ee+ee(         f         d=ee(         d&e)d'ee(         d(e(d)ee(         d*e(d+e"d,e*d-e#d.ee+         d/ee         d0ej'        f$d>            Z0 ed?          dddej1        dfd@            Z2dAe ej'                 d%e)d/ed0ej'        fdBZ3	 dSdDZ4dE Z5dF Z6 eddG          dH             Z7dIdddddddddddddddJdKddLd7ej'        dMe)d"e(d#e)d$ee$         d&e)d'e(d(e(d)ee(         d*e(d+e"d,e*d-e#d.e+d/ee         d8ee)         dNe(dOee+         dPeee)ej8        j9        ej8        j:        f                  d0ej'        f(dQZ;d&e)d0ej'        fdRZ<dS )Tz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?hannT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/   r0   r1   r2   r3   r4   r5   r6   returnc                |    t          di d| d|d|d|d|dddd	d
|d|d|d|d|	d|
d|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]])
    r7   r)   r*   r+   r,   	intervalsequalgammar   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6    )r    )r7   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   s                  P/root/voice-cloning/.venv/lib/python3.11/site-packages/librosa/core/constantq.pyr   r      s    `    
!2 : T	
 v ' a ( v "\ T  v e   !" e#     c                *   |t          d          }|t          | ||          }|d||z  z  z  }t          |||          }|dk    rt          |          }nt	          j        |          }t	          j        ||||
|          \  }}dt          j        t          j	        |                    z  d	|z  k     }t          t          j        |                    }||z
  }g }|d
k    rIt          j        ||                   }|                    t          | ||||||||	|
|||                     |d
k    rB|                    t          j        t!          | ||||||||	|
||||                               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r7   r)   r-          @)r+   r-   r   freqs)rE   r)   r/   r2   alphar   r   )r)   r*   r+   r,   r-   r/   r0   r1   r2   r3   r4   r6   )r)   r*   r+   r,   r-   r/   r0   r1   r2   r3   r4   r5   r6   )r	   r   r   __et_relative_bwr   _relative_bandwidthwavelet_lengthsnpceillog2intsumminappendr   absr   __trim_stackr6   )r7   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rE   rF   lengths_pseudo_filtersn_bins_pseudon_bins_fullcqt_respfmin_pseudos                           r>   r   r      s   P |$~ 1_MMM #&?233D FOOOE {{ 11+%888 (fE  JGQ BGBGG$4$4555JFN~..//M=(KHqfU>233% $ /)!!  	
 	
 	
$ QF)&$3!-%!%%   	
 	
 	
, &(2,*<===r?   )r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   c                |   |t          d          }|t          | ||          }|t          j        | j                  }|d||z  z  z  }t          |||          }|dk    rt          |          }nt          j        |          }t          j	        |||
||          \  }}t          |||||	||
||		  	        \  }}}t          j        |          }t          | ||||d
|d          }|r|t          j        |          z  }n6t          j        ||j        d          }|t          j        ||z            z  }|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.
    NrA   rB   rC   r+   r,   r-   r   rD   rE   r)   r2   r/   rF   )r*   r2   r6   rF   r&   F)r2   r6   phasendimaxes)r	   r   r   	dtype_r2cr6   r   rH   r   rI   rJ   __vqt_filter_fftrK   rR   __cqt_responsesqrt	expand_tora   )r7   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   rE   rF   rT   rU   	fft_basisn_fftCs                        r>   r   r   x  s   @ |$~ 1_MMM}qw'' #&?233DfoVVVE{{ 11+%888(6E  JGQ +

 
 
Iua y!!I #		 	 	A  &	RWU^^ .qvB???	RWWu_%%%Hr?   (   )r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   lengthr5   r6   rj   rl   c                   |t          d          }|d||z  z  z  }| j        d         }t          t          j        t          |          |z                      }t          |||          }|dk    rt          |          }nt          j	        |          }t          j
        |||	||          \  }}|@t          t          j        |t          |          z   |z                      }| d	d|f         } t          j        |          }d}|g}|g}t          |dz
            D ]}|d
         dz  d
k    r?|                    d
|d
         dz             |                    d
|d
         dz             P|                    d
|d
                    |                    d
|d
                    t          t!          ||                    D ]{\  }\  }}t#          ||||z  z
            }t%          ||z  ||z  |z             }t'          |||         ||||	||                   \  }}} |                                j                                        }!dt          j        t1          j        t          j        |!                    d
          z  }"|"|||         z  z  }"|
r,t          j        d|!||         |"| d	|ddf         d          }#n$t          j        d|!|"| d	|ddf         d          }#t9          |#d||          }$t;          j        |$d||z  |dd          }$||$}\|d	d|$j        d         fxx         |$z  cc<   }|J |rt1          j        ||          }|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)
    NrA   rC   r_   r\   r   rD   r]   .r   r   g      ?)r2   rF   )axiszfc,c,c,...ct->...ftT)optimizezfc,c,...ct->...ftones)r2   r*   r6   F)orig_sr	target_srr5   r3   fixrG   size) r	   shaperN   rK   rL   floatr   rH   r   rI   rJ   maxrf   rangeinsert	enumerateziprP   slicerd   	conjugateTtodenserO   r   abs2asarrayeinsumr   r   resample
fix_length)%rj   r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   rl   r5   r6   r,   	n_octavesrE   rF   rT   f_cutoffn_framesC_scaler7   srshopsimy_srmy_hop	n_filtersslrh   ri   rU   	inv_basis
freq_powerD_octy_octs%                                        r>   r   r     s   d |$ #&?233D WR[FBGE&MMO;<<==IfoVVVE{{ 11+%888/6E  GX
 rwW 5CDDEEc9H9nggG #A $C<D9q=!! $ $7Q;!JJq#a&3,'''KK47a<(((( JJq#a&!!!KK47####'C77 8/ 8/?E6/A2E)EFF	 ?Q&!(;i(GHH."I)
 
 
	5! ''))+3355	 	"*Y*?*? @ @qIIII
 	egbk))
  	I%#r111*  EE I#Y
Ac2qqqjMTX  E eFvUKKKEk
 
 
 9AAc$U[_$$%%%.%%%%=== ,OAF+++Hr?   r;   )r)   r*   r+   r,   r:   r<   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r:   r<   c                   t          |t                    st          |          }t          t	          j        t          |          |z                      }t          ||          }|t          d          }|t          | ||          }|t          j        | j                  }|d||z  z  z  }t          ||||d          }|| d         }t	          j        |          }|dk    rt          |          }nt!          j        |          }t!          j        ||||	||	          \  }}|dz  }||k    rt'          d
| d| d          |t)          j        dt,          d           d}t/          | |||||||          \  } }}g }| ||}}}t1          |          D ]}|dk    rt3          | d          }nt3          | |dz   z  | |z            }||         } ||         }!t5          || |	|
|||||!	  	        \  }"}#}$|"ddxx         t	          j        ||z            z  cc<   |                    t;          ||#||"||                     |dz  dk    r#|dz  }|dz  }t=          j        |dd|d          }tA          |||          }%|rPt!          j        ||||	||	          \  }}$t          j!        ||%j"        d          }|%t	          j        |          z  }%|%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")
    NrA   rB   rC   T)r,   r+   r:   r-   sortr   rD   )rE   r)   r2   r/   r<   rF   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   )r2   r<   r6   rF   r6   rq   rr   r5   r3   r_   r`   )#
isinstancestrlenrN   rK   rL   rw   rP   r	   r   r   rc   r6   r   rx   rH   r   rI   rJ   r   warningswarnFutureWarning__early_downsamplery   r}   rd   rf   rQ   re   r   r   rS   rg   ra   )&r7   r)   r*   r+   r,   r:   r<   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r   r   rE   	freqs_topfmax_trF   rT   filter_cutoffnyquistvqt_respmy_yr   r   r   r   	freqs_oct	alpha_octrh   ri   rU   Vs&                                         r>   r    r      s   z i%% )i.. BGE&MMO;<<==IOV,,I|$~ 1_MMM}qw'' #&?233D !'  E &''(IF9%%F{{ 11+%888$4!  G] 3hGw9 9 9\c 9 9 9
 
 	

 2"		
 	
 	
 	
 *	2z8Y Ar: H R%D9 $ $66	z4((BB	zQU+iZ!^<<B "I	"I	.

 

 

	5! 	!!!U
+++ 	4	85QQQ	
 	
 	
 A:??qLFSLE>a1xt  D 	Xvu--A  ,%
 
 

 .qvB???	RWWHr?   
   c
           
      *   t          j        || ||d|||	          \  }
}|
j        d         }|i|ddt          j        t          j        |                    z   z  k     r9t          ddt          j        t          j        |                    z   z            }|
|ddt          j        f         t          |          z  z  }
t                      }|
                    |
|d          ddd|dz  dz   f         }t          j        |||          }|||fS )	z6Generate the frequency domain variable-Q filter basis.T)rE   r)   r/   r0   pad_fftr2   r<   rF   r   NrC   )nrn   r   )quantiler6   )r   waveletrv   rK   rL   rM   rN   newaxisrw   r   fftr   sparsify_rows)r)   rE   r/   r0   r1   r*   r2   r<   r6   rF   basisrT   ri   r   rh   s                  r>   rd   rd     s'    _!	 	 	NE7 KNE%#!bgbgj>Q>Q6R6R2R*S"S"SCA
(;(; < <<=>> 
WQQQ
]#eEll22E ,,CQ//3EeqjA5E3E0EFI "9xuMMMIeW$$r?   rY   c                 T   t          d | D                       }t          | d         j                  }||d<   ||d<   t          j        ||d          }|}| D ]N}|j        d         }||k     r|d| dd|f         |dd|ddf<   n|dd|f         |d||z
  |ddf<   ||z  }O|S )	z,Trim and stack a collection of CQT responsesc              3   0   K   | ]}|j         d          V  dS )rG   N)rv   ).0c_is     r>   	<genexpr>z__trim_stack.<locals>.<genexpr>H  s(      44C#)B-444444r?   r   r_   rG   F)r6   order.N)rP   listrv   rK   empty)	rY   r,   r6   max_colrv   cqt_outendr   n_octs	            r>   rS   rS   D  s     44844444G!"##EE"IE"IhuE555G C 
 
	" ;;$'cTUUHWH(<$=GC#qqqL!!14S(7(]1CGCus*AAA-.uNr?   rp   c                    t          | |||||          }|st          j        |          }|                    d|j        d         |j        d         f          }	t          j        |	j        d         |j        d         |	j        d         f|j                  }
t          |	j        d                   D ] }|                    |	|                   |
|<   !t          |j                  }|j        d         |d<   |
                    |          S )z3Compute the filter response with a target STFT hop.)ri   r*   r2   r4   r6   rG   r_   r   r   )
r
   rK   rR   reshaperv   r   r6   ry   dotr   )r7   ri   r*   rh   moder2   r^   r6   DDroutput_flatr   rv   s                r>   re   re   `  s   
 		:ftSX	 	 	A  F1II 
BQWR[1	2	2B(	!ioa("(2,7qw  K 28A; . ."r!u--A MME"E"Iu%%%r?   c                     t          dt          t          j        t          j        | |z                      dz
            dz
            }t          |          }t          d||z
  dz             }t          ||          S )z3Compute the number of early downsampling operationsr   r   )rx   rN   rK   rL   rM   __num_two_factorsrP   )r   r   r*   r   downsample_count1num_twosdownsample_count2s          r>   __early_downsample_countr   }  sw    As27277]3J+K+K#L#Lq#PQQTUUVV ,,HAx)3a788 "3444r?   c                 H   t          ||||          }|dk    rd|z  }	||	z  }| j        d         |	k     r%t          dt          |           dd|dd          |t	          |	          z  }
t          j        | |	d|d	
          } |s| t          j        |	          z  } |
}| ||fS )z=Perform early downsampling on an audio signal, if it applies.r   r   rG   zInput signal length=dz is too short for z-octave CQTr   Tr   )	r   rv   r   r   rw   r   r   rK   rf   )r7   r)   r*   r5   r   r   r   r3   downsample_countdownsample_factornew_srs              r>   r   r     s     0
I  !"23((
72;*** ,s1vvC , ,+, , ,  
 e-...N(APT
 
 
  	,*+++Ab*r?   )nopythonr   c                 R    | dk    rdS d}| dz  dk    r|dz  }| dz  } | dz  dk    |S )zjReturn how many times integer x can be evenly divided by 2.

    Returns 0 for non-positive integers.
    r   r   r   r=   )xr   s     r>   r   r     sL     	AvvqH
a%1**A	a a%1** Or?       gGz?random)n_iterr)   r*   r+   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rl   momentuminitrandom_stater   r   r   r   c                   |t          d          }|t          j                                        }nt	          |t
                    r!t          j                            |          }nTt	          |t          j        j        t          j        j        f          r|}n!t          |           t          d|          |dk    rt          j        d| dd	           n|d
k     rt          d| d          t          j        | j        t          j                  }t          j        |          }|dk    rCt          j        dt          j        z  |                    | j                  z            |dd<   n|d|dd<   nt          d| d          t          j        d          }t)          |          D ]}|}t+          | |z  |||||||
|||||	|          }t-          |||| j        d         |||||
|||	||          }||d|z   z  |z  z
  |dd<   |ddxx         t          j        |          |z   z  cc<   t+          | |z  |||||||
|||||	|          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')
    NrA   )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   rt   g      ?zinit=z must either None or 'random'r%   )r)   r*   r-   r+   r.   r/   r2   rl   r5   r0   r3   r1   r6   r_   )r)   r-   r,   r*   r+   r.   r/   r2   r0   r3   r1   r4   r5   )r)   r*   r-   r.   r/   r+   r2   rl   r5   r0   r3   r1   r6   )r	   rK   r   default_rngr   rN   RandomState	Generatorr   r   r   r   r   rv   	complex64r   tinyphasorpiarrayry   r   r   rR   )rj   r   r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rl   r   r   r   rnganglesepsrebuiltrU   tprevinverses                             r>   r   r     s   V |$i##%%	L#	&	& Ki###66	L29#8"):M"N	O	O Kl+++IIIJJJ!||$ $ $ $	
 	
 	
 	
 	

 
AThTTTUUU XagR\222F
)F

CxKBE	CJJAGJ,D,D DEEqqq			qqq		HTHHHIII (3--G6]] ** ** J!+%
 
 
$ +72;!%
 
 
$ x1x<8EAAqqq	qqq			RVF^^c))				 	F
'!   r?   c                 X    dd| z  z  }t          j        |dz  dz
  |dz  dz   z            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   )rK   
atleast_1d)r-   rs     r>   rH   rH     s8    $ 	
a/!"A=!Q$(q!tax0111r?   )rp   TN)=__doc__r   numpyrK   numbar    r   r:   r   r   r   convertr   r	   spectrumr
   r   pitchr   _cacher   r   r   util.exceptionsr   numpy.typingr   typingr   r   r   r   _typingr   r   r   r   __all__ndarrayrw   rN   boolr   r   r   r   r   r    r   rd   rS   re   r   r   r   r   r   r   r   rH   r=   r?   r>   <module>r      s	                      + + + + + +       0 0 0 0 0 0 0 0 ! ! ! ! ! ! ! ! " " " " " "                   , , , , , , " " " " " " 4 4 4 4 4 4 4 4 4 4 4 4 Q Q Q Q Q Q Q Q Q Q Q Q
N
N
N
 R $(! #'!%!a a a	za 	a 	a
 =
!a a a UOa a 5/a a a a a sma  I!a" Z#a a a aH R $(! #!%!t> t> t>	zt> 	t> 	t>
 =
!t> t> t> UOt> t> 5/t> t> t> t> t> t>  I!t>" Z#t> t> t> t>n R $(! #!%Y Y Y	zY 	Y 	Y
 =
!Y Y Y UOY Y 5/Y Y Y Y Y IY  Z!Y Y Y Yx R $(  !%^ ^ ^	z^ 	^ 	^
 =
!^ ^ ^ ^ 5/^ ^ ^ ^ SM^ ^ I^  Z!^ ^ ^ ^B R $(/6!! #'!%%] ] ]	z] 	] 	]
 =
!] ] S*U++,] E?] ] UO] ] 5/] ] ] ]  !]" sm#]$ I%]& Z'] ] ] ]@	 R 

,
(% (% (% (%V2:(+4=Z   : MQ& & & &:5 5 5  D d$   $ $( #!% " 	-M M M	zM M 		M
 M =
!M M M M 5/M M M M M M  I!M" SM#M$ %M& 3-'M( c29(")*==>)M. Z/M M M M`2c 2bj 2 2 2 2 2 2r?   