pythonmidimusic21

music21: get the voice/program/instrument of midi voice from a flat score?


I have a simple script that uses music21 to process the notes in a midi file:

import music21

score = music21.converter.parse('www.vgmusic.com/music/console/nintendo/nes/zanac1a.mid')

for i in score.flat.notes:
  
  print(i.offset, i.quarterLength, i.pitch.midi)

Is there a way to also obtain a note's voicing / midi program using a flat score? Any pointers would be appreciated!


Solution

  • MIDI channels and programs are stored on Instrument instances, so use getContextByClass(instrument.Instrument) to find the closest Instrument in the stream, and then access its .midiProgram.

    Be careful: