In [1]:
# inline plot
%matplotlib inline
# displaying output
import matplotlib.pyplot as plt
# audio library
import librosa
# display audio via IPython
import IPython.display
# numerical libaray
import numpy as np
In [2]:
# load the example track
y, sr = librosa.load(librosa.util.example_audio_file(), duration = 10.0)
In [3]:
# play back
IPython.display.Audio(data=y, rate=sr)
Out[3]:
In [5]:
o_env = librosa.onset.onset_strength(y, sr=sr)
onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)
In [6]:
D = np.abs(librosa.stft(y))**2
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.specshow(librosa.logamplitude(D, ref_power=np.max),x_axis='time', y_axis='log')
plt.title('Power spectrogram')
plt.subplot(2, 1, 2)
plt.plot(o_env, label='Onset strength')
plt.vlines(onset_frames, 0, o_env.max(), color='r', alpha=0.9, linestyle='--', label='Onsets')
plt.xticks([])
plt.axis('tight')
plt.legend(frameon=True, framealpha=0.75)
plt.show()
The librosa onset detection was impemented using the idea of Spectral Flux (see here) and a greedy search for peak picking.
In [ ]: