In [1]:
import numpy, scipy, matplotlib.pyplot as plt, librosa, mir_eval, IPython.display, urllib
plt.rcParams['figure.figsize'] = (14, 4)
We will use librosa.beat.estimate_tempo
to estimate the global tempo in an audio file.
Download an audio file:
In [2]:
filename = '1_bar_funk_groove.mp3'
urllib.urlretrieve('http://audio.musicinformationretrieval.com/' + filename,
filename=filename)
Out[2]:
Load an audio file:
In [3]:
x, fs = librosa.load('1_bar_funk_groove.mp3')
IPython.display.Audio(x, rate=fs)
Out[3]:
Estimate the tempo:
In [4]:
onset_env = librosa.onset.onset_strength(x, sr=fs)
tempo = librosa.beat.estimate_tempo(onset_env, sr=fs, start_bpm=60)
print tempo
Visualize the tempo estimate on top of the input signal:
In [5]:
t = numpy.arange(0, len(x))/float(fs)
plt.plot(t, x, alpha=0.5)
plt.xlabel('Time (seconds)')
T = len(x)/float(fs)
seconds_per_beat = 60.0/tempo
beat_times = numpy.arange(0, T, seconds_per_beat)
plt.vlines(beat_times, -1, 1, color='r')
Out[5]:
Listen to the input signal with a click track using the tempo estimate:
In [6]:
x_with_beeps = mir_eval.sonify.clicks(beat_times, fs, length=len(x))
IPython.display.Audio(x + x_with_beeps, rate=fs)
Out[6]: