Python中简单的节拍检测

时间:2018-12-11 03:36:24

标签: python audio processing minim aubio

我正在做一个涉及LED和音频处理的项目。我试图在Python中获得与Processing的Minim库相同的功能,但没有成功。 Minim的BeatDetect类具有isHat,isSnare等方法,可以轻松进行处理,因此可以打开相应的LED。

from aubio import source, pitch

def is_hat(frequency):
    lower = 3000
    upper = 5000
    return lower <= frequency <= upper


def is_kick(frequency):
    lower = 20
    upper = 100
    return lower <= frequency <= upper


def is_snare(frequency):
    lower = 300
    upper = 600
    return lower <= frequency <= upper


def read_audio(filename: str):
    downsample = 1
    samplerate = 44100 // downsample

    win_s = 4096 // downsample  # fft size
    hop_s = 512 // downsample  # hop size

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        pitch_s = pitch_o(samples)[0]
        # pitch = int(round(pitch))
        confidence = pitch_o.get_confidence()
        # if confidence < 0.8: pitch = 0.
        print('hat' if is_hat(pitch_s) else '')
        print('kick' if is_kick(pitch_s) else '')
        print('snare' if is_snare(pitch_s) else '')


        #print("%f %f %f" % (total_frames / float(samplerate), pitch_s, confidence))
        print(pitch_s)
        pitches += [pitch_s]
        confidences += [confidence]
        total_frames += read
        if read < hop_s:
            break

    if 0:
        sys.exit(0)


def main():
    read_audio("song.mp3")


if __name__ == '__main__':
    main()

在Minim的BeatDetect的文档([http://code.compartmental.net/minim/beatdetect_method_ishat.html][1])中,它说:

  

”在频率能量模式下,如果与   已检测到踩hat的频率范围。这个已经调好了   不能很好地与舞蹈/ Techno音乐配合使用,   其他风格的音乐。在声能模式下,这总是返回false。   “

我曾尝试研究小军鼓,大鼓和踩-的不同频率范围,但是is_kick()是唯一返回true的频率。我尝试使用aubio将当前频率与上述范围的频率进行比较,以使其与BeatDetect相同。我对音频知之甚少,因此将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用“高频内容”量度,这是一种用于非音高打击乐的方法。

有关鼓转录的研究论文。

Automatic Transcription of Drums and Vocalised percussion

您应该在信号处理堆栈交换中询问。