如何使用python基于音符音高分割midi文件?

问题描述

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

解决方法

尝试以下代码:

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”更改为文件名即可,然后尝试一下。如果您有任何问题,请告诉我。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...