Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def estimate_beats(audio_path, onset_type, plot=False):
- """Compute beat positions using either a spectral flux or a machine learned onset novelty function,
- followed by computing a tempogram and PLP.
- When onset_type = 'spectral_flux', use librosa.onset.onset strength to compute the novelty function.
- When onset_type = 'machine_learning', use madmom.features.beats.RNNBeatProcessor() to compute the
- novelty function.
- When `plot=True`, plot the onset novelty function and the estimated beats as vertical
- lines on the same plot. Your plot should include all axis labels and the time stamps
- should be in seconds.
- Parameters
- ----------
- audio_path : str
- Path to input audio file
- onset_type : str
- One of 'spectral_flux' or 'machine_learning'
- plot : bool
- If True, plots the onset novelty curve and the estimated beat positions.
- Returns
- -------
- beat_times : 1-d np.array
- Array of time stamps of the estimated beats in seconds.
- """
- #Load audio
- audio, sr = librosa.load(audio_path, sr=None)
- if onset_type == 'spectral_flux':
- #Onset envelope:
- onset_env = librosa.onset.onset_strength(y=audio, sr=sr)
- times = librosa.times_like(onset_env, sr)
- #PLP function
- plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
- beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
- if (plot == True):
- plt.figure(figsize=(20,10))
- ax1 = plt.subplot(2, 1, 1)
- #Plot onset
- plt.subplot(2, 1, 1, sharex=ax1)
- plt.ylabel('Onset & Est Beats')
- plt.plot(times, librosa.util.normalize(onset_env))
- plt.title('Spectral')
- #Est beats:
- plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='PLP Beats')
- return times[beats_plp]
- if onset_type == 'machine_learning':
- # use madmom.features.beats.RNNBeatProcessor() to compute the machine learned novelty curve
- #Onset envelope:
- proc = madmom.features.beats.RNNBeatProcessor()
- onset_env = proc(audio)
- #New attempt at times:
- times = np.linspace(0, len(audio)/sr, len(onset_env))
- #Another attempt at times using frames_to_time:
- #PLP function
- plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
- beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
- if plot is True:
- plt.figure(figsize=(20,5))
- plt.plot(times, librosa.util.normalize(onset_env))
- plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats [PLP]')
- plt.legend(frameon=True, framealpha=0.75)
- plt.ylabel('Onset & Est Beats')
- plt.title('Machine learning')
- return times[beats_plp]
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement