1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#! /usr/bin/env python
from aubio import source, tempo
from numpy import median, diff
def get_file_bpm(path, params=None):
""" Calculate the beats per minute (bpm) of a given file.
path: path to the file
param: dictionary of parameters
"""
if params is None:
params = {}
# default:
samplerate, win_s, hop_s = 44100, 1024, 512
if 'mode' in params:
if params.mode in ['super-fast']:
# super fast
samplerate, win_s, hop_s = 4000, 128, 64
elif params.mode in ['fast']:
# fast
samplerate, win_s, hop_s = 8000, 512, 128
elif params.mode in ['default']:
pass
else:
raise ValueError("unknown mode {:s}".format(params.mode))
# manual settings
if 'samplerate' in params:
samplerate = params.samplerate
if 'win_s' in params:
win_s = params.win_s
if 'hop_s' in params:
hop_s = params.hop_s
s = source(path, samplerate, hop_s)
samplerate = s.samplerate
o = tempo("specdiff", win_s, hop_s, samplerate)
# List of beats, in samples
beats = []
# Total number of frames read
total_frames = 0
while True:
samples, read = s()
is_beat = o(samples)
if is_beat:
this_beat = o.get_last_s()
beats.append(this_beat)
#if o.get_confidence() > .2 and len(beats) > 2.:
# break
total_frames += read
if read < hop_s:
break
def beats_to_bpm(beats, path):
# if enough beats are found, convert to periods then to bpm
if len(beats) > 1:
if len(beats) < 4:
print("few beats found in {:s}".format(path))
bpms = 60./diff(beats)
return median(bpms)
else:
print("not enough beats found in {:s}".format(path))
return 0
return beats_to_bpm(beats, path)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--mode',
help="mode [default|fast|super-fast]",
dest="mode", default='default')
parser.add_argument('sources',
nargs='+',
help="input_files")
args = parser.parse_args()
for f in args.sources:
bpm = get_file_bpm(f, params = args)
print("{:6s} {:s}".format("{:2f}".format(bpm), f))
|