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