%matplotlib inline
import seaborn
import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa, IPython.display as ipd, urllib
Automatic detection of musical events in an audio signal is one of the most fundamental tasks in music information retrieval. Here, we will show how to detect an onset, the very instant that marks the beginning of the transient part of a sound, or the earliest moment at which a transient can be reliably detected.
For more reading, see this tutorial on onset detection by Juan Bello.
Download an audio file:
filename = 'Classic Rock Beat 06.wav'
urllib.urlretrieve(
'http://audio.musicinformationretrieval.com/Jam Pack 1/' + filename,
filename=filename
)
Load the audio file into the NumPy array x
and sampling rate fs
.
x, fs = librosa.load(filename)
print x.shape, fs
Plot the signal:
librosa.display.waveplot(x, fs)
Listen:
ipd.Audio(x, rate=fs)
librosa.onset.onset_detect
¶librosa.onset.onset_detect
returns the frame indices for estimated onsets in a signal:
onset_frames = librosa.onset.onset_detect(x, sr=fs)
print onset_frames # frame numbers of estimated onsets
Plot the onsets on top of a spectrogram of the audio:
S = librosa.stft(x)
logS = librosa.logamplitude(S)
librosa.display.specshow(logS, fs, x_axis='time')
plt.vlines(onset_frames, 0, logS.shape[0], color='k', alpha=0.8)
Let's also plot the onsets with the time-domain waveform. Convert onsets to units of seconds:
onset_times = librosa.frames_to_time(onset_frames)
print onset_times
Plot the waveform with the detected onsets:
librosa.display.waveplot(x, sr=fs)
plt.vlines(onset_times*1000, -0.8, 0.79, color='r', alpha=0.8)
# waveplot uses 1000 samples per second for visualization
We can add a click at the location of each detected onset.
clicks = librosa.clicks(frames=onset_frames, sr=fs, length=len(x))
Listen to the original audio plus the detected onsets:
ipd.Audio(x + clicks, rate=fs)