繁体   English   中英

Music21:获取音符的曲目索引

[英]Music21: get track index of a note

我有一个用music21 阅读的多轨midi 文件

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

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

在此处输入图像描述

mido中,每个MidiFile都有一个tracks属性,其中包含每个轨道的一个音符列表。

有没有办法让music21得到同样的结果? 任何帮助,将不胜感激!

The music tracks are parsed into separate stream.Part objects, so you can just walk through the parts of the stream.Score that you produced if you avoid flattening it (here, I've just produced a stream with converter.parse() :

s = converter.parse('1079-02.mid')
for part in s.parts:
    for note in part.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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM