23def computeBeatHisto(x, f_s, cMethod='FFT', afWindow=None, iBlockLength=1024, iHopLength=8):
26 afWindow = ToolComputeHann(iBlockLength)
28 assert (afWindow.shape[0] == iBlockLength),
"parameter error: invalid window dimension"
31 x = ToolPreprocAudio(x)
34 [d, t, peaks] = computeNoveltyFunction(
'Flux', x, f_s, afWindow, iBlockLength, iHopLength)
38 r_dd = np.correlate(d, d,
"full") / np.dot(d, d)
39 r_dd = r_dd[np.arange(d.shape[0], r_dd.size)]
41 Bpm = np.flip(60 / t[np.arange(1, t.shape[0])])
44 elif cMethod ==
'FFT':
47 afWindow = np.zeros(2*iHistoLength)
48 afWindow[np.arange(0, iHistoLength)] = ToolComputeHann(iHistoLength)
49 f_s = f_s / iHopLength
50 if len(d) < 2 * iHistoLength:
51 d = [d, np.zeros([1, 2 * iHistoLength - len(d)])]
53 [D, f, t] = computeSpectrogram(d, f_s, afWindow, 2*iHistoLength, iHistoLength/4)
55 T = D.mean(axis=1, keepdims=
True)
59 lIdx = np.argwhere(Bpm < 30)[-1]
60 hIdx = np.argwhere(Bpm > 200)[0]
61 T = T[np.arange(lIdx, hIdx)]
62 Bpm = Bpm[np.arange(lIdx, hIdx)]
71 [f_s, afAudioData] = ToolReadAudio(cInPath)
73 [T, Bpm] = computeBeatHisto(afAudioData, f_s)
75 result = np.vstack((T, Bpm))
77 np.savetxt(cOutPath, result)