【问题标题】:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byteUnicodeDecodeError:“utf-8”编解码器无法解码位置 10 中的字节 0xa9:无效的起始字节
【发布时间】:2021-01-15 20:26:37
【问题描述】:

我正在尝试读取 MIDI 音乐文件并使用 music21 库对其进行一些处理。我正在使用自定义 read_midi 函数,并收到此错误“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])

这里是 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 版本中存在错误。
  • 是的,我想是的,检查了问题页面,它说类似的东西。感谢您的回复。
  • 另外,我发现了一件奇怪的事情,我更改了一些 MIDI 文件,现在它可以工作了,它读取了一些文件并卡在了其他一些文件上。

标签: python-3.x midi


【解决方案1】:

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

您好,感谢您的报告。这是由 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 上分享这个答案,因为我在那里不活跃。

干杯,快乐的音乐21-ing,

【讨论】:

    猜你喜欢
    • 2022-05-30
    • 2021-12-01
    • 2016-05-13
    • 2017-05-03
    • 2020-02-06
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多