【问题标题】:Music21 Manipulating Specific InstrumentMusic21 操纵特定乐器
【发布时间】:2017-11-29 15:56:34
【问题描述】:

我在 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.

【问题讨论】:

  • 您找到解决方案了吗?
  • @rafaelvalle 我刚刚发布了一个答案,如果有帮助请告诉我! :)

标签: python midi music21


【解决方案1】:

这就是我想要做的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2016-02-06
    相关资源
    最近更新 更多