[英]reading MIDI file using music21 with partitionByInstrument() to get notes and chords returning empty list
我正在嘗試使用這個github 存儲庫的 .py 文件(get_notes() 函數)中的代碼從這個 midi 文件中提取音符和和弦數據。 這是從我正在使用的 repo 復制的確切代碼:
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()
因為不同的部分應該是文件中不同的樂器,但它仍然返回一個空列表。 我該怎么辦?
v6.1 添加了一項功能,可根據 MIDI 樂器名稱和軌道名稱消息創建Instrument
對象。 今天發布的 v6.5 添加了一項功能,可以刪除在同一滴答聲中程序更改消息中還有一個Instrument
時導致的重復項。 如果您嘗試 v6.5,您可能會發現它更易於使用。
原因很簡單,它使用的是它發現的第一個樂器,它沒有音符、時長或偏移的數據。 遍歷儀器我得到了不同的返回完整列表的工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.