【发布时间】:2012-06-16 00:37:27
【问题描述】:
我正在编写一个脚本,根据各个像素的 RGBA 值将图片转换为 MIDI 音符。但是,我似乎无法完成最后一步,即实际将注释输出到文件中。
我尝试过使用 MIDIUtil 库,但是它的文档不是最好的,我似乎无法弄清楚。
如果有人能告诉我如何对音符进行排序(这样它们就不会从头开始),将不胜感激。
【问题讨论】:
-
你记得写出延迟吗?
我正在编写一个脚本,根据各个像素的 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)
【讨论】:
from midiutil.MidiFile3 import MIDIFile。
from midiutil.MidiFile import MIDIFile 可以解决问题
我知道这是一篇旧帖子,但我是该库的作者,我想提一下,python 2 和 3 支持现已统一,随着 Google Code 的消亡,代码现在托管在 @ 987654321@,可以通过pip安装,即:
pip install MIDIUtil
文档可通过Read The Docs获取。
(试图评论但我缺乏经验值。)
文件写入磁盘时会自动创建轨道结束消息。
【讨论】: