我正在尝试使用此github 存储库的 .py 文件中的代码(get_notes() 函数)从该 midi 文件中提取音符和和弦数据。这是从我正在使用的存储库中复制的确切代码:
def get_notes():
""" Get all the notes and chords from the midi files in the ./midi_songs directory """
notes = []
for file in glob.glob("midi_songs/*.mid"):
midi = converter.parse(file)
print("Parsing %s" % file)
notes_to_parse = None
try: # file has instrument parts
s2 = instrument.partitionByInstrument(midi)
notes_to_parse = s2.parts[0].recurse()
except: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
with open('data/notes', 'wb') as filepath:
pickle.dump(notes, filepath)
return notes
我的 midi 文件有多种乐器,我相信由于某种原因,没有任何东西被附加到笔记列表中。看到我刚刚克隆了存储库的代码并在我自己的 midi 文件上运行它,我不确定它为什么不起作用。我查看了 music21 的 partitionByInstrument() 的文档,并尝试通过更改来迭代不同的乐器:
notes_to_parse = s2.parts[0].recurse()
到
notes_to_parse = s2.parts[1].recurse()
因为不同的部分应该是文件中的不同工具,但它仍然返回一个空列表。我该怎么办?