Python: получите правильное начало и конец заметки из midi-файла

#python #python-3.x #midi #mido

Вопрос:

я играю с библиотекой python mido и пытаюсь получить данные заметок, но я этого не понимаю.

мой код:

     # open midi file
    midi = str(MidiFile('./midi/piano/WA_Mozart_Marche_Turque_Turkish_March_fingered.mid', clip=True))
    
    # copy content of midi file to text file for easier work
    midiTextFile = open("midi.txt", "w")
    midiTextFile.write(midi)
 

Некоторые примеры части вывода

  Message('note_on', channel=0, note=80, velocity=96, time=1),
    Message('note_on', channel=0, note=80, velocity=0, time=119),
    Message('note_on', channel=0, note=64, velocity=96, time=1),
    Message('note_on', channel=0, note=64, velocity=0, time=119),
    Message('note_on', channel=0, note=76, velocity=96, time=1),
    Message('note_on', channel=0, note=76, velocity=0, time=119),
    MetaMessage('time_signature', numerator=1, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=1),
    Message('note_on', channel=0, note=69, velocity=96, time=0),
    Message('note_on', channel=0, note=81, velocity=96, time=0),
    MetaMessage('set_tempo', tempo=500000, time=479),
    Message('note_on', channel=0, note=69, velocity=0, time=0),
    Message('note_on', channel=0, note=81, velocity=0, time=0),
    MetaMessage('key_signature', key='A', time=1),
    Message('note_on', channel=0, note=69, velocity=96, time=0),
    Message('note_on', channel=0, note=69, velocity=0, time=119),
    Message('note_on', channel=0, note=81, velocity=96, time=1),
    Message('note_on', channel=0, note=81, velocity=0, time=119),
    Message('note_on', channel=0, note=71, velocity=96, time=1),
    Message('note_on', channel=0, note=71, velocity=0, time=119),
    Message('note_on', channel=0, note=83, velocity=96, time=1),
    Message('note_on', channel=0, note=83, velocity=0, time=119),
    MetaMessage('time_signature', numerator=2, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=1),
    Message('note_on', channel=0, note=73, velocity=96, time=0),
    Message('note_on', channel=0, note=73, velocity=0, time=119),
    Message('note_on', channel=0, note=85, velocity=96, time=1),
    Message('note_on', channel=0, note=85, velocity=0, time=119),
    Message('note_on', channel=0, note=69, velocity=96, time=241),
    Message('note_on', channel=0, note=69, velocity=0, time=119),
    Message('note_on', channel=0, note=81, velocity=96, time=1),
    Message('note_on', channel=0, note=81, velocity=0, time=119),
    Message('note_on', channel=0, note=71, velocity=96, time=1),
 

Теперь я не уверен в нескольких вещах.
Эта часть означает, что примечание 64 начинается с 1 миллисекунды и заканчивается на 119 миллисекундах? И если да, то есть ли лучший способ получить начало и конец заметки, чем этот?

 Message('note_on', channel=0, note=64, velocity=96, time=1),
Message('note_on', channel=0, note=64, velocity=0, time=119),
 

И еще одна вещь, которую я не понимаю, это то, что песня в midi-редакторе длится 3 минуты, в то время как в этом текстовом файле midi нет значения времени больше, чем time=500

Спасибо за любую помощь.

Комментарии:

1. Может быть, взгляд на документацию поможет. «Синхронизация в MIDI-файлах сосредоточена вокруг тиков и ударов. Такт — это то же самое, что четверть ноты. Удары делятся на тики, наименьшую единицу времени в MIDI. Каждое сообщение в MIDI-файле имеет дельта-время, которое показывает, сколько тиков прошло с момента последнего сообщения. Длина тика определяется в тактах за такт. Это значение сохраняется как ticks_per_beat в объектах MidiFile и остается фиксированным на протяжении всей песни». mido.readthedocs.io/en/latest/…

2. @BoarGules да, теперь я понимаю, что есть дельта-время и абсолютное время, спасибо. Вопрос решен.