如何处理重复的 note_on、note_off、多个音轨中的速度变化,以及 midi 文件中没有 program_change 的音轨?

How to handle duplicate note_on, note_off, tempo change in more than one tracks, and tracks without program_change in a midi file?

我正在为 python 使用 Mido,致力于将 midi 文件解析为 <start_time, duration, program, pitch> 元组并遇到了一些问题。

我解析的一些文件有多个 note_on,导致同一音高的音符和同一程序被多次打开。

一些文件包含多个 note_off 导致尝试关闭由于之前关闭而不再打开的音符(假设同一程序和相同音高中只有一个音符可以打开)。

有些曲目在曲目开头没有 program_change(或者更糟的是,整个曲目中甚至没有)。

有些文件有不止一个轨道包含 set_tempo 条消息。

在每种情况下我应该怎么做才能确保得到正确的解释?

一般来说,要获得正确的 MIDI 消息流,您必须将所有音轨合并到一个类型 1 文件中。合成器重要的不是轨道,而是通道。

MIDI 规范说:

ASSIGNMENT OF NOTE ON/OFF COMMANDS

If an instrument receives two or more Note On messages with the same key number and MIDI channel, it must make a determination of how to handle the additional Note Ons. It is up to the receiver as to whether the same voice or another voice will be sounded, or if the messages will be ignored. The transmitter, however, must send a corresponding Note Off message for every Note On sent. If the transmitter were to send only one Note Off message, and if the receiver in fact assigned the two Note On messages to different voices, then one note would linger. Since there is no harm or negative side effect in sending redundant Note Off messages this is the recommended practice.


General MIDI System Level 1 Developer Guidelines 指出,为响应“GM System On”消息,设备应将 Program Change 设置为 0。因此您可以假设这是具有音符的通道的初始值没有先前的程序更改。


标准 MIDI 文件规范说

tempo information should always be stored in the first MTrk chunk.

但是 "should" 不是 "must"。