问题描述
我想解析一个已经存在的.mid文件,将其乐器从原声三角钢琴"更改为小提琴",然后将其保存回来或另存为另一个.mid文件.
I want to parse an already existing .mid file, change its instrument, from 'acoustic grand piano' to 'violin' for example, and save it back or as another .mid file.
根据我在文档中看到的内容,使用program_change
或patch_change
指令对乐器进行了更改,但是在已经存在的MIDI文件中找不到可以执行此操作的任何库.他们似乎都只支持从头开始创建的MIDI文件.
From what I saw in the documentation, the instrument gets altered with a program_change
or patch_change
directive but I cannot find any library that does this in MIDI files that exist already. They all seem to support it only MIDI files created from scratch.
推荐答案
使用 music21库 (插入我自己的系统,希望可以).如果零件中定义了补丁,请执行以下操作:
Use the music21 library (plugging my own system, hope that's okay). If there are patches defined in the parts, do:
from music21 import converter,instrument # or import *
s = converter.parse('/Users/cuthbert/Desktop/oldfilename.mid')
for el in s.recurse():
if 'Instrument' in el.classes: # or 'Piano'
el.activeSite.replace(el, instrument.Violin())
s.write('midi', '/Users/cuthbert/Desktop/newfilename.mid')
或当前未定义补丁更改:
or if there are no patch changes currently defined:
from music21 import converter,instrument # or import *
s = converter.parse('/Users/cuthbert/Desktop/oldfilename.mid')
for p in s.parts:
p.insert(0, instrument.Violin())
s.write('midi', '/Users/cuthbert/Desktop/newfilename.mid')
这篇关于如何读取Midi文件,更改其乐器并写回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!