SHARE
TWEET

Untitled

a guest Oct 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  29.    
  30.     #Load audio
  31.     audio, sr = librosa.load(audio_path, sr=None)
  32.  
  33.     if onset_type == 'spectral_flux':
  34.         #Onset envelope:
  35.         onset_env = librosa.onset.onset_strength(y=audio, sr=sr)
  36.         times = librosa.times_like(onset_env, sr)
  37.        
  38.         #PLP function
  39.         plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  40.         beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
  41.  
  42.        
  43.         if (plot == True):
  44.             plt.figure(figsize=(20,10))
  45.             ax1 = plt.subplot(2, 1, 1)
  46.  
  47.             #Plot onset
  48.             plt.subplot(2, 1, 1, sharex=ax1)
  49.             plt.ylabel('Onset & Est Beats')
  50.             plt.plot(times, librosa.util.normalize(onset_env))
  51.             plt.title('Spectral')
  52.  
  53.             #Est beats:
  54.            
  55.             plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='PLP Beats')
  56.  
  57.            
  58.         return times[beats_plp]
  59.            
  60.            
  61.            
  62.            
  63.     if onset_type == 'machine_learning':
  64.         # use madmom.features.beats.RNNBeatProcessor() to compute the machine learned novelty curve
  65.         #Onset envelope:
  66.         proc = madmom.features.beats.RNNBeatProcessor()
  67.         onset_env = proc(audio)        
  68.        
  69.         #New attempt at times:
  70.         times = np.linspace(0, len(audio)/sr, len(onset_env))
  71.  
  72.  
  73.         #Another attempt at times using frames_to_time:
  74.  
  75.         #PLP function
  76.         plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  77.         beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
  78.  
  79.        
  80.         if plot is True:
  81.             plt.figure(figsize=(20,5))
  82.             plt.plot(times, librosa.util.normalize(onset_env))
  83.             plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats [PLP]')
  84.             plt.legend(frameon=True, framealpha=0.75)
  85.             plt.ylabel('Onset & Est Beats')
  86.             plt.title('Machine learning')
  87.        
  88.         return times[beats_plp]
  89.          
  90.  
  91.    
  92.  
  93.     pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top