matlab中有一个用于生成midi文件的函数:
writemidi
但是测试发现该函数生成的midi文件不能直接在windows media player中播放,但是可以由miditoolbox中的readmidi函数读取。
经过分析发现,原因是该函数生成的midi文件缺少轨道的结束标志:00 FF 2F 00。对matlab文件进行如下修改:
文件名:writeNMat2Smf.m
在生成轨道数据的循环结束位置添加如下语句
databytes_track = [databytes_track; 0; hex2dec('ff'); hex2dec('2f'); 0];
实践证明,经过这样修改之后所生成的midi文件可以使用media player直接播放
另外,原有的settempo函数需要先计算旧的tempo值,再缩放到新的tempo值。但是如果直接根据原有的节拍数和新的tempo值计算更简单可靠,因此也修改了settempo函数。新的settempo函数如下:
function nmatf = settempo(nmat,bpm)
% Set tempo (in BPM)
% y = settempo(nmat,bpm)
% Assigns a new tempo to the NMAT in beats per minute (BPM)
%
% Input argument:
% NMAT = notematrix
% BPM = new tempo (in beats per minute)
%
% Output:
% NMATF = new notematrix
%
% Change History :
% Date Time Prog Note
% 1.7.2003 18:53 TE Created under MATLAB 5.3 (PC)
%� Part of the MIDI Toolbox, Copyright � 2004, University of Jyvaskyla, Finland
% See License.txt
if isempty(nmat), return; end
%t=gettempo(nmat);
x = 60/bpm; %x= bpm/t;
nmatf=nmat;
nmatf(:,6:7) = nmat(:,1:2)*x;