在同步mann中检测节拍和播放(wav)文件

2024-04-25 20:45:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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当控制台输出特定帧的结果时,该帧必须在扬声器上播放。


Tags: datalenlocalsyshistoryhistenergylast
3条回答

一个好的办法是尝试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)。

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 read或mic record的PyAudio示例将为您提供所需的信号数据。使用frombuffer()有效地创建NumPy数组

data = stream.read(CHUNK)
signal = numpy.frombuffer(data, numpy.int16)

一种简单的、非实时的方法

我对将控制台输出与实时音频同步并不乐观。我的方法会简单一点。在读取并处理该文件时,请将示例写入新的音频文件。每当检测到一个节拍时,在你正在写的音频中加入一些难以错过的声音,比如一个响亮的短正弦音调。这样,你就可以在听觉上评估结果的质量。

合成您的节拍指示器声音:

def testsignal(hz,seconds=5.,sr=44100.):
    '''
    Create a sine wave at hz for n seconds
    '''
    # cycles per sample
    cps = hz / sr
    # total samples
    ts = seconds * sr
    return np.sin(np.arange(0,ts*cps,cps) * (2*np.pi))

signal = testsignal(880,seconds = .02)

while循环中,如果检测到节拍,则将测试信号添加到输入帧,如果未检测到节拍,则保持帧不变。把这些帧写到一个文件中,听它来评估拍频检测的质量。

这是aubio库用来评估拍频检测结果的方法。请参阅文档here。特别感兴趣的是--output命令行选项的文档:

Save results in this file. The file will be created on the model of the input file. Results are marked by a very short wood-block sample.

优化

因为numpy已经是一个依赖项,所以使用它的功能来加速算法。您可以将sumsquared函数重写为:

def sumsquared(arr):
    return (arr**2).sum()

去掉Python for循环并将这些计算向下推到C代码中应该可以提高速度。

另外,查看this questionthis question以了解如何使用numpy.lib.stride_tricks方法对while循环中的局部到瞬时能量比较进行矢量化。

相关问题 更多 >