15

我正在编写一个脚本,根据各个像素的 RGBA 值将图片转换为 MIDI 音符。但是,我似乎无法完成最后一步,即实际将注释输出到文件中。

我曾尝试使用 MIDIUtil 库,但它的文档不是最好的,我似乎无法弄清楚。

如果有人能告诉我如何对音符进行排序(这样它们就不会从头开始),将不胜感激。

4

2 回答 2

26

看着样本,就像

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)
于 2012-06-16T01:56:22.377 回答
20

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

pip install MIDIUtil

文档可在Read The Docs获得。

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

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

于 2016-09-24T14:54:52.467 回答