# HG changeset patch # User Chris Cannam # Date 1349707614 -3600 # Node ID 9973b7cd0d31a44e322a00dade423f1b6ef7da2f # Parent cb43d088e36923727f5cdf5a35030456a0cb66b2 Implement rest of tempo estimator diff -r cb43d088e369 -r 9973b7cd0d31 tempo_estimator.py --- a/tempo_estimator.py Mon Oct 08 15:42:07 2012 +0100 +++ b/tempo_estimator.py Mon Oct 08 15:46:54 2012 +0100 @@ -14,12 +14,23 @@ df[n] = value return df +def find_tempo_from_autocorrelation(acf, bpm_min, bpm_max, hops_per_sec): + lag_min = sp.bpm_to_lag(bpm_max, hops_per_sec) + lag_max = sp.bpm_to_lag(bpm_min, hops_per_sec) + acf_subset = acf[lag_min:lag_max] + peakidx = np.argmax(acf_subset) + lag_peak = lag_min + peakidx + return sp.lag_to_bpm(lag_peak, hops_per_sec) + def estimate_tempo_of_samples(samples, samplerate): """Given some audio samples and their samplerate, return their estimated tempo in bpm.""" hop = 512 df = detection_function(samples, hop) - acf = autocorrelation(df) - tempo = find_tempo_from_autocorrelation(acf) + acf = sp.autocorrelation(df) + bpm_min = 70.0 + bpm_max = 150.0 + hops_per_sec = samplerate / float(hop) + tempo = find_tempo_from_autocorrelation(acf, bpm_min, bpm_max, hops_per_sec) return tempo def estimate_tempo_of_file(filename):