使用sign music21从midi文件中提取所有信息

2024-05-26 20:47:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用music21从一个包含10个乐器的midi文件中提取音符、和弦和休止符,下面是post我可以为每个乐器得到这些:

file = 'example.mid'
midi = converter.parse(file)  
parts = instrument.partitionByInstrument(midi) 
notes_for_instruments = []
for i in range(len(parts.parts)): 
    notes_to_parse = parts.parts[i].recurse()
    instr = parts.parts[i].getInstrument()
    instruments.append(instr.instrumentName)
    notes, quarters = [],[] 
    for element in notes_to_parse:   
        if isinstance(element, note.Note):  
        # if element is a note, extract pitch   
            notes.append(str(element.pitch))
        elif(isinstance(element, chord.Chord)):  
            # if element is a chord, append the normal form of the   
            # chord (a list of integers) to the list of notes.   
            notes.append('.'.join(str(n) for n in element.normalOrder))  
        elif isinstance(element, note.Rest):
            notes.append('Rest')

    notes_for_instruments.append(notes)

这是每个乐器的音符、和弦和休止符的数量

^{pr2}$

正如您所看到的,它们是非常不同的,这是因为我没有提取所有可用的信息。如何将这个结果进行同源化,以使和弦、音符和其他几种乐器同步?在


Tags: toinforifparseelementmidinote

热门问题