Music21 操纵特定乐器
Music21 Manipulating Specific Instrument
我在 Python 中使用 Music21 从 MIDI 文件中读取,我只想处理使用特定乐器的曲目。
例如,如果在我的 MIDI 文件中有两个使用钢琴的曲目,我希望能够打印音符、更改乐器等。
现在我有一个包含多个音轨(鼓、小号等)的文件,我只是在摆弄它,试图用另一种乐器替换某种乐器。然而,当我这样做时,我得到了一个错误,并且一些曲目被完全删除,尽管乐器已成功更改(假设它不是被删除的曲目之一)。
这是我当前的代码:
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.
这是我尝试做的事情:
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
我在 Python 中使用 Music21 从 MIDI 文件中读取,我只想处理使用特定乐器的曲目。 例如,如果在我的 MIDI 文件中有两个使用钢琴的曲目,我希望能够打印音符、更改乐器等。
现在我有一个包含多个音轨(鼓、小号等)的文件,我只是在摆弄它,试图用另一种乐器替换某种乐器。然而,当我这样做时,我得到了一个错误,并且一些曲目被完全删除,尽管乐器已成功更改(假设它不是被删除的曲目之一)。
这是我当前的代码:
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.
这是我尝试做的事情:
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