Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 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.  
  29.    
  30.     #Load audio
  31.     y, sr = librosa.load(audio_path)
  32.    
  33.     #Spectrogram for reference:
  34. #     D = np.abs(librosa.stft(y))
  35.  
  36.     if onset_type == 'spectral_flux':
  37.         #Onset envelope:
  38.         onset_env = librosa.onset.onset_strength(y=y, sr=sr)
  39.         times = librosa.times_like(onset_env)
  40. #         print('onset shape1:', onset_env.shape)
  41.        
  42.         #PLP function
  43.         plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  44.         beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
  45.  
  46.        
  47.         if (plot == True):
  48.             plt.figure(figsize=(20,10))
  49.             ax1 = plt.subplot(2, 1, 1)
  50. #             plt.axis('tight')
  51. #             librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), y_axis='log', x_axis='time')
  52. #             plt.title('Power spectrogram')
  53.            
  54.             #Plot onset
  55.             plt.subplot(2, 1, 1, sharex=ax1)
  56.             plt.ylabel('Onset & Est Beats')
  57.             plt.plot(times, librosa.util.normalize(onset_env))
  58.  
  59.             #Est beats:
  60.            
  61. #             times = librosa.times_like(onset_env, sr=sr)
  62.             plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='PLP Beats')
  63.  
  64.            
  65.         return times[beats_plp]
  66.            
  67.            
  68.            
  69.            
  70.            
  71.     if onset_type == 'machine_learning':
  72.         # use madmom.features.beats.RNNBeatProcessor() to compute the machine learned novelty curve
  73.         #Onset envelope:
  74.         proc = madmom.features.beats.RNNBeatProcessor()
  75.         onset_env = proc(audio)        
  76.         print('onset shape:', onset_env.shape)
  77. #         onset_env = librosa.samples_to_time(onset_env, sr)
  78. #         beat_samples = librosa.frames_to_samples(onset_env, 100)
  79. #         print('beat_samples shape', beat_samples.shape)
  80. #         times = librosa.times_like(onset_env, sr)#sr doesn't make a difference really
  81.         times = librosa.samples_to_time(onset_env, 480000)
  82.         print('time shape:', times.shape)
  83.        
  84. #         times_linspace = np.linspace(0, ( len(audio)/sr ) )
  85. #         print('linspace len: ', len(times_linspace))
  86.        
  87.         #PLP function
  88.         plp_function = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
  89.         beats_plp = np.flatnonzero(librosa.util.localmax(plp_function))
  90.  
  91.        
  92.         if plot is True:
  93.             plt.figure(figsize=(20,5))
  94.             plt.plot(times)
  95.             plt.vlines(times[beats_plp], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats [PLP]')
  96.             plt.legend(frameon=True, framealpha=0.75)
  97.             plt.title('Machine learning')
  98.        
  99.         return times[beats_plp]
  100.          
  101.    
  102.  
  103.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement