python mido如何在列表中获取[note、starttime、stoptime、track]?

python mido如何在列表中获取[note、starttime、stoptime、track]?,python,midi,mido,Python,Midi,Mido,我需要以下方面的帮助:我正在设计一种新的乐谱。我想读一个midi文件,得到一个包含每个音符/开始-停止时间/曲目的列表。预期结果: [[60, 0, 0.25, 1], [62, 0.25, 0.50, 1]]# the format is [note, start-time, stop-time, miditrack] *更新1-获取[备忘、备忘打开(时间)、备忘关闭(时间)、频道] 下面的代码创建了一个字典,其中delta时间转换为线性时间(但我不确定这是否是正确的方法): 我现在找不到合

我需要以下方面的帮助:我正在设计一种新的乐谱。我想读一个midi文件,得到一个包含每个音符/开始-停止时间/曲目的列表。预期结果:

[[60, 0, 0.25, 1], [62, 0.25, 0.50, 1]]# the format is [note, start-time, stop-time, miditrack]
*更新1-获取[备忘、备忘打开(时间)、备忘关闭(时间)、频道]

下面的代码创建了一个字典,其中delta时间转换为线性时间(但我不确定这是否是正确的方法):

我现在找不到合适的问题,但目标是:

[note, note_on(time), note_off(time), channel]

每一个音符。但问题是有两条消息(note on/off),我想把它变成一条。如果我找到了,我会发布我的解决方案。(或者可能有人知道一个很简单的中音图书馆的技巧来做到这一点…

我现在明白我的说法是错误的。我需要保持分开的笔记和留言。以下代码:

from mido import MidiFile

mid = MidiFile('testlilypond.mid')
mididict = []
output = []

# Put all note on/off in midinote as dictionary.
for i in mid:
    if i.type == 'note_on' or i.type == 'note_off' or i.type == 'time_signature':
        mididict.append(i.dict())
# change time values from delta to relative time.
mem1=0
for i in mididict:
    time = i['time'] + mem1
    i['time'] = time
    mem1 = i['time']
# make every note_on with 0 velocity note_off
    if i['type'] == 'note_on' and i['velocity'] == 0:
        i['type'] = 'note_off'
# put note, starttime, stoptime, as nested list in a list. # format is [type, note, time, channel]
    mem2=[]
    if i['type'] == 'note_on' or i['type'] == 'note_off':
        mem2.append(i['type'])
        mem2.append(i['note'])
        mem2.append(i['time'])
        mem2.append(i['channel'])
        output.append(mem2)
# put timesignatures
    if i['type'] == 'time_signature':
        mem2.append(i['type'])
        mem2.append(i['numerator'])
        mem2.append(i['denominator'])
        mem2.append(i['time'])
        output.append(mem2)
# viewing the midimessages.
for i in output:
    print(i)
print(mid.ticks_per_beat)
给出此输出:([类型、注释、时间、通道])


这就是我的目标:从MIDI文件中获取列表中所需的所有信息。这是一种将信息从消息中获取到python列表的方法。(我还将(delta)时间设置为线性。您需要使用for循环将上一个“时间”添加到当前时间)

对于每条消息上的注释,您必须找到相应的注释关闭消息,即具有相同注释和通道的下一条注释关闭消息。两个嵌套循环。谢谢!我已经发布了我的第一个目标的解决方案!
from mido import MidiFile

mid = MidiFile('testlilypond.mid')
mididict = []
output = []

# Put all note on/off in midinote as dictionary.
for i in mid:
    if i.type == 'note_on' or i.type == 'note_off' or i.type == 'time_signature':
        mididict.append(i.dict())
# change time values from delta to relative time.
mem1=0
for i in mididict:
    time = i['time'] + mem1
    i['time'] = time
    mem1 = i['time']
# make every note_on with 0 velocity note_off
    if i['type'] == 'note_on' and i['velocity'] == 0:
        i['type'] = 'note_off'
# put note, starttime, stoptime, as nested list in a list. # format is [type, note, time, channel]
    mem2=[]
    if i['type'] == 'note_on' or i['type'] == 'note_off':
        mem2.append(i['type'])
        mem2.append(i['note'])
        mem2.append(i['time'])
        mem2.append(i['channel'])
        output.append(mem2)
# put timesignatures
    if i['type'] == 'time_signature':
        mem2.append(i['type'])
        mem2.append(i['numerator'])
        mem2.append(i['denominator'])
        mem2.append(i['time'])
        output.append(mem2)
# viewing the midimessages.
for i in output:
    print(i)
print(mid.ticks_per_beat)
['time_signature', 4, 4, 0]
['note_on', 69, 0, 0]
['note_off', 69, 0.500053, 0]
['note_on', 71, 0.500053, 0]
['note_off', 71, 0.7500795, 0]
['note_on', 69, 0.7500795, 0]
['note_off', 69, 1.000106, 0]
['note_on', 71, 1.000106, 0]
['note_off', 71, 1.500159, 0]
['note_on', 69, 1.500159, 0]
['note_off', 69, 2.000212, 0]
['time_signature', 3, 4, 2.000212]
['note_on', 66, 2.000212, 0]
['note_off', 66, 2.5002649999999997, 0]
['note_on', 64, 2.5002649999999997, 0]
['note_off', 64, 3.0003179999999996, 0]
['note_on', 62, 3.0003179999999996, 0]
['note_off', 62, 3.5003709999999995, 0]
384
[Finished in 0.0s]