唱歌/哼唱音高识别-python

时间:2020-03-13 01:39:50

标签: python pitch-tracking pitch-detection

因此,我使用名为parselmouth的模块制作了该音高识别程序。 它适用于钢琴,长号和长笛等乐器,但不适用于唱歌(拉拉拉,ahhh,tatata等),嗡嗡声或吹口哨。

import parselmouth as pm
import winsound
from midiutil import MIDIFile
from math import log


def getmidinote(frequency):
    return 69+round(12*log(frequency/440, 2))   #using the formula piano key = 12log2(frequency/440) + 49


def midify(path, bpm):
    notelength = int(1500/bpm)  #one quarter beat per 15/bpm sec or 15000/bpm ms. each element in frequency is 10ms. 1500/bpm element per 1/4 beat
    sound = pm.Sound(path)
    time, frequency = sound.xs(), sound.to_pitch().selected_array['frequency'][::notelength]
    pitch = [getmidinote(f) if f>0 else 0 for f in frequency]
    #combine duplicate note as one
    duration = 1
    notes = []
    for i in range(1, len(pitch)):
        if pitch[i]==pitch[i-1]:    #if the previous note and current note is the same
            duration += 1
        else:
            if duration!=1: 
                notes.append([pitch[i-1], duration])
            else:
                notes[-1][1] += 1   #if the note is only 1 duration, just add it to last note's duration
            duration = 1
    notes.append((pitch[-1], duration)) #this makes sure the last note gets added
    print(notes)
    #play output
    for i in notes:
        p, d = i[0]-20, i[1]
        if p==-20:
            winsound.Beep(50, d*10*notelength)
        else:
            winsound.Beep(int(440*(2**((p-49)/12))), d*10*notelength)
    #convert to midi
    midi = MIDIFile(1, file_format=1, ticks_per_quarternote=960)
    #blah blah

midify('test8.wav', 100)

任何人都可以告诉我如何改善这一点,使其也适用于语音吗? 由于音调太低而无法听到,因此我也尝试将输出频率加倍,但是大多数情况下我认为我的声音是无声的。 谢谢。

0 个答案:

没有答案
相关问题