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