
    PiT                      4   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 dd	lmZmZmZmZmZ g d
Zdddddddddd	deej                 dedeej                 dededeej                 dededededej        fdZddddddddddd
deej                 dedeej                 ded ed!eej                 d"ed#ed$ee         d%eeeef                  dedej        fd&Zd'ej        dej        dej        fd(Z ed)*          dddd+dddddddddd,deej                 dedeej                 d-ededed ed!eej                 d"ed#ed$ee         d%eeeef                  d.eeee         ee         f                  dedej        fd/            ZdS )0z
Onset detection
===============
.. autosummary::
    :toctree: generated/

    onset_detect
    onset_backtrack
    onset_strength
    onset_strength_multi
    N   )cache)core)util)ParameterError)melspectrogram)AnyCallableOptionalUnionSequence)onset_detectonset_strengthonset_strength_multionset_backtracki"V  i   FframesT)	ysronset_envelope
hop_length	backtrackenergyunits	normalizesparser   r   r   r   r   r   r   r   r   kwargsreturnc        	         T   |#| t          d          t          | ||          }|rI|t          j        |dd          z
  }|t          j        |dd          t          j        |          z   z  }|J |                                r&t          j        t          j	        |                    s;|rt          j
        g t                    }
nt          j        |t                    }
n|	                    dd	|z  |z             |	                    d
d|z  |z  dz              |	                    dd|z  |z             |	                    dd|z  |z  dz              |	                    dd	|z  |z             |	                    dd           t          j        |f|dd|	}
|r)|st          d          ||}|J t!          |
|          }
|rT|dk    rnM|dk    rt#          j        |
|          }
n0|dk    rt#          j        |
||          }
nt          d|           |
S )a  Locate note onset events by picking peaks in an onset strength envelope.

    The `peak_pick` parameters were chosen by large-scale hyper-parameter
    optimization over the dataset provided by [#]_.

    .. [#] https://github.com/CPJKU/onset_db

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

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

    onset_envelope : np.ndarray [shape=(..., m)]
        (optional) pre-computed onset strength envelope

    hop_length : int > 0 [scalar]
        hop length (in samples)

    units : {'frames', 'samples', 'time'}
        The units to encode detected onset events in.
        By default, 'frames' are used.

    backtrack : bool
        If ``True``, detected onset events are backtracked to the nearest
        preceding minimum of ``energy``.

        This is primarily useful when using onsets as slice points for segmentation.

        .. note:: backtracking is only supported if ``sparse=True``.

    energy : np.ndarray [shape=(m,)] (optional)
        An energy function to use for backtracking detected onset events.
        If none is provided, then ``onset_envelope`` is used.

    normalize : bool
        If ``True`` (default), normalize the onset envelope to have minimum of 0 and
        maximum of 1 prior to detection.  This is helpful for standardizing the
        parameters of `librosa.util.peak_pick`.

        Otherwise, the onset envelope is left unnormalized.

    sparse : bool
        If ``True`` (default), detections are returned as an array of frames,
        samples, or time indices (as specified by ``units=``).

        If ``False``, detections are encoded as a dense boolean array where
        ``onsets[n]`` is True if there's an onset at frame index ``n``.

        .. note:: multi-channel input is only supported if ``sparse=False``.

    **kwargs : additional keyword arguments
        Additional parameters for peak picking.

        See `librosa.util.peak_pick` for details.

    Returns
    -------
    onsets : np.ndarray [shape=(n_onsets,) or onset_envelope.shape]
        estimated positions of detected onsets, in whichever units
        are specified.  By default, frame indices.

        If `sparse=False`, `onsets[..., n]` indicates an onset
        detection at frame index `n`.

        .. note::
            If no onset strength could be detected, onset_detect returns
            an empty array (sparse=True) or all-False array (sparse=False).

    Raises
    ------
    ParameterError
        if neither ``y`` nor ``onsets`` are provided

        or if ``units`` is not one of 'frames', 'samples', or 'time'

    See Also
    --------
    onset_strength : compute onset strength per-frame
    onset_backtrack : backtracking onset events
    librosa.util.peak_pick : pick peaks from a time series

    Examples
    --------
    Get onset times from a signal

    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> librosa.onset.onset_detect(y=y, sr=sr, units='time')
    array([0.07 , 0.232, 0.395, 0.604, 0.743, 0.929, 1.045, 1.115,
           1.416, 1.672, 1.881, 2.043, 2.206, 2.368, 2.554, 3.019])

    Or use a pre-computed onset envelope

    >>> o_env = librosa.onset.onset_strength(y=y, sr=sr)
    >>> times = librosa.times_like(o_env, sr=sr)
    >>> onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)

    >>> import matplotlib.pyplot as plt
    >>> D = np.abs(librosa.stft(y))
    >>> fig, ax = plt.subplots(nrows=2, sharex=True)
    >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
    ...                          x_axis='time', y_axis='log', ax=ax[0], sr=sr)
    >>> ax[0].set(title='Power spectrogram')
    >>> ax[0].label_outer()
    >>> ax[1].plot(times, o_env, label='Onset strength')
    >>> ax[1].vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9,
    ...            linestyle='--', label='Onsets')
    >>> ax[1].legend()
    Nz$y or onset_envelope must be provided)r   r   r   T)keepdimsaxis)dtypepre_maxgQ?post_max        r   pre_avgg?post_avgwaitdeltagQ?)r   r!   z3onset backtracking is only supported if sparse=Truer   samples)r   time)r   r   zInvalid unit type: )r   r   npminmaxr   tinyanyallisfinitearrayint
