3 from aubio import source, tempo
4 from numpy import median, diff
6 def get_file_bpm(path, params = None):
7 """ Calculate the beats per minute (bpm) of a given file.
9 param: dictionary of parameters
14 win_s = params['win_s']
15 samplerate = params['samplerate']
16 hop_s = params['hop_s']
20 samplerate, win_s, hop_s = 4000, 128, 64
22 samplerate, win_s, hop_s = 8000, 512, 128
25 samplerate, win_s, hop_s = 44100, 1024, 512
27 s = source(path, samplerate, hop_s)
28 samplerate = s.samplerate
29 o = tempo("specdiff", win_s, hop_s, samplerate)
30 # List of beats, in samples
32 # Total number of frames read
39 this_beat = o.get_last_s()
40 beats.append(this_beat)
41 #if o.get_confidence() > .2 and len(beats) > 2.:
47 # Convert to periods and to bpm
50 print("few beats found in {:s}".format(path))
51 bpms = 60./diff(beats)
55 print("not enough beats found in {:s}".format(path))
58 if __name__ == '__main__':
60 for f in sys.argv[1:]:
62 print("{:6s} {:s}".format("{:2f}".format(bpm), f))