[英]music21: writing notes to MIDI file by note position
I am using music21 to write midi file from the note values. 我正在使用music21从音符值写入midi文件。
Following code writes three notes sequentially. 以下代码按顺序写了三个笔记。
However, how do I insert an "empty" space between the notes? 但是,如何在音符之间插入“空白”空间?
It would be very easy if I could just insert the notes by their "position" so that I wouldn't have to calculate the position/length of each silent interval. 如果仅将音符的“位置”插入即可,这将非常容易,这样我就不必计算每个静默间隔的位置/长度。 Is that possible with music21? 音乐21有可能吗?
import music21
from music21 import *
mt = midi.MidiTrack(1)
mt2 = midi.MidiTrack(1)
# duration, pitch, velocity
data = [[1024, 60, 90], [1024, 50, 70], [1024, 51, 120],[1024, 62, 80], ]
t=0
tLast=0
for d,p,v in data:
dt = midi.DeltaTime(mt)
dt.time = t-tLast
#add to track events
mt.events.append(dt)
me=midi.MidiEvent(mt)
me.type="NOTE_ON"
me.channel=1
me.time= None #d
me.pitch = p
me.velocity = v
mt.events.append(me)
# add note off / velocity zero message
dt = midi.DeltaTime(mt)
dt.time = d
# add to track events
mt.events.append(dt)
me=midi.MidiEvent(mt)
me.type="NOTE_ON"
me.channel=1
me.time= None #d
me.pitch = p
me.velocity = 0
mt.events.append(me)
tLast = t+d
t +=d
dt=midi.DeltaTime(mt)
dt.time = 0
mt.events.append(dt)
me = midi.MidiEvent(mt)
me.type = "END_OF_TRACK"
me.channel = 1
me.data ='' # must set data to empty string
mt.events.append(me)
mf = midi.MidiFile()
mf.ticksPerQuarterNote = 1024 # cannot use: 10080
mf.tracks.append(mt)
mf.tracks.append(mt2)
mf.open('test.mid', 'wb')
mf.write()
mf.close()
In a MIDI file, all events are preceded by a delta time value that specifies how long to wait from the last event. 在MIDI文件中,所有事件之前都有一个增量时间值,该值指定了从上一个事件开始等待的时间。
In your code, t
and tLast
are both increased by the note's duration, so there is no pause between a note off and the following note on. 在您的代码中, t
和tLast
都随音符的持续时间增加,因此在音符关闭和后续音符打开之间没有暂停。
If you want the next note to start later, you have to increase t
by a larger amount. 如果希望下一个音符稍后开始,则必须将t
增加更大的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.