I am trying to read MIDI music files and processing them a bit using the music21 library. I am using the self defined read_midi function, and getting this error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byte"
import os
#Array Processing
import numpy as np
#specify the path
path='audio/'
#read all the filenames
files=[i for i in os.listdir(path) if i.endswith(".mid")]
#reading each midi file
notes_array = np.array([read_midi(path+i) for i in files])
here is the read_midi function:
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)
kindly suggest how can I get rid of this error.