Skip to content

Instantly share code, notes, and snippets.

@aravindpai
Last active August 23, 2022 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aravindpai/5cbe41bc474b610569e0a0ae8b164b56 to your computer and use it in GitHub Desktop.
Save aravindpai/5cbe41bc474b610569e0a0ae8b164b56 to your computer and use it in GitHub Desktop.
#defining function to read MIDI files
def read_midi(file):
print("Loading Music File:",file)
notes=[]
notes_to_parse = None
#parsing a midi file
midi = converter.parse(file)
#grouping based on different instruments
s2 = instrument.partitionByInstrument(midi)
#Looping over all the instruments
for part in s2.parts:
#select elements of only piano
if 'Piano' in str(part):
notes_to_parse = part.recurse()
#finding whether a particular element is note or a chord
for element in notes_to_parse:
#note
if isinstance(element, note.Note):
notes.append(str(element.pitch))
#chord
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
return np.array(notes)
@Una1111
Copy link

Una1111 commented Aug 23, 2022

The link to download the midi library is broken ; please help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment