如何用aubio找到.wav的节奏?

时间:2017-03-21 17:49:29

标签: python audio analysis tempo aubio

我正在寻找在python 3.6中找到文件的速度,但我真的不了解有关aubio的文档。因此,如果有人知道如何通过aubio或其他图书馆获得节奏,我将非常感激他。

2 个答案:

答案 0 :(得分:1)

我发现Paul Brossier的这个code可以帮助你,在这里:

#! /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)

答案 1 :(得分:1)

已更新

此命令将为您提供整个文件的速度估计值(可在0.4.5中获得):

aubio tempo foo.wav

aubio python/demos demo_bpm_extract.py中有一个简单的演示。

最重要的部分是以下两行,它们计算每个连续节拍之间的时段(np.diff),将这些时段转换为bpm(60./),并取中位数({{1 }})作为最有可能的 bpm这一系列节拍的候选者:

np.median

请注意中位数比这里的平均值更适合,因为它总是会给出原始种群#!/usr/bin/env python import numpy as np bpms = 60./np.diff(beats) median_bpm = np.median(bpms) 中存在的估计值。