我正在尝试用python中的this节拍检测算法进行音频处理。我已经实现了上述文章中的第一个(非优化版本)。虽然它打印了一些结果,但我无法检测它是否有一定的准确性,因为我不知道如何播放声音。
目前,我正在使用Popen
在进入计算循环之前用歌曲异步启动我的媒体播放器,但是我不确定这个策略是否有效并且是否给出同步结果。
#!/usr/bin/python
import scipy.io.wavfile, numpy, sys, subprocess
# Some abstractions for computation
def sumsquared(arr):
sum = 0
for i in arr:
sum = sum + (i[0] * i[0]) + (i[1] * i[1])
return sum
if sys.argv.__len__() < 2:
print 'USAGE: wavdsp <wavfile>'
sys.exit(1)
numpy.set_printoptions(threshold='nan')
rate, data = scipy.io.wavfile.read(sys.argv[1])
# Beat detection algorithm begin
# the algorithm has been implemented as per GameDev Article
# Initialisation
data_len = data.__len__()
idx = 0
hist_last = 44032
instant_energy = 0
local_energy = 0
le_multi = 0.023219955 # Local energy multiplier ~ 1024/44100
# Play the song
p = subprocess.Popen(['audacious', sys.argv[1]])
while idx < data_len - 48000:
dat = data[idx:idx+1024]
history = data[idx:hist_last]
instant_energy = sumsquared(dat)
local_energy = le_multi * sumsquared(history)
print instant_energy, local_energy
if instant_energy > (local_energy * 1.3):
print 'Beat'
idx = idx + 1024
hist_last = hist_last + 1024 # Right shift history buffer
p.terminate()
为了以时间同步的方式获得音频输出和算法(控制台)输出,我可以对脚本进行哪些修改/添加?i、 e当控制台输出特定帧的结果时,该帧必须在扬声器上播放。
一个好的办法是尝试portaudio(pyaudio)来实时获取数据,然后您应该能够看到它是否匹配。
下面是一个很好的例子,使用来自带有pyaudio的麦克风的fft:
http://www.swharden.com/blog/2010-03-05-realtime-fft-graph-of-audio-wav-file-or-microphone-input-with-python-scipy-and-wckgraph/
工作节拍检测码(NumPy/PyAudio)
如果您使用的是NumPy,这段代码可能会有帮助。它假设信号(用PyAudio读取)是16位宽的整数。如果不是这样,请更改或删除signal.astype()并调整标准化除法器(此处为max int16)。
用于wav read或mic record的PyAudio示例将为您提供所需的信号数据。使用
frombuffer()
有效地创建NumPy数组一种简单的、非实时的方法
我对将控制台输出与实时音频同步并不乐观。我的方法会简单一点。在读取并处理该文件时,请将示例写入新的音频文件。每当检测到一个节拍时,在你正在写的音频中加入一些难以错过的声音,比如一个响亮的短正弦音调。这样,你就可以在听觉上评估结果的质量。
合成您的节拍指示器声音:
在
while
循环中,如果检测到节拍,则将测试信号添加到输入帧,如果未检测到节拍,则保持帧不变。把这些帧写到一个文件中,听它来评估拍频检测的质量。这是aubio库用来评估拍频检测结果的方法。请参阅文档here。特别感兴趣的是
--output
命令行选项的文档:优化
因为numpy已经是一个依赖项,所以使用它的功能来加速算法。您可以将
sumsquared
函数重写为:去掉Python for循环并将这些计算向下推到C代码中应该可以提高速度。
另外,查看this question或this question以了解如何使用
numpy.lib.stride_tricks
方法对while
循环中的局部到瞬时能量比较进行矢量化。相关问题 更多 >
编程相关推荐