简体   繁体   English

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

[英]How to save output in music21 as a MIDI file?

How do I save audio output in Python using the music21 module? 如何使用music21模块在Python中保存音频输出? I have read the entire [user's guide]( http://music21.readthedocs.org/en/latest/usersGuide/index.html] of said module, but I couldn't find any information about saving output as an audio file that can be recognised by windows without any additional software (MIDI for example). 我已经阅读了所述模块的整个[用户指南]( http://music21.readthedocs.org/en/latest/usersGuide/index.html) ,但我找不到任何有关将输出保存为音频文件的信息可以通过Windows识别,无需任何其他软件(例如MIDI)。

If s is your Stream , just call: 如果s是你的Stream ,只需致电:

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

or to hear it immediately 或立即听到

s.show('midi')

Somewhere in this User's Guide Chapter 8, there is some important information about opening and saving file in many formats: http://web.mit.edu/music21/doc/usersGuide/usersGuide_08_installingMusicXML.html 在本用户指南第8章的某处,有一些关于以多种格式打开和保存文件的重要信息: http//web.mit.edu/music21/doc/usersGuide/usersGuide_08_installingMusicXML.html

if you have made your own music called 'stream1', you can easily save it as MIDI file like this: 如果您制作了自己的音乐'stream1',您可以轻松将其保存为MIDI文件,如下所示:

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

I am still new to this, but I think that's simpler than having to open file, etc. 我还是新手,但我认为这比打开文件等更简单。

There is a MidiFile object, which knows how to write a midi file . 有一个MidiFile对象,它知道如何编写midi文件

But the documentation on how to use it is non-existant. 但是关于如何使用它的文档是不存在的。

However, in its source there is a testBasicExport test, probably it's a good start, it does something like this: 但是,在它的源代码中有一个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()

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

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