How does librosa estimate tempo?

1.1k views Asked by At

I've inputted artifically made music with 120 bpm into:

y, sr = librosa.load(sys.argv[1])
tempo, beats = librosa.beat.beat_track(y,sr)
print("Tempo 1:", tempo)
first_beat_time, last_beat_time = librosa.frames_to_time((beats[0],beats[-1]),sr=sr)
print("Tempo 2:", 60/((last_beat_time-first_beat_time)/(len(beats)-1)))

With the output:

Tempo 1: 117.45383522727273
Tempo 2: 120.03683283914009

Shouldn't those numbers be the same, and almost equal to 120?

1

There are 1 answers

0
Lukasz Tracewski On

The algorithm is described in detail in the Beat Tracking by Dynamic Programming paper, as cited in the librosa docs. In essence:

  1. Estimate a global tempo.
  2. Use this tempo to construct a transition cost function.
  3. Use dynamic programming to find the best-scoring set of beat times that reflect the tempo as well as corresponding to moments of high "onset strength" in a function derived from the audio.

The algorithm is deterministic, but in order to get precisely the same result, you'd need to make sure that exactly the same frames fit in a processing window (which they don't in your case).