编写 MIDI 文件
Write MIDI file
我想用我从已连接的数码钢琴接收到的输入编写一个 MIDI 文件。我正在使用 pygame.midi 打开输入端口并使用 midiutil 编写 MIDI 文件。我无法理解的是时机。例如,在addNote(track, channel, pitch, time, duration, volume)
中,我如何知道一个音符的time
和duration
是什么?当读一个音符时,我得到音高和音量都很好,但其他我不知道......我尝试使用时间戳但无济于事,它把音符放在 MIDI 文件中很远的地方。
那么,如何计算音符的 'time' 和 'duration'?
time
指示应播放音符的音乐时间位置。确切的参数应该是什么部分取决于 Midi 文件对象是如何构建的(稍后会详细介绍)
实际上,MIDI 要求每个音符有两条消息:一条 NOTE On
消息和一条 NOTE Off
消息。 duration
将指示何时应发送 Note Off
消息,相对于注释的开头。同样,参数应该如何形成取决于文件对象的构造方式。
- time – the time at which the note sounds. The value can be either quarter notes [Float], or
ticks [Integer]. Ticks may be specified by passing eventtime_is_ticks=True to the MIDIFile
constructor. The default is quarter notes.
- duration – the duration of the note. Like the time argument, the value can be either
quarter notes [Float], or ticks [Integer]
演奏C大调音阶的完整范例
from midiutil import MIDIFile
degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track
# automatically created)
MyMIDI.addTempo(track,time, tempo)
for pitch in degrees:
MyMIDI.addNote(track, channel, pitch, time, duration, volume)
time = time + 1
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
文件tempo
(在当前时间)与笔记位置(time
)和duration
(就多少节拍而言),该库可以合成在正确时间播放(start/stop)音符所需的所有 MIDI 消息。
另一个例子
让我们尝试将其应用于以下乐句:
首先,设置一切。
from midiutil import MIDIFile
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track
# automatically created)
MyMIDI.addTempo(track,time, tempo)
要在 E 上添加前二分音符和在 G 上添加四分音符:
time = 0 # it's the first beat of the piece
quarter_note = 1 # equal to one beat, assuming x/4 time
half_note = 2 # Half notes are 2x value of a single quarter note
E3 = 64 # MIDI note value for E3
G3 = 67
# Add half note
MyMIDI.addNote(track, channel, pitch=E3, duration=half_note, time=0, volume=volume)
# Add quarter note
MyMIDI.addNote(track, channel, pitch=G3, duration=quarter_note, time=0, volume=volume)
现在让我们添加剩余的注释:
A3 = 69
C3 = 60
B3 = 71
C4 = 72
# add the remaining notes
for time, pitch, duration in [(1,A3, quarter_note),
(2,B3, quarter_note), (2, C3, half_note),
(3,C4, quarter_note)]:
MyMIDI.addNote(track, channel,
duration=duration,
pitch=pitch, time=time, volume=volume)
我想用我从已连接的数码钢琴接收到的输入编写一个 MIDI 文件。我正在使用 pygame.midi 打开输入端口并使用 midiutil 编写 MIDI 文件。我无法理解的是时机。例如,在addNote(track, channel, pitch, time, duration, volume)
中,我如何知道一个音符的time
和duration
是什么?当读一个音符时,我得到音高和音量都很好,但其他我不知道......我尝试使用时间戳但无济于事,它把音符放在 MIDI 文件中很远的地方。
那么,如何计算音符的 'time' 和 'duration'?
time
指示应播放音符的音乐时间位置。确切的参数应该是什么部分取决于 Midi 文件对象是如何构建的(稍后会详细介绍)
实际上,MIDI 要求每个音符有两条消息:一条 NOTE On
消息和一条 NOTE Off
消息。 duration
将指示何时应发送 Note Off
消息,相对于注释的开头。同样,参数应该如何形成取决于文件对象的构造方式。
- time – the time at which the note sounds. The value can be either quarter notes [Float], or ticks [Integer]. Ticks may be specified by passing eventtime_is_ticks=True to the MIDIFile constructor. The default is quarter notes.
- duration – the duration of the note. Like the time argument, the value can be either quarter notes [Float], or ticks [Integer]
演奏C大调音阶的完整范例
from midiutil import MIDIFile
degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track
# automatically created)
MyMIDI.addTempo(track,time, tempo)
for pitch in degrees:
MyMIDI.addNote(track, channel, pitch, time, duration, volume)
time = time + 1
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
文件tempo
(在当前时间)与笔记位置(time
)和duration
(就多少节拍而言),该库可以合成在正确时间播放(start/stop)音符所需的所有 MIDI 消息。
另一个例子
让我们尝试将其应用于以下乐句:
首先,设置一切。
from midiutil import MIDIFile
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track
# automatically created)
MyMIDI.addTempo(track,time, tempo)
要在 E 上添加前二分音符和在 G 上添加四分音符:
time = 0 # it's the first beat of the piece
quarter_note = 1 # equal to one beat, assuming x/4 time
half_note = 2 # Half notes are 2x value of a single quarter note
E3 = 64 # MIDI note value for E3
G3 = 67
# Add half note
MyMIDI.addNote(track, channel, pitch=E3, duration=half_note, time=0, volume=volume)
# Add quarter note
MyMIDI.addNote(track, channel, pitch=G3, duration=quarter_note, time=0, volume=volume)
现在让我们添加剩余的注释:
A3 = 69
C3 = 60
B3 = 71
C4 = 72
# add the remaining notes
for time, pitch, duration in [(1,A3, quarter_note),
(2,B3, quarter_note), (2, C3, half_note),
(3,C4, quarter_note)]:
MyMIDI.addNote(track, channel,
duration=duration,
pitch=pitch, time=time, volume=volume)