Generating Music with Machine Learning : Error in the tutorial using music21 library


I am following the tutorial on this article

The problem is that when we enter the code in the tutorial :

#library for understanding music
from music21 import *
#defining function to read MIDI files
def read_midi(file):

print("Loading Music File:",file)

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

    #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:
            if isinstance(element, note.Note):
            elif isinstance(element, chord.Chord):
                notes.append('.'.join(str(n) for n in element.normalOrder))

return np.array(notes)

#for listing down the file names
import os

#Array Processing
import numpy as np

#specify the path

#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])

I have this error (note that line 41 is the last line) :

Loading Music File: /Users/aydinabiar/Desktop/schubert/schumm-1.mid
Traceback (most recent call last):
File “/Users/aydinabiar/hello/.vscode/test”, line 41, in
notes_array = np.array([read_midi(path+i) for i in files])
File “/Users/aydinabiar/hello/.vscode/test”, line 41, in
notes_array = np.array([read_midi(path+i) for i in files])
File “/Users/aydinabiar/hello/.vscode/test”, line 10, in read_midi
midi = converter.parse(file)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/converter/”, line 1137, in parse
return parseFile(valueStr, number=number, format=m21Format,
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/converter/”, line 1010, in parseFile
v.parseFile(fp, number=number, format=format, forceSource=forceSource, **keywords)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/converter/”, line 551, in parseFile
self.parseFileNoPickle(fp, number, format, forceSource, **keywords)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/converter/”, line 485, in parseFileNoPickle
self.subConverter.parseFile(fp, number=number, **keywords)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/converter/”, line 1048, in parseFile
midiTranslate.midiFilePathToStream(fp,, **keywords)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 2151, in midiFilePathToStream
return midiFileToStream(mf, inputM21, **keywords)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 2311, in midiFileToStream
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 2050, in midiTracksToStreams
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 1657, in midiTrackToStream
metaEvents = getMetaEvents(events)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 1604, in getMetaEvents
metaObj = midiEventsToInstrument(e)
File “/Users/aydinabiar/hello/.venv/lib/python3.8/site-packages/music21/midi/”, line 700, in midiEventsToInstrument
i = instrument.fromString(‘utf-8’))
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa9 in position 10: invalid start byte

Do you have any idea how to fix it ? I encounter on other forum like stack overflow other people having this trouble when they follow this tutorial but they didn’t get answers

I’m working on MACOS with python 3.8.6 on Visual Studio and I download the latest version of music21

Thank you very much

© Copyright 2013-2020 Analytics Vidhya