Python 音乐21操纵特定乐器
我使用Python中的Music21来读取MIDI文件,我只想处理使用特定乐器的曲目。 例如,如果在我的MIDI文件中有两首使用钢琴的曲目,我希望能够打印音符、更换乐器等 现在我有一个多音轨(鼓、小号等)的文件,我只是在摆弄它,试图用另一种乐器替换某一种乐器。然而,当我这样做的时候,我得到了一个错误,一些轨迹被完全移除,尽管仪器被成功地更换(假设它不是被移除的轨迹之一) 以下是我当前的代码:Python 音乐21操纵特定乐器,python,midi,music21,Python,Midi,Music21,我使用Python中的Music21来读取MIDI文件,我只想处理使用特定乐器的曲目。 例如,如果在我的MIDI文件中有两首使用钢琴的曲目,我希望能够打印音符、更换乐器等 现在我有一个多音轨(鼓、小号等)的文件,我只是在摆弄它,试图用另一种乐器替换某一种乐器。然而,当我这样做的时候,我得到了一个错误,一些轨迹被完全移除,尽管仪器被成功地更换(假设它不是被移除的轨迹之一) 以下是我当前的代码: from music21 import converter, instrument s = conver
from music21 import converter, instrument
s = converter.parse('smells.mid')
s = instrument.partitionByInstrument(s)
s.parts[2].insert(0, instrument.Vocalist())
s.write('midi', 'newfilename.mid')
这就是我得到的错误:
midi.base.py: WARNING: Conversion error for <MidiEvent PROGRAM_CHANGE, t=0, track=1, channel=1>: Got incorrect data for <MidiEvent PROGRAM_CHANGE, t=0, track=1, channel=1> in .data: None,cannot parse Program Change; ignored.
midi.base.py:警告:转换错误:中获取了不正确的数据。数据:无,无法分析程序更改;忽略。
以下是我想要做的:
def printInstrument(self, strm, inst):
s2 = instrument.partitionByInstrument(strm)
if s2 is not None:
#print all the notes the instrument plays
for i in s2.recurse().parts:
if i.partName == inst:
iNotes = i.notesAndRests.stream()
for j in iNotes.elements:
if type(j) == chord.Chord:
#handle chords
elif j.name == "rest":
#handle rests
else:
#handle notes
你找到解决办法了吗?@rafaelvalle我刚刚发布了一个答案,如果有帮助,请告诉我!:)