python-以同步方式检测拍子和播放(wav)文件
作者:互联网
我正在尝试使用this Beat Detection算法来处理python中的音频处理.我已经实现了上述文章中的第一个(非优化版本).虽然它可以打印出一些结果,但由于不知道如何播放声音,因此我无法检测它是否以某种精度工作.
当前,在进入计算循环之前,我正在使用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()
我可以对脚本进行哪些修改/添加,以便以时间同步的方式获取音频输出和算法(控制台)输出?即,当控制台输出特定帧的结果时,该帧必须在扬声器上播放.
解决方法:
工作节拍检测代码(NumPy / PyAudio)
如果您使用的是NumPy,则此代码可能会有所帮助.假设信号(用PyAudio读取)是16位宽的Int.如果不是这种情况,请更改或删除signal.astype()并调整归一化除法器(此处为最大int16).
class SimpleBeatDetection:
"""
Simple beat detection algorithm from
http://archive.gamedev.net/archive/reference/programming/features/beatdetection/index.html
"""
def __init__(self, history = 43):
self.local_energy = numpy.zeros(history) # a simple ring buffer
self.local_energy_index = 0 # the index of the oldest element
def detect_beat(self, signal):
samples = signal.astype(numpy.int) # make room for squares
# optimized sum of squares, i.e faster version of (samples**2).sum()
instant_energy = numpy.dot(samples, samples) / float(0xffffffff) # normalize
local_energy_average = self.local_energy.mean()
local_energy_variance = self.local_energy.var()
beat_sensibility = (-0.0025714 * local_energy_variance) + 1.15142857
beat = instant_energy > beat_sensibility * local_energy_average
self.local_energy[self.local_energy_index] = instant_energy
self.local_energy_index -= 1
if self.local_energy_index < 0:
self.local_energy_index = len(self.local_energy) - 1
return beat
用于wav读取或麦克风录制的PyAudio示例将为您提供所需的信号数据.使用frombuffer()有效创建NumPy数组
data = stream.read(CHUNK)
signal = numpy.frombuffer(data, numpy.int16)
标签:audio-processing,python,beat-detection 来源: https://codeday.me/bug/20191031/1979336.html