UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byte

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byte

我正在尝试读取 MIDI 音乐文件并使用 music21 库 对它们进行一些处理。我正在使用自定义 read_midi 函数,并收到此错误“UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10 : 无效的起始字节"

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

这里是 read_midi 函数:

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)

请建议我如何摆脱这个错误。

我从 music21 Google 组得到的答案并解决了我的问题:

您好,感谢您的报告。这是由 6.1.0 中的一项新功能引起的回归,该功能根据 MIDI 轨道名称的文本创建乐器对象。它在下一个未发布的版本(可能是 6.2.0)中得到修复,现在可以在 GitHub 上使用。如果安装起来太麻烦,您也可以编辑自己的 music21 副本以应用此处找到的修复程序:https://github.com/cuthbertLab/music21/pull/607/files

出于好奇,原始功能错误地假定所有 MIDI 轨道名称都将使用 utf-8 编码。我们发现失败的每个文件在曲目名称中都有一个版权符号,并且每个文件都是由“www.piano-midi.de”创建的。您介意分享您创建文件的 MIDI 作者吗?

此外,非常感谢您在 Stack Overflow 上分享这个答案,因为我在那里不活跃。

干杯,快乐的音乐,