簡體   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