如何使用Python编写MIDI文件? [英] How can I write a MIDI file with Python?

查看:569
本文介绍了如何使用Python编写MIDI文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a script to convert a picture into MIDI notes based on the RGBA values of the individual pixels. However, I cannot seem to get the last step working, which is to actually output the notes to a file. I have tried using the MIDIUtil library, however it's documentation is not the greatest and I can't seem to figure it out. If anyone could tell me how to sequence the notes (so that they don't all begin at the beginning) it would be greatly appreciated.

谢谢.

推荐答案

看示例,就像

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编写MIDI文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