zeros_likebool
setdefault	peak_pickr   r   frames_to_samplesframes_to_time)r   r   r   r   r   r   r   r   r   r   onsetss              a/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/librosa/onset.pyr   r      s   z 9 !GHHH'!zJJJ  e ("&$UW*X*X*XX 	"&$RHHH49UcKdKddd %%%  5rvbk..I.I'J'J 5 	?Xb,,,FF]>>>>FF 	)TBY*%<===*dRi:&=&ABBB)TBY*%<===*dRi:&=&ABBB&$)z"9:::'4((( QvBQQ&QQ  	5 \$%Z[[[~'%%%$VV44F @Hi+FzJJJFFf__(J2NNNFF !>u!>!>???M    )
r   r   Slagmax_sizerefdetrendcenterfeature	aggregater>   r?   r@   rA   rB   rC   rD   rE   c        
         r    |	du rt          d          t          d| |||||||||	dd|
}|ddddf         S )u  Compute a spectral flux onset strength envelope.

    Onset strength at time ``t`` is determined by::

        mean_f max(0, S[f, t] - ref[f, t - lag])

    where ``ref`` is ``S`` after local max filtering along the frequency
    axis [#]_.

    By default, if a time series ``y`` is provided, S will be the
    log-power Mel spectrogram.

    .. [#] Böck, Sebastian, and Gerhard Widmer.
           "Maximum filter vibrato suppression for onset detection."
           16th International Conference on Digital Audio Effects,
           Maynooth, Ireland. 2013.

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

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

    S : np.ndarray [shape=(..., d, m)]
        pre-computed (log-power) spectrogram

    lag : int > 0
        time lag for computing differences

    max_size : int > 0
        size (in frequency bins) of the local max filter.
        set to `1` to disable filtering.

    ref : None or np.ndarray [shape=(..., d, m)]
        An optional pre-computed reference spectrum, of the same shape as ``S``.
        If not provided, it will be computed from ``S``.
        If provided, it will override any local max filtering governed by ``max_size``.

    detrend : bool [scalar]
        Filter the onset strength to remove the DC component

    center : bool [scalar]
        Shift the onset function by ``n_fft // (2 * hop_length)`` frames.
        This corresponds to using a centered frame analysis in the short-time Fourier
        transform.

    feature : function
        Function for computing time-series features, eg, scaled spectrograms.
        By default, uses `librosa.feature.melspectrogram` with ``fmax=sr/2``

    aggregate : function
        Aggregation function to use when combining onsets
        at different frequency bins.

        Default: `np.mean`

    **kwargs : additional keyword arguments
        Additional parameters to ``feature()``, if ``S`` is not provided.

    Returns
    -------
    onset_envelope : np.ndarray [shape=(..., m,)]
        vector containing the onset strength envelope.
        If the input contains multiple channels, then onset envelope is computed for each channel.

    Raises
    ------
    ParameterError
        if neither ``(y, sr)`` nor ``S`` are provided

        or if ``lag`` or ``max_size`` are not positive integers

    See Also
    --------
    onset_detect
    onset_strength_multi

    Examples
    --------
    First, load some audio and plot the spectrogram

    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('trumpet'), duration=3)
    >>> D = np.abs(librosa.stft(y))
    >>> times = librosa.times_like(D, sr=sr)
    >>> fig, ax = plt.subplots(nrows=2, sharex=True)
    >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
    ...                          y_axis='log', x_axis='time', ax=ax[0], sr=sr)
    >>> ax[0].set(title='Power spectrogram')
    >>> ax[0].label_outer()

    Construct a standard onset function

    >>> onset_env = librosa.onset.onset_strength(y=y, sr=sr)
    >>> ax[1].plot(times, 2 + onset_env / onset_env.max(), alpha=0.8,
    ...            label='Mean (mel)')

    Median aggregation, and custom mel options

    >>> onset_env = librosa.onset.onset_strength(y=y, sr=sr,
    ...                                          aggregate=np.median,
    ...                                          fmax=8000, n_mels=256)
    >>> ax[1].plot(times, 1 + onset_env / onset_env.max(), alpha=0.8,
    ...            label='Median (custom mel)')

    Constant-Q spectrogram instead of Mel

    >>> C = np.abs(librosa.cqt(y=y, sr=sr))
    >>> onset_env = librosa.onset.onset_strength(sr=sr, S=librosa.amplitude_to_db(C, ref=np.max))
    >>> ax[1].plot(times, onset_env / onset_env.max(), alpha=0.8,
    ...          label='Mean (CQT)')
    >>> ax[1].legend()
    >>> ax[1].set(ylabel='Normalized strength', yticks=[])
    FzPaggregate parameter cannot be False when computing full-spectrum onset strength.N)r   r   r>   r?   r@   rA   rB   rC   rD   rE   channels.r    )r   r   )r   r   r>   r?   r@   rA   rB   rC   rD   rE   r   odf_alls               r<   r   r      s    D E^
 
 	
 # 

   G 31119r=   eventsc                     t          j        |dd         |dd         k    |dd         |dd         k     z            }t          j        d|z   d          }|t          j        | |d	                   }|S )
