简体   繁体   English

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

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

I have some notes and what I want is create the MIDI file with Flute instrument.我有一些笔记,我想要的是用长笛乐器创建 MIDI 文件。 But what happens is that the output MIDI file plays Piano, instead of Flute.但结果是输出 MIDI 文件播放钢琴而不是长笛。 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')

According to the documentation , the only class with a storedInstrument property is note.Unpitched .根据 文档,唯一具有storedInstrument属性的类是note.Unpitched

And:和:

The Unpitched object does not currently do anything and should not be used. Unpitched对象当前不执行任何操作,不应使用。

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 Note s that should use it:无论如何, music21/midi/translate.pytestMidiProgramChangeA / B函数展示了这是如何完成的:只需将乐器对象添加到应该使用它的Note之前的Stream

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM