2

如何使用 python 根据音高分割 MIDI 文件?我尝试了 music21 库,但我真的不知道它是如何工作的......

4

1 回答 1

2

试试这个代码:

import pretty_midi
import copy

pitch_cutoff = 65

mid = pretty_midi.PrettyMIDI('my_file.mid')
low_notes = copy.deepcopy(mid)
high_notes = copy.deepcopy(mid)

for instrument in low_notes.instruments:
    for note in instrument.notes:
        if note.pitch > pitch_cutoff:
            note.velocity = 0

for instrument in high_notes.instruments:
    for note in instrument.notes:
        if note.pitch < pitch_cutoff:
            note.velocity = 0

low_notes.write("low_notes.mid")
high_notes.write("high_notes.mid")

它使用该pretty_midi模块将 MIDI 文件拆分为两个不同的文件。名为“high_notes.mid”的文件将仅在您将pitch_cutoff变量设置为的上方有注释,而“low_notes.mid”将仅在其下方有注释。只需将“my_file.mid”更改为您的文件名并尝试一下。如果您有任何问题,请告诉我。

于 2020-11-01T06:29:20.580 回答