繁体   English   中英

如何用Python编写MIDI文件?

[英]How can I write a MIDI file with Python?

我正在编写一个脚本,根据各个像素的RGBA值将图片转换为MIDI音符。 但是,我似乎无法完成最后一步,即实际将注释输出到文件。 我曾尝试使用MIDIUtil库,但它的文档并不是最好的,我似乎无法弄明白。 如果有人能告诉我如何对音符进行排序(以便它们不是从头开始),我们将不胜感激。

提前致谢。

看样品,像

from midiutil.MidiFile import MIDIFile

# create your MIDI object
mf = MIDIFile(1)     # only 1 track
track = 0   # the only track

time = 0    # start at the beginning
mf.addTrackName(track, time, "Sample Track")
mf.addTempo(track, time, 120)

# add some notes
channel = 0
volume = 100

pitch = 60           # C4 (middle C)
time = 0             # start on beat 0
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

pitch = 64           # E4
time = 2             # start on beat 2
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

pitch = 67           # G4
time = 4             # start on beat 4
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

# write it to disk
with open("output.mid", 'wb') as outf:
    mf.writeFile(outf)

我知道这是一个老帖子,但我是图书馆的作者,我想提一下现在已经统一了python 2和3支持,随着Google Code的消亡,代码现在托管在GitHub上 ,可以是通过pip安装,即:

pip install MIDIUtil

阅读文档中提供了文档

(试图发表评论,但我缺乏经验值。)

当文件写入磁盘时,会自动创建跟踪结束消息。

暂无
暂无

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

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