我用下面的代碼獲取一個WAV文件的BPMS: https://github.com/scaperot/the-BPM-detector-python/blob/master/bpm_detection/bpm_detection.py呼叫Python腳本,並得到一個特定的值
我想從我自己的劇本,但因爲調用此腳本bpm_detection.py只是打印(而不是返回任何東西),我可以得到的底線值:
print 'Completed. Estimated Beats Per Minute:', bpm
我試圖加入,我可以調用一個main()函數來編輯bpm_detection.py腳本。但它擺烏龍和BMP導致楠:
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
正如你可以看到,我加了很多打印的調試它。似乎所有的價值觀都很好,直到下面的行,其中max_window_nds越來越0.0:
max_window_ndx = nsamps/window_samps
print ("max_window_nds")
打印既nsamps和window_samps導致: 6195200和333333
所以我沒有找到什麼我做錯了嗎?我的最終目標是找到一種獲得bmp變量的方法。
你嘗試'max_window_ndx = float(nsamps)/ float(window_samps)'? –