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

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 10:16:08

问题


I have some notes and what I want is create the MIDI file with Flute instrument. But what happens is that the output MIDI file plays Piano, instead of Flute. I tried other instruments, but it's always the same, Piano. What is going on?

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

回答1:


According to the documentation, the only class with a storedInstrument property is note.Unpitched.

And:

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

Anyway, the testMidiProgramChangeA/B functions in music21/midi/translate.py show how this is to be done: just add the instrument object into the Stream before the Notes that should use it:

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


来源:https://stackoverflow.com/questions/55170188/how-to-make-midi-file-from-notes-with-flute-instrument-in-python-music21-librar

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