Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. def estimate_beats(audio_path, onset_type, plot=False):
  2.     """Compute beat positions using either a spectral flux or a machine learned onset novelty function,
  3.    followed by computing a tempogram and PLP.
  4.    
  5.    When onset_type = 'spectral_flux', use librosa.onset.onset strength to compute the novelty function.
  6.    When onset_type = 'machine_learning', use madmom.features.beats.RNNBeatProcessor() to compute the
  7.    novelty function.
  8.    
  9.    When `plot=True`, plot the onset novelty function and the estimated beats as vertical
  10.    lines on the same plot. Your plot should include all axis labels and the time stamps
  11.    should be in seconds.
  12.    
  13.    Parameters
  14.    ----------
  15.    audio_path : str
  16.        Path to input audio file
  17.    onset_type : str
  18.        One of 'spectral_flux' or 'machine_learning'
  19.    plot : bool
  20.        If True, plots the onset novelty curve and the estimated beat positions.
  21.  
  22.    Returns
  23.    -------
  24.    beat_times : 1-d np.array
  25.        Array of time stamps of the estimated beats in seconds.
  26.  
  27.    """
  28.     # use librosa.onset.onset_strength to compute the spectral flux onset novelty curve
  29.     # use madmom.features.beats.RNNBeatProcessor() to compute the machine learned novelty curve
  30.     # use librosa.beat.plp to compute the PLP function
  31.    
  32.     #load audio file
  33.     y, sr = librosa.load(audio_path, sr = 22050)
  34.    
  35.     #SPECTRAL FLUX
  36.     if onset_type == 'spectral_flux':
  37.         onset_env = librosa.onset.onset_strength(y=y, sr=sr)
  38.         #PLP
  39.         pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  40.         beat_times = np.flatnonzero(librosa.util.localmax(pulse))
  41.         #Time stamps
  42.         times = librosa.times_like(onset_env, sr=sr)            
  43.         times_pulse = librosa.times_like(pulse, sr=sr)
  44.         if plot is True:
  45.             ax = plt.subplot(2,1,1)
  46.             plt.plot(times, librosa.util.normalize(onset_env), label='Onset Envelope')
  47.             plt.vlines(times_pulse[beat_times], 0, 1, alpha=0.5, color='r', linestyle='--', label='Estimated Beats [PLP]')
  48.             plt.legend(frameon=True, framealpha=0.75)
  49.             plt.title('SF Onset Envelope')
  50.             plt.xlim(0, 20)
  51.             ax.xaxis.set_major_formatter(librosa.display.TimeFormatter())
  52.             plt.tight_layout()
  53.             plt.show()
  54.        
  55.            
  56.     #MACHINE LEARNING
  57.     if onset_type == 'machine_learning':
  58.         proc = madmom.features.beats.RNNBeatProcessor()
  59.         onset_env = proc(audio_path)
  60.        
  61.        
  62.         #samplestotime = librosa.core.samples_to_time(onset_env)
  63.         #print("onset_env = ", onset_env)
  64.         #print("len(onset_env) = ", len(onset_env))
  65.         #print("samplestotime = ", samplestotime)
  66.         #print("len(samplestotime) = ", len(samplestotime))
  67.        
  68.        
  69.         #PLP
  70.         pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  71.         beat_times = np.flatnonzero(librosa.util.localmax(pulse))
  72.         #Time stamps
  73.         times = librosa.times_like(onset_env, sr=sr)            
  74.         times_pulse = librosa.times_like(pulse, sr=sr)
  75.         if plot is True:
  76.             ax = plt.subplot(2,1,1)
  77.             plt.plot(times, librosa.util.normalize(onset_env), label='Onset Envelope')
  78.             plt.vlines(times_pulse[beat_times], 0, 1, alpha=0.5, color='r', linestyle='--', label='Estimated Beats [PLP]')
  79.             plt.legend(frameon=True, framealpha=0.75)
  80.             plt.title('ML Onset Envelope')
  81.             plt.xlim(0, 20)
  82.             ax.xaxis.set_major_formatter(librosa.display.TimeFormatter())
  83.             plt.tight_layout()
  84.             plt.show()
  85.        
  86.        
  87.     #print("beat_times = ", beat_times)
  88.     #print("len(beat_times) = ", len(beat_times))
  89.  
  90.     return beat_times
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement