Wea*_*ped 4 python audio-processing 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()
Run Code Online (Sandbox Code Playgroud)
我可以对脚本进行哪些修改/添加,以便以时间同步的方式获取音频输出和算法(控制台)输出?即当控制台输出特定帧的结果时,该帧必须在扬声器上播放。
如果您使用 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
Run Code Online (Sandbox Code Playgroud)
用于 wav 读取或麦克风记录的 PyAudio 示例将为您提供所需的信号数据。使用以下命令高效创建 NumPy 数组frombuffer()
data = stream.read(CHUNK)
signal = numpy.frombuffer(data, numpy.int16)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10699 次 |
最近记录: |