python-以同步方式检测拍子和播放(wav)文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-以同步方式检测拍子和播放(wav)文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2756字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试使用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)
内容总结
以上是互联网集市为您收集整理的python-以同步方式检测拍子和播放(wav)文件全部内容,希望文章能够帮你解决python-以同步方式检测拍子和播放(wav)文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。