UnicodeDecodeError:“ utf-8”编解码器无法解码位置10的字节0xa9:无效的起始字节

问题描述

我正在尝试读取MIDI音乐文件,并使用 music21库对其进行一些处理。我正在使用自定义的 read_midi 函数,并收到此错误“ UnicodeDecodeError:'utf-8'编解码器无法解码位置10的字节0xa9:无效的起始字节

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轨道名称的文本创建Instrument对象。它已在下一个未发布的版本(可能是6.2.0)中修复,该版本现已在GitHub上提供。如果安装起来太麻烦,您还可以编辑自己的music21副本以应用在这里找到的修复程序:https://github.com/cuthbertLab/music21/pull/607/files

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

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

欢呼声和快乐的音乐,

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...