Music21 Manipulating Specific Instrument

这一生的挚爱 提交于 2020-04-14 07:27:47

问题


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).

Here's my current code:

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')

and this is the error I'm getting:

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.

回答1:


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


来源:https://stackoverflow.com/questions/47556847/music21-manipulating-specific-instrument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!