如何使用 Python 中的长笛乐器从音符中制作 MIDI 文件(music21 库) [英] How to make MIDI file from notes with Flute instrument in Python (music21 library)
问题描述
我有一些笔记,我想要的是用长笛乐器创建 MIDI 文件.但结果是输出 MIDI 文件播放钢琴而不是长笛.我试过其他乐器,但总是一样的,钢琴.这是怎么回事?
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')
推荐答案
根据 文档,唯一具有 storedInstrument
属性的类是 note.Unpitched
.
According to the documentation, the only class with a storedInstrument
property is note.Unpitched
.
还有:
Unpitched
对象当前没有任何作用,不应使用.
The
Unpitched
object does not currently do anything and should not be used.
无论如何,music21/midi/translate.py
中的 testMidiProgramChangeA
/B
函数展示了这是如何完成的:只需添加在应该使用它的 Note
之前将对象插入到 Stream
中:
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:
output_notes.append(instrument.Flute())
new_note = ...
output_notes.append(new_note)
...
这篇关于如何使用 Python 中的长笛乐器从音符中制作 MIDI 文件(music21 库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!