Music21:获取音符的曲目索引

Music21: get track index of a note

我有一个 multi-track midi file 正在用音乐阅读21:

import music21

f = music21.midi.MidiFile()
f.open('1079-02.mid')
f.read()
stream = music21.midi.translate.midiFileToStream(f).flat
note_filter = music21.stream.filters.ClassFilter('Note')
for n in stream.recurse().addFilter(note_filter):
  offset = n.offset # offset from song start in beats
  note = n.pitch # letter of the note, e.g. C4, F5
  midi_note = n.pitch.midi # midi number of the pitch, e.g. 60, 72
  duration = n.duration # duration of the note in beats
  instrument = n.activeSite.getInstrument() # instrument voice

我想弄清楚此流中的每个音符属于哪个音轨。例如。当我在 GarageBand 中打开文件时,笔记被组织成轨道:

mido 中,每个 MidiFile 都有一个 tracks 属性,其中包含每个曲目的一个注释列表。

有没有办法让 music21 也一样?任何帮助将不胜感激!

音乐曲目被解析为单独的 stream.Part 对象,因此如果您避免展平它,您可以只遍历您制作的 stream.Score 部分(在这里,我刚刚制作converter.parse():

的流
s = converter.parse('1079-02.mid')
for part in s.parts:
    for note in part.recurse().notes:
       print("I WAS IN PART ", part)

或查找包含部分:

s = converter.parse('1079-02.mid')
for note in s.recurse().notes:
    part = note.sites.getObjByClass('Part')
    print("I WAS IN PART ", part)

我怀疑你真的需要压平任何东西。祝你好运!