简体   繁体   English

写入 MIDI 文件

[英]Write MIDI file

I want to write a MIDI file with the inputs I receive from the digital piano I have connected.我想用我从连接的数码钢琴收到的输入编写一个 MIDI 文件。 I am using pygame.midi to open an input port and midiutil to write the MIDI file.我正在使用 pygame.midi 打开一个输入端口并使用 midiutil 来编写 MIDI 文件。 What I can't wrap my head around is the timing.我无法理解的是时机。 For example, in addNote(track, channel, pitch, time, duration, volume) , how do I know what time and duration of a note is?例如,在addNote(track, channel, pitch, time, duration, volume)中,我如何知道一个音符的timeduration是多少? When reading a note, I get pitch, and volume just fine, but the others I have no idea... I tried using the timestamp but to no avail, it places the note really far away in the MIDI file.阅读音符时,我的音调和音量都很好,但其他我不知道......我尝试使用时间戳但无济于事,它将音符放置在 MIDI 文件中非常远的地方。

So, how do I compute the 'time' and 'duration' of a note?那么,如何计算音符的“时间”和“持续时间”?

time dictates the position in musical time the note should be played. time决定了音符应该在音乐时间中演奏的位置。 Exactly what the parameter should be depends in part how the Midi file object was constructed (more on that soon)确切的参数应该是什么部分取决于 Midi 文件对象的构造方式(稍后会详细介绍)

In practice, MIDI asks for two messages per note: a NOTE On message and a NOTE Off message.在实践中,MIDI 要求每个音符有两条消息:一条NOTE On消息和一条NOTE Off消息。 The duration will indicate when the Note Off message should be sent, relative to the start of the note. duration将指示应该何时发送Note Off消息,相对于音符的开始。 Again, how the parameter should be formed depends on how the file object is constructed.同样,应如何形成参数取决于文件对象的构造方式。

From theMIDIUtil docs :MIDIUtil 文档

  • time – the time at which the note sounds. time – 音符响起的时间。 The value can be either quarter notes [Float], or ticks [Integer].该值可以是四分音符 [Float] 或刻度 [Integer]。 Ticks may be specified by passing eventtime_is_ticks=True to the MIDIFile constructor.可以通过将 eventtime_is_ticks=True 传递给 MIDIFile 构造函数来指定刻度。 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]与时间参数一样,该值可以是四分音符 [Float] 或刻度 [Integer]

A complete example that plays the C major scale演奏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)

The combination of the file tempo (at the current time ) combined with the note's position ( time ) and duration (in terms of how many beats), the library can synthesize all the midi messages needed to play (start/stop) the note at the correct time.文件tempo (在当前时间)结合音符的位置( time )和duration (根据多少节拍),库可以合成播放(开始/停止)音符所需的所有 MIDI 信息正确的时间。

Another Example另一个例子

Let's try applying this to the following musical phrase:让我们尝试将其应用于以下乐句:

乐句

First, set everything up.首先,设置一切。

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)

To add the first half note on E and quarter note on G:要添加 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)

Now let's add the remaining notes:现在让我们添加剩余的注释:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM