Note
Go to the end to download the full example code.
Beat tracking with time-varying tempo
This notebook demonstrates how to use the beat tracker in situations where the tempo may change over time.
By default, the beat tracking algorithm [1] estimates a single tempo for the entire signal, though it does tolerate small amounts of fluctuation around that tempo. This is well suited for songs that have an approximately constant tempo, but where individual beats may not be exactly spaced accordingly. It is not well suited for songs that have radical shifts in tempo, e.g., entire sections that speed up or slow down, or gradual accelerations over long periods of time.
The implementation in librosa (librosa.beat.beat_track
) extends this algorithm
to support different tempo estimates at each time point in the signal, as
demonstrated below.
# Code source: Brian McFee
# License: ISC
import numpy as np
import librosa
import matplotlib.pyplot as plt
from IPython.display import Audio
A recording with dynamic tempo
The example recording included in this notebook consists of a drum beat that gradually increases in tempo from 30 BPM to 240 BPM over a 30-second time interval.
y, sr = librosa.load('audio/snare-accelerate.ogg')
We can visualize the spectrogram of this recording and listen to it. From the spectrogram, we can see that the spacing between drum beats becomes smaller over time, indicating a gradual increase of tempo.
fig, ax = plt.subplots()
melspec = librosa.power_to_db(librosa.feature.melspectrogram(y=y, sr=sr), ref=np.max)
librosa.display.specshow(melspec, y_axis='mel', x_axis='time', ax=ax)
ax.set(title='Mel spectrogram')
Audio(data=y, rate=sr)