问题描述
我正在Python中使用Music21从MIDI文件读取,我只想处理使用某种乐器的音轨.例如,如果我的MIDI文件中有两条使用钢琴的音轨,那么我希望能够打印音符,更换乐器等.
I am using Music21 in Python to read from a MIDI file and I want to only deal with the tracks that use a certain instrument.For example, if, in my MIDI file I have two tracks that use piano I want to be able to print the notes, change the instrument, etc.
现在,我有一个具有多个音轨(鼓,小号等)的文件,而我只是在弄乱它,试图用另一把乐器代替另一把乐器.但是,当我这样做时,我遇到了一个错误,尽管成功更换了乐器,但某些音轨还是被完全删除了(假设它不是被删除的其中一个).
Right now I have a file with multiple tracks (drums, trumpet etc.) and I am just messing around with it trying to replace a certain instrument with another. However, when I do I get an error and some of the tracks are removed completely although the instrument is successfully changed (assuming it's not one of the ones removed).
这是我当前的代码:
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.
推荐答案
这就是我正在尝试做的事情:
Here's what I was trying to do:
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
这篇关于Music21操纵特定乐器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!