a	  Backtrack detected onset events to the nearest preceding local
    minimum of an energy function.

    This function can be used to roll back the timing of detected onsets
    from a detected peak amplitude to the preceding minimum.

    This is most useful when using onsets to determine slice points for
    segmentation, as described by [#]_.

    .. [#] Jehan, Tristan.
           "Creating music by listening"
           Doctoral dissertation
           Massachusetts Institute of Technology, 2005.

    Parameters
    ----------
    events : np.ndarray, dtype=int
        List of onset event frame indices, as computed by `onset_detect`
    energy : np.ndarray, shape=(m,)
        An energy function

    Returns
    -------
    events_backtracked : np.ndarray, shape=events.shape
        The input events matched to nearest preceding minima of ``energy``.

    Examples
    --------
    Backtrack the events using the onset envelope

    >>> y, sr = librosa.load(librosa.ex('trumpet'), duration=3)
    >>> oenv = librosa.onset.onset_strength(y=y, sr=sr)
    >>> times = librosa.times_like(oenv, sr=sr)
    >>> # Detect events without backtracking
    >>> onset_raw = librosa.onset.onset_detect(onset_envelope=oenv,
    ...                                        backtrack=False)
    >>> onset_bt = librosa.onset.onset_backtrack(onset_raw, oenv)

    Backtrack the events using the RMS values

    >>> S = np.abs(librosa.stft(y=y))
    >>> rms = librosa.feature.rms(S=S)
    >>> onset_bt_rms = librosa.onset.onset_backtrack(onset_raw, rms[0])

    Plot the results

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=3, sharex=True)
    >>> librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max),
    ...                          y_axis='log', x_axis='time', ax=ax[0])
    >>> ax[0].label_outer()
    >>> ax[1].plot(times, oenv, label='Onset strength')
    >>> ax[1].vlines(librosa.frames_to_time(onset_raw), 0, oenv.max(), label='Raw onsets')
    >>> ax[1].vlines(librosa.frames_to_time(onset_bt), 0, oenv.max(), label='Backtracked', color='r')
    >>> ax[1].legend()
    >>> ax[1].label_outer()
    >>> ax[2].plot(times, rms[0], label='RMS')
    >>> ax[2].vlines(librosa.frames_to_time(onset_bt_rms), 0, rms.max(), label='Backtracked (RMS)', color='r')
    >>> ax[2].legend()
    r   r   N   r   )x_minF)right)r,   flatnonzeror   
fix_framesmatch_events)rJ   r   minimaresultss       r<   r   r   q  s    @ ^VAbD\VCRC[8VAbD\FSTSUSUJ=VWXXF _QZq111F !!266!O!O!OPGNr=      )leveli   )r   r   r>   n_fftr   r?   r@   rA   rB   rC   rD   rE   rG   rW   rG   c           
      L   |
 t           }
|                    dd|z             |t          j        }t	          j        |          st          d| d          t	          j        |          st          d| d          |3t          j         |
d| |||d|          }t          j	        |          }|J t          j
        |          }|,|dk    r|}nRt          j                            ||d	
          }n/|j        |j        k    rt          d|j         d|j                   |d|df         |dd| f         z
  }t          j        d|          }d}|t!          d          g}nd}t#          |          rt	          j        ||||d	          }|}|	r||d|z  z  z  }d |j        D             }t'          |          df|d<   t          j        ||d          }|r't          j                            ddgddg|d
          }|	r|dd|j        d         f         }|S )a  Compute a spectral flux onset strength envelope across multiple channels.

    Onset strength for channel ``i`` at time ``t`` is determined by::

        mean_{f in channels[i]} max(0, S[f, t+1] - S[f, t])

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

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

    S : np.ndarray [shape=(..., d, m)]
        pre-computed (log-power) spectrogram

    n_fft : int > 0 [scalar]
        FFT window size for use in ``feature()`` if ``S`` is not provided.

    hop_length : int > 0 [scalar]
        hop length for use in ``feature()`` if ``S`` is not provided.

    lag : int > 0
        time lag for computing differences

    max_size : int > 0
        size (in frequency bins) of the local max filter.
        set to `1` to disable filtering.

    ref : None or np.ndarray [shape=(d, m)]
        An optional pre-computed reference spectrum, of the same shape as ``S``.
        If not provided, it will be computed from ``S``.
        If provided, it will override any local max filtering governed by ``max_size``.

    detrend : bool [scalar]
        Filter the onset strength to remove the DC component

    center : bool [scalar]
        Shift the onset function by ``n_fft // (2 * hop_length)`` frames.
        This corresponds to using a centered frame analysis in the short-time Fourier
        transform.

    feature : function
        Function for computing time-series features, eg, scaled spectrograms.
        By default, uses `librosa.feature.melspectrogram` with ``fmax=sr/2``

        Must support arguments: ``y, sr, n_fft, hop_length``

    aggregate : function or False
        Aggregation function to use when combining onsets
        at different frequency bins.

        If ``False``, then no aggregation is performed.

        Default: `np.mean`

    channels : list or None
        Array of channel boundaries or slice objects.
        If `None`, then a single channel is generated to span all bands.

    **kwargs : additional keyword arguments
        Additional parameters to ``feature()``, if ``S`` is not provided.

    Returns
    -------
    onset_envelope : np.ndarray [shape=(..., n_channels, m)]
        array containing the onset strength envelope for each specified channel

    Raises
    ------
    ParameterError
        if neither ``(y, sr)`` nor ``S`` are provided

    See Also
    --------
    onset_strength

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

    Examples
    --------
    First, load some audio and plot the spectrogram

    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('choice'), duration=5)
    >>> D = np.abs(librosa.stft(y))
    >>> fig, ax = plt.subplots(nrows=2, sharex=True)
    >>> img1 = librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
    ...                          y_axis='log', x_axis='time', ax=ax[0])
    >>> ax[0].set(title='Power spectrogram')
    >>> ax[0].label_outer()
    >>> fig.colorbar(img1, ax=[ax[0]], format="%+2.f dB")

    Construct a standard onset function over four sub-bands

    >>> onset_subbands = librosa.onset.onset_strength_multi(y=y, sr=sr,
    ...                                                     channels=[0, 32, 64, 96, 128])
    >>> img2 = librosa.display.specshow(onset_subbands, x_axis='time', ax=ax[1])
    >>> ax[1].set(ylabel='Sub-bands', title='Sub-band onset strength')
    >>> fig.colorbar(img2, ax=[ax[1]])
    Nfmaxg      ?zlag=z must be a positive integerz	max_size=)r   r   rW   r   r   rL   )r!   zReference spectrum shape z must match input spectrum .r%   TF)rE   padr!   rM   c                     g | ]}d S ))r   r   rH   ).0_s     r<   
<listcomp>z(onset_strength_multi.<locals>.<listcomp>u  s    ///!v///r=   r   r   constant)modeg      ?g      gGzrH   )r   r7   r,   meanr   is_positive_intr   absr   power_to_db
atleast_2dscipyndimagemaximum_filter1dshapemaximumslicecallablesyncr4   rZ   signallfilter)r   r   r>   rW   r   r?   r@   rA   rB   rC   rD   rE   rG   r   	onset_envrZ   	pad_widthpaddings                     r<   r   r     s   t  &#(+++G	$$ FDCDDDEEE)) PNNNNOOO 	yF77TQ2UzTTVTTUU Q === 	aA
 {q==CC-00H20FFCC	ag		W	WWagWW
 
 	

 #stt)s3#:.I 
3	**I C$KK=	 
Ix9#B
 
 
	
 I /Uq:~..	//y///Gy>>1%GBKy'
;;;I  XL((#tsElITV(WW	  2c=QWR[=01	r=   )__doc__numpyr,   rf   _cacher    r   r   util.exceptionsr   feature.spectralr   typingr	   r
   r   r   r   __all__ndarrayfloatr4   r6   strr   r   r   rk   r   rH   r=   r<   <module>r~      s   
 
                        + + + + + + , , , , , , ; ; ; ; ; ; ; ; ; ; ; ; ; ;
W
W
W
 #+/#'x x x
x 	x RZ(	x
 x x RZ x x x x x Zx x x xz #" $"&15V V V
V 	V 
	V
 
V V 
"*	V V V hV hn-.V V ZV V V VrHBJ H
 Hrz H H H HV R #" $"&15@DD D D
D 	D 
	D
 D D 
D D 
"*	D D D hD hn-.D uXc]HUO;<=D D  Z!D D D D D Dr=   