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.
- """
- # use librosa.onset.onset_strength to compute the spectral flux onset novelty curve
- # use madmom.features.beats.RNNBeatProcessor() to compute the machine learned novelty curve
- # use librosa.beat.plp to compute the PLP function
- #load audio file
- y, sr = librosa.load(audio_path, sr = 22050)
- #SPECTRAL FLUX
- if onset_type == 'spectral_flux':
- onset_env = librosa.onset.onset_strength(y=y, sr=sr)
- #PLP
- pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
- beat_times = np.flatnonzero(librosa.util.localmax(pulse))
- #Time stamps
- times = librosa.times_like(onset_env, sr=sr)
- times_pulse = librosa.times_like(pulse, sr=sr)
- if plot is True:
- ax = plt.subplot(2,1,1)
- plt.plot(times, librosa.util.normalize(onset_env), label='Onset Envelope')
- plt.vlines(times_pulse[beat_times], 0, 1, alpha=0.5, color='r', linestyle='--', label='Estimated Beats [PLP]')
- plt.legend(frameon=True, framealpha=0.75)
- plt.title('SF Onset Envelope')
- plt.xlim(0, 20)
- ax.xaxis.set_major_formatter(librosa.display.TimeFormatter())
- plt.tight_layout()
- plt.show()
- #MACHINE LEARNING
- if onset_type == 'machine_learning':
- proc = madmom.features.beats.RNNBeatProcessor()
- onset_env = proc(audio_path)
- #samplestotime = librosa.core.samples_to_time(onset_env)
- #print("onset_env = ", onset_env)
- #print("len(onset_env) = ", len(onset_env))
- #print("samplestotime = ", samplestotime)
- #print("len(samplestotime) = ", len(samplestotime))
- #PLP
- pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
- beat_times = np.flatnonzero(librosa.util.localmax(pulse))
- #Time stamps
- times = librosa.times_like(onset_env, sr=sr)
- times_pulse = librosa.times_like(pulse, sr=sr)
- if plot is True:
- ax = plt.subplot(2,1,1)
- plt.plot(times, librosa.util.normalize(onset_env), label='Onset Envelope')
- plt.vlines(times_pulse[beat_times], 0, 1, alpha=0.5, color='r', linestyle='--', label='Estimated Beats [PLP]')
- plt.legend(frameon=True, framealpha=0.75)
- plt.title('ML Onset Envelope')
- plt.xlim(0, 20)
- ax.xaxis.set_major_formatter(librosa.display.TimeFormatter())
- plt.tight_layout()
- plt.show()
- #print("beat_times = ", beat_times)
- #print("len(beat_times) = ", len(beat_times))
- return beat_times
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement