2017-01-16 57 views
1

我用下面的代碼獲取一個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變量的方法。

+0

你嘗試'max_window_ndx = float(nsamps)/ float(window_samps)'? –

回答

0

你確實需要直接訪問變量或者你還能解析您實際上是調用腳本什麼打印?因爲你總是可以使用subprocess模塊調用另一個腳本。

import subprocess 

p = subprocess.Popen(
    ['python','-u','bpm_detection.py', 
    'arg_to_bmp_detection.py'], stdout=subprocess.PIPE) 

prints_out = p.communicate() 

如果你想要的東西花哨(如進度條的包裝),使用一個線程異步過程的輸出,而你的主要程序做一些事情等待子進程來完成:Non-blocking read on a subprocess.PIPE in python

+0

什麼是PIPE?我收到了一個名稱錯誤。謝謝! –

+0

subprocess.PIPE對不起 – cowbert

+0

這不是我希望的最好的方法,但有了一些正則表達式,我能夠獲得所需的值。非常感謝你! –

0

bpm_detection.py中的代碼是爲Python 2編寫的,您可以在最終的打印語句中看到此代碼。您正在使用Python 3(也可以從您使用打印函數中看出 另一件已經從Python 2更改爲Python 3並且您必須移除的東西是除法運算符/。在Python 2中,如果兩個操作數都是整數它意味着整數除法,在Python 3中,它總是返回一個浮點數,你必須檢查所有的分區,如果一個整數除法意圖,然後使用Python 3整數除法運算符// 注意,這可能不是唯一的端口Python 3的

+0

我的確忘了提到我將腳本從Python 2轉換爲Python 3.我試圖將分割運算符更改爲//但它仍然導致0而不是0.0 –

+0

請注意,移植此代碼並不適合心靈的懦弱,如果你不知道涉及的數學。 – mkiever

+0

@Yakir Mordehay:仔細檢查。根據你在問題中給出的值,結果應該是'18'。 – mkiever