music21: parsing notes and durations per track

人走茶凉 提交于 2021-02-08 03:31:08

问题


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..


回答1:


There is more than one way to do this in music21, so this is just one simple way. Note that the durational value is expressed as a float, such that a quarter note equals 1.0, a half note equals 2.0, etc.:

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)

An alternative solution would be to use the vis-framework, which accesses music files in symbolic notation via music21 and stores the information in pandas dataframes. You can do this:

pip install vis-framework

Another solution would be to use Humdrum instead of music21.



来源:https://stackoverflow.com/questions/40992288/music21-parsing-notes-and-durations-per-track

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!