2

I'm trying to use music21 to convert multi-track midi files into array of notes and durations per each track.

For example, given a midi file test.mid with 16 tracks in it,

I would like to get 16 arrays of tuples, consisting of (pitch, duration (plus maybe position of the note)).

Documentation for music21 is rather difficult to follow, and I would really appreciate any help on this..

4

1 回答 1

2

在 music21 中有不止一种方法可以做到这一点,所以这只是一种简单的方法。请注意,持续时间值表示为浮点数,例如四分音符等于 1.0,二分音符等于 2.0,等等:

import music21
from music21 import *

piece = converter.parse("full_path_to_piece.midi")
all_parts = []
for part in piece.parts:
  part_tuples = []
  for event in part:
    for y, in event.contextSites():
      if y[0] is part:
        offset = y[1]
    if getattr(event, 'isNote', None) and event.isNote:
      part_tuples.append((event.nameWithOctave, event.quarterLength, offset))
    if getattr(event, 'isRest', None) and event.isRest:
      part_tuples.append(('Rest', event.quarterLength, offset))
  all_parts.append(part_tuples)

另一种解决方案是使用 vis 框架,它通过 music21 以符号符号访问音乐文件并将信息存储在pandas数据帧中。你可以这样做:

pip install vis-framework

另一种解决方案是使用Humdrum而不是 music21。

于 2016-12-07T14:08:13.917 回答