如何在 Python(music21 库)中使用长笛乐器从音符制作 MIDI 文件

How to make MIDI file from notes with Flute instrument in Python (music21 library)

我有一些笔记,我想要用长笛乐器创建 MIDI 文件。但是发生的是输出 MIDI 文件播放钢琴,而不是长笛。我试过其他乐器,但总是一样,钢琴。这是怎么回事?

(...)
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
(...)
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='output.midi')

根据 documentation,唯一带有 storedInstrument 属性 的 class 是 note.Unpitched

并且:

The Unpitched object does not currently do anything and should not be used.

无论如何,music21/midi/translate.py 中的 testMidiProgramChangeA/B 函数展示了如何做到这一点:只需将 instrument 对象添加到 Stream 之前的 Note应该使用它的人:

output_notes.append(instrument.Flute())
new_note = ...
output_notes.append(new_note)
...