I'm using the following code to get the BPMS of an WAV file: https://github.com/scaperot/the-BPM-detector-python/blob/master/bpm_detection/bpm_detection.py
I'm trying to call this script from my own script but since bpm_detection.py just printing (and not returning anything) I can get the bottom line value:
print 'Completed. Estimated Beats Per Minute:', bpm
I tried to edit bpm_detection.py script by adding a main() function that I can call. But it mess something up and bmp resulting in Nan:
def main(filename, window):
parser = argparse.ArgumentParser(description='Process .wav file to determine the Beats Per Minute.')
parser.add_argument('--filename',
help='.wav file for processing')
parser.add_argument('--window', type=float, default=3,
help='size of the the window (seconds) that will be scanned to determine the bpm. Typically less than 10 seconds. [3]')
#args = parser.parse_args()
samps, fs = read_wav(filename)
print ("testing")
#print (fs,samps)
data = []
correl = []
bpm = 0
n = 0
nsamps = len(samps)
print ("nsamps")
print(nsamps)
window_samps = int(window * fs)
print ("window, window samp")
print (window, window_samps)
print ("fs")
print (fs)
samps_ndx = 0 # first sample in window_ndx
max_window_ndx = nsamps / window_samps
print ("max_window_nds")
print (max_window_ndx)
bpms = numpy.zeros(max_window_ndx)
print ("bmps")
print (bpms)
# iterate through all windows
for window_ndx in range(0, int(max_window_ndx)):
# get a new set of samples
# print n,":",len(bpms),":",max_window_ndx,":",fs,":",nsamps,":",samps_ndx
data = samps[samps_ndx:samps_ndx + window_samps]
if not ((len(data) % window_samps) == 0):
raise AssertionError(str(len(data)))
bpm, correl_temp = bpm_detector(data, fs)
if bpm == None:
continue
bpms[window_ndx] = bpm
correl = correl_temp
# iterate at the end of the loop
samps_ndx = samps_ndx + window_samps;
n = n + 1; # counter for debug...
bpm = numpy.median(bpms)
print('Completed. Estimated Beats Per Minute:', bpm)
return bpm
As you could see, I added a lot of print to debug it. It seems that all the values are fine up until the following line where max_window_nds is getting 0.0:
max_window_ndx = nsamps / window_samps
print ("max_window_nds")
printing both nsamps and window_samps resulting in: 6195200 and 333333
So I'm failing to find what am I doing wrong. My final goal is to find a way to get bmp variable.