【发布时间】: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