
    }Kgǰ              +          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jN                  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jN                  f d1       Z, ed      ddddddddddddddd d!ejN                  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jN                  f d2       Z- ed      dddddddddddddd3d!ejN                  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jN                  fd4       Z. ed5      dddddddddddddd6d7ejN                  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jN                  fd9       Z/ ed      ddddd:dddddddddddd;d!ejN                  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jN                  f$d>       Z0 ed?      dddejb                  dfd@       Z2dAe ejN                     d%e)d/ed0ejN                  fdBZ3	 dRdCZ4dD Z5dE Z6 eddF      dG        Z7dHdddddddddddddddIdJddKd7ejN                  dLe)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)   dMe(dNee+   dOeee)ejp                  jr                  ejp                  jt                  f      d0ejN                  f(dPZ;d&e)d0ejN                  fdQZ<y)Sz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                z    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                  Z/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/librosa/core/constantq.pyr   r      s    `  
  	
    (  "       !" #     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	kD  r?t        j                  ||         }|j                  t        | ||||||||	|
|||
             |d	kD  r;|j                  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.
    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 #&?233D FOE { 1++%8 ((fEJGQ BGGBGGG$455JFN~./M=(KHqffU>23% $ /)!!	
$ QFF)&$3!-%!%%	
, &(2,*<*<==r?   )r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   c                n   |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  }|S t        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.
    rA   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  sV   @ |$~ 1_M}qww' #&?233DfoVE{ 1++%8((6EJGQ +

Iua y!I #		A 	RWWU^ H	 ..qvvB?	RWWWu_%%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                  |||	||      \  }}|6t        t        j                  |t        |      z   |z              }| d	d|f   } t        j                  |      }d}|g}|g}t        |dz
        D ]h  }|d
   dz  d
k(  r1|j                  d
|d
   dz         |j                  d
|d
   dz         ?|j                  d
|d
          |j                  d
|d
          j t        t!        ||            D ]M  \  }\  }}t#        ||||z  z
        }t%        ||z  ||z  |z         }t'        |||   ||||	||         \  }}} |j(                  j+                         }!dt        j,                  t/        j0                  t        j2                  |!            d
      z  }"|"|||   z  z  }"|
r't        j4                  d|!||   |"| d	|ddf   d      }#n"t        j4                  d|!|"| d	|ddf   d      }#t7        |#d||      }$t9        j:                  |$d||z  |dd      }$||$}0|d	d|$j                  d   fxx   |$z  cc<   P |J |rt/        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   H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     s0   d |$ #&?233D WWR[FBGGE&MO;<=IfoVE{ 1++%8//6EGX
 rwwW 5CDEc9H9ngggG #A $C<D9q=!7Q;!JJq#a&3,'KK47a<( JJq#a&!KK47# " (C7?E6/A2E)EF	 ?Q&!(;i(GH."I)
	5! KK'')	 		"**Y*? @qII
 	egbk))
 II%#r1*E II#Y
Ac2qjMTXE eFvUKEk
 9Ac$U[[_$$%.%q 8t ==OOAF+Hr?   r;   )r)   r*   r+   r,   r:   r<   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r:   r<   c                6   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  }||kD  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x t	        j6                  ||z        z  ccc |j9                  t;        ||#||"||             |dz  dk(  s|dz  }|dz  }t=        j>                  |dd|d      } tA        |||      }%|rXt!        j$                  ||||	||	      \  }}$t        jB                  ||%jD                  d      }|%t	        j6                  |      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. BGGE&MO;<=IOV,I|$~ 1_M}qww' #&?233D !'E &'(IFF9%F{ 1++%8$44!G] 3hGw/x7[\c[d e9 9
 	

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

 ..qvvB?	RWWWHr?   
   c
           
         t        j                  || ||d|||	      \  }
}|
j                  d   }|h|ddt        j                  t        j
                  |            z   z  k  r7t        ddt        j                  t        j
                  |            z   z        }|
|ddt        j                  f   t        |      z  z  }
t               }|j                  |
|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 KKNE%#!bggbggj>Q6R2R*S"SCA
(; <<=> 
WQ

]#eEl22E ,CQ/3EeqjA5E3E0EFI ""9xuMIeW$$r?   rY   c                 >   t        d | D              }t        | d   j                        }||d<   ||d<   t        j                  ||d      }|}| D ]I  }|j                  d   }||k  r|d| dd|f   |dd|ddf<   n|dd|f   |d||z
  |ddf<   ||z  }K |S )	z,Trim and stack a collection of CQT responsesc              3   :   K   | ]  }|j                   d      yw)rG   N)rv   ).0c_is     r>   	<genexpr>z__trim_stack.<locals>.<genexpr>H  s     48C#))B-8s   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     4844G!""#EE"IE"IhhuE5G C		" ;$'cTUHWH(<$=GC#qL!14S(7(]1CGCus*A-.u  Nr?   c                    t        | |||||      }|st        j                  |      }|j                  d|j                  d   |j                  d   f      }	t        j
                  |	j                  d   |j                  d   |	j                  d   f|j                        }
t        |	j                  d         D ]  }|j                  |	|         |
|<    t        |j                        }|j                  d   |d<   |
j                  |      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 FF1I 
BQWWR[1	2B((	!iooa("((2,7qwwK 288A;"r!u-A   ME"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   }  sf    As2772777]3J+K#Lq#PQTUUV ,HAx)3a78 "344r?   c                 ,   t        ||||      }|dkD  r}d|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((
772;** &s1vaj0BQ-{, 
 e-..NN(APT
 *++Ab*r?   )nopythonr   c                 L    | dk  ryd}| dz  dk(  r|dz  }| dz  } | dz  dk(  r|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     sD     	Av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                  j                         }nt	        |t
              r!t        j                  j                  |      }nZt	        |t        j                  j                  t        j                  j                  f      r|}nt        |       t        d|      |dkD  rt        j                  d| dd	       n|d
k  rt        d| d      t        j                  | j                  t        j                        }t        j                   |      }|dk(  rGt        j"                  dt        j$                  z  |j                  | 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x t        j.                  |      |z   z  c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     si   V |$ii##%	L#	&ii###6	L299#8#8")):M:M"N	Ol+88HIJJ!|(
 3$ $	

 
AEhZtTUU XXaggR\\2F
))F
CxKKBEE	CJJAGGJ,D DEq		q	uTF*GHII ((3-G6] J!+%
$ +772;!%
$ x1x<8EAAq	q	RVVF^c))	U Z 	F
'! r?   c                 Z    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     s7    $ 	
a/!"A==!Q$(q!tax0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 ! "    , " 4 4 Q Q
N
 R $(! #'!%!a	zza 	a 	a
 =
!a a a UOa a 5/a a a a a sma  I!a" ZZ#a aH R $(! #!%!t>	zzt> 	t> 	t>
 =
!t> t> t> UOt> t> 5/t> t> t> t> t> t>  I!t>" ZZ#t> t>n R $(! #!%Y	zzY 	Y 	Y
 =
!Y Y Y UOY Y 5/Y Y Y Y Y IY  ZZ!Y Yx R $(  !%^	zz^ 	^ 	^
 =
!^ ^ ^ ^ 5/^ ^ ^ ^ SM^ ^ I^  ZZ!^ ^B R $(/6!! #'!%%]	zz] 	] 	]
 =
!] ] S*U++,] E?] ] UO] ] 5/] ] ] ]  !]" sm#]$ I%]& ZZ'] ]@	 R 

,,
(% (%V2::(+4=ZZ: MQ&:5D d$  $ $( #!% " 	-M	zzM M 		M
 M =
!M M M M 5/M M M M M M  I!M" SM#M$ %M& 3-'M( c299(("))*=*==>)M. ZZ/M`2c 2bjj 2r?   