Measure Tempo¶
DrumScript estimates the tempo (BPM) of any audio file using a spectral onset envelope approach.
Under the hood, ds.detect_tempo() works by:
Computing an onset strength envelope — a curve that peaks wherever a drum hit occurs.
Building a tempogram — a matrix that measures how periodic (i.e. “beat-like”) the onset envelope is at every possible BPM.
Summing the tempogram energy across time, then picking the BPM with the strongest peak — but only within a plausible musical range (60–240 BPM) to avoid artifacts.
If the audio is shorter than 1 second, tempo estimation is unreliable, so DrumScript defaults to 120 BPM.
(Note: We are using a copyright-free synthetic track for this demonstration.)
from IPython.display import Audio, display
import drumscript as ds
# 1. Load audio
audio_file_path = "audio/test_track_1.wav"
audio_file = ds.load_audio(audio_file_path)
print("Audio loaded!")
display(Audio(audio_file_path))
Audio loaded!
# 2. Detect tempo
bpm = ds.detect_tempo(audio_file_path)
print(f"Detected Tempo: {bpm:.2f} BPM")
Detected Tempo: 76.00 BPM