Last active
August 23, 2022 16:22
-
-
Save aravindpai/5cbe41bc474b610569e0a0ae8b164b56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The link to download the midi library is broken ; please help !