簡體   English   中英

如何使用 aubio 找到 .wav 的節奏?

[英]How to find the tempo of a .wav with aubio?

我想在 python 3.6 中檢測音頻文件的速度,但我不太了解關於 aubio 的文檔。 有人可以指出如何使用aubio或其他圖書館提取節奏嗎?

更新

此命令將為您提供整個文件的速度估計(在0.4.5可用):

aubio tempo foo.wav

aubiopython/demos有一個簡單的python/demosdemo_bpm_extract.py

最重要的部分是以下兩行,它們計算每個連續節拍之間的周期 ( 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存在的估計值。

我發現 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM