我正在寻找在 python 3.6 中检测音频文件的速度,但我不太了解有关 aubio 的文档。有人可以指出如何用 aubio 或其他库提取速度吗?
问问题
3763 次
2 回答
6
更新
此命令将为您提供整个文件的速度估计(在 中可用0.4.5
):
aubio tempo foo.wav
aubio中有一个简单的演示:demo_bpm_extract.py。python/demos
最重要的部分是以下两行,它们计算每个连续节拍之间的周期 ( np.diff
),将这些周期转换为 bpm ( 60./
),并将中值 ( np.median
) 作为这一系列节拍的最可能bpm 候选:
#!/usr/bin/env python
import numpy as np
bpms = 60./np.diff(beats)
median_bpm = np.median(bpms)
请注意中位数如何比此处的均值更适合,因为它总是会给出原始总体中存在的估计值bpms
。
于 2017-03-22T16:31:07.057 回答
3
我发现 Paul Brossier 的这段代码可以帮助你,这里是:
#! /usr/bin/env python
from aubio import source, tempo
from numpy import median, diff
def get_file_bpm(path, params = None):
""" Calculate the beats per minute (bpm) of a given file.
path: path to the file
param: dictionary of parameters
"""
if params is None:
params = {}
try:
win_s = params['win_s']
samplerate = params['samplerate']
hop_s = params['hop_s']
except KeyError:
"""
# super fast
samplerate, win_s, hop_s = 4000, 128, 64
# fast
samplerate, win_s, hop_s = 8000, 512, 128
"""
# default:
samplerate, win_s, hop_s = 44100, 1024, 512
s = source(path, samplerate, hop_s)
samplerate = s.samplerate
o = tempo("specdiff", win_s, hop_s, samplerate)
# List of beats, in samples
beats = []
# Total number of frames read
total_frames = 0
while True:
samples, read = s()
is_beat = o(samples)
if is_beat:
this_beat = o.get_last_s()
beats.append(this_beat)
#if o.get_confidence() > .2 and len(beats) > 2.:
# break
total_frames += read
if read < hop_s:
break
# Convert to periods and to bpm
if len(beats) > 1:
if len(beats) < 4:
print("few beats found in {:s}".format(path))
bpms = 60./diff(beats)
b = median(bpms)
else:
b = 0
print("not enough beats found in {:s}".format(path))
return b
if __name__ == '__main__':
import sys
for f in sys.argv[1:]:
bpm = get_file_bpm(f)
print("{:6s} {:s}".format("{:2f}".format(bpm), f))
这是关键部分:
bpms = 60./np.diff(beats)
median_bpm = np.median(bpms)
于 2017-03-21T18:04:29.907 回答