如何将 music21 中的输出保存为 MIDI 文件?

How to save output in music21 as a MIDI file?

如何使用 music21 模块在 Python 中保存音频输出?我已经阅读了所述模块的整个[用户指南](http://music21.readthedocs.org/en/latest/usersGuide/index.html],但我找不到任何关于将输出保存为音频文件的信息,无需任何其他软件即可被windows识别(MIDI例如)。

有个MidiFile object, which knows how to write a midi file.

但是没有关于如何使用它的文档。

然而,在它的 source there is a testBasicExport 测试中,它可能是一个好的开始,它做了这样的事情:

mt = MidiTrack(1)

# duration, pitch, velocity
data = [[1024, 60, 90], [1024, 50, 70], [1024, 51, 120],[1024, 62, 80], ]

# Omit this part here, but full code in the links above
populateTrackFromData(mt, data)

mf = MidiFile()
mf.ticksPerQuarterNote = 1024 # cannot use: 10080
mf.tracks.append(mt)

mf.open('/src/music21/music21/midi/out.mid', 'wb')
mf.write()
mf.close()

如果 s 是你的 Stream,只需拨打:

fp = s.write('midi', fp='pathToWhereYouWantToWriteIt')

或立即收听

s.show('midi')

在本用户指南第 8 章的某处,有一些关于以多种格式打开和保存文件的重要信息: http://web.mit.edu/music21/doc/usersGuide/usersGuide_08_installingMusicXML.html

如果您自己制作了名为 'stream1' 的音乐,您可以像这样轻松地将其保存为 MIDI 文件:

stream1.write("midi", "blah.mid")

我对此还是个新手,但我认为这比必须打开文件等更简单